114 lines
3.2 KiB
C
114 lines
3.2 KiB
C
/*****************************************************************************
|
|
* Copyright (C) 2008
|
|
* ProbeStar Telematics, LLC
|
|
* All Rights Reserved. Proprietary and Confidential.
|
|
*============================================================================
|
|
* Microsecond Delay Routine using Timer 0
|
|
*----------------------------------------------------------------------------
|
|
* 200nsec instruction cycle at 20Mhz, 100nsec at 40Mhz
|
|
* You have to examine the asm output to count the instructions
|
|
* Make sure this code is compiled with optimization -O -Zg4
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* includes
|
|
*****************************************************************************/
|
|
|
|
#include "system.h"
|
|
#include "delay.h"
|
|
|
|
/*****************************************************************************
|
|
* defines
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* macros
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* structs & typedefs
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* global constants
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* global variables
|
|
*****************************************************************************/
|
|
|
|
// used by DelayUs() macro
|
|
LIBBANK volatile BYTE _dcnt = 0;
|
|
|
|
/*****************************************************************************
|
|
* static constants
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* static variables
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* static prototypes
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* C functions
|
|
*****************************************************************************/
|
|
|
|
void DelayInit( void )
|
|
{
|
|
// timer #0 as counter, 2 nsec counts (5 per usec)
|
|
#ifdef _PIC18
|
|
#if XTAL==40
|
|
T0CON = 0b10000000; // prescalar = 2
|
|
#else
|
|
T0CON = 0b10001000; // no prescalar
|
|
#endif
|
|
#else
|
|
OPTION = 0b10000000;
|
|
#endif
|
|
}
|
|
|
|
// short delays up to 255 usec
|
|
void DelayUsec( BYTE n )
|
|
{
|
|
// clear counter
|
|
#ifdef _PIC18
|
|
TMR0H = 0;
|
|
TMR0L = 0;
|
|
#else
|
|
TMR0 = 0;
|
|
#endif
|
|
// # ticks per usec
|
|
n--; // calling overhead
|
|
n *= 5;
|
|
|
|
// wait till count reaches
|
|
#ifdef _PIC18
|
|
while ( TMR0L < n )
|
|
;
|
|
#else
|
|
while ( TMR0 < n )
|
|
;
|
|
#endif
|
|
}
|
|
|
|
// longer delays up to 10000 usec (10 msec)
|
|
#ifdef _PIC18
|
|
void DelayUsecW( WORD n )
|
|
{
|
|
// clear counter
|
|
TMR0H = 0;
|
|
TMR0L = 0;
|
|
|
|
// 5 ticks per usec
|
|
n--; // calling overhead
|
|
n *= 5;
|
|
|
|
// wait till count reaches
|
|
while ( TMR0 < n )
|
|
;
|
|
}
|
|
#endif
|