-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
635 lines (460 loc) · 17.1 KB
/
main.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include <stdint.h>
#include <stdbool.h>
#include "15348.h"
#include "serial.h"
#include "timer.h"
#include <math.h>
#include "finalProjectConstants.h"
//#include "TM4C123GH6PM.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "Serial.h"
#include "timer.h"
#include "15348.h"
//#include "TM4C123GH6PM.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "MPU6050.h"
void PLLInit()
{
SYSCTL_RCC2_R |= 0x80000000;
SYSCTL_RCC2_R |= 0x00000800;
SYSCTL_RCC_R = (SYSCTL_RCC_R & ~0x000007C0) + 0x00000540;
SYSCTL_RCC2_R &= ~0x00000070;
SYSCTL_RCC2_R &= ~0x00002000;
SYSCTL_RCC2_R |= 0x40000000;
SYSCTL_RCC2_R = (SYSCTL_RCC2_R & ~0x1FC00000) + (4 << 22);
while ((SYSCTL_RIS_R & 0x00000040) == 0)
{
};
SYSCTL_RCC2_R &= ~0x00000800;
}
// ADC initialization
volatile unsigned long delay;
void ADC_Init(void){
SYSCTL_RCGCADC_R |= 0x01; // activate ADC0
SYSCTL_RCGCGPIO_R |= 0x10; // activate clock for PORT E
SYSCTL_RCGCTIMER_R |= 0x01; // activate timer0
GPIO_PORTE_DIR_R &= ~0x1F; // make PE0-4 input
GPIO_PORTE_AFSEL_R |= 0x1F; // enable alternate fun on PE0-4
GPIO_PORTE_DEN_R &= ~0x1F; // disable digital I/O on PE0-4
GPIO_PORTE_AMSEL_R |= 0x1F; // enable analog fun on PE0-4
// extra time to stabilize
delay = SYSCTL_RCGCADC_R;
delay = SYSCTL_RCGCADC_R;
delay = SYSCTL_RCGCADC_R;
delay = SYSCTL_RCGCADC_R;
// configuring ADC
ADC0_PC_R = 0x01; // 125k sampling rate
ADC0_SSPRI_R = 0x3210; // Sequencer priorities
// configuring timer
TIMER0_CTL_R = 0x0; // disable timer during setup
TIMER0_CTL_R |= 0x20; // enable timer0A trigger to ADC
TIMER0_CFG_R = 0; // 32-bit timer mode
TIMER0_TAMR_R = 0x02; // configure for periodic mode
TIMER0_TAPR_R = 0; // pre-scale value
TIMER0_TAILR_R = 0x2625A00; // 500 ms
TIMER0_IMR_R = 0x0; // disable all interrupts
TIMER0_CTL_R |= 0x01; // enable timer0A
// configuring ADC - more
ADC0_ACTSS_R &= ~0x01; // disable SS0
ADC0_EMUX_R = (ADC0_EMUX_R&0xFFFFFFF0) + 0x0005; // Seq0 timer trigger
ADC0_SSMUX0_R = 0x93210; // channels. Sample AIN0 (PE3), then AIN1 (PE2), then AIN2 (PE1), then AIN3 (PE0), then AIN9 (PE4)
ADC0_SSCTL0_R = 0x60000; // set flag (ENDS AT 5th SAMPLE)
ADC0_IM_R |= 0x01; // enable SS0 interrupts
ADC0_ACTSS_R |= 0x01; // enable SS0
// enabling interrupts
NVIC_PRI3_R = (NVIC_PRI3_R&0xFF00FFFF) | 0x00400000; // ADC0 priority 2
NVIC_EN0_R = 1 << 14;
}
void PortF_Init(void)
{
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000020; // 1) F clock
delay = SYSCTL_RCGC2_R; // reading register adds a delay, which we might need
GPIO_PORTF_LOCK_R = 0x4C4F434B; // 2) unlock PortF PF0
GPIO_PORTF_CR_R = 0x1F; // 3) allow changes to PF4-0
GPIO_PORTF_AMSEL_R = 0x00; // 4) disable analog function
GPIO_PORTF_AFSEL_R = 0x00; // 5) no alternate function
GPIO_PORTF_PCTL_R = 0x00000000; // 6) GPIO clear bit PCTL
GPIO_PORTF_PUR_R = 0x11; // 7) enable pullup resistors on PF4,PF0
GPIO_PORTF_DEN_R = 0x1F; // 8) enable digital pins PF4-PF0
GPIO_PORTF_DIR_R = 0x0E; // 9) PF4,PF0 input, PF3,PF2,PF1 output
}
void PortD_Init(void)
{
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000008; // 1) D clock
delay = SYSCTL_RCGC2_R; // reading register adds a delay, which we might need
GPIO_PORTD_LOCK_R = 0x4C4F434B; // 2) unlock PortD
GPIO_PORTD_CR_R |= 0x0F; // 3) allow changes to PD
GPIO_PORTD_AMSEL_R = 0x00; // 4) disable analog function
GPIO_PORTD_AFSEL_R = 0x00; // 5) no alternate function
GPIO_PORTD_PCTL_R = 0x00000000; // 6) GPIO clear bit PCTL
GPIO_PORTD_PUR_R = 0x00; // 7) disable pull up resistors
GPIO_PORTD_DEN_R |= 0x0F; // 8) enable digital pins PD
GPIO_PORTD_DIR_R |= 0x0F; // 9) PD output
GPIO_PORTD_DATA_R = 0x0;
}
void PortA_Init(void)
{
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000001; // 1) A clock
delay = SYSCTL_RCGC2_R; // reading register adds a delay, which we might need
GPIO_PORTA_LOCK_R = 0x4C4F434B; // 2) unlock PortA
GPIO_PORTA_CR_R |= 0xF0; // 3) allow changes to PA
GPIO_PORTA_AMSEL_R &= ~0xF0; // 4) disable analog function
GPIO_PORTA_AFSEL_R &= ~0xF0; // 5) no alternate function
GPIO_PORTA_PCTL_R &= ~0xFFFF0000; // 6) GPIO clear bit PCTL
GPIO_PORTA_PUR_R &= ~0xF0; // 7) disable pull up resistors
GPIO_PORTA_DEN_R |= 0xF0; // 8) enable digital pins PA
GPIO_PORTA_DIR_R |= 0xF0; // 9) PA output
GPIO_PORTA_DATA_R &= ~0xF0;
}
// WAIT functions
void delay_us(int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<3;j++) {}
}
// LCD helper functions - https://microcontrollerslab.com/16x2-lcd-interfacing-with-tm4c123-tiva-launchpad-keil-uvision/
void writeNibble(unsigned char data, unsigned char control){
// take four upper bytes of data
data &= 0xF0;
// write data and set control (RS - cmd or data)
GPIO_PORTA_DATA_R |= data;
GPIO_PORTD_DATA_R = control;
// enable pins
GPIO_PORTA_DATA_R |= data;
GPIO_PORTD_DATA_R = control | 0x04;
delay_us(0);
// disable pins - pulsed enable
GPIO_PORTA_DATA_R |= data;
GPIO_PORTD_DATA_R = control;
// clear data
GPIO_PORTA_DATA_R &= ~0xF0;
GPIO_PORTD_DATA_R = 0;
}
void sendCommand(unsigned char command){
// send upper and lower 4 bits one after the other
writeNibble(command & 0xF0, 0x0);
writeNibble(command << 4, 0x0);
if (command < 4){
SysTick_Wait1ms(2);
}
else {
delay_us(40);
}
}
void writeChar(unsigned char data){
writeNibble(data & 0xF0, 0x01);
writeNibble(data << 4, 0x01);
delay_us(40);
}
void writeString(char *str)
{
int i;
for(i=0; str[i]!=0; i++)
{
delay_us(40);
writeChar(str[i]);
}
}
// LCD initialization
void LCDInit(void)
{
sendCommand(Set5x7FontSize);
sendCommand(Function_set_4bit);
sendCommand(moveCursorRight);
sendCommand(clear_display);
sendCommand(cursorBlink);
}
// SysTick Interrupt initialization
void SysTickInterruptInit()
{
NVIC_ST_CTRL_R = 0;
NVIC_ST_RELOAD_R = 80000; // exactly 1ms
NVIC_ST_CURRENT_R = 0;
NVIC_ST_CTRL_R = 0x00000007;
}
unsigned long timer_val1 = 0;
unsigned long timer_val2 = 0;
void SysTick_Handler(void)
{
timer_val1 = timer_val1 + 1;
timer_val2 = timer_val2 + 1;
}
uint32_t sensor1 = 0;
uint32_t sensor2 = 0;
uint32_t sensor3 = 0;
uint32_t sensor4 = 0;
uint32_t sensor5 = 0;
uint32_t filteredSensor1 = 0;
uint32_t filteredSensor2 = 0;
uint32_t filteredSensor3 = 0;
uint32_t filteredSensor4 = 0;
uint32_t filteredSensor5 = 0;
unsigned long interruptCount = 0;
char currLetter = '\0';
char dispLetter = '\0';
char msg[60];
float AX, AY, AZ;
float GX, GY, GZ;
char *handOrienStatus = ""; // "LEANING"
// variables to construct word
bool button1Status = 0;
bool button2Status = 0;
bool dispWord = 0;
void getHandOrienStatus (void){
// plank
if ((AX < 20) && (AY > 20)) handOrienStatus = "PLANK";
// leaning
if ((AX > 20) && (AY > 20)) handOrienStatus = "LEANING";
// otherwise
if ((AX < 20) && (AY < 20)) handOrienStatus = "";
}
bool listEq(char **list1, char **list2, int n){
int i;
for (i = 0; i < n; i++) if (list1[i] != list2[i]) return 0;
return 1;
}
// each sensor has 3 states: BENT, MID or UNBENT - for non-complex letters
char *fingStatus[5] = {"UNBENT", "UNBENT", "UNBENT", "UNBENT", "UNBENT"};
void getFingStatus(void){
// sensor 1
if (filteredSensor1 <= 800) fingStatus[0] = "UNBENT";
else if (800 < filteredSensor1 && filteredSensor1 <= 900) fingStatus[0] = "MID";
else if (900 < filteredSensor1) fingStatus[0] = "BENT";
// sensor 2
if (filteredSensor2 < 100) fingStatus[1] = "UNBENT";
else if (150 < filteredSensor2) fingStatus[1] = "BENT";
// sensor 3
if (filteredSensor3 <= 650) fingStatus[2] = "UNBENT";
else if (650 < filteredSensor3) fingStatus[2] = "BENT";
// sensor 4
if (filteredSensor4 <= 100) fingStatus[3] = "UNBENT";
else if (100 < filteredSensor4) fingStatus[3] = "BENT";
// sensor 5
if (filteredSensor5 <= 100) fingStatus[4] = "UNBENT";
else if (100 < filteredSensor5) fingStatus[4] = "BENT";
}
// de-bouncing using state machine from demo code
// We have four states
#define WAITING 0x00
#define PRESSED 0x01
#define DEBOUNCE_RELEASE 0x02
#define RELEASED 0x03
struct State
{
unsigned char next[2]; // Next state based on 1-bit input (switch value)
unsigned char out; // Whether or not we "output"/take action in this take. (0 or non-zero)
unsigned int time; // min time to stay in this state (intervals of 1 ms)
};
struct State FSM[4] = {
{ { WAITING, PRESSED }, 0x00, 0 }, // State WAITING
{ { DEBOUNCE_RELEASE, PRESSED }, 0x00, 7 }, // State PRESSED
{ { RELEASED, RELEASED }, 0x00, 3 }, // State DEBOUNCE_RELEASE
{ { WAITING, PRESSED }, 0x01, 0 } // State RELEASED
};
unsigned char curState1 = WAITING;
unsigned char curState2 = WAITING;
bool getButtonStatus(int buttonNum){
// get button one status
if (buttonNum == 1){
// Check if we've been in the state long enough to move on if needed
if (timer_val1 >= FSM[curState1].time) {
// Check SW1 status
int inp = (GPIO_PORTF_DATA_R & 0x10)>>4;
inp ^= 0x01;
// Choose the next state
if (curState1 != FSM[curState1].next[inp]) {
// If we are going to change states, then reset the counter
timer_val1 = 0;
}
curState1 = FSM[curState1].next[inp];
}
return FSM[curState1].out;
}
// get button two status
if (timer_val2 >= FSM[curState2].time) {
// Check SW2 status
int inp = (GPIO_PORTF_DATA_R & 0x01);
inp ^= 0x01;
// Choose the next state
if (curState2 != FSM[curState2].next[inp]) {
// If we are going to change states, then reset the counter
timer_val2 = 0;
}
curState2 = FSM[curState2].next[inp];
}
return FSM[curState2].out;
}
char getCurrASLLetter(void){
// with orientation
SerialWrite(handOrienStatus);
// P
char *P[5] = {"UNBENT", "UNBENT", "UNBENT", "BENT", "BENT"};
if (listEq(fingStatus, P, 5) && (handOrienStatus == "PLANK")) return 'p';
// Q
char *Q[5] = {"UNBENT", "UNBENT", "BENT", "BENT", "BENT"};
if (listEq(fingStatus, Q, 5) && (handOrienStatus == "PLANK")) return 'q';
// G
char *G[5] = {"UNBENT", "UNBENT", "BENT", "BENT", "BENT"};
if (listEq(fingStatus, G, 5) && (handOrienStatus == "LEANING")) return 'g';
// H
char *H[5] = {"UNBENT", "UNBENT", "UNBENT", "BENT", "BENT"};
if (listEq(fingStatus, H, 5) && (handOrienStatus == "LEANING")) return 'h';
// others
if (handOrienStatus == ""){
// A
char *A[5] = {"UNBENT", "BENT", "BENT", "BENT", "BENT"};
if (listEq(fingStatus, A, 5)) return 'a';
// B
char *B1[5] = {"MID", "UNBENT", "UNBENT", "UNBENT", "UNBENT"};
char *B2[5] = {"BENT", "UNBENT", "UNBENT", "UNBENT", "UNBENT"};
if (listEq(fingStatus, B1, 5) || listEq(fingStatus, B2, 5)) return 'b';
// D
char *D[5] = {"BENT", "UNBENT", "BENT", "BENT", "BENT"};
if (listEq(fingStatus, D, 5)) return 'd';
// E
char *E[5] = {"BENT", "BENT", "BENT", "BENT", "BENT"};
if (listEq(fingStatus, E, 5)) return 'e';
// F
char *F1[5] = {"MID", "BENT", "UNBENT", "UNBENT", "UNBENT"};
char *F2[5] = {"BENT", "BENT", "UNBENT", "UNBENT", "UNBENT"};
if (listEq(fingStatus, F1, 5) || listEq(fingStatus, F2, 5)) return 'f';
// I
char *I1[5] = {"MID", "BENT", "BENT", "BENT", "UNBENT"};
char *I2[5] = {"BENT", "BENT", "BENT", "BENT", "UNBENT"};
if (listEq(fingStatus, I1, 5) || listEq(fingStatus, I2, 5)) return 'i';
// K
char *K[5] = {"UNBENT", "UNBENT", "UNBENT", "BENT", "BENT"};
if (listEq(fingStatus, K, 5)) return 'k';
// L
char *L[5] = {"UNBENT", "UNBENT", "BENT", "BENT", "BENT"};
if (listEq(fingStatus, L, 5)) return 'l';
// R
char *R1[5] = {"BENT", "UNBENT", "UNBENT", "BENT", "BENT"};
char *R2[5] = {"MID", "UNBENT", "UNBENT", "BENT", "BENT"};
if (listEq(fingStatus, R1, 5) || listEq(fingStatus, R2, 5)) return 'r';
// W
char *W1[5] = {"MID", "UNBENT", "UNBENT", "UNBENT", "BENT"};
char *W2[5] = {"BENT", "UNBENT", "UNBENT", "UNBENT", "BENT"};
if (listEq(fingStatus, W1, 5) || listEq(fingStatus, W2, 5)) return 'w';
// V
char *V[5] = {"BENT", "UNBENT", "UNBENT", "BENT", "BENT"};
if (listEq(fingStatus, V, 5)) return 'v';
// Y
char *Y[5] = {"UNBENT", "BENT", "BENT", "BENT", "UNBENT"};
if (listEq(fingStatus, Y, 5)) return 'y';
}
return dispLetter;
}
uint32_t exponentialFilter (uint32_t xn, uint32_t yn1){
float w = 0.4;
uint32_t yn = (w*xn) + ((1-w)*yn1);
return yn;
}
void ADC0Seq0_Handler (void){
ADC0_ISC_R = 0x01; // ack interrupt
// extract sensor values from ADC
sensor1 = ADC0_SSFIFO0_R&0xFFF;
sensor2 = ADC0_SSFIFO0_R&0xFFF;
sensor3 = ADC0_SSFIFO0_R&0xFFF;
sensor4 = ADC0_SSFIFO0_R&0xFFF;
sensor5 = ADC0_SSFIFO0_R&0xFFF;
if (interruptCount == 0){
// get initial value for filtering
filteredSensor1 = sensor1;
filteredSensor2 = sensor2;
filteredSensor3 = sensor3;
filteredSensor4 = sensor4;
filteredSensor5 = sensor5;
}
else{
// use exponential filter to filter data
filteredSensor1 = exponentialFilter(sensor1, filteredSensor1);
filteredSensor2 = exponentialFilter(sensor2, filteredSensor2);
filteredSensor3 = exponentialFilter(sensor3, filteredSensor3);
filteredSensor4 = exponentialFilter(sensor4, filteredSensor4);
filteredSensor5 = exponentialFilter(sensor5, filteredSensor5);
// display sensor values on serial [FOR DEBUGGING/TESTING PURPOSES]
SerialWriteLine("flex sensors");
SerialWriteInt(filteredSensor1);
SerialWriteInt(filteredSensor2);
SerialWriteInt(filteredSensor3);
SerialWriteInt(filteredSensor4);
SerialWriteInt(filteredSensor5);
SerialWriteLine("---");
SerialWriteLine("a vals");
SerialWriteInt(AX);
SerialWriteInt(AY);
SerialWriteInt(AZ);
SerialWriteLine("---");
}
// every 5 seconds check if letter has changed and then display on LCD
if (interruptCount % 10 == 0){
// determine new character
getFingStatus();
getHandOrienStatus();
currLetter = getCurrASLLetter();
// if current character has not changed from displayed character then don't redisplay the same thing
if (currLetter != dispLetter){
dispLetter = currLetter;
writeChar(dispLetter);
}
}
interruptCount ++;
}
// main.c
int main(void)
{
// initializations
PLLInit();
SystickInit();
SysTickInterruptInit();
SetupSerial();
SysTick_Wait10ms(10);
MPU6050_init();
SysTick_Wait10ms(10);
char status = MPU6050_begin(1,0);
sprintf(msg,"MPU6050 status = %d ",(int)status);
SerialWriteLine(msg);
SysTick_Wait10ms(10);
SerialWriteLine("Calculating offsets, do not move MPU6050 ..");
SysTick_Wait10ms(10);
// mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
MPU6050_calcOffsets(1,1); // gyro and accelero
SerialWriteLine("Done!");
PortD_Init();
PortA_Init();
LCDInit();
PortF_Init();
ADC_Init();
sendCommand(clear_display);
sendCommand(FirstRow);
writeString("START");
while(1){
button1Status = getButtonStatus(1);
button2Status = getButtonStatus(2);
if (button1Status){
sendCommand(clear_display);
sendCommand(FirstRow);
}
if (button2Status){
sendCommand(0x10);
}
// extracting vals from MPU6050
MPU6050_update();
AX = MPU6050_getAngleX();
AY = MPU6050_getAngleY();
AZ = MPU6050_getAngleZ();
GX = MPU6050_getGyroX();
GY = MPU6050_getGyroY();
GZ = MPU6050_getGyroZ();
SysTick_Wait10ms(10);
}
}