-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
626 lines (546 loc) · 16.3 KB
/
main.c
File metadata and controls
626 lines (546 loc) · 16.3 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
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
// Alexandre Santana Sousa
// configuração: P6.0 - A0 eixo x
// P6.2 - A2 eixo y
// P6.4 - SW
#include <msp430.h>
void ADC_config(void);
void config_I2C(void);
void LCD_inic(void);
void LCD_aux(char dado);
void PCF_STT_STP(void);
int PCF_read(void);
void PCF_write(char dado);
void delay(long limite);
//funções de controle
void lcd_write_string(char *s, char linha);
void lcd_write( unsigned char valor);
void lcd_write_decimal8b(int valor);
void controla_lcd(char valor);
void posiciona_cursor(char posicao);
void char_to_hex8b(char valor);
void char_to_hex16b(int valor);
void write_float(float valor);
void config_timer();
void config_chav();
int verifica_sw();
void limpa_linha(int linha);
void atualiza_lcd();
void modo_1();
void modo_2();
void modo_3();
void modo_4();
#define BR100K 10
#define RS BIT0
#define RW BIT1
#define BIT_E BIT2 //Enable
#define BIT_BL BIT3 //Back light
#define first_line 0x80
#define second_line 0xC0
#define all_display 1
#define PCF_ADR 0x27
#define TRUE 1
#define FALSE 0
#define open 1
#define closed 0
//variáveis globais
volatile float voltX = 0, voltY = 0;
volatile int valorsw = 0;
volatile unsigned int media_y = 0;
volatile unsigned int media_x = 0;
volatile unsigned int flag = 0, novo = 0, velho = 8;
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
config_I2C();
LCD_inic();
controla_lcd(BIT3 | BIT2 ); //Cursor apagado
ADC_config();
config_timer();
config_chav();
__enable_interrupt();
while(1){
switch(valorsw){
case 0:
limpa_linha(all_display);
lcd_write_string("Modo 1", first_line);
delay(50000);
limpa_linha(all_display);
lcd_write_string("A0: ", first_line);
lcd_write_string("A2: ", second_line);
posiciona_cursor(first_line + 9);
lcd_write('V');
posiciona_cursor(second_line + 9);
lcd_write('V');
while(!verifica_sw()){
if(flag == 1){
flag = 0;
modo_1();
}
}
break;
case 1:
limpa_linha(all_display);
lcd_write_string("Modo 2", first_line);
delay(50000);
limpa_linha(all_display);
lcd_write_string("A0: ", first_line);
posiciona_cursor(first_line + 9);
lcd_write('V');
while(!verifica_sw()){
if(flag == 1){
flag = 0;
modo_2();
}
}
break;
case 2:
limpa_linha(all_display);
lcd_write_string("Modo 3", first_line);
delay(50000);
limpa_linha(all_display);
lcd_write_string("A2: ", first_line);
posiciona_cursor(first_line + 9);
lcd_write('V');
while(!verifica_sw()){
if(flag == 1){
flag = 0;
modo_3();
}
}
break;
case 3:
limpa_linha(all_display);
lcd_write_string("Modo 4", first_line);
delay(50000);
limpa_linha(all_display);
while(!verifica_sw()){
if(flag == 1){
flag = 0;
modo_4();
}
}
break;
default:
valorsw = 0;
break;
}
}
}
void modo_1(){
posiciona_cursor(first_line + 4);
write_float(voltX);
posiciona_cursor(second_line + 4);
write_float(voltY);
posiciona_cursor(first_line + 12);
char_to_hex16b(media_x);
posiciona_cursor(second_line + 12);
char_to_hex16b(media_y);
atualiza_lcd();
}
void modo_2(){
volatile int i;
novo = (media_x >> 8);
posiciona_cursor(first_line + 4);
write_float(voltX);
posiciona_cursor(first_line + 12);
char_to_hex16b(media_x);
if(novo < velho){
for(i = velho; i > novo; i--){
posiciona_cursor(second_line + i);
lcd_write(' ');
}
}
else if(novo > velho){
for(i = velho; i < novo; i++){
posiciona_cursor(second_line + i);
lcd_write(0xFF);
}
}else{
for(i = 0; i < novo; i++){
posiciona_cursor(second_line + i);
lcd_write(0xFF);
}
}
velho = novo;
atualiza_lcd();
}
void modo_3(){
volatile int i;
novo = (media_y >> 8);
posiciona_cursor(first_line + 4);
write_float(voltY);
posiciona_cursor(first_line + 12);
char_to_hex16b(media_y);
if(novo < velho){
for(i = velho; i > novo; i--){
posiciona_cursor(second_line + i);
lcd_write(' ');
}
}
else if(novo > velho){
for(i = velho; i < novo; i++){
posiciona_cursor(second_line + i);
lcd_write(0xFF);
}
}else{
for(i = 0; i < novo; i++){
posiciona_cursor(second_line + i);
lcd_write(0xFF);
}
}
velho = novo;
atualiza_lcd();
}
void modo_4(){
volatile int meiox = 2048, meioy = 2048, i = 0, tamanho = 0;
posiciona_cursor(first_line + 7);
lcd_write(0x7F);
lcd_write(0xFF);
posiciona_cursor(second_line + 7);
lcd_write(0xFF);
lcd_write(0x7E);
if(media_x < meiox){
tamanho = (media_x >> 8);
posiciona_cursor(first_line + 7);
lcd_write(0xFF);
lcd_write(0x7E);
for(i = tamanho; i > 0; i--){
posiciona_cursor(first_line + 7 - i);
lcd_write(0xFF);
}
}
else if(media_x > meiox){
tamanho = ((media_x >> 8) - 8);
for(i = 0; i < tamanho; i++){
posiciona_cursor(first_line + 9 +i);
lcd_write(0xFF);
}
}
if( media_y > 2070 && media_y <= 2100){
// posiciona no meio
posiciona_cursor(second_line + 7);
lcd_write(0xFF);
lcd_write(0x7E);
}
else if(media_y < meioy){
tamanho = (media_y >> 8);
posiciona_cursor(second_line + 7);
for(i = tamanho; i > 0; i--){
posiciona_cursor(second_line + 7 - i);
lcd_write(0xFF);
}
}
else if(media_y > meioy){
tamanho = ((media_y >> 8) - 8);
posiciona_cursor(second_line + 7);
lcd_write(0x7F);
lcd_write(0xFF);
for(i = 0; i < tamanho; i++){
posiciona_cursor(second_line + 9 +i);
lcd_write(0xFF);
}
}
atualiza_lcd();
limpa_linha(all_display);
}
#pragma vector = ADC12_VECTOR
__interrupt void adc_int(void){
volatile unsigned int *pt;
unsigned int i,soma;
pt = &ADC12MEM0;
soma = 0;
for (i=0; i<8; i++){
soma += pt[i];
}
media_x = soma/8;
soma = 0;
for (i=8; i<16; i++){
soma += pt[i];
}
media_y = soma/8;
voltX = (media_x*3.3)/4095;
voltY = (media_y*3.3)/4095;
flag = 1;
}
void lcd_write_string(char *s, char linha) {
switch(linha){
case first_line:
posiciona_cursor(first_line);
while (*s != '\0'){
lcd_write(*s++);
}
break;
case second_line:
posiciona_cursor(second_line);
while (*s != '\0'){
lcd_write(*s++);
}
break;
}
}
void lcd_write(unsigned char valor){
volatile char escrever;
escrever = (valor&0XF0) | BIT_BL | RS | BIT_E; //E=1
PCF_write(escrever);
escrever = ((valor&0XF0) | BIT_BL | RS) & ~BIT_E; //E=0
PCF_write(escrever);
escrever = ((valor<<4)&0XF0) | BIT_BL | RS | BIT_E; //E=1
PCF_write(escrever);
escrever = (((valor<<4)&0XF0) | BIT_BL | RS) & ~BIT_E; //E=0
PCF_write(escrever);
}
void controla_lcd(char valor){
char escrever;
// para controlar o lcd preciso do RS = 0
escrever = ((valor)&0XF0) | BIT_BL | BIT_E; //E=1
PCF_write(escrever);
escrever = (((valor)&0XF0) | BIT_BL ) & ~BIT_E; //E=0
PCF_write(escrever);
escrever = ((valor<<4)&0XF0) | BIT_BL | BIT_E; //E=1
PCF_write(escrever);
escrever = (((valor<<4)&0XF0) | BIT_BL ) & ~BIT_E; //E=0
PCF_write(escrever);
}
void posiciona_cursor(char posicao){
controla_lcd(posicao);
}
void lcd_write_decimal8b(int valor){
volatile int cem = 0, dezena = 0, unidade = 0;
if((valor/100) >= 1){
cem = valor/100;
dezena = (valor%100)/10;
unidade = (valor%100)%10;
cem += 0x30;
dezena += 0x30;
unidade += 0x30;
lcd_write(cem);
lcd_write(dezena);
lcd_write(unidade);
}
else if((valor/10) >= 1){
dezena = valor/10;
unidade = valor%10;
dezena += 0x30;
unidade += 0x30;
lcd_write(dezena);
lcd_write(unidade);
}
else{
unidade = valor + 0x30;
lcd_write(unidade);
}
}
void char_to_hex8b(char valor){
if(((valor>>4)&0x0F) >= 10){
lcd_write(((valor>>4)&0x0F) + 0x37);
}
else{
lcd_write(((valor>>4)&0x0F) + 0x30);
}
if((valor&0x0F) >= 10){
lcd_write((valor&0x0F) + 0x37);
}
else{
lcd_write((valor&0x0F) + 0x30);
}
}
void char_to_hex16b(int valor){
char_to_hex8b(valor >> 8);
char_to_hex8b(valor&0xFF);
}
void write_float(float valor){
int inteiro = (int)valor;
int fracao = (valor - inteiro)*1000;
lcd_write(inteiro + 0x30);
lcd_write(',');
lcd_write_decimal8b(fracao);
}
void limpa_linha(int linha){
volatile int i;
if(linha == first_line){
posiciona_cursor(first_line);
for(i = 0; i < 16; i++){
lcd_write(' ');
}
}
if(linha == second_line){
posiciona_cursor(second_line);
for(i = 0; i < 16; i++){
lcd_write(' ');
}
}
if(linha == all_display){
posiciona_cursor(first_line);
for(i = 0; i < 16; i++){
lcd_write(' ');
}
posiciona_cursor(second_line);
for(i = 0; i < 16; i++){
lcd_write(' ');
}
}
}
void ADC_config(void){
volatile unsigned char *pt;
unsigned char i;
ADC12CTL0 &= ~ADC12ENC; //Desabilitar para configurar
ADC12CTL0 = ADC12ON; //Ligar ADC
ADC12CTL1 = ADC12CONSEQ_3 | //Modo autoscan repetido
ADC12SHS_1 | //Selecionar timer
ADC12CSTARTADD_0 |
ADC12SSEL_3; //ADC12CLK = SMCLK
pt = &ADC12MCTL0;
for (i=0; i<8; i++){
pt[i]=ADC12SREF_0 | ADC12INCH_0; //ADC12MCTL0 até ADC12MCTL7
}
for (i=8; i<16; i++){
pt[i]=ADC12SREF_0 | ADC12INCH_2; //ADC12MCTL8 até ADC12MCTL15
}
pt[15] |= ADC12EOS; //EOS em ADC12MCTL15
P6SEL |= BIT2|BIT0;
ADC12CTL0 |= ADC12ENC; //Habilitar ADC12
ADC12IE |= ADC12IE15;
}
void config_I2C(void){
P3SEL |= BIT1 | BIT0; // Use dedicated module
UCB0CTL1 |= UCSWRST; // UCSI B0 em ressete
UCB0CTL0 = UCSYNC | //Síncrono
UCMODE_3 | //Modo I2C
UCMST; //Mestre
UCB0BRW = BR100K; //100 kbps
UCB0CTL1 = UCSSEL_2; //SMCLK e remove ressete
}
void LCD_inic(void){
PCF_STT_STP(); //Colocar PCF em estado conhecido
UCB0I2CSA = PCF_ADR; //Endereço Escravo
UCB0CTL1 |= UCTR | //Mestre TX
UCTXSTT; //Gerar START
while ( (UCB0IFG & UCTXIFG) == 0); //Esperar TXIFG=1
UCB0TXBUF = 0; //Saída PCF = 0;
while ( (UCB0CTL1 & UCTXSTT) == UCTXSTT); //Esperar STT=0
if ( (UCB0IFG & UCNACKIFG) == UCNACKIFG) //NACK?
while(1);
LCD_aux(0); //RS=RW=0, BL=1
delay(20000);
LCD_aux(3); //3
delay(10000);
LCD_aux(3); //3
delay(10000);
LCD_aux(3); //3
delay(10000);
LCD_aux(2); //2
// Entrou em modo 4 bits
LCD_aux(2); LCD_aux(8); //0x28
LCD_aux(0); LCD_aux(8); //0x08
LCD_aux(0); LCD_aux(1); //0x01
LCD_aux(0); LCD_aux(6); //0x06
LCD_aux(0); LCD_aux(0xF); //0x0F
while ( (UCB0IFG & UCTXIFG) == 0) ; //Esperar TXIFG=1
UCB0CTL1 |= UCTXSTP; //Gerar STOP
while ( (UCB0CTL1 & UCTXSTP) == UCTXSTP) ; //Esperar STOP
delay(50);
}
void LCD_aux(char dado){
while ( (UCB0IFG & UCTXIFG) == 0); //Esperar TXIFG=1
UCB0TXBUF = ((dado<<4)&0XF0) | BIT_BL; //PCF7:4 = dado;
delay(50);
while ( (UCB0IFG & UCTXIFG) == 0); //Esperar TXIFG=1
UCB0TXBUF = ((dado<<4)&0XF0) | BIT_BL | BIT_E; //E=1
delay(50);
while ( (UCB0IFG & UCTXIFG) == 0); //Esperar TXIFG=1
UCB0TXBUF = ((dado<<4)&0XF0) | BIT_BL; //E=0;
}
void PCF_STT_STP(void){
int x=0;
UCB0I2CSA = PCF_ADR; //Endereço Escravo
while (x<5){
UCB0CTL1 |= UCTR | //Mestre TX
UCTXSTT; //Gerar START
while ( (UCB0IFG & UCTXIFG) == 0); //Esperar TXIFG=1
UCB0CTL1 |= UCTXSTP; //Gerar STOP
delay(200);
if ( (UCB0CTL1 & UCTXSTP) == 0) break; //Esperar STOP
x++;
}
while ( (UCB0CTL1 & UCTXSTP) == UCTXSTP); //I2C Travado (Desligar / Ligar)
}
int PCF_read(void){
int dado;
UCB0I2CSA = PCF_ADR; //Endereço Escravo
UCB0CTL1 &= ~UCTR; //Mestre RX
UCB0CTL1 |= UCTXSTT; //Gerar START
while ( (UCB0CTL1 & UCTXSTT) == UCTXSTT);
UCB0CTL1 |= UCTXSTP; //Gerar STOP + NACK
while ( (UCB0IFG & UCRXIFG) == 0); //Esperar RX
dado=UCB0RXBUF;
delay(50);
return dado;
}
void PCF_write(char dado){
UCB0I2CSA = PCF_ADR; //Endereço Escravo
UCB0CTL1 |= UCTR | //Mestre TX
UCTXSTT; //Gerar START
while ( (UCB0IFG & UCTXIFG) == 0) ; //Esperar TXIFG=1
UCB0TXBUF = dado; //Escrever dado
while ( (UCB0CTL1 & UCTXSTT) == UCTXSTT) ; //Esperar STT=0
if ( (UCB0IFG & UCNACKIFG) == UCNACKIFG) //NACK?
while(1); //Escravo gerou NACK
UCB0CTL1 |= UCTXSTP; //Gerar STOP
while ( (UCB0CTL1 & UCTXSTP) == UCTXSTP) ; //Esperar STOP
delay(50);
}
void delay(long limite){
volatile long cont=0;
while (cont++ < limite);
}
void config_timer(){
TA0CTL = TASSEL_1 | MC_1;
TA0CCTL1 = OUTMOD_6;
TA0CCR0 = 32767/32;
TA0CCR1 = TA0CCR0/2;
//timer para atraso 1s
TA1CTL = TASSEL_1 | MC_1 | ID_3;
TA1CCR0 = 4096; //1s de atraso
}
void config_chav(){
P6DIR &= ~BIT4;
P6REN |= BIT4;
P6OUT |= BIT4;
P1DIR |= BIT0;
P1OUT &= ~BIT0;
}
volatile int psw = 1;
int verifica_sw(){
if((P6IN&BIT4) == closed){
if(psw == open){
delay(5000);
psw = closed;
valorsw++;
return TRUE;
}
else{
return FALSE;
}
}
else{
if(psw == open){
return FALSE;
}
else{
delay(5000);
psw = open;
return FALSE;
}
}
}
void atualiza_lcd(){
TA1CTL |= TACLR; //Zerar o contador
TA1CTL &= ~TAIFG; //Apagar passado de TAIFG
while ( (TA1CTL&TAIFG) == 0){
if(verifica_sw()){
P1OUT ^= BIT0;
main();
}
}
P1OUT ^= BIT0;
}