0
\$\begingroup\$

I have a project to make an LED fade in for 1 sec, stay on for 1 sec, fade out for 1 sec and then stay off for 1 sec, all the project must be done with registers and with the help of the timer0 fast PWM functionality without any functions (e.g. analogWrite).

I already have to logical part of the problem, I know how to generate an interrupt on timer0, but I can't figure out the fading part. How do I give a specific brightness to the LED with registers?

Maybe I am missing the documentation, I don't mind if you suggest something to read first and then came back with questions.

\$\endgroup\$
5
  • \$\begingroup\$ You have to change pulse width in smaller steps so it looks like fade. \$\endgroup\$
    – KIIV
    Commented Dec 12, 2016 at 21:39
  • \$\begingroup\$ Read this and you will know all the answers. The documentation you are probably missing is ATmega328 datasheet. \$\endgroup\$
    – Chupo_cro
    Commented Dec 12, 2016 at 22:38
  • \$\begingroup\$ Why are you asking here in the Arduino stackexchange area if you are not going to use Arduino methods like analogWrite? It sounds like you want to do bare metal programming. It is much harder. But you should start by going to the processor's manufacturer's web page. If it is an Atmel processor go here and search for the spec sheet of the processor you are using. \$\endgroup\$
    – st2000
    Commented Dec 12, 2016 at 23:42
  • \$\begingroup\$ You shouldn't use timer0 if you are using millis or delay! \$\endgroup\$
    – Gerben
    Commented Dec 13, 2016 at 11:32
  • \$\begingroup\$ Since this is basically a question about "raw" programming of the Atmega328P (not using Arduino libraries) I think this question is better suited to Electronics Stack Exchange. \$\endgroup\$ Commented Dec 13, 2016 at 23:55

1 Answer 1

1
\$\begingroup\$

If you know how to generate an interrupt on timer0, I assume you already finish the fast PWM registers setup parts. Based on mentioned documentation by @Chupo_cro you can change the brightness by change the OCR0A or OCR0B value. i.e. your setup was using timer0 - A, and it was on non inverted mode.

OCR0A =   0; //  0% duty cycle
OCR0A = 128; //50% duty cycle
OCR0A = 255; //100% duty cycle (since timer 0 is 8 bit)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I assume you already finish the fast PWM registers setup parts. - an assumption I wouldn't make in the complete absence of posted code in the question. \$\endgroup\$ Commented Dec 13, 2016 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.