-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
461 lines (403 loc) · 9.97 KB
/
main.c
File metadata and controls
461 lines (403 loc) · 9.97 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* I2C Example project for the mcb32 toolchain
* Demonstrates the temperature sensor and display of the Basic IO Shield
* Make sure your Uno32-board has the correct jumper settings, as can be seen
* in the rightmost part of this picture:
* https://reference.digilentinc.com/_media/chipkit_uno32:jp6_jp8.png?w=300&tok=dcceb2
*/
#include <pic32mx.h>
#include <stdint.h>
#include <stdbool.h>
#define DISPLAY_VDD_PORT PORTF
#define DISPLAY_VDD_MASK 0x40
#define DISPLAY_VBATT_PORT PORTF
#define DISPLAY_VBATT_MASK 0x20
#define DISPLAY_COMMAND_DATA_PORT PORTF
#define DISPLAY_COMMAND_DATA_MASK 0x10
#define DISPLAY_RESET_PORT PORTG
#define DISPLAY_RESET_MASK 0x200
/* Address of the temperature sensor on the I2C bus */
#define TEMP_SENSOR_ADDR 0x48
/* Temperature sensor internal registers */
typedef enum TempSensorReg TempSensorReg;
enum TempSensorReg {
TEMP_SENSOR_REG_TEMP,
TEMP_SENSOR_REG_CONF,
TEMP_SENSOR_REG_HYST,
TEMP_SENSOR_REG_LIMIT,
};
char textbuffer[4][16];
static const uint8_t const font[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 2, 5, 2, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 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, 34, 20, 8, 0, 0,
0, 2, 1, 81, 9, 6, 0, 0,
0, 28, 34, 89, 89, 82, 12, 0,
0, 0, 126, 9, 9, 126, 0, 0,
0, 0, 127, 73, 73, 54, 0, 0,
0, 0, 62, 65, 65, 34, 0, 0,
0, 0, 127, 65, 65, 62, 0, 0,
0, 0, 127, 73, 73, 65, 0, 0,
0, 0, 127, 9, 9, 1, 0, 0,
0, 0, 62, 65, 81, 50, 0, 0,
0, 0, 127, 8, 8, 127, 0, 0,
0, 0, 65, 127, 65, 0, 0, 0,
0, 0, 32, 64, 64, 63, 0, 0,
0, 0, 127, 8, 20, 99, 0, 0,
0, 0, 127, 64, 64, 64, 0, 0,
0, 127, 2, 4, 2, 127, 0, 0,
0, 127, 6, 8, 48, 127, 0, 0,
0, 0, 62, 65, 65, 62, 0, 0,
0, 0, 127, 9, 9, 6, 0, 0,
0, 0, 62, 65, 97, 126, 64, 0,
0, 0, 127, 9, 9, 118, 0, 0,
0, 0, 38, 73, 73, 50, 0, 0,
0, 1, 1, 127, 1, 1, 0, 0,
0, 0, 63, 64, 64, 63, 0, 0,
0, 31, 32, 64, 32, 31, 0, 0,
0, 63, 64, 48, 64, 63, 0, 0,
0, 0, 119, 8, 8, 119, 0, 0,
0, 3, 4, 120, 4, 3, 0, 0,
0, 0, 113, 73, 73, 71, 0, 0,
0, 0, 127, 65, 65, 0, 0, 0,
0, 2, 4, 8, 16, 32, 64, 0,
0, 0, 0, 65, 65, 127, 0, 0,
0, 4, 2, 1, 2, 4, 0, 0,
0, 64, 64, 64, 64, 64, 64, 0,
0, 0, 1, 2, 4, 0, 0, 0,
0, 0, 48, 72, 40, 120, 0, 0,
0, 0, 127, 72, 72, 48, 0, 0,
0, 0, 48, 72, 72, 0, 0, 0,
0, 0, 48, 72, 72, 127, 0, 0,
0, 0, 48, 88, 88, 16, 0, 0,
0, 0, 126, 9, 1, 2, 0, 0,
0, 0, 80, 152, 152, 112, 0, 0,
0, 0, 127, 8, 8, 112, 0, 0,
0, 0, 0, 122, 0, 0, 0, 0,
0, 0, 64, 128, 128, 122, 0, 0,
0, 0, 127, 16, 40, 72, 0, 0,
0, 0, 0, 127, 0, 0, 0, 0,
0, 120, 8, 16, 8, 112, 0, 0,
0, 0, 120, 8, 8, 112, 0, 0,
0, 0, 48, 72, 72, 48, 0, 0,
0, 0, 248, 40, 40, 16, 0, 0,
0, 0, 16, 40, 40, 248, 0, 0,
0, 0, 112, 8, 8, 16, 0, 0,
0, 0, 72, 84, 84, 36, 0, 0,
0, 0, 8, 60, 72, 32, 0, 0,
0, 0, 56, 64, 32, 120, 0, 0,
0, 0, 56, 64, 56, 0, 0, 0,
0, 56, 64, 32, 64, 56, 0, 0,
0, 0, 72, 48, 48, 72, 0, 0,
0, 0, 24, 160, 160, 120, 0, 0,
0, 0, 100, 84, 84, 76, 0, 0,
0, 0, 8, 28, 34, 65, 0, 0,
0, 0, 0, 126, 0, 0, 0, 0,
0, 0, 65, 34, 28, 8, 0, 0,
0, 0, 4, 2, 4, 2, 0, 0,
0, 120, 68, 66, 68, 120, 0, 0,
};
void delay(int cyc) {
int i;
for(i = cyc; i > 0; i--);
}
uint8_t spi_send_recv(uint8_t data) {
while(!(SPI2STAT & 0x08));
SPI2BUF = data;
while(!(SPI2STAT & 0x01));
return SPI2BUF;
}
void display_init() {
DISPLAY_COMMAND_DATA_PORT &= ~DISPLAY_COMMAND_DATA_MASK;
delay(10);
DISPLAY_VDD_PORT &= ~DISPLAY_VDD_MASK;
delay(1000000);
spi_send_recv(0xAE);
DISPLAY_RESET_PORT &= ~DISPLAY_RESET_MASK;
delay(10);
DISPLAY_RESET_PORT |= DISPLAY_RESET_MASK;
delay(10);
spi_send_recv(0x8D);
spi_send_recv(0x14);
spi_send_recv(0xD9);
spi_send_recv(0xF1);
DISPLAY_VBATT_PORT &= ~DISPLAY_VBATT_MASK;
delay(10000000);
spi_send_recv(0xA1);
spi_send_recv(0xC8);
spi_send_recv(0xDA);
spi_send_recv(0x20);
spi_send_recv(0xAF);
}
void display_string(int line, char *s) {
int i;
if(line < 0 || line >= 4)
return;
if(!s)
return;
for(i = 0; i < 16; i++)
if(*s) {
textbuffer[line][i] = *s;
s++;
} else
textbuffer[line][i] = ' ';
}
void display_update() {
int i, j, k;
int c;
for(i = 0; i < 4; i++) {
DISPLAY_COMMAND_DATA_PORT &= ~DISPLAY_COMMAND_DATA_MASK;
spi_send_recv(0x22);
spi_send_recv(i);
spi_send_recv(0x0);
spi_send_recv(0x10);
DISPLAY_COMMAND_DATA_PORT |= DISPLAY_COMMAND_DATA_MASK;
for(j = 0; j < 16; j++) {
c = textbuffer[i][j];
if(c & 0x80)
continue;
for(k = 0; k < 8; k++)
spi_send_recv(font[c*8 + k]);
}
}
}
/* Wait for I2C bus to become idle */
void i2c_idle() {
while(I2C1CON & 0x1F || I2C1STAT & (1 << 14)); //TRSTAT
}
/* Send one byte on I2C bus, return ack/nack status of transaction */
bool i2c_send(uint8_t data) {
i2c_idle();
I2C1TRN = data;
i2c_idle();
return !(I2C1STAT & (1 << 15)); //ACKSTAT
}
/* Receive one byte from I2C bus */
uint8_t i2c_recv() {
i2c_idle();
I2C1CONSET = 1 << 3; //RCEN = 1
i2c_idle();
I2C1STATCLR = 1 << 6; //I2COV = 0
return I2C1RCV;
}
/* Send acknowledge conditon on the bus */
void i2c_ack() {
i2c_idle();
I2C1CONCLR = 1 << 5; //ACKDT = 0
I2C1CONSET = 1 << 4; //ACKEN = 1
}
/* Send not-acknowledge conditon on the bus */
void i2c_nack() {
i2c_idle();
I2C1CONSET = 1 << 5; //ACKDT = 1
I2C1CONSET = 1 << 4; //ACKEN = 1
}
/* Send start conditon on the bus */
void i2c_start() {
i2c_idle();
I2C1CONSET = 1 << 0; //SEN
i2c_idle();
}
/* Send restart conditon on the bus */
void i2c_restart() {
i2c_idle();
I2C1CONSET = 1 << 1; //RSEN
i2c_idle();
}
/* Send stop conditon on the bus */
void i2c_stop() {
i2c_idle();
I2C1CONSET = 1 << 2; //PEN
i2c_idle();
}
/* Convert 8.8 bit fixed point to string representation*/
char *fixed_to_string(uint16_t num, char *buf) {
bool neg = false;
uint32_t n;
char *tmp;
if(num & 0x8000) {
num = ~num + 1;
neg = true;
}
buf += 4;
n = num >> 8;
tmp = buf;
do {
*--tmp = (n % 10) + '0';
n /= 10;
} while(n);
if(neg)
*--tmp = '-';
n = num;
if(!(n & 0xFF)) {
*buf = 0;
return tmp;
}
*buf++ = '.';
while((n &= 0xFF)) {
n *= 10;
*buf++ = (n >> 8) + '0';
}
*buf = 0;
return tmp;
}
uint32_t strlen(char *str) {
uint32_t n = 0;
while(*str++)
n++;
return n;
}
int main(void) {
uint16_t temp;
char buf[32], *s, *t;
/* Set up peripheral bus clock */
OSCCON &= ~0x180000;
OSCCON |= 0x080000;
/* Set up output pins */
AD1PCFG = 0xFFFF;
ODCE = 0x0;
TRISECLR = 0xFF;
PORTE = 0x0;
/* Output pins for display signals */
PORTF = 0xFFFF;
PORTG = (1 << 9);
ODCF = 0x0;
ODCG = 0x0;
TRISFCLR = 0x70;
TRISGCLR = 0x200;
/* Set up input pins */
TRISDSET = (1 << 8);
TRISFSET = (1 << 1);
/* Set up SPI as master */
SPI2CON = 0;
SPI2BRG = 4;
/* Clear SPIROV*/
SPI2STATCLR &= ~0x40;
/* Set CKP = 1, MSTEN = 1; */
SPI2CON |= 0x60;
/* Turn on SPI */
SPI2CONSET = 0x8000;
/* Set up i2c */
I2C1CON = 0x0;
/* I2C Baud rate should be less than 400 kHz, is generated by dividing
the 40 MHz peripheral bus clock down */
I2C1BRG = 0x0C2;
I2C1STAT = 0x0;
I2C1CONSET = 1 << 13; //SIDL = 1
I2C1CONSET = 1 << 15; // ON = 1
temp = I2C1RCV; //Clear receive buffer
/* Set up input pins */
TRISDSET = (1 << 8);
TRISFSET = (1 << 1);
display_init();
display_string(0, "Temperature:");
display_string(1, "");
display_string(2, "");
display_string(3, "");
display_update();
/* Send start condition and address of the temperature sensor with
write mode (lowest bit = 0) until the temperature sensor sends
acknowledge condition */
do {
i2c_start();
} while(!i2c_send(TEMP_SENSOR_ADDR << 1));
/* Send register number we want to access */
i2c_send(TEMP_SENSOR_REG_CONF);
/* Set the config register to 0 */
i2c_send(0x0);
/* Send stop condition */
i2c_stop();
for(;;) {
/* Send start condition and address of the temperature sensor with
write flag (lowest bit = 0) until the temperature sensor sends
acknowledge condition */
do {
i2c_start();
} while(!i2c_send(TEMP_SENSOR_ADDR << 1));
/* Send register number we want to access */
i2c_send(TEMP_SENSOR_REG_TEMP);
/* Now send another start condition and address of the temperature sensor with
read mode (lowest bit = 1) until the temperature sensor sends
acknowledge condition */
do {
i2c_start();
} while(!i2c_send((TEMP_SENSOR_ADDR << 1) | 1));
/* Now we can start receiving data from the sensor data register */
temp = i2c_recv() << 8;
i2c_ack();
temp |= i2c_recv();
/* To stop receiving, send nack and stop */
i2c_nack();
i2c_stop();
s = fixed_to_string(temp, buf);
t = s + strlen(s);
*t++ = ' ';
*t++ = 7;
*t++ = 'C';
*t++ = 0;
display_string(1, s);
display_update();
delay(1000000);
}
return 0;
}