C1541-III mbed edition

Dependencies:   mbed

Committer:
gertk
Date:
Mon Aug 22 21:11:59 2011 +0000
Revision:
1:0cbbb66a6100
Parent:
0:28557a4d2215
updated the nRESET pin to an interrupt capable pin (p29)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gertk 0:28557a4d2215 1 /*For Microchip 18Fxxx or 18Cxxx and Hi-Tech C
gertk 0:28557a4d2215 2
gertk 0:28557a4d2215 3 Designed by Shane Tolmie of www.microchipC.com corporation. Freely distributable.
gertk 0:28557a4d2215 4 Questions and comments to webmaster@microchipC.com.
gertk 0:28557a4d2215 5 Lots of Hi-Tech C FAQ and sample source code at http://www.microchipC.com/.
gertk 0:28557a4d2215 6
gertk 0:28557a4d2215 7 Example C:
gertk 0:28557a4d2215 8
gertk 0:28557a4d2215 9 #define PIC_CLK 4000000
gertk 0:28557a4d2215 10
gertk 0:28557a4d2215 11 unsigned int timeout_int, timeout_char;
gertk 0:28557a4d2215 12
gertk 0:28557a4d2215 13 DelayUs(40); //do NOT do DelayUs(N) of N<5 @ 4Mhz or else it executes DelayUs(255) !!!!
gertk 0:28557a4d2215 14 DelayUs(255); //max
gertk 0:28557a4d2215 15
gertk 0:28557a4d2215 16 dly250n; //delay 250ns
gertk 0:28557a4d2215 17 dly1u; //delay 1us
gertk 0:28557a4d2215 18
gertk 0:28557a4d2215 19 timeout_char=timeout_char_us(1147);
gertk 0:28557a4d2215 20 while(timeout_char-- && (RA1==0)); //wait up to 1147us for port RA1 to go high
gertk 0:28557a4d2215 21 // - this is the max timeout
gertk 0:28557a4d2215 22
gertk 0:28557a4d2215 23 timeout_int=timeout_int_us(491512);
gertk 0:28557a4d2215 24 while(timeout_int-- && (RA1==0)); //wait up to 491512us for port RA1 to go high
gertk 0:28557a4d2215 25 // - this is the max timeout
gertk 0:28557a4d2215 26
gertk 0:28557a4d2215 27 */
gertk 0:28557a4d2215 28
gertk 0:28557a4d2215 29 #ifndef __DELAY_H
gertk 0:28557a4d2215 30 #define __DELAY_H
gertk 0:28557a4d2215 31
gertk 0:28557a4d2215 32
gertk 0:28557a4d2215 33 extern unsigned char delayus_variable;
gertk 0:28557a4d2215 34
gertk 0:28557a4d2215 35 #define DelayUs wait_us
gertk 0:28557a4d2215 36
gertk 0:28557a4d2215 37 /*
gertk 0:28557a4d2215 38
gertk 0:28557a4d2215 39 timeouts:
gertk 0:28557a4d2215 40
gertk 0:28557a4d2215 41 C code for testing with ints:
gertk 0:28557a4d2215 42
gertk 0:28557a4d2215 43 unsigned int timeout;
gertk 0:28557a4d2215 44 timeout=4000;
gertk 0:28557a4d2215 45 PORT_DIRECTION=OUTPUT;
gertk 0:28557a4d2215 46 while(1)
gertk 0:28557a4d2215 47 {
gertk 0:28557a4d2215 48 PORT=1;
gertk 0:28557a4d2215 49 timeout=8000;
gertk 0:28557a4d2215 50 while(timeout-- >= 1); //60ms @ 8Mhz, opt on, 72ms @ 8Mhz, opt off
gertk 0:28557a4d2215 51 PORT=0;
gertk 0:28557a4d2215 52 }
gertk 0:28557a4d2215 53
gertk 0:28557a4d2215 54 Time taken: optimisations on: 16cyc/number loop, 8us @ 8Mhz
gertk 0:28557a4d2215 55 optimisations off: 18cyc/number loop, 9us @ 8Mhz
gertk 0:28557a4d2215 56 with extra check ie: && (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz
gertk 0:28557a4d2215 57
gertk 0:28557a4d2215 58 C code for testing with chars:
gertk 0:28557a4d2215 59
gertk 0:28557a4d2215 60 similar to above
gertk 0:28557a4d2215 61
gertk 0:28557a4d2215 62 Time taken: optimisations on: 9cyc/number loop, 4.5us @ 8Mhz
gertk 0:28557a4d2215 63 with extra check ie: && (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz
gertk 0:28557a4d2215 64
gertk 0:28557a4d2215 65 Formula: rough timeout value = (<us desired>/<cycles per loop>) * (PIC_CLK/4.0)
gertk 0:28557a4d2215 66
gertk 0:28557a4d2215 67 To use: //for max timeout of 1147us @ 8Mhz
gertk 0:28557a4d2215 68 #define LOOP_CYCLES_CHAR 9 //how many cycles per loop, optimizations on
gertk 0:28557a4d2215 69 #define timeout_char_us(x) (unsigned char)((x/LOOP_CYCLES_CHAR)*(PIC_CLK/4.0))
gertk 0:28557a4d2215 70 unsigned char timeout;
gertk 0:28557a4d2215 71 timeout=timeout_char_us(1147); //max timeout allowed @ 8Mhz, 573us @ 16Mhz
gertk 0:28557a4d2215 72 while((timeout-- >= 1) && (<extra condition>)); //wait
gertk 0:28557a4d2215 73
gertk 0:28557a4d2215 74 To use: //for max 491512us, half sec timeout @ 8Mhz
gertk 0:28557a4d2215 75 #define LOOP_CYCLES_INT 16 //how many cycles per loop, optimizations on
gertk 0:28557a4d2215 76 #define timeout_int_us(x) (unsigned int)((x+/LOOP_CYCLES_INT)*(PIC_CLK/4.0))
gertk 0:28557a4d2215 77 unsigned int timeout;
gertk 0:28557a4d2215 78 timeout=timeout_int_us(491512); //max timeout allowed @ 8Mhz
gertk 0:28557a4d2215 79 while((timeout-- >= 1) && (<extra condition>)); //wait
gertk 0:28557a4d2215 80 */
gertk 0:28557a4d2215 81
gertk 0:28557a4d2215 82
gertk 0:28557a4d2215 83 //function prototypes
gertk 0:28557a4d2215 84 void DelayBigUs(unsigned int cnt);
gertk 0:28557a4d2215 85 void DelayMs(unsigned char cnt);
gertk 0:28557a4d2215 86 void DelayMs_interrupt(unsigned char cnt);
gertk 0:28557a4d2215 87 void DelayBigMs(unsigned int cnt);
gertk 0:28557a4d2215 88 void DelayS(unsigned char cnt);
gertk 0:28557a4d2215 89
gertk 0:28557a4d2215 90 #endif
gertk 0:28557a4d2215 91
gertk 0:28557a4d2215 92