Author Topic: Delay Trouble  (Read 2958 times)

0 Members and 1 Guest are viewing this topic.

Offline michaelsaneTopic starter

  • Jr. Member
  • **
  • Posts: 31
  • Helpful? 0
Delay Trouble
« 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;
}

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Delay Trouble
« Reply #1 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.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline michaelsaneTopic starter

  • Jr. Member
  • **
  • Posts: 31
  • Helpful? 0
Re: Delay Trouble
« Reply #2 on: March 31, 2008, 06:46:51 PM »
It was a bad oscillator.

Offline michaelsaneTopic starter

  • Jr. Member
  • **
  • Posts: 31
  • Helpful? 0
Re: Delay Trouble
« Reply #3 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. 

Offline michaelsaneTopic starter

  • Jr. Member
  • **
  • Posts: 31
  • Helpful? 0
Re: Delay Trouble
« Reply #4 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. 

 


Get Your Ad Here

data_list