-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicduino.c
More file actions
286 lines (246 loc) · 9.51 KB
/
picduino.c
File metadata and controls
286 lines (246 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//PICduino code base for PIC10/12/16/18 chips
#include "picduino_config.h" //configuration words - for XC8
#include "picduino.h" //PICduino-related definitions / prototypes
//global variables
//global variables
//for time base off TIMER1 @ 1:1 prescaler
//volatile uint32_t timer1_millis = 0;
volatile uint32_t timer_ticks = 0;
//static uint16_t timer1_fract = 0;
//timer1 overflow isr
//void _ISR _T1Interrupt(void) {
void interrupt isr(void) {
if (T0IF) {
T0IF = 0; //_T1IF=0; //clear tmr1 interrupt flag
timer_ticks+=0x100ul; //increment overflow count: 8-bit timer
}
}
//declare pins
//ALL PINS ARE MAPPED, WHETHER THEY EXIST OR NOT
//SO MAKE SURE THAT THE PINS YOU PICKED ACTUALLY EXIST FOR YOUR PACKAGE
//Pin 0.. 7 -> GPIOA
//Pin 8..15 -> GPIOB
//Pin 16..23 -> GPIOC
//Pin 24..31 -> GPIOD
//Pin 32..39 -> GPIOE
//Pin 40..47 -> GPIOF
//Pin 48..55 -> GPIOG
//Pin 56..63 -> GPIOH
//Pin 64..71 -> GPIOI
const PIN2GPIO GPIO_PinDef[]={
#if defined(USE_GPIO)
{&GPIO, &TRISIO, 1<<0}, //PICduino Pin 0 = RP0/PB0/CHIP PIN4
{&GPIO, &TRISIO, 1<<1}, //PICduino Pin 1 = RP1/PB1/CHIP PIN5
{&GPIO, &TRISIO, 1<<2}, //PICduino Pin 2 = RP2/PB2/CHIP PIN6
{&GPIO, &TRISIO, 1<<3}, //PICduino Pin 3 = RP3/PB3/CHIP PIN7
{&GPIO, &TRISIO, 1<<4}, //PICduino Pin 4 = RP4/PB4/CHIP PIN11
{&GPIO, &TRISIO, 1<<5}, //PICduino Pin 5 = RP5/PB5/CHIP PIN14
{&GPIO, &TRISIO, 1<<6}, //PICduino Pin 6 = RP6/PB6/CHIP PIN15
{&GPIO, &TRISIO, 1<<7}, //PICduino Pin 7 = RP7/PB7/CHIP PIN16
#endif
#if defined(USE_PORTA)
{&PORTA, &TRISA, 1<<0}, //PICduino Pin 16 = RP0/PB0/CHIP PIN4
{&PORTA, &TRISA, 1<<1}, //PICduino Pin 17 = RP1/PB1/CHIP PIN5
{&PORTA, &TRISA, 1<<2}, //PICduino Pin 18 = RP2/PB2/CHIP PIN6
{&PORTA, &TRISA, 1<<3}, //PICduino Pin 19 = RP3/PB3/CHIP PIN7
{&PORTA, &TRISA, 1<<4}, //PICduino Pin 20 = RP4/PB4/CHIP PIN11
{&PORTA, &TRISA, 1<<5}, //PICduino Pin 21 = RP5/PB5/CHIP PIN14
{&PORTA, &TRISA, 1<<6}, //PICduino Pin 22 = RP6/PB6/CHIP PIN15
{&PORTA, &TRISA, 1<<7}, //PICduino Pin 23 = RP7/PB7/CHIP PIN16
#endif
#if defined(USE_PORTB)
{&PORTB, &TRISB, 1<<0}, //PICduino Pin 16 = RP0/PB0/CHIP PIN4
{&PORTB, &TRISB, 1<<1}, //PICduino Pin 17 = RP1/PB1/CHIP PIN5
{&PORTB, &TRISB, 1<<2}, //PICduino Pin 18 = RP2/PB2/CHIP PIN6
{&PORTB, &TRISB, 1<<3}, //PICduino Pin 19 = RP3/PB3/CHIP PIN7
{&PORTB, &TRISB, 1<<4}, //PICduino Pin 20 = RP4/PB4/CHIP PIN11
{&PORTB, &TRISB, 1<<5}, //PICduino Pin 21 = RP5/PB5/CHIP PIN14
{&PORTB, &TRISB, 1<<6}, //PICduino Pin 22 = RP6/PB6/CHIP PIN15
{&PORTB, &TRISB, 1<<7}, //PICduino Pin 23 = RP7/PB7/CHIP PIN16
#endif
#if defined(USE_PORTC)
{&PORTC, &TRISC, 1<<0}, //PICduino Pin 32 = RP0/PB0/CHIP PIN4
{&PORTC, &TRISC, 1<<1}, //PICduino Pin 33 = RP1/PB1/CHIP PIN5
{&PORTC, &TRISC, 1<<2}, //PICduino Pin 34 = RP2/PB2/CHIP PIN6
{&PORTC, &TRISC, 1<<3}, //PICduino Pin 35 = RP3/PB3/CHIP PIN7
{&PORTC, &TRISC, 1<<4}, //PICduino Pin 36 = RP4/PB4/CHIP PIN11
{&PORTC, &TRISC, 1<<5}, //PICduino Pin 37 = RP5/PB5/CHIP PIN14
{&PORTC, &TRISC, 1<<6}, //PICduino Pin 38 = RP6/PB6/CHIP PIN15
{&PORTC, &TRISC, 1<<7}, //PICduino Pin 39 = RP7/PB7/CHIP PIN16
#endif
#if defined(USE_PORTD)
{&PORTD, &TRISD, 1<<0}, //PICduino Pin 16 = RP0/PB0/CHIP PIN4
{&PORTD, &TRISD, 1<<1}, //PICduino Pin 17 = RP1/PB1/CHIP PIN5
{&PORTD, &TRISD, 1<<2}, //PICduino Pin 18 = RP2/PB2/CHIP PIN6
{&PORTD, &TRISD, 1<<3}, //PICduino Pin 19 = RP3/PB3/CHIP PIN7
{&PORTD, &TRISD, 1<<4}, //PICduino Pin 20 = RP4/PB4/CHIP PIN11
{&PORTD, &TRISD, 1<<5}, //PICduino Pin 21 = RP5/PB5/CHIP PIN14
{&PORTD, &TRISD, 1<<6}, //PICduino Pin 22 = RP6/PB6/CHIP PIN15
{&PORTD, &TRISD, 1<<7}, //PICduino Pin 23 = RP7/PB7/CHIP PIN16
#endif
};
//Arduino Functions: GPIO
//set a pin mode to INPUT or OUTPUT
//no error checking on PIN
void pinMode(PIN_TypeDef pin, uint8_t mode) {
if (mode==INPUT) IO_IN(*(GPIO_PinDef[pin].ddr), GPIO_PinDef[pin].mask);
else IO_OUT(*(GPIO_PinDef[pin].ddr), GPIO_PinDef[pin].mask);
}
//set / clear a pin
inline void digitalWrite(PIN_TypeDef pin, uint8_t val) {
if (val==LOW) IO_CLR(*(GPIO_PinDef[pin].port), GPIO_PinDef[pin].mask);
else IO_SET(*(GPIO_PinDef[pin].port), GPIO_PinDef[pin].mask);
}
//read a pin
inline uint8_t digitalRead(PIN_TypeDef pin) {
return (IO_GET(*(GPIO_PinDef[pin].port), GPIO_PinDef[pin].mask))?HIGH:LOW;
}
//end GPIO
//Arduino Functions: Time
//return timer ticks
uint32_t ticks(void) {
uint32_t m;
uint8_t f; //for 8-bit timer0
do {
m = timer_ticks;
f = TMR0;
} while (m != timer_ticks);
//now m and f are consistent
return (m | f) << 8; //corrected for prescaler
}
//return microseconds
uint32_t micros(void) {
uint32_t m; //stores overflow count
uint8_t f; //return the fractions / TMR1 value
//use double reads
do {
m = timer_ticks;
f = TMR0;
} while (m != timer_ticks);
//now m and f are consistent
return ((m | f) / clockCyclesPerMicrosecond()) << 8;
}
//return milliseconds
//alternatively, = micros()/1000
uint32_t millis(void) {
uint32_t m;
uint8_t f;
//use double reads
do {
m = timer_ticks;
f = TMR0;
} while (m != timer_ticks);
return ((m | f) / clockCyclesPerMillisecond()) << 8;
}
//delay millisseconds
void delay(uint32_t ms) {
uint32_t start_time = ticks();
ms *= clockCyclesPerMillisecond();
while (ticks() - start_time < ms) continue;
}
//delay micros seconds
void delayMicroseconds(uint32_t us) {
uint32_t start_time = ticks();
us *= clockCyclesPerMicrosecond();
while (ticks() - start_time < us) continue;
}
//end Time
//Arduino Functions: Advanced IO
//shift in - from arduino code base / not optimized
uint8_t shiftIn(PIN_TypeDef dataPin, PIN_TypeDef clockPin, uint8_t bitOrder) {
uint8_t value = 0;
uint8_t i;
for (i = 0; i < 8; ++i) {
digitalWrite(clockPin, HIGH);
if (bitOrder == LSBFIRST)
value |= digitalRead(dataPin) << i;
else
value |= digitalRead(dataPin) << (7 - i);
digitalWrite(clockPin, LOW);
}
return value;
}
//shift out - from arduino code base / not optimized
void shiftOut(PIN_TypeDef dataPin, PIN_TypeDef clockPin, uint8_t bitOrder, uint8_t val) {
uint8_t i;
for (i = 0; i < 8; i++) {
if (bitOrder == LSBFIRST)
digitalWrite(dataPin, !!(val & (1 << i)));
else
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
}
//wait for a pulse and return timing
uint32_t pulseIn(PIN_TypeDef pin, uint8_t state) {
uint32_t tmp;
state = (state == LOW)?LOW:HIGH;
while (digitalRead(pin) == state) continue; //wait for the pin to opposite
//now pin is _state
tmp = micros();
state = (state == LOW)?HIGH:LOW; //calculate the state to end the wait
while (digitalRead(pin) == state) continue; //wait for the pin to go back to its original state
tmp = micros() - tmp; //calculate the pulse width
return tmp;
}
//end Advanced IO
//reset the mcu
//start timer1 at 1:1 prescaler
void mcu_init(void) {
//set the clock bits if so desired
//initialize pins into gpio mode
#if defined(_12F629) || defined(_12F675)
CMCON = 0x07; //turn off analog comparator
ANSEL = 0x00; //all pins gpio
#elif defined(_16F684)
ANSEL = 0x00; //porta are digital io
//ANSELH = 0x00; //all portB is digital io
CM2=CM1=CM0=1; //CMCON0 = 0x07; //analog comparators off
IRCF2 = 1, IRCF1 = 0, IRCF0 = 1; //0b111->16Mhz, 0b101->4Mhz, ircf2..0=0b011 -> 1Mhz
#elif defined(_16F882) || defined(_16F883) || defined(_16F884) || defined(_16F886) || defined(_16F887)
ANSEL = ANSELH = 0; //all ports digital
C1ON = 0; //CM1CON0 = 0x00; //turn off comparator 1
C2ON = 0; //CM2CON0 = 0x00; //turn off comparator 2
IRCF2 = 1, IRCF1 = 1, IRCF0 = 0; //0b110 = 4Mhz.
//18f2455/2550/4455/4550
//18f2458/2553/4458/4553
#elif defined(_18F2455) || defined(_18F2550) || defined(_18F4455) || defined(_18F4550) || \
defined(_18F2458) || defined(_18F2553) || defined(_18F4458) || defined(_18F4553)
ADCON1 = 0x0f; //all pins digital
CMCON = 0x07; //all analog comparators off
IRCF2 = 1, IRCF1 = 1, IRCF0 = 0; //0b111->8Mhz, 0b110->4Mhz, 0b101->2Mhz, ...
#endif
//initialize timer1 as time base
//initialize timer_ticks
timer_ticks=0;
//configure timer0 as time base
T0CS = 0; //use internal clock = Fosc / 4
PSA = 0; //prescaler assigned to tmr0
#if defined(_18F2455) || defined(_18F2550) || defined(_18F4455) || defined(_18F4550) || \
defined(_18F2458) || defined(_18F2553) || defined(_18F4458) || defined(_18F4553)
#define TMR0 TMR0H //for compatability
// tmr0 prescaler bits in OPTION_REG - on most chips
T08BIT = 1; //tmr0 is 16-bit
T0PS2=T0PS1=T0PS0=0; //0b000->prescaler = 1:1 -> lowest 8bits ignored
TMR0ON = 1; //turn on tmr0
#else
// tmr0 prescaler bits in OPTION_REG - on most chips
PS2=PS1=PS0=1; //0b111->prescaler = 256:1
#endif
//tmr0_offset=-period; //update tmr0_period
TMR0 = 0; //tmr0_offset; //set the period
T0IF = 0; //clera the flag
T0IE = 1; //enable timer0 interrupt
//enable all interrupts
interrupts();
}
//templated code for main()
int main(void) {
mcu_init(); //reset the chip
setup(); //user-set up the code
while (1) {
loop(); //user specified loop
}
return 0;
}