Check please...
Hi please check this for me, whether I made a mistake or not.
To count up to 2ms Pulse Width, I use 8bit timer/counter from timer0 (since PCB is made and timer1 is already used in this PIC18F1330).
-8bit timer0
-Prescaler of 8
-FOSC/4 = 1Mhz
-frequency input to timer0 would be 1Mhz/8 = 125kHz or T (period) = 8us
-since, 8 bit, 0-255 then overflow, 256x8us = 2.048ms which is enough and useable in this case.
CALCULATION:
2ms = (1/1Mhz) x 8 x ins.cycle
ins.cycle = 0.002s / 0.000008s = 250
RESULT:
250 decimal would be 2ms Pulse Width.
divide by two:
125 decimal would be 1ms Pulse Width.
center position of servo:
125/2 + 125 = 187.5 ~ 188.
then my code would be:
Code:
int GetPulse1(){
T0CON=0b01000010; /*Init timer0 8 bit, PSA assigned, 1:8 prescale value, reset timer0*/
TMR0L=0;
TMR0H=0;
TRISAbits.TRISA0=1; /*Make this pin as an input*/
while(PORTAbits.RA0==1); /*Make pin low*/
while(PORTAbits.RA0==0); /*While pin is high*/
T0CONbits.TMR0ON=1; /*Start timer0*/
while(PORTAbits.RA0==1); /*When pin is low*/
T0CONbits.TMR0ON=0; /*Stop timer0*/
}
question is: will this work??
Later I will use 20 gap in between 188.
168 or less as turning left (servo)
169-207 as middle position
more than 208 as turning right.
Thanks for ur comments!!