superbrew
Member
I am trying to get a handle on controlling a servo with C18 using an 18f1320. I am using the code below and it is working fine, but I don't understand why I need to modify the value of Timer0 the way that I did.
Using the 8MHz internal clock, I would assume that I would need to set TMR0L to 56 for a 100uS interrupt, but this is not the case. I have verified that this code produces a .9mS or 2mS pulse with a 20mS period with an o'scope. I am assuming that the interrupt routine call takes up some clock cycles, but how do I calculate this? I also understand that this is not the best way to control a servo, but I did not want to just copy and paste someone else's code.
Thanks
Code:
#include <p18f1320.h>
#include <timers.h>
#include <delays.h>
int pulseON = 0;
char pos = 0;
#pragma config WDT = OFF, LVP = OFF, OSC = INTIO2
void timer_isr(void);
#pragma code high_vector=0x08
void high_interrupt (void)
{
_asm GOTO timer_isr _endasm
}
#pragma code
#pragma interrupt timer_isr
void timer_isr (void)
{
if(pulseON < pos)
{
PORTBbits.RB3 = 1;
pulseON++;
}
else if(pulseON < 200)
{
PORTBbits.RB3 = 0;
pulseON++;
}
else
{
pulseON = 0;
}
INTCONbits.TMR0IF = 0;
TMR0L = 100;
}
#pragma code
void main(void)
{
TRISB = 0x00;
ADCON1 = 0x00;
OSCCON = 0x70;
OpenTimer0 (TIMER_INT_ON & T0_SOURCE_INT & T0_8BIT);
INTCONbits.GIE = 1;
while(1)
{
pos = 9;
Delay10KTCYx(100);
pos = 20;
Delay10KTCYx(100);
}
}
Using the 8MHz internal clock, I would assume that I would need to set TMR0L to 56 for a 100uS interrupt, but this is not the case. I have verified that this code produces a .9mS or 2mS pulse with a 20mS period with an o'scope. I am assuming that the interrupt routine call takes up some clock cycles, but how do I calculate this? I also understand that this is not the best way to control a servo, but I did not want to just copy and paste someone else's code.
Thanks