/***************************************************************************** * Copyright (C) 2008 * ProbeStar Telematics, LLC * All Rights Reserved. Proprietary and Confidential. *============================================================================ * Delay functions for PIC-16/18 at 20/40 Mhz *---------------------------------------------------------------------------- * Make sure this code is compiled with optimization -O -Zg4 * * Note accuracy issues below XTAL=16 MHz. * At or above this the loop is exactly 1 micro-second per iteration. * The 20MHz version is correct down to 2 micro-seconds. * * Sample delay routines from HI-TECH were pretty sucky *****************************************************************************/ /* prevent multiple inclusions */ #ifndef _DELAY_H_ #define _DELAY_H_ /***************************************************************************** * includes *****************************************************************************/ #include "system.h" #ifdef __linux__ #include #endif /***************************************************************************** * defines *****************************************************************************/ #ifdef __linux__ //----------------------------------------------------------------------------- // Delays mapped to sleep functions //----------------------------------------------------------------------------- #define DelayInit() #define DelayUsec(C) usleep(C) #define DelayMsec(C) usleep((C)*1000) #define DelaySec(C) sleep(C) #else //----------------------------------------------------------------------------- // Microsecond delay routine //----------------------------------------------------------------------------- #if XTAL == 20 // PIC-18 20Mhz #define DelayUs(x) { \ _dcnt = x; \ while(--_dcnt) { \ asm("nop"); \ asm("nop"); \ asm("nop"); \ } \ } #elif XTAL == 40 // PIC-18 40Mhz #define DelayUs(x) { \ _dcnt = x; \ while(--_dcnt) { \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ } \ } #endif #endif /***************************************************************************** * macros *****************************************************************************/ #ifndef __linux__ //----------------------------------------------------------------------------- // Fixed Delays //----------------------------------------------------------------------------- #ifdef XTAL == 20 // 20Mhz PIC-16 or PIC-18 #define DelayNs0100() { \ asm("nop"); \ } #define DelayNs0200() { \ asm("nop"); \ } #define DelayNs0300() { \ asm("nop"); \ asm("nop"); \ } #define DelayNs0400() { \ asm("nop"); \ asm("nop"); \ } #define DelayNs0500() { \ asm("nop"); \ asm("nop"); \ asm("nop"); \ } #define DelayNs1000() { \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ } #elif XTAL == 40 // PIC-18 40Mhz #define DelayNs0100() { \ asm("nop"); \ } #define DelayNs0200() { \ asm("nop"); \ asm("nop"); \ } #define DelayNs0300() { \ asm("nop"); \ asm("nop"); \ asm("nop"); \ } #define DelayNs0400() { \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ } #define DelayNs0500() { \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ } #define DelayNs1000() { \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ asm("nop"); \ } #endif // Nanosecond delays #define DelayNs1500() { \ DelayNs0500(); \ DelayNs1000(); \ } #define DelayNs2000() { \ DelayNs1000(); \ DelayNs1000(); \ } #define DelayNs3000() { \ DelayNs2000(); \ DelayNs1000(); \ } #define DelayNs4000() { \ DelayNs2000(); \ DelayNs2000(); \ } #define DelayNs5000() { \ DelayNs2000(); \ DelayNs2000(); \ DelayNs1000(); \ } // Microsecond delays #define DelayUs1() DelayNs1000() #define DelayUs2() DelayNs2000() #define DelayUs3() DelayNs3000() #define DelayUs4() DelayNs4000() #define DelayUs5() DelayNs5000() #define DelayUs10() { \ DelayUs5(); \ DelayUs5(); \ } #endif /***************************************************************************** * structs & typedefs *****************************************************************************/ /***************************************************************************** * global constants *****************************************************************************/ /***************************************************************************** * global variables *****************************************************************************/ #ifndef __linux__ extern LIBBANK volatile BYTE _dcnt; #endif /***************************************************************************** * C function prototypes *****************************************************************************/ /* export C functions to C++ */ #ifdef __cplusplus extern "C" { #endif #ifndef __linux__ extern void DelayInit( void ); // Timer #0 init // library delay routines extern void DelayUsec( BYTE byCount ); // short delays up to 50usec extern void DelayUsecW( WORD wCount ); // longer delays over 5usec extern void DelayMsec( WORD wCount ); extern void DelaySec( WORD wCount ); #endif #ifdef __cpluscplus } #endif #endif