The Biggie PWM control

I’m new to pics and I’m trying out the code on the spark fun website from the Biggie area, and the pwm control software on those pages has a delay called delay_us(step) on line 125. I’m using the CC5x compiler and they delay file has no delay_us routine in it. I currently don’t know how to write one. Thankyou for any ideas how to.

Here is the link:

http://www.sparkfun.com/commerce/presen … e%20Biggie

it also seems like the code is for a 4MHz clock. I’m using 20. Here is the code:

/*

DELAYS AND TIMING

=================

Delays are frequently used. There are various

ways to generate them:

  1. Instruction cycle counting

  2. Using the TMR0 timer

  3. Watchdog timeout for low power consumption

  4. Using variables achieves longer delays

*/

void delay_ms( uns16 millisec)

// Delays a multiple of 1 milliseconds at 4 MHz

// using the TMR0 timer

{

char next = 0;

OPTION = 2; // prescaler divide TMR0 rate by 8

TMR0 = 2; // deduct 2*8 fixed instruction cycles delay

do {

next += 125;

clrwdt(); // needed only if watchdog is enabled

while (TMR0 != next) // 125 * 8 = 1000 (= 1 ms)

;

} while ( – millisec != 0);

}

void delay10( char n)

/*

Delays a multiple of 10 milliseconds using the TMR0 timer

Clock : 4 MHz => period T = 0.25 microseconds

1 IS = 1 Instruction Cycle = 1 microsecond

error: 0.16 percent

*/

{

char i;

OPTION = 7;

do {

clrwdt(); // only if watchdog enabled

i = TMR0 + 39; /* 256 microsec * 39 = 10 ms */

while ( i != TMR0)

;

} while ( --n > 0);

}

void _delay10( char x)

/*

Delays a multiple of 10 milliseconds

using instruction cycle counting

Clock : 32768 Hz => period T = 30.518 microseconds

1 Instruction Cycle = 1 IS = 4 * T = 122 microseconds

10 ms = 82 IS (81.92) => error: 0.1 percent

*/

{

do {

char i = 26; /* 2 IS */

do ; while ( --i > 0); /* 26 * 3 - 1 = 77 IS */

} while ( --x > 0); /* 3 IS */

}

char counterT;

void maintimer( void)

{

if ( TO == 1) {

/* power up or MCLR */

PORTA = 0; /* write output latch first */

TRISA = 0; /* all outputs */

TRISB = 0xFF; /* all inputs */

}

else {

/* watchdog wakeup */

if ( --counterT > 0) {

OPTION = 0x0B; /* WDT divide by 16 */

sleep(); /* waiting 16 * 18 ms =

288 ms = 0.288 seconds */

}

}

// …

delay_ms( 5500); /* 5.5 seconds */

// …

delay10( 100); /* 1 second */

// …

counterT = 7; // 7 * 0.288 sec. = 2 sec. totally

OPTION = 0x0B; // 0 1011: WDT divide by 16

// main terminates by sleep();, allows low power consumption

// waiting for watchdog timeout: approx. 16*18 ms = 288 ms

}

I found this on an old sparkfun website. It definately helped.

/*
7/23/02
Nathan Seidle
nathan.seidle@colorado.edu
Delays for... Well, everything.
11-11 Updated the delays - now they actually delay what they say they should.
10-11-03 Updated delays. New CC5X compiler is muy optimized.
*/
//Really short delay
void delay_us(uns16 x)
{
#ifdef Clock_4MHz
//Calling with 10us returns 69us
for ( ; x > 0 ; x--);
#endif
#ifdef Clock_8MHz
//Calling with 1us returns 11us
//Calling with 10us returns 56us
//for ( ; x > 0 ; x--);
//Calling with 1us returns 7.5us
//Calling with 10us returns 48
//Calling with 1000us returns 4.5ms
while(--x);
//while(x--);
#endif
#ifdef HS_Osc
//Calling with 10us returns 13 us
//Calling with 1us returns 1.8us
while(--x) nop();
#endif
}
//General short delay
void delay_ms(uns16 x)
{
#ifdef Clock_4MHz
//Clocks out at 1002us per 1ms
int y;
for ( ; x > 0 ; x--)
for ( y = 0 ; y < 108 ; y++);
#endif
#ifdef Clock_8MHz
//Clocks out at 1006us per 1ms
uns8 y, z;
for ( ; x > 0 ; x--)
for ( y = 0 ; y < 4 ; y++)
for ( z = 0 ; z < 69 ; z++);
#endif
#ifdef HS_Osc
uns8 y, z;
//Clocks out to 1.00ms per 1ms
//9.99 ms per 10ms
for ( ; x > 0 ; x--)
for ( y = 0 ; y < 4 ; y++)
for ( z = 0 ; z < 176 ; z++);
#endif
}
43
//Delays in 31.25kHz Low Power mode using the internal 31.25kHz oscillator
void delay_s_lp(uns16 x)
{
uns16 y;
//Clocks out to 1.001s per 1s
for ( ; x > 0 ; x--)
for ( y = 0 ; y < 775 ; y++);