-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.c
556 lines (438 loc) · 12.3 KB
/
project.c
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Includes
//------------------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
#include <math.h>
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define ACC_ADDR 0x98 // Address for accelerometer 8 bit(lsb is not significant here, set to 0)
#define LCD_ADDR 0x7c // Address for LCD display 8 bit(lsb is not significant here, set to 0)
#define THERM_ADDR 0x90 // Address for thermometer 8 bit(lsb is not significant here, set to 0)
#define WRITE 0xfe
#define READ 0x01
// SMBus states:
// MT = Master Transmitter
// MR = Master Receiver
#define SMB_BUS_ERROR 0x00 // (all modes) BUS ERROR
#define SMB_START 0x08 // (MT & MR) START transmitted
#define SMB_RP_START 0x10 // (MT & MR) repeated START
#define SMB_MTADDACK 0x18 // (MT) Slave address + W transmitted;
// ACK received
#define SMB_MTADDNACK 0x20 // (MT) Slave address + W transmitted;
// NACK received
#define SMB_MTDBACK 0x28 // (MT) data byte transmitted; ACK rec’vd
#define SMB_MTDBNACK 0x30 // (MT) data byte transmitted; NACK rec’vd
#define SMB_MTARBLOST 0x38 // (MT) arbitration lost
#define SMB_MRADDACK 0x40 // (MR) Slave address + R transmitted;
// ACK received
#define SMB_MRADDNACK 0x48 // (MR) Slave address + R transmitted;
// NACK received
#define SMB_MRDBACK 0x50 // (MR) data byte rec’vd; ACK transmitted
#define SMB_MRDBNACK 0x58 // (MR) data byte rec’vd; NACK transmitted
//------------------------------------------------------------------------------------
// Global VARIABLES
//------------------------------------------------------------------------------------
bit SM_BUSY; // This bit is set when a send or receive is started
// It is cleared by the ISR when the operation is finished
unsigned char COMMAND;
char *DATA;
unsigned char BYTE_LEFT_S;
unsigned char BYTE_LEFT_R;
char ACCELERATION[3];
unsigned char TEMPERATURE[2];
unsigned char TEMP;
unsigned char index;
float DEGREE_CONV = 57.295f;
float BUFFERX[8];
float BUFFERY[8];
float BUFFERZ[8];
//01 clear display, c0 goto second line commands: 0x80
unsigned char FIRSTLINE[17] = "@X= ß Y= ß ";
unsigned char SECONDLINE[17]= "@Z= ß T=+ ßC ";
unsigned char clear_display[2] = {0x80, 0x01};
unsigned char goto_secondline[2] = {0x80, 0xc0};
bit rs;
bit getData;
bit sendData;
bit getTemp;
sbit Led = P0^6;
unsigned char pwm_width;
unsigned char count = 0;
unsigned char t2count = 0;
unsigned char t4count3 = 0;
unsigned char t4count10 = 0;
bit pwm_flag = 0;
bit raising = 1;
bit config = 0;
bit button_mode = 0;
bit Led_state = 1;
//------------------------------------------------------------------------------------
// function prototypes
//------------------------------------------------------------------------------------
void SMBUS_ISR (void);
void Timer2_ISR(void);
void Button_ISR(void);
void timer3_ISR(void);
void init_ACC(char *array_acc);
void send_DISPLAY(char *display_init, unsigned char length);
void read_ACC(void);
void read_THERM(void);
void Timer4_ISR(void);
float media(float *buffer);
void refresh_angle(float x, float y, float z);
void refresh_temperature(void);
//------------------------------------------------------------------------------------
// MAIN routine
//------------------------------------------------------------------------------------
void main (void) {
unsigned char toSend[8] = {0x38, 0x39, 0x14, 0x74, 0x54, 0x6f, 0x0c, 0x01};
float convert, sin, angle, mediax = 0, mediay = 0, mediaz = 0;
char array_acc[2] = {0x07, 0x01};
convert = 0.047;
EA = 0;
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
OSCICN &= 0x14; // Disable missing clock detector and set
// internal osc at 2 MHz as the clock source
XBR0 = 0x05; // Route SMBus to GPIO pins through crossbar, enable UART0
// in order to properly connect pins.
XBR2 = 0x40; // Enable crossbar and weak pull-ups
SMB0CN = 0x44; // Enable SMBus with ACKs on acknowledge cycle
SMB0CR = -80; // SMBus clock rate = 100kHz.
EIE1 |= 2; // SMBus interrupt enable
SM_BUSY = 0; // Free SMBus for first transfer.
P0MDOUT |= 0x40; // Set P1.6 to push-pull
pwm_width = 0;
EIE2 |= 0x20;
// Timer 2 (100 ms)
RCAP2H = 0xb8; // Reload value, 47150
RCAP2L = 0x2e;
TH2 = 0xb8; // Starting value, 47150
TL2 = 0x2e;
IE |= 0x20; // Enable Timer 2 interrupt (ET2)
// End Timer 2
// Timer 3 (dim)
TMR3H = 0xff; // Starting value, 65280
TMR3L = 0x00;
TMR3RLH = 0xff; // Reload value, 65280
TMR3RLL = 0x00;
EIE2 |= 0x01; // Enable Timer 3 interrupt (ET3)
TMR3CN |= 0x04; // Start Timer 3 (TR3)
// End Timer 3
// Timer 4 (100ms, always)
RCAP4H = 0xb8; // Reload value, 47150
RCAP4L = 0x2e;
TH4 = 0xb8; // Starting value, 47150
TL4 = 0x2e;
EIE2 |= 0x04; // Enable Timer 4 interrupt
T4CON |= 0x04; // Start Timer 4
// End Timer 4
getData = 0;
index = 0;
EA = 1; // Global interrupt enable
init_ACC(array_acc);
send_DISPLAY(toSend, 8);
while(1) {
if(getData){
read_ACC();
getData = 0;
if((ACCELERATION[0] >> 5) == 1)
ACCELERATION[0] -= 64;
sin = ACCELERATION[0] * convert;
if(sin >= -1 && sin <= 1)
angle = asin(sin);
BUFFERX[index] = angle * DEGREE_CONV;
if((ACCELERATION[1] >> 5) == 1)
ACCELERATION[1] -= 64;
sin = ACCELERATION[1] * convert;
if(sin >= -1 && sin <= 1)
angle = asin(sin);
BUFFERY[index] = angle * DEGREE_CONV;
if((ACCELERATION[2] >> 5) == 1)
ACCELERATION[2] -= 64;
sin = ACCELERATION[2] * convert;
if(sin >= -1 && sin <= 1)
angle = acos(sin);
else
angle = 0;
BUFFERZ[index] = angle * DEGREE_CONV;
if(index == 7)
index = 0;
else
index++;
}
if(sendData){
sendData = 0;
mediax = media(BUFFERX);
mediay = media(BUFFERY);
mediaz = media(BUFFERZ);
refresh_angle(mediax, mediay, mediaz);
send_DISPLAY(clear_display, 2);
send_DISPLAY(FIRSTLINE, 15);
send_DISPLAY(goto_secondline, 2);
send_DISPLAY(SECONDLINE, 15);
}
if(getTemp) {
getTemp = 0;
read_THERM();
refresh_temperature();
}
}
}
//------------------------------------------------------------------------------------
// SMBus interrupt service routine
//------------------------------------------------------------------------------------
void SMBUS_ISR (void) interrupt 7 {
switch (SMB0STA) { // Status code for SMBus
case SMB_START:
SMB0DAT = COMMAND;
STA = 0;
break;
case SMB_RP_START:
SMB0DAT = COMMAND | READ;
STA = 0;
break;
case SMB_MTADDACK:
SMB0DAT = *DATA;
DATA++;
BYTE_LEFT_S--;
STA = 0;
break;
case SMB_MTADDNACK:
STO = 1;
STA = 1;
break;
case SMB_MTDBACK:
if(BYTE_LEFT_S > 0) {
SMB0DAT = *DATA;
BYTE_LEFT_S--;
DATA++;
}
else if(rs){
STO = 0;
STA = 1;
}
else {
STO = 1;
SM_BUSY = 0;
}
break;
case SMB_MTDBNACK:
STA = 1;
break;
case SMB_MTARBLOST:
STO = 1;
break;
case SMB_MRADDACK:
STA = 0;
break;
case SMB_MRADDNACK:
STA = 1;
STO = 1;
break;
case SMB_MRDBACK:
if(BYTE_LEFT_R > 0){
if(COMMAND == 0x98)
ACCELERATION[3-BYTE_LEFT_R] = SMB0DAT;
else if(COMMAND == 0x91)
TEMPERATURE[2-BYTE_LEFT_R] = SMB0DAT;
BYTE_LEFT_R--;
}
else {
AA = 0;
STO = 0;
SM_BUSY = 0;
}
break;
case SMB_MRDBNACK:
STO = 1;
break;
}
SI = 0;
}
void send_DISPLAY(char *display_init, unsigned char length){
while(SM_BUSY);
SM_BUSY = 1;
SMB0CN = 0x44;
DATA = display_init;
COMMAND = LCD_ADDR & WRITE;
BYTE_LEFT_S = length;
BYTE_LEFT_R = 0;
rs = 0;
STO = 0;
STA = 1;
while(SM_BUSY);
}
void init_ACC(char *array_acc) {
while(SM_BUSY);
SM_BUSY = 1;
SMB0CN = 0x44;
COMMAND = ACC_ADDR;
DATA = array_acc;
BYTE_LEFT_S = 2;
rs = 0;
STO = 0;
STA = 1;
while(SM_BUSY);
}
void read_ACC(void) {
char begin = 0x00;
while(SM_BUSY);
SM_BUSY = 1;
SMB0CN = 0x44;
COMMAND = ACC_ADDR;
DATA = &begin; // funziona solo se c'è il while SM_BUSY in fondo (puntatore a variabile sullo stack)
BYTE_LEFT_R = 3;
BYTE_LEFT_S = 1;
rs = 1;
STO = 0;
STA = 1;
while(SM_BUSY);
}
void read_THERM() {
while(SM_BUSY);
SM_BUSY = 1;
SMB0CN = 0x44;
rs = 0;
COMMAND = THERM_ADDR | READ;
BYTE_LEFT_S = 0;
BYTE_LEFT_R = 2;
STO = 0;
STA = 1;
while(SM_BUSY);
}
void Timer2_ISR(void) interrupt 5 {
t2count++;
if (t2count > 10){ // Button remained pressed for more than 1000ms
config = 1;
}
T2CON &= 0x7f; // Clear Timer 2 overflow flag (TF2)
}
void Button_ISR(void) interrupt 19 {
button_mode = P3IF & 0x08;
P3IF ^= 0x08; // Toggle falling/rising
if(!button_mode){ // Pressing the button
TH2 = 0xb8; // "forced" reload
TL2 = 0x2e;
T2CON |= 0x04; // Start Timer 2 (TR2)
t2count = 0;
}
else if (button_mode && (t2count < 5)){ // Releasing the button in a 500ms window (fast press)
t2count = 0;
Led_state = !Led_state;
T2CON &= 0x00;
}
else if (button_mode && (t2count > 10)){ // Releasing the button after more than 1000ms
t2count = 0;
config = 0;
T2CON &= 0x00;
}
P3IF &= 0x7f; // reset interrupt bit
}
void timer3_ISR(void) interrupt 14 {
if (Led_state){
if (!pwm_flag) { // High level
pwm_flag = 1;
Led = 1; // Set PWM pin
TMR3L = 255 - pwm_width; // Load timer
} else { // Low level
pwm_flag = 0;
Led = 0; // Clear PWM pin
TMR3L = pwm_width; // Load timer
}
}
if (!Led_state) Led = 0;
if (config && count==10){ // Step Speed
if (raising) pwm_width++;
if (!raising) pwm_width--;
if (pwm_width == 250) raising = 0;
if (pwm_width == 5) raising = 1;
count = 0;
}
if (config) count++;
TMR3CN &= 0x7f; // Clear Timer 3 overflow flag (TF3)
}
void Timer4_ISR(void) interrupt 16 {
t4count3++;
t4count10++;
getData = 1;
if(t4count3 == 3){
sendData = 1;
t4count3 = 0;
}
if(t4count10 == 10){
getTemp = 1;
t4count10 = 0;
}
T4CON &= 0x7f;
}
float media(float *valori) {
unsigned char i = 0;
float acc = 0;
for (i; i<8; i++){
//if(!valori[i])
acc += valori[i];
}
return acc/8;
}
void refresh_angle(float x, float y, float z){
if(z >= 90){
z -= 90;
z = -z;
}
if(x >= 90){
x -= 90;
x = -x;
}
if(y >= 90){
y -= 90;
y = -y;
}
if (x >= 0)
FIRSTLINE[3] = '+';
else{
x = -x;
FIRSTLINE[3] = '-';
}
if (y >= 0)
FIRSTLINE[10] = '+';
else{
FIRSTLINE[10] = '-';
y = -y;
}
if (z >= 0)
SECONDLINE[3] = '+';
else{
SECONDLINE[3] = '-';
z = -z;
}
FIRSTLINE[4] = (unsigned char) ((x+0.5) / 10) + 48;
FIRSTLINE[5] = (unsigned char) (x+0.5) % 10;
FIRSTLINE[5] += 48;
FIRSTLINE[11] = (unsigned char) ((y+0.5) / 10) + 48;
FIRSTLINE[12] = (unsigned char) (y+0.5) % 10;
FIRSTLINE[12] += 48;
SECONDLINE[4] = (unsigned char) ((z+0.5) / 10) + 48;
SECONDLINE[5] = (unsigned char) (z+0.5) % 10;
SECONDLINE[5] += 48;
}
void refresh_temperature(void) {
unsigned char first_digit;
unsigned char second_digit;
TEMP = 0;
TEMPERATURE[1] &= 0xf8;
if(TEMPERATURE[0] >> 7) {
char ekomi = 'a';
// robe da fare se negativo mancano i due segni
}
else {
TEMP = TEMPERATURE[0];
TEMP = TEMP << 1;
TEMPERATURE[1] = TEMPERATURE[1] >> 7;
TEMP |= TEMPERATURE[1];
first_digit = TEMP / 10;
second_digit = TEMP % 10;
SECONDLINE[11] = first_digit + 48;
SECONDLINE[12] = second_digit + 48;
}
}