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.

Servo/Interrupt with C18

Status
Not open for further replies.

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.
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
 
deleted, saw that you have the OSCCON set for 8mhz.
 
Last edited:
You basically already answered yourself for the first question. The different things you do in the interrupt routing take up some cycles. You can calculate the exact times by looking at the assembly file produced by your compiler. Most instructions on the PIC take one instruction cycle except instructions that need to branch to another address other than the next address in the PIC, these take two instruction cycles. You could figure these easily if you coded in assembly, however since you're using C, you'd need to look at the resulting assembly listing to figure it out. Or do as I do, code it up, hook up an oscilloscope and adjust the TMR0L until you get the duration you need.
 
...You could figure these easily if you coded in assembly, however since you're using C, you'd need to look at the resulting assembly listing to figure it out. Or do as I do, code it up, hook up an oscilloscope and adjust the TMR0L until you get the duration you need.

Or you can use the stopwatch in MPLABSIM and time it.
 
Status
Not open for further replies.

Latest threads

Back
Top