systemchaos
Newbie level 2
generate pwm to control servo motor
i am a newbie and i'm working on a c programming code to control servo motor via pwm. i'm using pic16f877A because it has 2 CCP pins that i can use to generate pwm with 20MHz osc. Since then i have a few problem to make the servo to turn clockwise, counterclockwise and stop in writing the program's code. The most problem i'm probably faced is how to set the timer value.
According to the PIC16F877A datasheet, it provide an equation which is
pwm period= [PR2+1]*4*Tosc*[TMR2 prescale value]
From the equation what is 'pwm period' , 'PR2' and 'TMR2 prescale value'?
I've found a c programming code on the internet where he use an equation
pwm period = (1/clock)*4*t2div*(period+1)
I think this equation might be same as the equation provided by the datasheet. From his calculations, he use 1 MHz osc to find the period where the period must be an 8-bit value due to the fact that the PIc use an 8-bit divisor which 0-255. Then he use pwm period=10ms , and t2div=16 since the t2div can have three possible values 1, 4, or 16). Finally he get the period=155 (in the range of 0-255).
Since i used osc=20MHz and pwm period=20 ms, i get the period= 6249 which is too big. Now i'm don't know what to do. i've tried to search the solution through the internet but still not get it.
So, what is the possible solution that i should make this done? Which timer should i use, is it tmr0, tmr1 or tmr 2? and how to find the pwm value to stop, cw and ccw the servomotors?
The next problem is when i'm using source boost IDE (boostc) . How to define and set up the CCP pin and the timer? Should i have to use C2C-plus? And how to write the programming in c?
I hope that anyone can guide me and i'm really appreciate that..regards
systemchaos
i am a newbie and i'm working on a c programming code to control servo motor via pwm. i'm using pic16f877A because it has 2 CCP pins that i can use to generate pwm with 20MHz osc. Since then i have a few problem to make the servo to turn clockwise, counterclockwise and stop in writing the program's code. The most problem i'm probably faced is how to set the timer value.
According to the PIC16F877A datasheet, it provide an equation which is
pwm period= [PR2+1]*4*Tosc*[TMR2 prescale value]
From the equation what is 'pwm period' , 'PR2' and 'TMR2 prescale value'?
I've found a c programming code on the internet where he use an equation
pwm period = (1/clock)*4*t2div*(period+1)
I think this equation might be same as the equation provided by the datasheet. From his calculations, he use 1 MHz osc to find the period where the period must be an 8-bit value due to the fact that the PIc use an 8-bit divisor which 0-255. Then he use pwm period=10ms , and t2div=16 since the t2div can have three possible values 1, 4, or 16). Finally he get the period=155 (in the range of 0-255).
Since i used osc=20MHz and pwm period=20 ms, i get the period= 6249 which is too big. Now i'm don't know what to do. i've tried to search the solution through the internet but still not get it.
So, what is the possible solution that i should make this done? Which timer should i use, is it tmr0, tmr1 or tmr 2? and how to find the pwm value to stop, cw and ccw the servomotors?
The next problem is when i'm using source boost IDE (boostc) . How to define and set up the CCP pin and the timer? Should i have to use C2C-plus? And how to write the programming in c?
I hope that anyone can guide me and i'm really appreciate that..regards
systemchaos