Society of Robots - Robot Forum

Software => Software => Topic started by: michaelsane on March 31, 2008, 05:33:32 PM

Title: Delay Trouble
Post by: michaelsane on March 31, 2008, 05:33:32 PM
I am working on a circuit board for my science fair project right now, and am having some trouble with c18 delays.  I can call the delay functions and the code compiles fine, but when I check the signal with my oscilloscope, the delays aren't the length they should be.  According to the datasheet for the 18f4320 (the chip i am using) each instruction cycle is 100ns, so If I delay 10 instruction cycles it should be 1 microsecond, but for some reason I suspect it has to do with the oscillator being improperly configured, but I have it set to HSPLL.  If anyone has any suggestions they would be greatly appreciated!

Code: [Select]
#include <p18f4320.h>
#include <delays.h>
#include <adc.h>
#pragma config OSC = HSPLL    /* Sets the oscillator mode to HSPLL */
#pragma config WDT = OFF   /* Turns the watchdog timer off */
#pragma config LVP = OFF   /* Turns low voltage programming off */
#pragma config DEBUG = OFF /* Compiles without extra debug code */

void MvServo (unsigned char, int);

void main (void)
{

LATB=0x00;
TRISB=0x00;
while(1)
{
MvServo(0xFF, 1);
}

}

void MvServo (unsigned char bports, int inst)
{
int i;
LATB=0x00;
TRISB=0x00;

for(i=0; i<100; i++)
{
LATB=bports;
Delay10TCYx(inst);
LATB=0x00;
Delay10TCYx(20-inst);
}
return;
}
Title: Re: Delay Trouble
Post by: Webbot on March 31, 2008, 05:56:20 PM
Don't forget that each line of C code may generate several machine language instructions - each of which may take 100ns. So your 'scope is probably correct.
Title: Re: Delay Trouble
Post by: michaelsane on March 31, 2008, 06:46:51 PM
It was a bad oscillator.
Title: Re: Delay Trouble
Post by: michaelsane on March 31, 2008, 11:22:39 PM
The bad oscillator was apparently only the beginning of my troubles... I can output near perfect 1 microsecond square waves, but when I set the delay any greater than 10 instructions it flips out.  For instance the code
Code: [Select]
void main (void)
{

LATB=0x00;
TRISB=0x00;
while(1)
{
LATBbits.LATB7=1;
Delay10TCYx(10);
LATBbits.LATB7=0;
Delay10TCYx(10);
}

}

The thing that perplexes me the most, is it appears to execute the code in the while loop only once, whereas with 10 instruction delay it loops infinitely. 
Title: Re: Delay Trouble
Post by: michaelsane on April 01, 2008, 12:05:01 AM
I kind of feel like a moron answering my own question, but when I set the OSC mode to HS instead of HSPLL it works just fine, so I guess it is just being unstable at 40mhz.