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
00048
00049
00050
00051 extern uns8 port_shadow[NUMBER_PORTS];
00052 volatile uns8 port_array[NUMBER_PORTS] @PORTA;
00053 volatile uns8 tris_array[NUMBER_PORTS] @TRISA;
00054
00055
00056
00057
00058
00059
00060 #ifdef _PIC16F88
00061 #define turn_analog_inputs_off() ansel = 0
00062 #endif
00063 #ifdef _PIC16F876A
00064 #define turn_analog_inputs_off() adcon1 = 6
00065 #endif
00066 #ifdef _PIC18F4520
00067 #define turn_analog_inputs_off() adcon1 = 15
00068 #endif
00069 #ifdef _PIC18F452
00070 #define turn_analog_inputs_off() adcon1 = 6
00071 #endif
00072 #ifdef _PIC18F2620
00073 #define turn_analog_inputs_off() adcon1 = 15
00074 #endif
00075 #ifndef turn_analog_inputs_off()
00076 #warning "!!! - See pic_utils.h for turning off analog inputs on your pic"
00077 #define turn_analog_intputs_off()
00078 #endif
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 #define set_pin(port, pin) \
00113 set_bit(port_shadow[port - PORTA], pin); \
00114 port_array[port - PORTA] = port_shadow[port - PORTA];
00115
00116 #define clear_pin(port, pin) \
00117 clear_bit(port_shadow[port - PORTA], pin); \
00118 port_array[port - PORTA] = port_shadow[port - PORTA];
00119
00120 #define toggle_pin(port, pin) \
00121 port_shadow[port - PORTA] ^= (1 << (pin)); \
00122 port_array[port - PORTA] = port_shadow[port - PORTA];
00123
00124
00125 #define test_pin(port, pin) \
00126 ((port_array[port - PORTA] & (1 << pin)) != 0)
00127
00128 #define change_pin(port, pin, value) \
00129 if (value) { \
00130 set_pin(port, pin); \
00131 } else { \
00132 clear_pin(port, pin); \
00133 }
00134
00135 void set_pin_var(uns8 port, uns8 pin);
00136 void clear_pin_var(uns8 port, uns8 pin);
00137 void toggle_pin_var(uns8 port, uns8 pin);
00138 #define test_pin_var(port, pin) test_pin(port, pin)
00139 void change_pin_var(uns8 port, uns8 pin, bit value);
00140
00141
00142
00143
00144
00145
00146
00147 #define make_output(port, pin) clear_bit(tris_array[port - PORTA], pin)
00148 #define make_input(port, pin) set_bit(tris_array[port - PORTA], pin)
00149
00150 #define turn_peripheral_ints_on() set_bit(intcon, PEIE)
00151 #define turn_peripheral_ints_off() clear_bit(intcon, PEIE)
00152
00153 #define turn_global_ints_on() set_bit(intcon, GIE)
00154 #define turn_global_ints_off() clear_bit(intcon, GIE)
00155
00156
00157
00158 #define kill_interrupts() do { \
00159 bit store_gie; \
00160 intcon.GIE = 0; \
00161 } while (intcon.GIE != 0)
00162
00163
00164 #define start_crit_sec() \
00165 bit store_gie = intcon.GIE; \
00166 kill_interrupts()
00167
00168
00169 #define end_crit_sec() \
00170 intcon.GIE = store_gie
00171
00172 #ifdef PLATFORM_CLOCK 4000000
00173 #pragma CLOCK_FREQ 4000000
00174 #endif
00175 #ifdef PLATFORM_CLOCK 12000000
00176 #pragma CLOCK_FREQ 12000000
00177 #endif
00178 #ifdef PLATFORM_CLOCK 20000000
00179 #pragma CLOCK_FREQ 20000000
00180 #endif
00181 #ifdef PLATFORM_CLOCK 8000000
00182 #pragma CLOCK_FREQ 8000000
00183 #endif
00184
00185
00186 #ifdef _PIC16
00187 #define boostbloader() \
00188 kill_interrupts(); \
00189 delay_ms(250); \
00190 delay_ms(250); \
00191 delay_ms(250); \
00192 delay_ms(250); \
00193 asm bcf _pclath,3 \
00194 asm bcf _pclath,4 \
00195 asm data 0x2000
00196 #endif
00197
00198
00199 #ifdef _PIC18
00200 #define boostbloader() \
00201 kill_interrupts(); \
00202 delay_ms(250); \
00203 delay_ms(250); \
00204 delay_ms(250); \
00205 delay_ms(250); \
00206 asm reset
00207 #endif
00208
00209 #define MAGIC_BOOSTBLOADER_REQUEST 4
00210
00211
00212 #endif