So here are specifications: -prescaler of 8 -Channel A -pulse_width is determined by user defined degrees (converted to clock ticks) -pulse_period is 40000 clock ticks (20ms) -Normal mode -Timer3

So we have a move_servo(unsigned degrees) where all it does is convert the degrees to clock ticks and store it in pulse_width. Ok no problem.

Then we need ISR(TIMER3_COMPA_vect). This interrupt is supposed to actually generate the waveform where pulse_width would be high and (pulse_period-pulse_width) would be low (so total pulse length is 20ms).

Now I am not quite understanding how the OCR3A register works. My thinking for the ISR was to have a flag variable so that the first time ISR goes off, it sends pulse_width out of OCR3A, then set flag, next time it goes off it sends off pulse_period-pulse_width (which will be low and is the wait time between each movement of servo) and will set flag to 0 again.

But writing to OCR3A will move servo right? Will my plan work in the toggle mode for normal mode? So every other trip into the ISR sends out the "wait time" which is low so it doesn't move servo?

My question is basically am I on the right track? Or if not can anyone help me understand how to generate a random lengthed pulse followed by a "wait time" waveform in normal mode?

This is ATmega128 again fyi


  • \$\begingroup\$ Explain again why you aren't using PWM mode? \$\endgroup\$ – Ignacio Vazquez-Abrams Oct 29 '14 at 3:31
  • \$\begingroup\$ Haha oh yeah I should have said, sorry. This is a homework question and we used PWM in a lab, which seemed simple enough. But are to do the same thing using normal mode in homework. But I am just unsure if my flag idea will make OCR3A behave correctly \$\endgroup\$ – Tyler Dahle Oct 29 '14 at 3:41

In Normal mode a timer does not reset when an output compare matches. Your first value in OCR3A should be the pulse width, and the second should be the pulse period. You will need to reset the timer by hand once the pulse period elapses. And read very carefully all the warnings in the datasheet about modifying OCR3A and TCNT3 while the timer is running.

|improve this answer|||||
  • \$\begingroup\$ Ok, so in toggle mode my first value would go if(flag == 0) where I set OCR3A to pulse_width and flag to 1 and counter to 0. Then second value in if(flag == 1) where I set OCR3A to pulse period, counter to 0, and flag to 0? \$\endgroup\$ – Tyler Dahle Oct 29 '14 at 4:18
  • \$\begingroup\$ You only reset the counter to 0 when setting the pulse width. You want the timer to continue on to the pulse period once you've reached the width. \$\endgroup\$ – Ignacio Vazquez-Abrams Oct 29 '14 at 4:20
  • \$\begingroup\$ Oh yes I see. This way I can just set OCR3A to pulse period instead of pulse period - pulse width. Alright :). I think I see how the OCR3A register is working. Thanks for your help! I could handle a simple square waveform in normal mode but the different lengthed pulses were throwing me off a bit. \$\endgroup\$ – Tyler Dahle Oct 29 '14 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.