| The program
shown on the right contains the blinkled.c program
updated with the delay routines.
Notice that starting on line 8 we use the #define
statement to do 'something'. What this 'something'
does is tell the compiler to substitute or replace the
delay_3c with the 3 assembly functions using the
string "nop" as the argument. The
asm("nop") function tells the compiler to
insert a nop instruction inline whenever it compiles
this statement. In this case, whenever the compiler
encounters delay_3c
it will insert 3 nop instructions.
The next line builds upon this by now telling the
compiler to insert delay_3c 5 times whenever it finds
delay_5us in the program.
The last #define delay_10us replaces this with 2
delay_5us.
This can quickly get out of hand but this works
well for small inline delays.
|
#include
<htc.H>
//---------------------------------------------
// file: delay.c
// description: This is the PicWiser delay project
// author: Emicros
// compiler: Hi-Tech
//----------------------------------------------
#define delay_3c asm( "nop"); asm( "nop");
asm("nop");
#define delay_5us
delay_3c;delay_3c;delay_3c;delay_3c;delay_3c;
#define delay_10us
delay_5us;delay_5us;
/*--------------------------------------------
** function: InterruptServiceRoutine
** description: This is just a place holder
** for now and isn't used in the program.
**------------------------------------------*/
void interrupt InterruptServiceRoutine( void )
{
if( (TMR1IE) && (TMR1IF) )
{
TMR1IF = 0;
}
}
void delay_50usec( unsigned char delay )
{
unsigned char x;
for( x=0; x<delay; x++)
{
delay_10us;
delay_10us;
delay_10us; delay_10us;
delay_10usec; delay_3c;
delay_3c;
}
}
void delay_msec( unsigned char delay )
{
unsigned char x;
for( x=0; x<delay; x++)
{
delay_50usec(22);
}
}
/*----------------------------------------
** function: main()
** description: This is the main function.
**--------------------------------------*/
void main( void )
{
TRISC = 0b11111011;
while( 1 )
{
PORTC = 0b00000100;
delay_msec(50);
PORTC = 0b00000000;
delay_msec(50);
}
}
|