00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef __pic_utils_h
00016 #define __pic_utils_h
00017
00018 #include <system.h>
00019 #include "config.h"
00020
00021
00022
00023 #ifdef PORTB
00024 #define NUMBER_PORTS 2
00025 #endif
00026 #ifdef PORTC
00027 #undef NUMBER_PORTS
00028 #define NUMBER_PORTS 3
00029 #endif
00030 #ifdef PORTD
00031 #undef NUMBER_PORTS
00032 #define NUMBER_PORTS 4
00033 #endif
00034 #ifdef PORTE
00035 #undef NUMBER_PORTS
00036 #define NUMBER_PORTS 5
00037 #endif
00038
00039
00040
00041
00042
00043 #define int8 char
00044 #define uns8 unsigned char
00045 #define uns16 unsigned int
00046 #define int16 int
00047 #define uns32 unsigned long
00048 #define int32 long
00049
00050
00051
00052
00053
00054 extern uns8 port_shadow[NUMBER_PORTS];
00055 volatile uns8 port_array[NUMBER_PORTS] @PORTA;
00056 volatile uns8 tris_array[NUMBER_PORTS] @TRISA;
00057
00058
00059
00060
00061
00062
00063 #ifdef _PIC16F88
00064 #define turn_analog_inputs_off() ansel = 0
00065 #endif
00066 #ifdef _PIC16F876A
00067 #define turn_analog_inputs_off() adcon1 = 6
00068 #endif
00069
00070 #ifdef _PIC16F877A
00071 #define turn_analog_inputs_off() adcon1 = 6
00072 #endif
00073 #ifdef _PIC18F4520
00074 #define turn_analog_inputs_off() adcon1 = 15
00075 #endif
00076 #ifdef _PIC18F452
00077 #define turn_analog_inputs_off() adcon1 = 6
00078 #endif
00079 #ifdef _PIC18F2620
00080 #define turn_analog_inputs_off() adcon1 = 15
00081 #endif
00082 #ifdef _PIC18F4550
00083 #define turn_analog_inputs_off() adcon1 = 15
00084 #endif
00085
00086 #ifndef turn_analog_inputs_off()
00087 #define turn_analog_inputs_off() \
00088 #warning "!!! - See pic_utils.h for turning off analog inputs on your pic"
00089 #endif
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 #define set_pin(port, pin) \
00124 set_bit(port_shadow[port - PORTA], pin); \
00125 port_array[port - PORTA] = port_shadow[port - PORTA];
00126
00127 #define clear_pin(port, pin) \
00128 clear_bit(port_shadow[port - PORTA], pin); \
00129 port_array[port - PORTA] = port_shadow[port - PORTA];
00130
00131 #define toggle_pin(port, pin) \
00132 port_shadow[port - PORTA] ^= (1 << (pin)); \
00133 port_array[port - PORTA] = port_shadow[port - PORTA];
00134
00135
00136 #define test_pin(port, pin) \
00137 ((port_array[port - PORTA] & (1 << pin)) != 0)
00138
00139 #define change_pin(port, pin, value) \
00140 if (value) { \
00141 set_pin(port, pin); \
00142 } else { \
00143 clear_pin(port, pin); \
00144 }
00145
00146 void set_pin_var(uns8 port, uns8 pin);
00147 void clear_pin_var(uns8 port, uns8 pin);
00148 void toggle_pin_var(uns8 port, uns8 pin);
00149 #define test_pin_var(port, pin) test_pin(port, pin)
00150 void change_pin_var(uns8 port, uns8 pin, bit value);
00151
00152
00153
00154
00155
00156
00157
00158 #define make_output(port, pin) clear_bit(tris_array[port - PORTA], pin)
00159 #define make_input(port, pin) set_bit(tris_array[port - PORTA], pin)
00160
00161 #define turn_peripheral_ints_on() set_bit(intcon, PEIE)
00162 #define turn_peripheral_ints_off() clear_bit(intcon, PEIE)
00163
00164 #define turn_global_ints_on() set_bit(intcon, GIE)
00165 #define turn_global_ints_off() clear_bit(intcon, GIE)
00166
00167
00168
00169 #define kill_interrupts() do { \
00170 bit store_gie; \
00171 intcon.GIE = 0; \
00172 } while (intcon.GIE != 0)
00173
00174
00175 #define start_crit_sec() \
00176 bit store_gie = intcon.GIE; \
00177 kill_interrupts()
00178
00179
00180 #define end_crit_sec() \
00181 intcon.GIE = store_gie
00182
00183 #if PLATFORM_CLOCK == 4000000
00184 #pragma CLOCK_FREQ 4000000
00185 #define GOT_CLOCK
00186 #endif
00187 #if PLATFORM_CLOCK == 16000000
00188 #pragma CLOCK_FREQ 16000000
00189 #define GOT_CLOCK
00190 #endif
00191 #if PLATFORM_CLOCK == 12000000
00192 #pragma CLOCK_FREQ 12000000
00193 #define GOT_CLOCK
00194 #endif
00195 #if PLATFORM_CLOCK == 20000000
00196 #pragma CLOCK_FREQ 20000000
00197 #define GOT_CLOCK
00198 #endif
00199 #if PLATFORM_CLOCK == 8000000
00200 #pragma CLOCK_FREQ 8000000
00201 #define GOT_CLOCK
00202 #endif
00203 #if PLATFORM_CLOCK == 4000000
00204 #pragma CLOCK_FREQ 4000000
00205 #define GOT_CLOCK
00206 #endif
00207
00208 #if PLATFORM_CLOCK == 48000000
00209 #pragma CLOCK_FREQ 48000000
00210 #define GOT_CLOCK
00211 #endif
00212
00213 #ifndef GOT_CLOCK
00214 #warning "Don't know about your clock. See pic_utils.h"
00215 #endif
00216
00217 #ifdef _PIC16
00218 #define boostbloader() \
00219 kill_interrupts(); \
00220 delay_ms(250); \
00221 delay_ms(250); \
00222 delay_ms(250); \
00223 delay_ms(250); \
00224 asm bcf _pclath,3 \
00225 asm bcf _pclath,4 \
00226 asm data 0x2000
00227 #endif
00228
00229
00230 #ifdef _PIC18
00231 #define boostbloader() \
00232 kill_interrupts(); \
00233 delay_ms(250); \
00234 delay_ms(250); \
00235 delay_ms(250); \
00236 delay_ms(250); \
00237 asm reset
00238 #endif
00239
00240 #define MAGIC_BOOSTBLOADER_REQUEST 4
00241
00242
00243 #endif