Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Driving a servo motor- Timer0, Timer1

Status
Not open for further replies.

Wingmax

New Member
This is continuous from "Driving a servo motor - Timer0."

This time we'll use Timer0 to set the pulse width, and Timer1 to set the
period of the pulse train. When Timer1 interrupt is software triggered, the pulses will be generated by the interrupt routine, thus the micro can do something else while generating pulses in the background. MicroController used: P16F628A.

T0T1grapha.jpg

The period of pulses is fixed and pulse width is variable.
The alogorithm is as follow:

Timer1 was set for 16 mS.
Timer0 was set for required pulse width.

START:
Turn on Timer1 and wait for it to overflow.
.....Set pulse high. Turn on Timer0 and wait for Timer0 overflow.
.....When Timer0 overflows, set pulse low. Still waiting for Timer1 overflows.
When Timer1 overflows, it's one period. Reset values in Timer0 and Timer1 and repeat from START.

Timer1 register is consists of TMR1H and TMR1L registers. Maximum steps of increment the two registers
combine= 65536. For Fosc= 4Mhz, each step is 1 uS.

Example to set value in TMR1H and TMR1L register:

Fosc= 4Mhz
Period of pulses= 16 mS
Prescale= 1:1

65536 - (16000 (in uS))/1

= 49536
Convert this number into binary number= 11000001 10000000

TMR1H= 11000001 or 193d
TMR1L= 10000000 or 128d

Refer following program for detail of setup.
This program only send pulses with width of 1.3 mS, which is middle position.

View attachment Servo Motor_T0T1_C.txt

Next we'll modify the program to use Timer0 and Timer2........
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top