Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<p30fxxxx.h>
- #include <stdlib.h>
- #include "driverGLCD.h"
- #include "adc.h"
- #define PASSWORD "1234"
- #define ALCOHOL_THRESHOLD 3200
- #define TIME_THRESHOLD 20 // 10otkucaja ~ 2sekundi
- #define DELAY 10
- #define Y_one_to_five 2
- #define Y_six_to_zero 4
- #define DEBOUNCE_CYCLES 0
- #define DEBOUNCE_TIME 20
- #define COORDINATE_RADIUS 10
- #define STATE2_NUMBER 2
- int x_prev = -300, y_prev = -300;
- int x = 2000, y = 2000;
- //_FOSC(CSW_FSCM_OFF & XT_PLL4);//instruction takt je isti kao i kristal//
- _FOSC(CSW_ON_FSCM_OFF & HS3_PLL4);
- _FWDT(WDT_OFF);
- _FGS(CODE_PROT_OFF);
- unsigned int X, Y,x_vrednost, y_vrednost;
- //const unsigned int ADC_THRESHOLD = 900;
- const unsigned int AD_Xmin =220;
- const unsigned int AD_Xmax =3642;
- const unsigned int AD_Ymin =520;
- const unsigned int AD_Ymax =3450;
- int vrijeme=0;
- unsigned int sirovi0,sirovi1;
- unsigned int broj,broj1,broj2,temp0,temp1;
- //#define DRIVE_A PORTBbits.RB10
- //#define DRIVE_B PORTCbits.RC13
- #define DRIVE_A PORTCbits.RC13
- #define DRIVE_B PORTCbits.RC14
- void ConfigureTSPins(void)
- {
- //ADPCFGbits.PCFG10=1;
- //ADPCFGbits.PCFG7=digital;
- //TRISBbits.TRISB10=0;
- TRISCbits.TRISC13=0;
- TRISCbits.TRISC14=0;
- //LATCbits.LATC14=0;
- //LATCbits.LATC13=0;
- }
- void initUART1(void)
- {
- U1BRG=0x0015;//ovim odredjujemo baudrate
- U1MODEbits.ALTIO=0;//biramo koje pinove koristimo za komunikaciju osnovne ili alternativne
- IEC0bits.U1RXIE=1;//omogucavamo rx1 interupt
- U1STA&=0xfffc;
- U1MODEbits.UARTEN=1;//ukljucujemo ovaj modul
- U1STAbits.UTXEN=1;//ukljucujemo predaju
- }
- void __attribute__((__interrupt__)) _U1RXInterrupt(void)
- {
- IFS0bits.U1RXIF = 0;
- // tempRX=U1RXREG;
- }
- void WriteUART1(unsigned int data)
- {
- while(!U1STAbits.TRMT);
- if(U1MODEbits.PDSEL == 3)
- U1TXREG = data;
- else
- U1TXREG = data & 0xFF;
- }
- /***********************************************************************
- * Ime funkcije : WriteUART1dec2string *
- * Opis : Funkcija salje 4-cifrene brojeve (cifru po cifru)*
- * Parameteri : unsigned int data-podatak koji zelimo poslati *
- * Povratna vrednost : Nema *
- ************************************************************************/
- void WriteUART1dec2string(unsigned int data)
- {
- unsigned char temp;
- temp=data/1000;
- WriteUART1(temp+'0');
- data=data-temp*1000;
- temp=data/100;
- WriteUART1(temp+'0');
- data=data-temp*100;
- temp=data/10;
- WriteUART1(temp+'0');
- data=data-temp*10;
- WriteUART1(data+'0');
- }
- void Delay(unsigned int N)
- {
- unsigned int i;
- for(i=0;i<N;i++);
- }
- void Touch_Panel (void)
- {
- // vode horizontalni tranzistori
- DRIVE_A = 1;
- DRIVE_B = 0;
- LATCbits.LATC13=1;
- LATCbits.LATC14=0;
- Delay(500); //cekamo jedno vreme da se odradi AD konverzija
- // ocitavamo x
- x_vrednost = temp0;//temp0 je vrednost koji nam daje AD konvertor na BOTTOM pinu
- // vode vertikalni tranzistori
- LATCbits.LATC13=0;
- LATCbits.LATC14=1;
- DRIVE_A = 0;
- DRIVE_B = 1;
- Delay(500); //cekamo jedno vreme da se odradi AD konverzija
- // ocitavamo y
- y_vrednost = temp1;// temp1 je vrednost koji nam daje AD konvertor na LEFT pinu
- //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
- //skaliranje x-koordinate
- X=(x_vrednost-161)*0.03629;
- //X= ((x_vrednost-AD_Xmin)/(AD_Xmax-AD_Xmin))*128;
- //vrednosti AD_Xmin i AD_Xmax su minimalne i maksimalne vrednosti koje daje AD konvertor za touch panel.
- //Skaliranje Y-koordinate
- Y= ((y_vrednost-500)*0.020725);
- // Y= ((y_vrednost-AD_Ymin)/(AD_Ymax-AD_Ymin))*64;
- }
- void __attribute__((__interrupt__)) _ADCInterrupt(void)
- {
- sirovi0=ADCBUF0;//0
- sirovi1=ADCBUF1;//1
- temp0=sirovi0;
- temp1=sirovi1;
- IFS0bits.ADIF = 0;
- }
- void Write_GLCD(unsigned int data)
- {
- unsigned char temp;
- temp=data/1000;
- Glcd_PutChar(temp+'0');
- data=data-temp*1000;
- temp=data/100;
- Glcd_PutChar(temp+'0');
- data=data-temp*100;
- temp=data/10;
- Glcd_PutChar(temp+'0');
- data=data-temp*10;
- Glcd_PutChar(data+'0');
- }
- inline void swap(char *x, char *y) {
- char t = *x; *x = *y; *y = t;
- }
- char* reverse(char *buffer, int i, int j)
- {
- while (i < j)
- swap(&buffer[i++], &buffer[j--]);
- return buffer;
- }
- char* int_to_array(int value, char* buffer, int base)
- {
- // invalid input
- if (base < 2 || base > 32)
- return buffer;
- // consider absolute value of number
- int n = abs(value);
- int i = 0;
- while (n)
- {
- int r = n % base;
- if (r >= 10)
- buffer[i++] = 65 + (r - 10);
- else
- buffer[i++] = 48 + r;
- n = n / base;
- }
- // if number is 0
- if (i == 0)
- buffer[i++] = '0';
- // If base is 10 and value is negative, the resulting string
- // is preceded with a minus sign (-)
- // With any other base, value is always considered unsigned
- if (value < 0 && base == 10)
- buffer[i++] = '-';
- buffer[i] = '\0'; // null terminate string
- // reverse the string and return it
- return reverse(buffer, 0, i - 1);
- }
- unsigned int brojac_ms,stoperica,ms,sekund;
- void Delay_ms (int vreme)//funkcija za kasnjenje u milisekundama
- {
- stoperica = 0;
- while(stoperica < vreme);
- }
- void __attribute__ ((__interrupt__)) _T2Interrupt(void) // svakih 1ms
- {
- TMR2 =0;
- ms=1;//fleg za milisekundu ili prekid;potrebno ga je samo resetovati u funkciji
- brojac_ms++;//brojac milisekundi
- stoperica++;//brojac za funkciju Delay_ms
- if (brojac_ms==1000)//sek
- {
- brojac_ms=0;
- sekund=1;//fleg za sekundu
- }
- IFS0bits.T2IF = 0;
- }
- #define TMR2_period 10000 /* Fosc = 10MHz,
- 1/Fosc = 0.1us !!!, 0.1us * 10000 = 1ms */
- void Init_T2(void)
- {
- TMR2 = 0;
- PR2 = TMR2_period;
- T2CONbits.TCS = 0; // 0 = Internal clock (FOSC/4)
- //IPC1bits.T2IP = 3 // T2 interrupt pririty (0-7)
- //SRbits.IPL = 3; // CPU interrupt priority is 3(11)
- IFS0bits.T2IF = 0; // clear interrupt flag
- IEC0bits.T2IE = 1; // enable interrupt
- T2CONbits.TON = 1; // T2 on
- }
- // ZEKI DODAO BAZER
- void bazer(void){ //pwm signal za bazer za svaki interapt i jedan if ya loyinku
- LATAbits.LATA11=1;
- Delay_ms(2); // faktor ispoune 50posto
- LATAbits.LATA11=0;
- Delay_ms(1);
- }
- unsigned int check_password(unsigned int *curr_state2, unsigned int *next_state, unsigned int number)
- {
- Delay_ms(DEBOUNCE_TIME);
- if (!(*next_state))
- {
- *next_state = 1;
- if (PASSWORD[(*curr_state2)++] != number+'0')
- return 0;
- }
- return 1;
- }
- 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)
- {
- if ((65<=x)&&(x<=126) && (47<=y)&&(y<=63)) // pritisnuto dugme "nazad"
- {
- *curr_state = 0, *next_state = 1, *curr_state2 = 0, *password_valid = 1;
- Touch_Panel();
- GoToXY(4,Y_one_to_five);
- Delay_ms(DEBOUNCE_TIME);
- }
- }
- int inside_radius(int x, int x_prev)
- {
- if (x>x_prev+COORDINATE_RADIUS || x<x_prev-COORDINATE_RADIUS)
- return 0;
- return 1;
- }
- int inside_square(int x, int y, int x_prev, int y_prev)
- {
- if (inside_radius(x,x_prev) && inside_radius(y,y_prev))
- return 1;
- return 0;
- }
- void read_xy(int *x, int *y, int *x_prev, int *y_prev)
- {
- *x_prev = *x;
- *y_prev = *y;
- Touch_Panel();
- *x = X;
- *y = 64-Y;
- }
- void main(void)
- {
- Init_T2();
- ConfigureLCDPins();
- ConfigureTSPins();
- GLCD_LcdInit();
- GLCD_ClrScr();
- initUART1();
- ADCinit();
- ConfigureADCPins();
- ADCON1bits.ADON=1;
- TRISAbits.TRISA11=0;
- unsigned int curr_state = 0, next_state = 1;
- unsigned int curr_state2 = 0, next_state2 = 1;
- unsigned int password_valid = 1;
- int alcohol_quantity = 3208;
- int tmp = -1;
- unsigned int state3_time = 0;
- int debouncer = 0;
- Touch_Panel();
- x=X;
- y=64-Y;
- while(1)
- {
- if (debouncer++ == DEBOUNCE_CYCLES)
- debouncer = 0;
- else continue;
- if (curr_state == 0)
- {
- // TODO: ugasi bazer
- state3_time = 0;
- char password[5][5] = {" \0", "* \0", "** \0", "*** \0", "****\0"};
- if (next_state)
- {
- GLCD_ClrScr();
- GoToXY(4,0);
- GLCD_Printf ("Password:");
- GLCD_Printf (password[curr_state2]);
- GLCD_Rectangle(1,13,25,29);
- GLCD_Rectangle(26,13,50,29);
- GLCD_Rectangle(51,13,76,29);
- GLCD_Rectangle(77,13,101,29);
- GLCD_Rectangle(102,13,126,29);
- GLCD_Rectangle(1,30,25,46);
- GLCD_Rectangle(26,30,50,46);
- GLCD_Rectangle(51,30,76,46);
- GLCD_Rectangle(77,30,101,46);
- GLCD_Rectangle(102,30,126,46);
- GoToXY(4,Y_one_to_five);
- GLCD_Printf ("1");
- GoToXY(29,Y_one_to_five);
- GLCD_Printf ("2");
- GoToXY(54,Y_one_to_five);
- GLCD_Printf ("3");
- GoToXY(80,Y_one_to_five);
- GLCD_Printf ("4");
- GoToXY(105,Y_one_to_five);
- GLCD_Printf ("5");
- GoToXY(4,Y_six_to_zero);
- GLCD_Printf ("6");
- GoToXY(29,Y_six_to_zero);
- GLCD_Printf ("7");
- GoToXY(54,Y_six_to_zero);
- GLCD_Printf ("8");
- GoToXY(80,Y_six_to_zero);
- GLCD_Printf ("9");
- GoToXY(105,Y_six_to_zero);
- GLCD_Printf ("0");
- GLCD_Rectangle(65,47,126,63);
- GoToXY(80,6);
- GLCD_Printf ("Nazad");
- next_state = 0;
- }
- if (inside_square(x,y,x_prev,y_prev))
- {
- read_xy(&x,&y,&x_prev,&y_prev);
- continue;
- }
- // prepoznaje se sifra "1234"
- if (( 1<=x)&&(x<= 25) && (13<=y)&&(y<=29)) // pritisnuto dugme "1"
- {
- password_valid &= check_password(&curr_state2, &next_state, 1);
- }
- if (( 26<=x)&&(x<= 50) && (13<=y)&&(y<=29)) // pritisnuto dugme "2"
- {
- password_valid &= check_password(&curr_state2, &next_state, 2);
- }
- if (( 51<=x)&&(x<= 76) && (13<=y)&&(y<=29)) // pritisnuto dugme "3"
- {
- password_valid &= check_password(&curr_state2, &next_state, 3);
- }
- if (( 77<=x)&&(x<=101) && (13<=y)&&(y<=29)) // pritisnuto dugme "4"
- {
- password_valid &= check_password(&curr_state2, &next_state, 4);
- }
- if ((102<=x)&&(x<=126) && (13<=y)&&(y<=29)) // pritisnuto dugme "5"
- {
- password_valid &= check_password(&curr_state2, &next_state, 5);
- }
- if (( 1<=x)&&(x<= 25) && (30<=y)&&(y<=46)) // pritisnuto dugme "6"
- {
- password_valid &= check_password(&curr_state2, &next_state, 6);
- }
- if (( 26<=x)&&(x<= 50) && (30<=y)&&(y<=46)) // pritisnuto dugme "7"
- {
- password_valid &= check_password(&curr_state2, &next_state, 7);
- }
- if (( 51<=x)&&(x<= 76) && (30<=y)&&(y<=46)) // pritisnuto dugme "8"
- {
- password_valid &= check_password(&curr_state2, &next_state, 8);
- }
- if (( 77<=x)&&(x<=101) && (30<=y)&&(y<=46)) // pritisnuto dugme "9"
- {
- password_valid &= check_password(&curr_state2, &next_state, 9);
- }
- if ((102<=x)&&(x<=126) && (30<=y)&&(y<=46)) // pritisnuto dugme "0"
- {
- password_valid &= check_password(&curr_state2, &next_state, 0);
- }
- check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
- if (curr_state2 == STATE2_NUMBER)
- {
- Delay_ms(50);
- curr_state2 = 0;
- next_state = 1;
- if (password_valid)
- {
- curr_state = 1;
- next_state = 1;
- }
- else
- {
- curr_state = 2;
- next_state = 1;
- }
- }
- }//state 0
- if (curr_state == 1)
- {
- if (next_state)
- {
- GLCD_ClrScr();
- GLCD_Rectangle(1,1,126,46);
- GoToXY(4,1);
- GLCD_Printf ("Sifra je tacna.");
- GoToXY(4,3);
- GLCD_Printf ("Molim Vas,");
- GoToXY(4,4);
- GLCD_Printf ("duvajte u alkometar.");
- next_state = 0;
- read_xy(&x,&y,&x_prev,&y_prev);
- }
- if (inside_square(x,y,x_prev,y_prev))
- {
- read_xy(&x,&y,&x_prev,&y_prev);
- continue;
- }
- if ((1<=x)&&(x<=126) && (1<=y)&&(y<=46)) // pritisnuto dugme "pristupite alkotestu"
- curr_state = 3, next_state = 1, Delay_ms(DEBOUNCE_TIME);
- }//state 1
- if (curr_state == 2)
- {
- if (next_state)
- {
- GLCD_ClrScr();
- GoToXY(4,1);
- GLCD_Printf ("Sifra je netacna");
- GLCD_Rectangle(65,47,126,63);
- GoToXY(80,6);
- GLCD_Printf ("Nazad");
- next_state = 0;
- }
- if (inside_square(x,y,x_prev,y_prev))
- {
- read_xy(&x,&y,&x_prev,&y_prev);
- continue;
- }
- check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
- }//state2
- if (curr_state == 3)
- {
- if (next_state)
- {
- GLCD_ClrScr();
- GoToXY(4,1);
- GLCD_Printf ("Kolicina:");
- char alcohol_quantity_str[5];
- GLCD_Printf (int_to_array(alcohol_quantity,alcohol_quantity_str,10));
- GLCD_Rectangle(65,47,126,63);
- GoToXY(80,6);
- GLCD_Printf ("Nazad");
- next_state = 0;
- }
- state3_time++;
- if (alcohol_quantity > ALCOHOL_THRESHOLD) // prekoracen opseg za alkohol
- curr_state = 4, next_state = 1, Delay_ms(DEBOUNCE_TIME);
- if (state3_time > TIME_THRESHOLD) // proslo odredjeno vreme, osoba nije pijana
- curr_state = 5, next_state = 1, Delay_ms(DEBOUNCE_TIME);
- if (inside_square(x,y,x_prev,y_prev))
- {
- read_xy(&x,&y,&x_prev,&y_prev);
- continue;
- }
- check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
- }//state 3
- if (curr_state == 4)
- {
- if (next_state)
- {
- GLCD_ClrScr();
- GoToXY(4,1);
- GLCD_Printf ("Pijani ste");
- GoToXY(4,1);
- GLCD_Printf ("Kazna je 50km");
- //TODO: dodati bazer
- GLCD_Rectangle(65,47,126,63);
- GoToXY(80,6);
- GLCD_Printf ("Nazad");
- next_state = 0;
- }
- bazer();
- if (inside_square(x,y,x_prev,y_prev))
- {
- read_xy(&x,&y,&x_prev,&y_prev);
- continue;
- }
- check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
- }//state 4
- if (curr_state == 5)
- {
- if (next_state)
- {
- GLCD_ClrScr();
- GoToXY(4,1);
- GLCD_Printf ("Niste pijani");
- GLCD_Rectangle(65,47,126,63);
- GoToXY(80,6);
- GLCD_Printf ("Nazad");
- next_state = 0;
- }
- if (inside_square(x,y,x_prev,y_prev))
- {
- read_xy(&x,&y,&x_prev,&y_prev);
- continue;
- }
- check_reset(x,y, &curr_state, &next_state, &curr_state2, &password_valid);
- }//state 5
- read_xy(&x,&y,&x_prev,&y_prev);
- }//while
- }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement