Hello.
OK, so I need to control all 10 servos simultaneously with an ATmega16. Here's how I do it:
- Clock frequency is 16 MHz.
- I set Timer 1 to the system clock (16 MHz), in Fast PWM TOP=ICR1.
- I want the timer to count each passing of 10 microseconds: 1/16 = 0.0625 us (timer1's period). I want 10 us so 10/0.0625 = 160 periods equal 10 us
- In Timer1's interrupt I increment a variable (every 10 microseconds). When it reaches 2000 I set it back to 0.
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
t++; //increment t every 10 us
if(t==2000) //20 ms have passed
{
t=0; //set t back to 0
per++; //increment per (I need this in my function)
}
}
This is the interrupt of Timer 1: per keeps the number of 20 ms signals.
So basically, I create my own PWM using Timer 1.
In main() I do something like this (but for independent control and speed of 10 servos):
while(1)
{ if(t<150) //150 means 1.5 ms like I said
PORTC.0=1;
else PORTC.0=0;
}
Controlling all 10 servos is just an expansion of that code.
What do you think? Is it OK? I can't use the built-in PWM to control all 10 servos and I don't want to waste precious resources with delays. Is it correct? Are there any better solutions (remember that I have 10 servos to control)?