Description: C1541-III mbed edition
delay.h
00001 /*For Microchip 18Fxxx or 18Cxxx and Hi-Tech C 00002 00003 Designed by Shane Tolmie of www.microchipC.com corporation. Freely distributable. 00004 Questions and comments to webmaster@microchipC.com. 00005 Lots of Hi-Tech C FAQ and sample source code at http://www.microchipC.com/. 00006 00007 Example C: 00008 00009 #define PIC_CLK 4000000 00010 00011 unsigned int timeout_int, timeout_char; 00012 00013 DelayUs(40); //do NOT do DelayUs(N) of N<5 @ 4Mhz or else it executes DelayUs(255) !!!! 00014 DelayUs(255); //max 00015 00016 dly250n; //delay 250ns 00017 dly1u; //delay 1us 00018 00019 timeout_char=timeout_char_us(1147); 00020 while(timeout_char-- && (RA1==0)); //wait up to 1147us for port RA1 to go high 00021 // - this is the max timeout 00022 00023 timeout_int=timeout_int_us(491512); 00024 while(timeout_int-- && (RA1==0)); //wait up to 491512us for port RA1 to go high 00025 // - this is the max timeout 00026 00027 */ 00028 00029 #ifndef __DELAY_H 00030 #define __DELAY_H 00031 00032 00033 extern unsigned char delayus_variable; 00034 00035 #define DelayUs wait_us 00036 00037 /* 00038 00039 timeouts: 00040 00041 C code for testing with ints: 00042 00043 unsigned int timeout; 00044 timeout=4000; 00045 PORT_DIRECTION=OUTPUT; 00046 while(1) 00047 { 00048 PORT=1; 00049 timeout=8000; 00050 while(timeout-- >= 1); //60ms @ 8Mhz, opt on, 72ms @ 8Mhz, opt off 00051 PORT=0; 00052 } 00053 00054 Time taken: optimisations on: 16cyc/number loop, 8us @ 8Mhz 00055 optimisations off: 18cyc/number loop, 9us @ 8Mhz 00056 with extra check ie: && (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz 00057 00058 C code for testing with chars: 00059 00060 similar to above 00061 00062 Time taken: optimisations on: 9cyc/number loop, 4.5us @ 8Mhz 00063 with extra check ie: && (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz 00064 00065 Formula: rough timeout value = (<us desired>/<cycles per loop>) * (PIC_CLK/4.0) 00066 00067 To use: //for max timeout of 1147us @ 8Mhz 00068 #define LOOP_CYCLES_CHAR 9 //how many cycles per loop, optimizations on 00069 #define timeout_char_us(x) (unsigned char)((x/LOOP_CYCLES_CHAR)*(PIC_CLK/4.0)) 00070 unsigned char timeout; 00071 timeout=timeout_char_us(1147); //max timeout allowed @ 8Mhz, 573us @ 16Mhz 00072 while((timeout-- >= 1) && (<extra condition>)); //wait 00073 00074 To use: //for max 491512us, half sec timeout @ 8Mhz 00075 #define LOOP_CYCLES_INT 16 //how many cycles per loop, optimizations on 00076 #define timeout_int_us(x) (unsigned int)((x+/LOOP_CYCLES_INT)*(PIC_CLK/4.0)) 00077 unsigned int timeout; 00078 timeout=timeout_int_us(491512); //max timeout allowed @ 8Mhz 00079 while((timeout-- >= 1) && (<extra condition>)); //wait 00080 */ 00081 00082 00083 //function prototypes 00084 void DelayBigUs(unsigned int cnt); 00085 void DelayMs(unsigned char cnt); 00086 void DelayMs_interrupt(unsigned char cnt); 00087 void DelayBigMs(unsigned int cnt); 00088 void DelayS(unsigned char cnt); 00089 00090 #endif 00091 00092
