Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "tm4c123gh6pm.h" //Including the standard header for our chip
- #include <stdint.h>
- #include <stdbool.h>
- #include <string.h>
- #define col 3
- #define row 4
- #define RS 0x40
- #define EN 0x80
- #define LCD_DATA GPIO_PORTB_DATA_R
- #define LCD_CTRL GPIO_PORTB_DATA_R
- unsigned char ReadKeypad()
- {
- unsigned char colCount = 0;
- unsigned char rowCount = 0;
- while(1)
- {
- for(colCount = 0;colCount < col;colCount++)
- {
- GPIO_PORTE_DATA_R = 1<<colCount;
- for(rowCount = 0;rowCount < row;rowCount++)
- {
- if((GPIO_PORTA_DATA_R &(1<<(rowCount+2))) != 0)
- {
- msDelay(400);
- return ((rowCount*4)+colCount+1); //Returns the corresponding matrix value for this scenario
- }
- }
- }
- }
- }
- char GetKeyPressed(unsigned char value){
- char key;
- switch(value)
- {
- case 1:
- key = 1;
- break;
- case 2:
- key = 2;
- break;
- case 3:
- key = 3;
- break;
- case 4:
- key = -5; //These keys cannot be returned by a keypad press because of the algorithm we used to detect the keypress
- break;
- case 5:
- key = 4;
- break;
- case 6:
- key = 5;
- break;
- case 7:
- key = 6;
- break;
- case 8:
- key = -3; //These keys cannot be returned by a keypad press because of the algorithm we used to detect the keypress
- break;
- case 9:
- key = 7;
- break;
- case 10:
- key = 8;
- break;
- case 11:
- key = 9;
- break;
- case 12:
- key = -6; //These keys cannot be returned by a keypad press because of the algorithm we used to detect the keypress
- break;
- case 13:
- key = -6;
- break;
- case 14:
- key = 0;
- break;
- case 15:
- key = -13;
- break;
- }
- return key;
- }
- void KeypadInit()
- {
- volatile unsigned long delay;
- //Port E Init
- SYSCTL_RCGC2_R |= 0x00000010; //enable clock for Port E (columns)
- delay = SYSCTL_RCGC2_R;
- GPIO_PORTE_AMSEL_R = 0x00;
- GPIO_PORTE_PCTL_R = 0x00000000;
- GPIO_PORTE_DIR_R = 0xFF; //Port E as O/P
- GPIO_PORTE_AFSEL_R = 0x00;
- GPIO_PORTE_PUR_R = 0x00;
- GPIO_PORTE_DEN_R = 0xFF; //Digital Enable ON
- //Port A Init
- SYSCTL_RCGC2_R |= 0x00000001; //enable clock for Port A (rows)
- delay = SYSCTL_RCGC2_R;
- GPIO_PORTA_AMSEL_R = 0x00;
- GPIO_PORTA_PCTL_R = 0x00000000;
- GPIO_PORTA_DIR_R = 0x00; //Port A as I/P
- GPIO_PORTA_AFSEL_R = 0x00;
- GPIO_PORTA_PDR_R = 0xFF;
- GPIO_PORTA_DEN_R = 0xFF;
- }
- void msDelay(uint32_t t)
- /*Delay subroutine in millisecond*/
- {
- uint32_t i,j;
- for(i=0;i<t;i++)
- {
- for(j=0;j<2000;j++);
- }
- return;
- }
- void portB_Int(void) //Used to initialize PortB for the LCD
- {
- SYSCTL_RCGC2_R |= 0x00000002; //activate clock for PortA
- msDelay(5);
- GPIO_PORTB_AMSEL_R &= ~0xFF; //disable analog on PA
- msDelay(20);
- GPIO_PORTB_PCTL_R &= ~0xFFFFFF00; //PCTL GPIO on PA7-PA2
- msDelay(20);
- GPIO_PORTB_DIR_R |= 0xFC; //PA7-PA2 output
- msDelay(20);
- GPIO_PORTB_AFSEL_R &= 0x03; //disable alt function on PA7-PA2
- msDelay(20);
- GPIO_PORTB_DEN_R |= 0xFC; //enable digital I/O on PA7-PA2
- msDelay(20);
- return;
- }
- void LCD_CMD(uint32_t cmd)
- /*Used to write a command to the LCD. For sending command we used 4 bit mode of the LCD.
- The high 4 bits are sent first. For commands RS is set low. In order to capture the
- data E must be high and then low*/
- {
- uint8_t x;
- x = (cmd & 0xF0) >> 2; //shifting higher 4 bits to PA5-PA2
- LCD_DATA &= ~0x3C; //clearing bits PA5_PA2
- LCD_DATA |= x; //sending higher 4 bits to PortA
- msDelay(1);
- LCD_CTRL &= ~RS; //seting RS command to low
- msDelay(1);
- LCD_CTRL |= EN; //raising enable
- msDelay(1);
- LCD_CTRL &= ~EN; //droping enable
- msDelay(2); //waiting for command to be captured
- x = (cmd & 0x0F) << 2; //shifting lower 4 bits to PA5-PA2
- LCD_DATA &= ~0x3C; //clearing bits PA5_PA2
- LCD_DATA |= x; //sending lower 4 bits to PortA
- msDelay(1);
- LCD_CTRL |= EN; //raiseing enable
- msDelay(1);
- LCD_CTRL &= ~EN; //droping enable
- msDelay(2); //waiting for command to be captured
- return;
- }
- void LCD_WRT(uint32_t cmd)
- /*Used to write data to the LCD. For sending data we used 4 bit mode of LCD.
- The high 4 bits are sent first. For data RS is set high. In order to capture the
- data E has to be toggled high then low*/
- {
- uint8_t x;
- x = (cmd & 0xF0) >> 2; //shifting higher 4 bits to PA5-PA2
- LCD_DATA &= ~0x3C; //clearing bits A5_A2
- LCD_DATA |= x; //sending high 4 bits to PortA
- msDelay(1);
- LCD_CTRL |= RS; //seting RS command to high
- msDelay(1);
- LCD_CTRL |= EN; //raisijng enable
- msDelay(1);
- LCD_CTRL &= ~EN; //droping enable
- msDelay(2); //waiting for command to be captured
- x = (cmd & 0x0F) << 2; //shifting lower 4 bits to PA5-PA2
- LCD_DATA &= ~0x3C; //clearing bits PA5_PA2
- LCD_DATA |= x; //sending lower 4 bits to port A
- msDelay(1);
- LCD_CTRL |= EN; //raising enable
- msDelay(1);
- LCD_CTRL &= ~EN; //droping enable
- msDelay(2); //waiting for command to be captured
- return;
- }
- void LCD_HIGH_4_BITS(uint32_t cmd)
- /*Used to write only the higher 4 bits of a command to the LCD as we dont want
- to send lower 4 bits*/
- {
- uint8_t x;
- x = (cmd & 0xF0) >> 2; //shifting higher 4 bits to PA5-PA2
- LCD_DATA &= ~0x3C; //clearing bits PA5_PA2
- LCD_DATA |= x; //sending higher 4 bits to PortA
- msDelay(1);
- LCD_CTRL &= ~RS; //reseting RS command to low
- msDelay(1);
- LCD_CTRL |= EN; //raising enable
- msDelay(1);
- LCD_CTRL &= ~EN; //droping enable
- msDelay(2); //waiting for command to be captured
- return;
- }
- void LCD_Int(void)
- /*Setting up the LCD*/
- {
- LCD_CMD(0x30);
- LCD_CMD(0x30);
- LCD_CMD(0x30); //we must send three times to turn on the LCD
- LCD_HIGH_4_BITS(0x20); //seting The LCD in 4 bit mode
- LCD_CMD(0x28); //seting on 4 bit data length, 2 display lines and 5x8 dots
- LCD_CMD(0x01); //clearing display
- LCD_CMD(0x0C); //turning on display, cursor and blinking cursor
- LCD_CMD(0x14); //seting direction
- }
- void LCD_CLR(void)
- /*Used to clear the LCD and return the cursor to home position*/
- {
- LCD_CMD(0x01); //clears display
- LCD_CMD(0x02); //returns cursor home
- return;
- }
- void LCD_PRNT(char ch) //Used to print a single character to the LCD.
- {
- LCD_WRT(ch);
- return;
- }
- void LCD_PRNTstring(char *line)
- /*Used to print a string to the LCD. It uses strlen so a terminator doesn't have to
- be used*/
- {
- uint8_t count = 0;
- while(count != strlen(line)) //looping through the string until strlen is reached
- {
- LCD_WRT(line[count]);
- count++;
- }
- return;
- }
- void main(void){
- portB_Int();
- LCD_Int();
- LCD_CLR();
- KeypadInit();
- unsigned char aKey = 0;
- unsigned char aKey1 = 0;
- unsigned char aKey2 = 0;
- unsigned char aKey3 = 0;
- unsigned char pass = 1;
- unsigned char pass1 = 2;
- unsigned char pass2 = 3;
- unsigned char pass3 = 4;
- int menuStage = 1;
- int flag = 0;
- int flag1 = 0;
- while(1)
- {
- if(menuStage==1)
- {
- LCD_CLR();
- LCD_PRNTstring("Enter the password: ");
- aKey=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(42);
- aKey1=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(42);
- aKey2=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(42);
- aKey3=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(42);
- //LCD_PRNT(aKey2+48);
- msDelay(500);
- if(aKey==pass && aKey1==pass1 && aKey2==pass2 && aKey3==pass3)
- {
- menuStage=2;
- }
- else
- {
- menuStage=1;
- LCD_CLR();
- LCD_PRNTstring("Incorrect Password! ");
- LCD_PRNTstring("Please try again. ");
- msDelay(3000);
- }
- }
- else if(menuStage==2)
- {
- LCD_CLR();
- LCD_PRNTstring("1. Motor Control ");
- LCD_PRNTstring("3. Change Password ");
- LCD_PRNTstring("2. Bluetooth Control");
- LCD_PRNTstring("4. More options ");
- char m2;
- m2=(GetKeyPressed(ReadKeypad()));
- //msDelay(250);
- if(m2==1)
- {
- LCD_CLR();
- LCD_PRNTstring("1. Motor ON/OFF ");
- //LCD_PRNTstring(" ");
- LCD_PRNTstring("2. Back to main menu");
- char m2a;
- m2a=(GetKeyPressed(ReadKeypad()));
- //msDelay(500);
- if(m2a==1)
- {
- LCD_CLR();
- if(flag==0)
- {
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" Motor ON ");
- msDelay(1000);
- flag=1;
- }
- else if(flag==1)
- {
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" Motor OFF ");
- msDelay(1000);
- flag=0;
- }
- }
- else if(m2a==2)
- {
- LCD_CLR();
- //msDelay(250);
- }
- }
- else if(m2==2)
- {
- LCD_CLR();
- LCD_PRNTstring("1. Bluetooth ON/OFF ");
- //LCD_PRNTstring(" ");
- LCD_PRNTstring("2. Back to main menu");
- char m2b;
- m2b=(GetKeyPressed(ReadKeypad()));
- //msDelay(500);
- if(m2b==1)
- {
- LCD_CLR();
- if(flag1==0)
- {
- LCD_CLR();
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" Bluetooth ON ");
- msDelay(1000);
- flag1=1;
- }
- else if(flag1==1)
- {
- LCD_CLR();
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" Bluetooth OFF ");
- msDelay(1000);
- flag1=0;
- }
- }
- else if(m2b==2)
- {
- LCD_CLR();
- //msDelay(250);
- }
- }
- else if(m2==3)
- {
- LCD_CLR();
- LCD_PRNTstring("Enter old password: ");
- char oldKey=pass;
- char oldKey1=pass1;
- char oldKey2=pass2;
- char oldKey3=pass3;
- char temp;
- char temp1;
- char temp2;
- char temp3;
- temp=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(temp+48);
- temp1=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(temp1+48);
- temp2=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(temp2+48);
- temp3=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(temp3+48);
- msDelay(750);
- if(temp==oldKey && temp1==oldKey1 && temp2==oldKey2 && temp3==oldKey3)
- {
- LCD_CLR();
- LCD_PRNTstring("Enter new password: ");
- temp=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(temp+48);
- temp1=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(temp1+48);
- temp2=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(temp2+48);
- temp3=(GetKeyPressed(ReadKeypad()));
- LCD_PRNT(temp3+48);
- msDelay(750);
- pass=temp;
- pass1=temp1;
- pass2=temp2;
- pass3=temp3;
- LCD_CLR();
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" New password set! ");
- msDelay(1000);
- }
- else
- {
- LCD_CLR();
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" ");
- LCD_PRNTstring(" Invalid password! ");
- msDelay(1000);
- }
- }
- else if(m2==4)
- {
- menuStage=3;
- }
- }
- else if(menuStage==3)
- {
- LCD_CLR();
- LCD_PRNTstring("1. Lock the screen ");
- LCD_PRNTstring(" ");
- LCD_PRNTstring("2. Previous screen ");
- char m3;
- m3=(GetKeyPressed(ReadKeypad()));
- //msDelay(500);
- if(m3==1)
- {
- menuStage=1;
- }
- else if(m3==2)
- {
- menuStage=2;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement