PWM frequency
how change frequency period on 5ms? need control servos without worrying functions may take on 20ms execute.
i'm pouring on register descriptions in atmega168 datasheet , it's not clear have change.
i can't lower clock rate. external servo controller option don't idea.
edit:
i found this:
http://www.arduino.cc/cgi-bin/yabb2/yabb.pl?num=1170282999
what timer0 , timer2? know 8 bits, do this?
tccr0a = 0x00;
tccr0b = 0x12;
tccr2a = 0x00;
tccr2b = 0x12;
thanks
i'm pouring on register descriptions in atmega168 datasheet , it's not clear have change.
i can't lower clock rate. external servo controller option don't idea.
edit:
i found this:
http://www.arduino.cc/cgi-bin/yabb2/yabb.pl?num=1170282999
quote
thanks hints, mellis.
after hours of diggin' in datasheets , webpages came solution works:
(hope it's of interest)quote/* servopwm
* ---------------
*
* comments
*
* (copyright notice) 2007 moe
* <http://youraddress.here>
* <mailto:you@youraddress.here>
*
*/
int val = 0;
void setup(void) {
pinmode(9,output); // servo pin
serial.begin(9600);
tccr1a = 0x00; // sets timer control bits pwm phase , frequency correct mode
tccr1b = 0x12; // sets timer control bits prescaler n = 8
icr1 = 0x0fa0; // upper timer limit = 4000 (in hex) equals 4ms => 1/5 of servo frequency
}
void loop(void) {
val = analogread(0); // read potentiometer
val = (val * 1.76) + 600; // convert potentiometer reading value in microseconds (my servo responds signals in 600us - 2400us range)
analogwrite(9,val); // pulse servo
serial.println(val); // output servo value
}
what does:
- initializes pwm timer frequency 4ms - 1/5 of servo's frequency of 20ms
(as of servo's signals fall in between 0.5ms - 2.5ms range).
- analogwrite takes values 0 4000 (for = 0.001ms)
edit:
it works on pins 9 , 10 timer1 drives these two.
best regards,
moe
what timer0 , timer2? know 8 bits, do this?
tccr0a = 0x00;
tccr0b = 0x12;
tccr2a = 0x00;
tccr2b = 0x12;
thanks
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > PWM frequency
arduino
Comments
Post a Comment