Driving a servo motor- Timer0, Timer2

Status
Not open for further replies.

Wingmax

New Member
This is continuous from "Driving a servo motor- Timer0, Timer1".
This time we just modify the previous program and use Timer0 and Timer2.
The only thing to change is to use Timer2 to set the period of the pulse.

When TMR2 increments to the value = calculated value in PR2, TMR2 interrupt is triggered and TMR2 will start counting from zero again. There's no need to reload the value into PR2.

To set up Timer2:

1) Set value in PR2.
2) Set Prescale in T2CON- bit 0 and bit 1.
3) Set Postscale in T2CON- bit 3 to bit 6.
4) Set TMR2ON to "1".

2) to 4) can be set in one instruction.

To calculate value for PR2:

F= Fosc/ machine cycle/ Prescale/ PR2/ Postscale

Example:

Period= 16mS, Fosc= 4Mhz, choose Prescale= Postscale= 16

1/0.016= 4000000/4 /16 / PR2/ 16

PR2= 62.5 round off to 63

Please refer program for detail. This program will make the servo motor's shaft to rotate to far right, pause, to the middle position, pause, and to the far left postion.

View attachment Servo motor_T0T2.txt

Next we will control the servo motor by switches connected on PortA.......
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…