-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisto3v2.c
More file actions
458 lines (411 loc) · 12 KB
/
Visto3v2.c
File metadata and controls
458 lines (411 loc) · 12 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
#include <msp430.h>
#include <math.h>
#include <stdio.h>
#define BR32 32768 //32 hertz com smCLK
#define BR10K 105
#define BR50 20971 //50hz
#define BR100K 11
#define ABERTA 380
#define FECHADA 44
#define TRUE 1
#define FALSE 0
#define DBC 1000
#define VRX 1
#define VRY 0
#define BL BIT3
#define RS BIT0
#define EN BIT2
#define PCF_ADDR 0x3F
typedef unsigned char uint8;
unsigned char flag = 0;
int maxx,maxy = 0;
int minx = 4094;
int miny = 4095;
int vrx,vry = 0;
int tensx,tensy = 0;
int decx,decy = 0;
int minxD,minyD = 0;
int maxxI,maxyI = 0;
int minxI,minyI = 0;
int maxxD,maxyD = 0;
char minxDstr[20];
int teste_vrx;
void debounce(long x){
volatile long i;
for(i = 0; i < x; i++);
}
void setUSCIB0master(){
UCB0CTL1 = UCSWRST;
UCB0CTL0 = UCMST | UCSYNC | UCMODE_3; // Mestre/ Síncrono / I2C
UCB0BRW = BR100K; // Divisor de Freq
UCB0CTL1 = UCSSEL_3; // SMCLK
//Configuração dos pinos (3.0 e 3.1)
P3SEL |= BIT1 | BIT0; // Selecionar pino para função USCI
P3REN |= BIT1 | BIT0;
P3OUT |= BIT1 | BIT0;
}
void lcdbyte(char byte, char rs){
LCDWriteNB(((byte>>4)&0x0F),rs);
LCDWriteNB((byte&0x0F),rs);
}
void i2c_write(char dado){
UCB0CTL1 |= UCTR | UCTXSTT;
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = dado;
while((UCB0CTL1 & UCTXSTT) == UCTXSTT);
if((UCB0IFG & UCNACKIFG) == UCNACKIFG){
P1OUT |= BIT0;
while(1);
}
UCB0CTL1 |= UCTXSTP;
while((UCB0CTL1 & UCTXSTP) == UCTXSTP);
}
void LCDWriteNB(uint8 nibble, uint8 ischar){
nibble <<= 4;
i2c_write(nibble | ischar | BL);
i2c_write(nibble | ischar | BL | EN);
i2c_write(nibble | ischar | BL);
}
void lcd_aux(char dado){
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = ((dado<<4)&0XF0) | BL;
debounce(50);
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = ((dado<<4)&0XF0) | BL | EN;
debounce(50);
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = ((dado<<4)&0XF0) | BL;
}
void lcd_inic(){
UCB0CTL1 |= UCTR | UCTXSTT;
while((UCB0IFG & UCTXIFG) == 0);
UCB0TXBUF = 0;
while((UCB0CTL1 & UCTXSTT) == UCTXSTT);
if((UCB0IFG & UCNACKIFG) == UCNACKIFG){
P1OUT |= BIT0;
while(1);
}
lcd_aux(0);
debounce(20000);
lcd_aux(3);
debounce(10000);
lcd_aux(3);
debounce(10000);
lcd_aux(3);
debounce(10000);
lcd_aux(2);
//Entrou em modo de 4 bits
lcd_aux(2); lcd_aux(8);
lcd_aux(0); lcd_aux(8);
lcd_aux(0); lcd_aux(1);
lcd_aux(0); lcd_aux(6);
lcd_aux(0); lcd_aux(0xC);
while((UCB0IFG & UCTXIFG) == 0);
UCB0CTL1 |= UCTXSTP;
while((UCB0CTL1 & UCTXSTP) == UCTXSTP);
debounce(50);
}
void lcdWrite(char *strlcd){
int cont = 0;
while (!(*strlcd == '\0')){
if ((cont == 16) || (*strlcd == '\n')){
lcdbyte(0xC0, 0);
cont = 0;
strlcd++;
continue;
}
lcdbyte(*strlcd, 1);
strlcd++;
cont++;
}
}
void TA0config(){
TA0CTL = TASSEL_2 | MC__UP;
TA0CCTL1 = OUTMOD_7;
TA0CCR0 = BR32;
TA0CCR1 = TA0CCR0/2;
}
void GPIOconfig(){
P6DIR &= ~BIT3;
P6OUT |= BIT3;
P6REN |= BIT3;
P1DIR |= BIT0;
P1OUT &= ~BIT0;
P4DIR |= BIT7;
P4OUT &= ~BIT7;
}
void configadc(){
volatile unsigned char *pt1;
volatile unsigned int cont;
ADC12CTL0 &= ~ADC12ENC; //Desabilitar para configuração
ADC12CTL0 = ADC12ON; //ligar adc
ADC12CTL1 = ADC12CONSEQ_3 | // Multichannel com rep
ADC12SHS_1 | //Acionamento pelo Timer A0.1
ADC12CSTARTADD_0 | //MEM0
ADC12SSEL_3; //SMCLK
ADC12CTL2 = ADC12RES_2; //12 Bits bits
pt1 = &ADC12MCTL0;
for(cont = 0; cont <= 7; cont++){
if(cont % 2 == 0){
*pt1 = ADC12SREF_0 | ADC12INCH_1;
}
else{
*pt1 = ADC12SREF_0 | ADC12INCH_2;
}
++pt1;
}
ADC12MCTL7 |= ADC12EOS;
ADC12IE |= ADC12IE7;
P6SEL |= BIT1|BIT2; //Dedicada
ADC12CTL0 |= ADC12ENC; //Habilitar
}
void servoconfig(void){
TA2CTL = TASSEL__SMCLK | MC__UP;
TA2CCTL2 = OUTMOD_7;
TA2CCR0 = BR50;
TA2CCR2 = 687;
P2SEL |= BIT5;
P2DIR |= BIT5;
}
int sw_mon(){
static int psw = ABERTA;
if((P6IN&BIT3) == 0){
if(psw == ABERTA){
debounce(DBC);
psw = FECHADA;
return TRUE;
}
}
else{
if(psw == FECHADA){
debounce(DBC);
psw = ABERTA;
return FALSE;
}
}
return FALSE;
}
void USCI_A1_config(){
UCA1CTL1 = UCSWRST;
UCA1CTL1 = 0;
UCA1BRW = 6;
UCA1MCTL |= UCBRF_13 | UCOS16;
P4SEL |= BIT4;
UCA1CTL1 = UCSSEL_2;
}
void bt_char(char c){
while((UCA1IFG&UCTXIFG) == 0);
UCA1TXBUF = c;
}
void bt_str(char *vet){
unsigned int i = 0;
while (vet[i] != '\0'){
bt_char(vet[i++]);
}
}
void solve_dec(char* buffer,int decrep){
if(decrep < 10){
snprintf(buffer,sizeof(buffer),"00%d",decrep);
}
else if(decrep < 100){
snprintf(buffer,sizeof(buffer),"0%d",decrep);
}
else{
snprintf(buffer,sizeof(buffer),"%d",decrep);
}
}
#pragma vector = ADC12_VECTOR
__interrupt void isr_media(){
volatile unsigned int *pt;
unsigned int cont,somaX,somaY = 0;
pt = &ADC12MEM0;
for(cont = 0; cont <= 7; cont++){
if(cont % 2 == 0){
somaX += *pt;
maxx = (*pt > maxx) ? *pt : maxx;
minx = (*pt < minx) ? *pt : minx;
}
else{
somaY += *pt;
maxy = (*pt > maxy) ? *pt : maxy;
miny = (*pt < miny) ? *pt : miny;
}
++pt;
}
//teste_vrx = minx;
vrx = somaX >>2;
vry = somaY >>2;
maxxI = (int)(0.0008*maxx);
maxyI = (int)(0.0008*maxy);
//minxI = maxxI;
minxI = (int)(0.0008*minx);
minyI = (int)(0.0008*miny);
maxxD = ((int)(100*((0.0008*maxx) - maxxI)));
maxyD = ((int)(100*((0.0008*maxy) - maxyI)));
/*if(maxxD - 20 < 0){
minxI = (maxxI - 1 >= 0) ? maxxI - 1 : 0;
minxD = (maxxI > 0) ? 100 - maxxD : 0;
if(minxD > 10){
snprintf(minxDstr,sizeof(minxDstr),"%d",minxD);
}
else{
snprintf(minxDstr,sizeof(minxDstr),"0%d",minxD);
}
}
else if (maxxD - 20 < 10){
minxI = maxxI;
int jaj = maxxD - 20;
snprintf(minxDstr,sizeof(minxDstr),"0%d",jaj);
}
else{
minxD = maxxD - 20;
snprintf(minxDstr,sizeof(minxDstr),"%d",minxD);
}*/
minyD = (int)(100*((0.0008*miny) - minyI));
minxD = (int)(100*((0.0008*minx) - minxI));
tensx = (int)(0.0008*vrx);
tensy = (int)(0.0008*vry);
decx = (int)(1000*((0.0008*vrx) - tensx));
decy = (int)(1000*((0.0008*vry) - tensy));
minx = 4095;
miny = 4095;
maxx = 0;
maxy = 0;
flag = 1;
}
int main(void)
{
__disable_interrupt();
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
char canal = VRX;
char stringlcd[60];
char uartstring[60];
int contador = 0;
char stringcontador[60];
char stringvrx[60];
char stringvry[60];
char stringdecx [60];
char stringdecy [60];
GPIOconfig();
USCI_A1_config();
setUSCIB0master();
UCB0I2CSA = PCF_ADDR;
i2c_write(0);
lcd_inic();
TA0config();
servoconfig();
configadc();
bt_str("\rCont: ---Canal A1---- ---Canal A2----\r\n");
__enable_interrupt();
while(TRUE){
if(flag == 1){
flag = 0;
lcdbyte(1,0);
debounce(1200);
lcdbyte(3,0);
debounce(1200);
/*if(maxxD - 20 < 0){
minxI = (maxxI - 1 >= 0) ? maxxI - 1 : 0;
minxD = (maxxI > 0) ? 100 - maxxD : 0;
if(minxD > 10){
snprintf(minxDstr,sizeof(minxDstr),"%d",minxD);
}
else{
snprintf(minxDstr,sizeof(minxDstr),"0%d",minxD);
}
}
else if (maxxD - 20 < 10){
minxI = maxxI;
int jaj = maxxD - 20;
snprintf(minxDstr,sizeof(minxDstr),"0%d",jaj);
}
else{
minxD = maxxD - 20;
snprintf(minxDstr,sizeof(minxDstr),"%d",minxD);
}*/
if(contador < 10){
snprintf(stringcontador,sizeof(stringcontador),"000%d",contador);
}
else if(contador < 100){
snprintf(stringcontador,sizeof(stringcontador),"00%d",contador);
}
else if(contador < 1000){
snprintf(stringcontador,sizeof(stringcontador),"0%d",contador);
}
else{
snprintf(stringcontador,sizeof(stringcontador),"%d",contador);
}
if(vrx < 10){
snprintf(stringvrx,sizeof(stringvrx),"000%d",vrx);
}
else if (vrx < 100){
snprintf(stringvrx,sizeof(stringvrx),"00%d",vrx);
}
else if (vrx < 1000){
snprintf(stringvrx,sizeof(stringvrx),"0%d",vrx);
}
else{
snprintf(stringvrx,sizeof(stringvrx),"%d",vrx);
}
if(vry < 10){
snprintf(stringvry,sizeof(stringvry),"000%d",vry);
}
else if (vry < 100){
snprintf(stringvry,sizeof(stringvry),"00%d",vry);
}
else if (vry < 1000){
snprintf(stringvry,sizeof(stringvry),"0%d",vry);
}
else{
snprintf(stringvry,sizeof(stringvry),"%d",vry);
}
solve_dec(stringdecx,decx);
solve_dec(stringdecy,decy);
if(canal == VRX){
if(vrx < 10){
snprintf(stringlcd,sizeof(stringlcd),"A1=%d,%sV 000%d\nMn=%d,%d0 Mx=%d,%d0",tensx,stringdecx,vrx,minxI,minxD,maxxI,maxxD);
}
else if(vrx < 100){
snprintf(stringlcd,sizeof(stringlcd),"A1=%d,%sV 00%d\nMn=%d,%d0 Mx=%d,%d0",tensx,stringdecx,vrx,minxI,minxD,maxxI,maxxD);
}
else if(vrx < 1000){
snprintf(stringlcd,sizeof(stringlcd),"A1=%d,%sV 0%d\nMn=%d,%d Mx=%d,%d",tensx,stringdecx,vrx,minxI,minxD,maxxI,maxxD);
}
else {
snprintf(stringlcd,sizeof(stringlcd),"A1=%d,%sV %d\nMn=%d,%d Mx=%d,%d",tensx,stringdecx,vrx,minxI,minxD,maxxI,maxxD);
}
snprintf(uartstring,sizeof(uartstring),"\r%s: %s --> %d,%sV %s --> %d,%sV\r\n",stringcontador,stringvrx,tensx,stringdecx,stringvry,tensy,stringdecy);
lcdWrite(stringlcd);
bt_str(uartstring);
TA2CCR2 = 524 + round((0.33*vrx));
}
else{
if(vry < 10){
snprintf(stringlcd,sizeof(stringlcd),"A2=%d,%sV 000%d\nMn=%d,%d0 Mx=%d,%d0",tensy,stringdecy,vry,minyI,minyD,maxyI,maxyD);
}
else if(vry < 100){
snprintf(stringlcd,sizeof(stringlcd),"A2=%d,%sV 00%d\nMn=%d,%d0 Mx=%d,%d0",tensy,stringdecy,vry,minyI,minyD,maxyI,maxyD);
}
else if(vry < 1000){
snprintf(stringlcd,sizeof(stringlcd),"A2=%d,%sV 0%d\nMn=%d,%d Mx=%d,%d",tensy,stringdecy,vry,minyI,minyD,maxyI,maxyD);
}
else {
snprintf(stringlcd,sizeof(stringlcd),"A2=%d,%sV %d\nMn=%d,%d Mx=%d,%d",tensy,stringdecy,vry,minyI,minyD,maxyI,maxyD);
}
snprintf(uartstring,sizeof(uartstring),"\r%s: %s --> %d,%sV %s --> %d,%sV\r\n",stringcontador,stringvrx,tensx,stringdecx,stringvry,tensy,stringdecy);
lcdWrite(stringlcd);
bt_str(uartstring);
TA2CCR2 = 524 + round((4095 - vry)*0.33);
}
debounce(1000);
if(contador == 9999){
contador = 0;
}
else{
contador++;
}
if(sw_mon() == TRUE) canal ^= 1;
}
}
return 0;
}