Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.00 KB | None | 0 0
  1. #include<p30fxxxx.h>
  2. #include <stdlib.h>
  3. #include "driverGLCD.h"
  4. #include "adc.h"
  5.  
  6.  
  7. #define PASSWORD "1234"
  8. #define ALCOHOL_THRESHOLD 3200
  9. #define TIME_THRESHOLD 20 // 10otkucaja ~ 2sekundi
  10. #define DELAY 10
  11. #define Y_one_to_five 2
  12. #define Y_six_to_zero 4
  13. #define DEBOUNCE_CYCLES 0
  14. #define DEBOUNCE_TIME 20
  15. #define COORDINATE_RADIUS 10
  16. #define STATE2_NUMBER 2
  17.  
  18. int x_prev = -300, y_prev = -300;
  19. int x = 2000, y = 2000;
  20.  
  21. //_FOSC(CSW_FSCM_OFF & XT_PLL4);//instruction takt je isti kao i kristal//
  22. _FOSC(CSW_ON_FSCM_OFF & HS3_PLL4);
  23. _FWDT(WDT_OFF);
  24. _FGS(CODE_PROT_OFF);
  25.  
  26. unsigned int X, Y,x_vrednost, y_vrednost;
  27.  
  28.  
  29. //const unsigned int ADC_THRESHOLD = 900;
  30. const unsigned int AD_Xmin =220;
  31. const unsigned int AD_Xmax =3642;
  32. const unsigned int AD_Ymin =520;
  33. const unsigned int AD_Ymax =3450;
  34.  
  35. int vrijeme=0;
  36. unsigned int sirovi0,sirovi1;
  37. unsigned int broj,broj1,broj2,temp0,temp1;
  38.  
  39. //#define DRIVE_A PORTBbits.RB10
  40. //#define DRIVE_B PORTCbits.RC13
  41. #define DRIVE_A PORTCbits.RC13
  42. #define DRIVE_B PORTCbits.RC14
  43.  
  44. void ConfigureTSPins(void)
  45. {
  46. //ADPCFGbits.PCFG10=1;
  47. //ADPCFGbits.PCFG7=digital;
  48.  
  49. //TRISBbits.TRISB10=0;
  50. TRISCbits.TRISC13=0;
  51. TRISCbits.TRISC14=0;
  52.  
  53. //LATCbits.LATC14=0;
  54. //LATCbits.LATC13=0;
  55. }
  56.  
  57. void initUART1(void)
  58. {
  59. U1BRG=0x0015;//ovim odredjujemo baudrate
  60.  
  61. U1MODEbits.ALTIO=0;//biramo koje pinove koristimo za komunikaciju osnovne ili alternativne
  62.  
  63. IEC0bits.U1RXIE=1;//omogucavamo rx1 interupt
  64.  
  65. U1STA&=0xfffc;
  66.  
  67. U1MODEbits.UARTEN=1;//ukljucujemo ovaj modul
  68.  
  69. U1STAbits.UTXEN=1;//ukljucujemo predaju
  70. }
  71.  
  72. void __attribute__((__interrupt__)) _U1RXInterrupt(void)
  73. {
  74. IFS0bits.U1RXIF = 0;
  75. // tempRX=U1RXREG;
  76.  
  77. }
  78.  
  79. void WriteUART1(unsigned int data)
  80. {
  81. while(!U1STAbits.TRMT);
  82.  
  83. if(U1MODEbits.PDSEL == 3)
  84. U1TXREG = data;
  85. else
  86. U1TXREG = data & 0xFF;
  87. }
  88.  
  89. /***********************************************************************
  90. * Ime funkcije : WriteUART1dec2string *
  91. * Opis : Funkcija salje 4-cifrene brojeve (cifru po cifru)*
  92. * Parameteri : unsigned int data-podatak koji zelimo poslati *
  93. * Povratna vrednost : Nema *
  94. ************************************************************************/
  95. void WriteUART1dec2string(unsigned int data)
  96. {
  97. unsigned char temp;
  98.  
  99. temp=data/1000;
  100. WriteUART1(temp+'0');
  101. data=data-temp*1000;
  102. temp=data/100;
  103. WriteUART1(temp+'0');
  104. data=data-temp*100;
  105. temp=data/10;
  106. WriteUART1(temp+'0');
  107. data=data-temp*10;
  108. WriteUART1(data+'0');
  109. }
  110.  
  111.  
  112.  
  113. void Delay(unsigned int N)
  114. {
  115. unsigned int i;
  116. for(i=0;i<N;i++);
  117. }
  118.  
  119. void Touch_Panel (void)
  120. {
  121. // vode horizontalni tranzistori
  122. DRIVE_A = 1;
  123. DRIVE_B = 0;
  124.  
  125. LATCbits.LATC13=1;
  126. LATCbits.LATC14=0;
  127.  
  128. Delay(500); //cekamo jedno vreme da se odradi AD konverzija
  129.  
  130. // ocitavamo x
  131. x_vrednost = temp0;//temp0 je vrednost koji nam daje AD konvertor na BOTTOM pinu
  132.  
  133. // vode vertikalni tranzistori
  134. LATCbits.LATC13=0;
  135. LATCbits.LATC14=1;
  136. DRIVE_A = 0;
  137. DRIVE_B = 1;
  138.  
  139. Delay(500); //cekamo jedno vreme da se odradi AD konverzija
  140.  
  141. // ocitavamo y
  142. y_vrednost = temp1;// temp1 je vrednost koji nam daje AD konvertor na LEFT pinu
  143.  
  144. //Ako želimo da nam X i Y koordinate budu kao rezolucija ekrana 128x64 treba skalirati vrednosti x_vrednost i y_vrednost tako da budu u opsegu od 0-128 odnosno 0-64
  145. //skaliranje x-koordinate
  146.  
  147. X=(x_vrednost-161)*0.03629;
  148.  
  149.  
  150.  
  151. //X= ((x_vrednost-AD_Xmin)/(AD_Xmax-AD_Xmin))*128;
  152. //vrednosti AD_Xmin i AD_Xmax su minimalne i maksimalne vrednosti koje daje AD konvertor za touch panel.
  153.  
  154.  
  155. //Skaliranje Y-koordinate
  156. Y= ((y_vrednost-500)*0.020725);
  157.  
  158. // Y= ((y_vrednost-AD_Ymin)/(AD_Ymax-AD_Ymin))*64;
  159. }
  160.  
  161.  
  162.  
  163. void __attribute__((__interrupt__)) _ADCInterrupt(void)
  164. {
  165.  
  166.  
  167. sirovi0=ADCBUF0;//0
  168. sirovi1=ADCBUF1;//1
  169.  
  170. temp0=sirovi0;
  171. temp1=sirovi1;
  172.  
  173. IFS0bits.ADIF = 0;
  174. }
  175.  
  176. void Write_GLCD(unsigned int data)
  177. {
  178. unsigned char temp;
  179.  
  180. temp=data/1000;
  181. Glcd_PutChar(temp+'0');
  182. data=data-temp*1000;
  183. temp=data/100;
  184. Glcd_PutChar(temp+'0');
  185. data=data-temp*100;
  186. temp=data/10;
  187. Glcd_PutChar(temp+'0');
  188. data=data-temp*10;
  189. Glcd_PutChar(data+'0');
  190. }
  191.  
  192. inline void swap(char *x, char *y) {
  193. char t = *x; *x = *y; *y = t;
  194. }
  195.  
  196. char* reverse(char *buffer, int i, int j)
  197. {
  198. while (i < j)
  199. swap(&buffer[i++], &buffer[j--]);
  200.  
  201. return buffer;
  202. }
  203.  
  204. char* int_to_array(int value, char* buffer, int base)
  205. {
  206. // invalid input
  207. if (base < 2 || base > 32)
  208. return buffer;
  209.  
  210. // consider absolute value of number
  211. int n = abs(value);
  212.  
  213. int i = 0;
  214. while (n)
  215. {
  216. int r = n % base;
  217.  
  218. if (r >= 10)
  219. buffer[i++] = 65 + (r - 10);
  220. else
  221. buffer[i++] = 48 + r;
  222.  
  223. n = n / base;
  224. }
  225.  
  226. // if number is 0
  227. if (i == 0)
  228. buffer[i++] = '0';
  229.  
  230. // If base is 10 and value is negative, the resulting string
  231. // is preceded with a minus sign (-)
  232. // With any other base, value is always considered unsigned
  233. if (value < 0 && base == 10)
  234. buffer[i++] = '-';
  235.  
  236. buffer[i] = '\0'; // null terminate string
  237.  
  238. // reverse the string and return it
  239. return reverse(buffer, 0, i - 1);
  240. }
  241.  
  242. unsigned int brojac_ms,stoperica,ms,sekund;
  243.  
  244. void Delay_ms (int vreme)//funkcija za kasnjenje u milisekundama
  245. {
  246. stoperica = 0;
  247. while(stoperica < vreme);
  248. }
  249.  
  250. void __attribute__ ((__interrupt__)) _T2Interrupt(void) // svakih 1ms
  251. {
  252.  
  253. TMR2 =0;
  254. ms=1;//fleg za milisekundu ili prekid;potrebno ga je samo resetovati u funkciji
  255.  
  256. brojac_ms++;//brojac milisekundi
  257. stoperica++;//brojac za funkciju Delay_ms
  258.  
  259. if (brojac_ms==1000)//sek
  260. {
  261. brojac_ms=0;
  262. sekund=1;//fleg za sekundu
  263. }
  264. IFS0bits.T2IF = 0;
  265.  
  266. }
  267.  
  268. #define TMR2_period 10000 /* Fosc = 10MHz,
  269. 1/Fosc = 0.1us !!!, 0.1us * 10000 = 1ms */
  270. void Init_T2(void)
  271. {
  272. TMR2 = 0;
  273. PR2 = TMR2_period;
  274.  
  275. T2CONbits.TCS = 0; // 0 = Internal clock (FOSC/4)
  276. //IPC1bits.T2IP = 3 // T2 interrupt pririty (0-7)
  277. //SRbits.IPL = 3; // CPU interrupt priority is 3(11)
  278. IFS0bits.T2IF = 0; // clear interrupt flag
  279. IEC0bits.T2IE = 1; // enable interrupt
  280.  
  281. T2CONbits.TON = 1; // T2 on
  282. }
  283. // ZEKI DODAO BAZER
  284. void bazer(void){ //pwm signal za bazer za svaki interapt i jedan if ya loyinku
  285. LATAbits.LATA11=1;
  286. Delay_ms(2); // faktor ispoune 50posto
  287. LATAbits.LATA11=0;
  288. Delay_ms(1);
  289. }
  290. unsigned int check_password(unsigned int *curr_state2, unsigned int *next_state, unsigned int number)
  291. {
  292. Delay_ms(DEBOUNCE_TIME);
  293. if (!(*next_state))
  294. {
  295. *next_state = 1;
  296. if (PASSWORD[(*curr_state2)++] != number+'0')
  297. return 0;
  298. }
  299. return 1;
  300. }
  301.  
  302. void check_reset(unsigned int x, unsigned int y, unsigned int *curr_state, unsigned int *next_state, unsigned int *curr_state2, unsigned int *password_valid)
  303. {
  304. if ((65<=x)&&(x<=126) && (47<=y)&&(y<=63)) // pritisnuto dugme "nazad"
  305. {
  306. *curr_state = 0, *next_state = 1, *curr_state2 = 0, *password_valid = 1;
  307. Touch_Panel();
  308. GoToXY(4,Y_one_to_five);
  309. Delay_ms(DEBOUNCE_TIME);
  310. }
  311. }
  312.  
  313. int inside_radius(int x, int x_prev)
  314. {
  315. if (x>x_prev+COORDINATE_RADIUS || x<x_prev-COORDINATE_RADIUS)
  316. return 0;
  317. return 1;
  318. }
  319.  
  320. int inside_square(int x, int y, int x_prev, int y_prev)
  321. {
  322. if (inside_radius(x,x_prev) && inside_radius(y,y_prev))
  323. return 1;
  324. return 0;
  325. }
  326.  
  327. void read_xy(int *x, int *y, int *x_prev, int *y_prev)
  328. {
  329. *x_prev = *x;
  330. *y_prev = *y;
  331. Touch_Panel();
  332. *x = X;
  333. *y = 64-Y;
  334. }
  335.  
  336. void main(void)
  337. {
  338. Init_T2();
  339. ConfigureLCDPins();
  340. ConfigureTSPins();
  341. GLCD_LcdInit();
  342.  
  343. GLCD_ClrScr();
  344. initUART1();
  345.  
  346. ADCinit();
  347. ConfigureADCPins();
  348. ADCON1bits.ADON=1;
  349. TRISAbits.TRISA11=0;
  350.  
  351.  
  352. unsigned int curr_state = 0, next_state = 1;
  353. unsigned int curr_state2 = 0, next_state2 = 1;
  354. unsigned int password_valid = 1;
  355. int alcohol_quantity = 3208;
  356. int tmp = -1;
  357. unsigned int state3_time = 0;
  358.  
  359. int debouncer = 0;
  360. Touch_Panel();
  361. x=X;
  362. y=64-Y;
  363. while(1)
  364. {
  365. if (debouncer++ == DEBOUNCE_CYCLES)
  366. debouncer = 0;
  367. else continue;
  368.  
  369. if (curr_state == 0)
  370. {
  371. // TODO: ugasi bazer
  372. state3_time = 0;
  373. char password[5][5] = {" \0", "* \0", "** \0", "*** \0", "****\0"};
  374. if (next_state)
  375. {
  376. GLCD_ClrScr();
  377.  
  378. GoToXY(4,0);
  379. GLCD_Printf ("Password:");
  380. GLCD_Printf (password[curr_state2]);
  381.  
  382. GLCD_Rectangle(1,13,25,29);
  383. GLCD_Rectangle(26,13,50,29);
  384. GLCD_Rectangle(51,13,76,29);
  385. GLCD_Rectangle(77,13,101,29);
  386. GLCD_Rectangle(102,13,126,29);
  387. GLCD_Rectangle(1,30,25,46);
  388. GLCD_Rectangle(26,30,50,46);
  389. GLCD_Rectangle(51,30,76,46);
  390. GLCD_Rectangle(77,30,101,46);
  391. GLCD_Rectangle(102,30,126,46);
  392.  
  393. GoToXY(4,Y_one_to_five);
  394. GLCD_Printf ("1");
  395. GoToXY(29,Y_one_to_five);
  396. GLCD_Printf ("2");
  397. GoToXY(54,Y_one_to_five);
  398. GLCD_Printf ("3");
  399. GoToXY(80,Y_one_to_five);
  400. GLCD_Printf ("4");
  401. GoToXY(105,Y_one_to_five);
  402. GLCD_Printf ("5");
  403.  
  404. GoToXY(4,Y_six_to_zero);
  405. GLCD_Printf ("6");
  406. GoToXY(29,Y_six_to_zero);
  407. GLCD_Printf ("7");
  408. GoToXY(54,Y_six_to_zero);
  409. GLCD_Printf ("8");
  410. GoToXY(80,Y_six_to_zero);
  411. GLCD_Printf ("9");
  412. GoToXY(105,Y_six_to_zero);
  413. GLCD_Printf ("0");
  414.  
  415. GLCD_Rectangle(65,47,126,63);
  416. GoToXY(80,6);
  417. GLCD_Printf ("Nazad");
  418.  
  419. next_state = 0;
  420. }
  421. if (inside_square(x,y,x_prev,y_prev))
  422. {
  423. read_xy(&x,&y,&x_prev,&y_prev);
  424. continue;
  425. }
  426. // prepoznaje se sifra "1234"
  427. if (( 1<=x)&&(x<= 25) && (13<=y)&&(y<=29)) // pritisnuto dugme "1"
  428. {
  429. password_valid &= check_password(&curr_state2, &next_state, 1);
  430. }
  431. if (( 26<=x)&&(x<= 50) && (13<=y)&&(y<=29)) // pritisnuto dugme "2"
  432. {
  433. password_valid &= check_password(&curr_state2, &next_state, 2);
  434. }
  435. if (( 51<=x)&&(x<= 76) && (13<=y)&&(y<=29)) // pritisnuto dugme "3"
  436. {
  437. password_valid &= check_password(&curr_state2, &next_state, 3);
  438. }
  439. if (( 77<=x)&&(x<=101) && (13<=y)&&(y<=29)) // pritisnuto dugme "4"
  440. {
  441. password_valid &= check_password(&curr_state2, &next_state, 4);
  442. }
  443. if ((102<=x)&&(x<=126) && (13<=y)&&(y<=29)) // pritisnuto dugme "5"
  444. {
  445. password_valid &= check_password(&curr_state2, &next_state, 5);
  446. }
  447. if (( 1<=x)&&(x<= 25) && (30<=y)&&(y<=46)) // pritisnuto dugme "6"
  448. {
  449. password_valid &= check_password(&curr_state2, &next_state, 6);
  450. }
  451. if (( 26<=x)&&(x<= 50) && (30<=y)&&(y<=46)) // pritisnuto dugme "7"
  452. {
  453. password_valid &= check_password(&curr_state2, &next_state, 7);
  454. }
  455. if (( 51<=x)&&(x<= 76) && (30<=y)&&(y<=46)) // pritisnuto dugme "8"
  456. {
  457. password_valid &= check_password(&curr_state2, &next_state, 8);
  458. }
  459. if (( 77<=x)&&(x<=101) && (30<=y)&&(y<=46)) // pritisnuto dugme "9"
  460. {
  461. password_valid &= check_password(&curr_state2, &next_state, 9);
  462. }
  463. if ((102<=x)&&(x<=126) && (30<=y)&&(y<=46)) // pritisnuto dugme "0"
  464. {
  465. password_valid &= check_password(&curr_state2, &next_state, 0);
  466. }
  467. check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
  468. if (curr_state2 == STATE2_NUMBER)
  469. {
  470. Delay_ms(50);
  471. curr_state2 = 0;
  472. next_state = 1;
  473. if (password_valid)
  474. {
  475. curr_state = 1;
  476. next_state = 1;
  477. }
  478. else
  479. {
  480. curr_state = 2;
  481. next_state = 1;
  482. }
  483. }
  484. }//state 0
  485. if (curr_state == 1)
  486. {
  487. if (next_state)
  488. {
  489. GLCD_ClrScr();
  490.  
  491. GLCD_Rectangle(1,1,126,46);
  492. GoToXY(4,1);
  493. GLCD_Printf ("Sifra je tacna.");
  494. GoToXY(4,3);
  495. GLCD_Printf ("Molim Vas,");
  496. GoToXY(4,4);
  497. GLCD_Printf ("duvajte u alkometar.");
  498.  
  499. next_state = 0;
  500. read_xy(&x,&y,&x_prev,&y_prev);
  501. }
  502. if (inside_square(x,y,x_prev,y_prev))
  503. {
  504. read_xy(&x,&y,&x_prev,&y_prev);
  505. continue;
  506. }
  507. if ((1<=x)&&(x<=126) && (1<=y)&&(y<=46)) // pritisnuto dugme "pristupite alkotestu"
  508. curr_state = 3, next_state = 1, Delay_ms(DEBOUNCE_TIME);
  509. }//state 1
  510. if (curr_state == 2)
  511. {
  512. if (next_state)
  513. {
  514. GLCD_ClrScr();
  515. GoToXY(4,1);
  516. GLCD_Printf ("Sifra je netacna");
  517.  
  518. GLCD_Rectangle(65,47,126,63);
  519. GoToXY(80,6);
  520. GLCD_Printf ("Nazad");
  521.  
  522. next_state = 0;
  523. }
  524. if (inside_square(x,y,x_prev,y_prev))
  525. {
  526. read_xy(&x,&y,&x_prev,&y_prev);
  527. continue;
  528. }
  529. check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
  530. }//state2
  531. if (curr_state == 3)
  532. {
  533. if (next_state)
  534. {
  535. GLCD_ClrScr();
  536. GoToXY(4,1);
  537. GLCD_Printf ("Kolicina:");
  538. char alcohol_quantity_str[5];
  539. GLCD_Printf (int_to_array(alcohol_quantity,alcohol_quantity_str,10));
  540.  
  541. GLCD_Rectangle(65,47,126,63);
  542. GoToXY(80,6);
  543. GLCD_Printf ("Nazad");
  544.  
  545. next_state = 0;
  546. }
  547. state3_time++;
  548. if (alcohol_quantity > ALCOHOL_THRESHOLD) // prekoracen opseg za alkohol
  549. curr_state = 4, next_state = 1, Delay_ms(DEBOUNCE_TIME);
  550. if (state3_time > TIME_THRESHOLD) // proslo odredjeno vreme, osoba nije pijana
  551. curr_state = 5, next_state = 1, Delay_ms(DEBOUNCE_TIME);
  552. if (inside_square(x,y,x_prev,y_prev))
  553. {
  554. read_xy(&x,&y,&x_prev,&y_prev);
  555. continue;
  556. }
  557. check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
  558.  
  559. }//state 3
  560.  
  561. if (curr_state == 4)
  562. {
  563. if (next_state)
  564. {
  565. GLCD_ClrScr();
  566. GoToXY(4,1);
  567. GLCD_Printf ("Pijani ste");
  568. GoToXY(4,1);
  569. GLCD_Printf ("Kazna je 50km");
  570.  
  571. //TODO: dodati bazer
  572.  
  573. GLCD_Rectangle(65,47,126,63);
  574. GoToXY(80,6);
  575. GLCD_Printf ("Nazad");
  576.  
  577. next_state = 0;
  578. }
  579. bazer();
  580. if (inside_square(x,y,x_prev,y_prev))
  581. {
  582. read_xy(&x,&y,&x_prev,&y_prev);
  583. continue;
  584. }
  585. check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
  586. }//state 4
  587.  
  588. if (curr_state == 5)
  589. {
  590. if (next_state)
  591. {
  592. GLCD_ClrScr();
  593. GoToXY(4,1);
  594. GLCD_Printf ("Niste pijani");
  595.  
  596. GLCD_Rectangle(65,47,126,63);
  597. GoToXY(80,6);
  598. GLCD_Printf ("Nazad");
  599.  
  600. next_state = 0;
  601. }
  602. if (inside_square(x,y,x_prev,y_prev))
  603. {
  604. read_xy(&x,&y,&x_prev,&y_prev);
  605. continue;
  606. }
  607. check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
  608. }//state 5
  609.  
  610. read_xy(&x,&y,&x_prev,&y_prev);
  611. }//while
  612. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement