# ATmega128 output compare normal mode. Generating a waveform with ISR

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

THANKS!!

• Explain again why you aren't using PWM mode? – Ignacio Vazquez-Abrams Oct 29 '14 at 3:31
• 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 – 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.