Advertisement
Guest User

Door Unlocking

a guest
Jan 18th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "NUC100Series.h"
  3. #include "MCU_init.h"
  4. #include "SYS_init.h"
  5. #include "LCD.h"
  6. #include "Draw2D.h"
  7. #include "picture.h"
  8. #define SYSTICK_DLAY_us 1000000
  9.  
  10. #define true 1
  11. #define false 0
  12. #define PASSWORD_SIZE 6
  13. #define BUZZER_BIPTIME 5
  14. #define BUZZER_BIPDELAY 2000000
  15. //------------------------------------------------------------------------------------------------------------------------------------
  16. // Functions declaration
  17. //------------------------------------------------------------------------------------------------------------------------------------
  18. typedef enum {initial_screen, password_input, password_fail,update_pass, welcome_screen, pass_change_success} STATES;
  19. STATES state = initial_screen;
  20. uint8_t interuptFlag = 0;
  21. void System_Config(void);
  22. void SPI3_Config(void);
  23. void LCD_start(void);
  24. void LCD_command(unsigned char temp);
  25. void LCD_data(unsigned char temp);
  26. void LCD_clear(void);
  27. void LCD_SetAddress(uint8_t PageAddr, uint8_t ColumnAddr);
  28. void KeyPadEnable(void);
  29. void EINT1_IRQHandler(void);
  30. void interrupt_configure(void);
  31. int equals(char* first, char* second);
  32. void handleInput(char number, char* inputDes, char* passwordDisplay);
  33. void startInitialScreen(void);
  34. void startPasswordInputScreen(void);
  35. void startPasswordUpdateScreen(void);
  36. void startWelcomeScreen(void);
  37. void startPasswordFailScreen(void);
  38. void startPasswordChangeScreen(void);
  39. void startStateMachine(void);
  40.  
  41. uint8_t KeyPadScanning(void);
  42. uint8_t a;
  43. uint8_t key_pressed = 0;
  44. uint8_t z;
  45. char password[]={'1','2','3','4','5','6'};
  46. char input[]={'0','0','0','0','0','0'};
  47. char passwordDisplay[] = {'-','-','-','-','-','-'};
  48. //------------------------------------------------------------------------------------------------------------------------------------
  49. // Main program
  50. //------------------------------------------------------------------------------------------------------------------------------------
  51.  
  52. int main(void)
  53. {
  54.     //System initialization
  55.     System_Config();
  56.     KeyPadEnable();
  57.     //SPI3 initialization
  58.     SPI3_Config();
  59.     //LCD initialization
  60.     LCD_start();
  61.     LCD_clear();
  62.     //Interrupt configuration
  63.     interrupt_configure();
  64.  
  65.     while (1) {
  66.         startStateMachine();
  67.         CLK_SysTickDelay(2000000);
  68.     }
  69. }
  70.  
  71. void startStateMachine(void) {
  72.     switch(state){
  73.             //Initial screen state
  74.             case initial_screen:
  75.                 startInitialScreen();
  76.                 break;
  77.             //Input password state
  78.             case password_input:
  79.                 startPasswordInputScreen();
  80.                 break;
  81.             //Update password state
  82.             case update_pass:
  83.                 startPasswordUpdateScreen();
  84.                 break;
  85.             //Password is correct state
  86.             case welcome_screen:
  87.                 startWelcomeScreen();
  88.                 break;
  89.             //Password is incorrect state
  90.             case password_fail:
  91.                 startPasswordFailScreen();
  92.                 break;
  93.             //Successfully change password state
  94.             case pass_change_success:
  95.                 startPasswordChangeScreen();
  96.                 break;
  97.             default: break;
  98.         }
  99. }
  100.  
  101. void interrupt_configure(void) {
  102.     //GPIO Interrupt configuration. GPIO-B15 is the interrupt source
  103.     PB->PMD &= (~(0x03ul << 30));
  104.     PB->IMD &= (~(1ul << 15));
  105.     PB->IEN |= (1ul << 15);
  106.     //NVIC interrupt configuration for GPIO-B15 interrupt source
  107.     NVIC->ISER[0] |= 1ul<<3;
  108.     NVIC->IP[0] &= (~(3ul<<30));
  109. }
  110.  
  111. void startPasswordChangeScreen(void) {
  112.     LCD_clear();
  113.     printS_5x7(1, 32, "PASSWORD CHANGED");
  114.     CLK_SysTickDelay(2000000);
  115.     state = initial_screen;
  116. }
  117.  
  118. void startPasswordFailScreen(void) {
  119.     LCD_clear();
  120.     printS_5x7(1, 0, "Password is incorrect");
  121.     CLK_SysTickDelay(5000000);
  122.     state = initial_screen;
  123. }
  124.  
  125. void startWelcomeScreen(void) {
  126.     LCD_clear();
  127.     print_Line(1, "Welcome home");
  128. }
  129.  
  130. void startPasswordUpdateScreen(void) {
  131.     LCD_clear();
  132.     a = 0;
  133.     printS_5x7(1, 0, "Enter the new password");
  134.     print_Line(2, passwordDisplay);
  135.     while(a<PASSWORD_SIZE && interuptFlag == 0){
  136.                    
  137.     while(key_pressed==0 && interuptFlag == 0) key_pressed = KeyPadScanning();
  138.     switch(key_pressed) {
  139.         case 1: handleInput('1', password, passwordDisplay); break;
  140.         case 2: handleInput('2', password, passwordDisplay); break;
  141.         case 3: handleInput('3', password, passwordDisplay); break;
  142.         case 4: handleInput('4', password, passwordDisplay); break;
  143.         case 5: handleInput('5', password, passwordDisplay); break;
  144.         case 6: handleInput('6', password, passwordDisplay); break;
  145.         case 7: handleInput('7', password, passwordDisplay); break;
  146.         case 8: handleInput('8', password, passwordDisplay); break;
  147.         case 9: handleInput('9', password, passwordDisplay); break;
  148.         default:;
  149.     }
  150.     key_pressed = 0;
  151.     a++;
  152.     }
  153.                
  154.     if (interuptFlag == 1){
  155.         interuptFlag = 0;
  156.         state = initial_screen;
  157.     }
  158.     else if (interuptFlag == 0)
  159.         state = pass_change_success;
  160.     for (z = 0; z<6; z++) passwordDisplay[z] = '-';
  161. }
  162.  
  163. void startPasswordInputScreen(void) {
  164.     LCD_clear();
  165.     a = 0;
  166.     printS_5x7(1, 0, "Input your password");
  167.     print_Line(2, passwordDisplay);
  168.     while(a<PASSWORD_SIZE && interuptFlag == 0){
  169.         while(key_pressed==0 && interuptFlag == 0) key_pressed = KeyPadScanning();
  170.         switch(key_pressed) {
  171.             case 1: handleInput('1', input, passwordDisplay); break;
  172.             case 2: handleInput('2', input, passwordDisplay); break;
  173.             case 3: handleInput('3', input, passwordDisplay); break;
  174.             case 4: handleInput('4', input, passwordDisplay); break;
  175.             case 5: handleInput('5', input, passwordDisplay); break;
  176.             case 6: handleInput('6', input, passwordDisplay); break;
  177.             case 7: handleInput('7', input, passwordDisplay); break;
  178.             case 8: handleInput('8', input, passwordDisplay); break;
  179.             case 9: handleInput('9', input, passwordDisplay); break;
  180.             default:;
  181.         }
  182.         key_pressed = 0;
  183.         a++;
  184.     }
  185.                
  186.     if (equals(password, input) && interuptFlag == 0)
  187.         state = welcome_screen;
  188.     else if (!equals(password, input) && interuptFlag == 0)
  189.         state = password_fail;
  190.     else if (!equals(password, input) && interuptFlag == 1)
  191.         state = initial_screen;
  192.     interuptFlag = 0;
  193.     for (z = 0; z<6; z++) passwordDisplay[z] = '-';
  194. }
  195.  
  196. void startInitialScreen(void) {
  197.     LCD_clear();
  198.     printS_5x7(15, 0, "EEET2481 - Door lock");
  199.     printS_5x7(45, 10, "system");          
  200.     printS_5x7(1, 30, "Press 1 to enter password");
  201.     printS_5x7(1, 40, "Press 2 to change password");
  202.     while(key_pressed==0 && interuptFlag == 0) key_pressed = KeyPadScanning();
  203.     interuptFlag = 0;
  204.     switch(key_pressed) {
  205.     case 1: state = password_input; break;
  206.     case 2: state = update_pass; break;
  207.     //default: break;
  208.     }
  209.     key_pressed = 0;
  210.     // state transition
  211. }
  212.  
  213. void handleInput(char number, char* inputDes, char* passwordDisplay){
  214.     input[a] = number;
  215.     passwordDisplay[a] = number;
  216.     print_Line(2, passwordDisplay);
  217.     CLK_SysTickDelay(200000);
  218.     passwordDisplay[a] = '*';
  219.     print_Line(2, passwordDisplay);
  220. }
  221.  
  222. int equals(char* first, char* second) {
  223.     uint8_t i;
  224.     for (i=0; i<PASSWORD_SIZE; i++)
  225.         if (first[i] != second[i])
  226.             return false;
  227.     return true;
  228. }
  229. void EINT1_IRQHandler(void){
  230.     LCD_clear();
  231.     state = initial_screen;
  232.     interuptFlag = 1;
  233.     PB->ISRC |= (1ul << 15);
  234. }
  235.  
  236. //------------------------------------------------------------------------------------------------------------------------------------
  237. // Functions definition
  238. //------------------------------------------------------------------------------------------------------------------------------------
  239. void LCD_start(void)
  240. {
  241.     LCD_command(0xE2); // Set system reset
  242.     LCD_command(0xA1); // Set Frame rate 100 fps
  243.     LCD_command(0xEB); // Set LCD bias ratio E8~EB for 6~9 (min~max)
  244.     LCD_command(0x81); // Set V BIAS potentiometer
  245.     LCD_command(0xA0); // Set V BIAS potentiometer: A0 ()
  246.     LCD_command(0xC0);
  247.     LCD_command(0xAF); // Set Display Enable
  248. }
  249. void LCD_command(unsigned char temp)
  250. {
  251.     SPI3->SSR |= 1ul << 0;
  252.     SPI3->TX[0] = temp;
  253.     SPI3->CNTRL |= 1ul << 0;
  254.     while(SPI3->CNTRL & (1ul << 0));
  255.     SPI3->SSR &= ~(1ul << 0);
  256. }
  257. void LCD_data(unsigned char temp)
  258. {
  259.     SPI3->SSR |= 1ul << 0;
  260.     SPI3->TX[0] = 0x0100+temp;
  261.     SPI3->CNTRL |= 1ul << 0;
  262.     while(SPI3->CNTRL & (1ul << 0));
  263.     SPI3->SSR &= ~(1ul << 0);
  264. }
  265. void LCD_clear(void)
  266. {
  267.     int16_t i;
  268.     LCD_SetAddress(0x0, 0x0);
  269.     for (i = 0; i < 132 *8; i++)
  270.         LCD_data(0x00);
  271.  
  272. }
  273. void LCD_SetAddress(uint8_t PageAddr, uint8_t ColumnAddr) {
  274.     LCD_command(0xB0 | PageAddr);
  275.     LCD_command(0x10 | (ColumnAddr>>4)&0xF);
  276.     LCD_command(0x00 | (ColumnAddr & 0xF));
  277. }
  278. void KeyPadEnable(void){
  279.     GPIO_SetMode(PA, BIT0, GPIO_MODE_QUASI);
  280.     GPIO_SetMode(PA, BIT1, GPIO_MODE_QUASI);
  281.     GPIO_SetMode(PA, BIT2, GPIO_MODE_QUASI);
  282.     GPIO_SetMode(PA, BIT3, GPIO_MODE_QUASI);
  283.     GPIO_SetMode(PA, BIT4, GPIO_MODE_QUASI);
  284.     GPIO_SetMode(PA, BIT5, GPIO_MODE_QUASI);
  285. }
  286. uint8_t KeyPadScanning(void){
  287.     PA0=1; PA1=1; PA2=0; PA3=1; PA4=1; PA5=1;
  288.     if (PA3==0) return 1;
  289.     if (PA4==0) return 4;
  290.     if (PA5==0) return 7;
  291.     PA0=1; PA1=0; PA2=1; PA3=1; PA4=1; PA5=1;
  292.     if (PA3==0) return 2;
  293.     if (PA4==0) return 5;
  294.     if (PA5==0) return 8;
  295.     PA0=0; PA1=1; PA2=1; PA3=1; PA4=1; PA5=1;
  296.     if (PA3==0) return 3;
  297.     if (PA4==0) return 6;
  298.     if (PA5==0) return 9;
  299.     return 0;
  300. }
  301. void System_Config (void){
  302.     SYS_UnlockReg(); // Unlock protected registers
  303.     // Enable clock sources
  304.     //LXT- External Low frequency Crystal 32 kHz
  305.     CLK->PWRCON |= (0x01ul << 1);
  306.     while(!(CLK->CLKSTATUS & (1ul << 1)));
  307.     CLK->PWRCON |= (0x01ul << 0);
  308.     while(!(CLK->CLKSTATUS & (1ul << 0)));
  309.  
  310.     //PLL configuration starts
  311.     CLK->PLLCON &= ~(1ul<<19); //0: PLL input is HXT
  312.     CLK->PLLCON &= ~(1ul<<16); //PLL in normal mode
  313.     CLK->PLLCON &= (~(0x01FFul << 0));
  314.     CLK->PLLCON |= 48;
  315.     CLK->PLLCON &= ~(1ul<<18); //0: enable PLLOUT
  316.     while(!(CLK->CLKSTATUS & (0x01ul << 2)));
  317.     //PLL configuration ends
  318.  
  319.     //clock source selection
  320.     CLK->CLKSEL0 &= (~(0x07ul << 0));
  321.     CLK->CLKSEL0 |= (0x02ul << 0);
  322.     //clock frequency division
  323.     CLK->CLKDIV &= (~0x0Ful << 0);
  324.  
  325.     //enable clock of SPI3
  326.     CLK->APBCLK |= 1ul << 15;
  327.  
  328.     SYS_LockReg(); // Lock protected registers
  329. }
  330.  
  331. void SPI3_Config (void){
  332.     SYS->GPD_MFP |= 1ul << 11; //1: PD11 is configured for alternative function
  333.     SYS->GPD_MFP |= 1ul << 9; //1: PD9 is configured for alternative function
  334.     SYS->GPD_MFP |= 1ul << 8; //1: PD8 is configured for alternative function
  335.  
  336.     SPI3->CNTRL &= ~(1ul << 23); //0: disable variable clock feature
  337.     SPI3->CNTRL &= ~(1ul << 22); //0: disable two bits transfer mode
  338.     SPI3->CNTRL &= ~(1ul << 18); //0: select Master mode
  339.     SPI3->CNTRL &= ~(1ul << 17); //0: disable SPI interrupt
  340.     SPI3->CNTRL |= 1ul << 11; //1: SPI clock idle high
  341.     SPI3->CNTRL &= ~(1ul << 10); //0: MSB is sent first
  342.     SPI3->CNTRL &= ~(3ul << 8); //00: one transmit/receive word will be executed in one data transfer
  343.  
  344.     SPI3->CNTRL &= ~(31ul << 3); //Transmit/Receive bit length
  345.     SPI3->CNTRL |= 9ul << 3; //9: 9 bits transmitted/received per data transfer
  346.  
  347.     SPI3->CNTRL |= (1ul << 2); //1: Transmit at negative edge of SPI CLK
  348.     SPI3->DIVIDER = 0; // SPI clock divider. SPI clock = HCLK / ((DIVIDER+1)*2). HCLK = 50 MHz
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement