Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "NUC100Series.h"
- #include "MCU_init.h"
- #include "SYS_init.h"
- #include "LCD.h"
- #include "Draw2D.h"
- #include "picture.h"
- #define SYSTICK_DLAY_us 1000000
- #define true 1
- #define false 0
- #define PASSWORD_SIZE 6
- #define BUZZER_BIPTIME 5
- #define BUZZER_BIPDELAY 2000000
- //------------------------------------------------------------------------------------------------------------------------------------
- // Functions declaration
- //------------------------------------------------------------------------------------------------------------------------------------
- typedef enum {initial_screen, password_input, password_fail,update_pass, welcome_screen, pass_change_success} STATES;
- STATES state = initial_screen;
- uint8_t interuptFlag = 0;
- void System_Config(void);
- void SPI3_Config(void);
- void LCD_start(void);
- void LCD_command(unsigned char temp);
- void LCD_data(unsigned char temp);
- void LCD_clear(void);
- void LCD_SetAddress(uint8_t PageAddr, uint8_t ColumnAddr);
- void KeyPadEnable(void);
- void EINT1_IRQHandler(void);
- void interrupt_configure(void);
- int equals(char* first, char* second);
- void handleInput(char number, char* inputDes, char* passwordDisplay);
- void startInitialScreen(void);
- void startPasswordInputScreen(void);
- void startPasswordUpdateScreen(void);
- void startWelcomeScreen(void);
- void startPasswordFailScreen(void);
- void startPasswordChangeScreen(void);
- void startStateMachine(void);
- uint8_t KeyPadScanning(void);
- uint8_t a;
- uint8_t key_pressed = 0;
- uint8_t z;
- char password[]={'1','2','3','4','5','6'};
- char input[]={'0','0','0','0','0','0'};
- char passwordDisplay[] = {'-','-','-','-','-','-'};
- //------------------------------------------------------------------------------------------------------------------------------------
- // Main program
- //------------------------------------------------------------------------------------------------------------------------------------
- int main(void)
- {
- //System initialization
- System_Config();
- KeyPadEnable();
- //SPI3 initialization
- SPI3_Config();
- //LCD initialization
- LCD_start();
- LCD_clear();
- //Interrupt configuration
- interrupt_configure();
- while (1) {
- startStateMachine();
- CLK_SysTickDelay(2000000);
- }
- }
- void startStateMachine(void) {
- switch(state){
- //Initial screen state
- case initial_screen:
- startInitialScreen();
- break;
- //Input password state
- case password_input:
- startPasswordInputScreen();
- break;
- //Update password state
- case update_pass:
- startPasswordUpdateScreen();
- break;
- //Password is correct state
- case welcome_screen:
- startWelcomeScreen();
- break;
- //Password is incorrect state
- case password_fail:
- startPasswordFailScreen();
- break;
- //Successfully change password state
- case pass_change_success:
- startPasswordChangeScreen();
- break;
- default: break;
- }
- }
- void interrupt_configure(void) {
- //GPIO Interrupt configuration. GPIO-B15 is the interrupt source
- PB->PMD &= (~(0x03ul << 30));
- PB->IMD &= (~(1ul << 15));
- PB->IEN |= (1ul << 15);
- //NVIC interrupt configuration for GPIO-B15 interrupt source
- NVIC->ISER[0] |= 1ul<<3;
- NVIC->IP[0] &= (~(3ul<<30));
- }
- void startPasswordChangeScreen(void) {
- LCD_clear();
- printS_5x7(1, 32, "PASSWORD CHANGED");
- CLK_SysTickDelay(2000000);
- state = initial_screen;
- }
- void startPasswordFailScreen(void) {
- LCD_clear();
- printS_5x7(1, 0, "Password is incorrect");
- CLK_SysTickDelay(5000000);
- state = initial_screen;
- }
- void startWelcomeScreen(void) {
- LCD_clear();
- print_Line(1, "Welcome home");
- }
- void startPasswordUpdateScreen(void) {
- LCD_clear();
- a = 0;
- printS_5x7(1, 0, "Enter the new password");
- print_Line(2, passwordDisplay);
- while(a<PASSWORD_SIZE && interuptFlag == 0){
- while(key_pressed==0 && interuptFlag == 0) key_pressed = KeyPadScanning();
- switch(key_pressed) {
- case 1: handleInput('1', password, passwordDisplay); break;
- case 2: handleInput('2', password, passwordDisplay); break;
- case 3: handleInput('3', password, passwordDisplay); break;
- case 4: handleInput('4', password, passwordDisplay); break;
- case 5: handleInput('5', password, passwordDisplay); break;
- case 6: handleInput('6', password, passwordDisplay); break;
- case 7: handleInput('7', password, passwordDisplay); break;
- case 8: handleInput('8', password, passwordDisplay); break;
- case 9: handleInput('9', password, passwordDisplay); break;
- default:;
- }
- key_pressed = 0;
- a++;
- }
- if (interuptFlag == 1){
- interuptFlag = 0;
- state = initial_screen;
- }
- else if (interuptFlag == 0)
- state = pass_change_success;
- for (z = 0; z<6; z++) passwordDisplay[z] = '-';
- }
- void startPasswordInputScreen(void) {
- LCD_clear();
- a = 0;
- printS_5x7(1, 0, "Input your password");
- print_Line(2, passwordDisplay);
- while(a<PASSWORD_SIZE && interuptFlag == 0){
- while(key_pressed==0 && interuptFlag == 0) key_pressed = KeyPadScanning();
- switch(key_pressed) {
- case 1: handleInput('1', input, passwordDisplay); break;
- case 2: handleInput('2', input, passwordDisplay); break;
- case 3: handleInput('3', input, passwordDisplay); break;
- case 4: handleInput('4', input, passwordDisplay); break;
- case 5: handleInput('5', input, passwordDisplay); break;
- case 6: handleInput('6', input, passwordDisplay); break;
- case 7: handleInput('7', input, passwordDisplay); break;
- case 8: handleInput('8', input, passwordDisplay); break;
- case 9: handleInput('9', input, passwordDisplay); break;
- default:;
- }
- key_pressed = 0;
- a++;
- }
- if (equals(password, input) && interuptFlag == 0)
- state = welcome_screen;
- else if (!equals(password, input) && interuptFlag == 0)
- state = password_fail;
- else if (!equals(password, input) && interuptFlag == 1)
- state = initial_screen;
- interuptFlag = 0;
- for (z = 0; z<6; z++) passwordDisplay[z] = '-';
- }
- void startInitialScreen(void) {
- LCD_clear();
- printS_5x7(15, 0, "EEET2481 - Door lock");
- printS_5x7(45, 10, "system");
- printS_5x7(1, 30, "Press 1 to enter password");
- printS_5x7(1, 40, "Press 2 to change password");
- while(key_pressed==0 && interuptFlag == 0) key_pressed = KeyPadScanning();
- interuptFlag = 0;
- switch(key_pressed) {
- case 1: state = password_input; break;
- case 2: state = update_pass; break;
- //default: break;
- }
- key_pressed = 0;
- // state transition
- }
- void handleInput(char number, char* inputDes, char* passwordDisplay){
- input[a] = number;
- passwordDisplay[a] = number;
- print_Line(2, passwordDisplay);
- CLK_SysTickDelay(200000);
- passwordDisplay[a] = '*';
- print_Line(2, passwordDisplay);
- }
- int equals(char* first, char* second) {
- uint8_t i;
- for (i=0; i<PASSWORD_SIZE; i++)
- if (first[i] != second[i])
- return false;
- return true;
- }
- void EINT1_IRQHandler(void){
- LCD_clear();
- state = initial_screen;
- interuptFlag = 1;
- PB->ISRC |= (1ul << 15);
- }
- //------------------------------------------------------------------------------------------------------------------------------------
- // Functions definition
- //------------------------------------------------------------------------------------------------------------------------------------
- void LCD_start(void)
- {
- LCD_command(0xE2); // Set system reset
- LCD_command(0xA1); // Set Frame rate 100 fps
- LCD_command(0xEB); // Set LCD bias ratio E8~EB for 6~9 (min~max)
- LCD_command(0x81); // Set V BIAS potentiometer
- LCD_command(0xA0); // Set V BIAS potentiometer: A0 ()
- LCD_command(0xC0);
- LCD_command(0xAF); // Set Display Enable
- }
- void LCD_command(unsigned char temp)
- {
- SPI3->SSR |= 1ul << 0;
- SPI3->TX[0] = temp;
- SPI3->CNTRL |= 1ul << 0;
- while(SPI3->CNTRL & (1ul << 0));
- SPI3->SSR &= ~(1ul << 0);
- }
- void LCD_data(unsigned char temp)
- {
- SPI3->SSR |= 1ul << 0;
- SPI3->TX[0] = 0x0100+temp;
- SPI3->CNTRL |= 1ul << 0;
- while(SPI3->CNTRL & (1ul << 0));
- SPI3->SSR &= ~(1ul << 0);
- }
- void LCD_clear(void)
- {
- int16_t i;
- LCD_SetAddress(0x0, 0x0);
- for (i = 0; i < 132 *8; i++)
- LCD_data(0x00);
- }
- void LCD_SetAddress(uint8_t PageAddr, uint8_t ColumnAddr) {
- LCD_command(0xB0 | PageAddr);
- LCD_command(0x10 | (ColumnAddr>>4)&0xF);
- LCD_command(0x00 | (ColumnAddr & 0xF));
- }
- void KeyPadEnable(void){
- GPIO_SetMode(PA, BIT0, GPIO_MODE_QUASI);
- GPIO_SetMode(PA, BIT1, GPIO_MODE_QUASI);
- GPIO_SetMode(PA, BIT2, GPIO_MODE_QUASI);
- GPIO_SetMode(PA, BIT3, GPIO_MODE_QUASI);
- GPIO_SetMode(PA, BIT4, GPIO_MODE_QUASI);
- GPIO_SetMode(PA, BIT5, GPIO_MODE_QUASI);
- }
- uint8_t KeyPadScanning(void){
- PA0=1; PA1=1; PA2=0; PA3=1; PA4=1; PA5=1;
- if (PA3==0) return 1;
- if (PA4==0) return 4;
- if (PA5==0) return 7;
- PA0=1; PA1=0; PA2=1; PA3=1; PA4=1; PA5=1;
- if (PA3==0) return 2;
- if (PA4==0) return 5;
- if (PA5==0) return 8;
- PA0=0; PA1=1; PA2=1; PA3=1; PA4=1; PA5=1;
- if (PA3==0) return 3;
- if (PA4==0) return 6;
- if (PA5==0) return 9;
- return 0;
- }
- void System_Config (void){
- SYS_UnlockReg(); // Unlock protected registers
- // Enable clock sources
- //LXT- External Low frequency Crystal 32 kHz
- CLK->PWRCON |= (0x01ul << 1);
- while(!(CLK->CLKSTATUS & (1ul << 1)));
- CLK->PWRCON |= (0x01ul << 0);
- while(!(CLK->CLKSTATUS & (1ul << 0)));
- //PLL configuration starts
- CLK->PLLCON &= ~(1ul<<19); //0: PLL input is HXT
- CLK->PLLCON &= ~(1ul<<16); //PLL in normal mode
- CLK->PLLCON &= (~(0x01FFul << 0));
- CLK->PLLCON |= 48;
- CLK->PLLCON &= ~(1ul<<18); //0: enable PLLOUT
- while(!(CLK->CLKSTATUS & (0x01ul << 2)));
- //PLL configuration ends
- //clock source selection
- CLK->CLKSEL0 &= (~(0x07ul << 0));
- CLK->CLKSEL0 |= (0x02ul << 0);
- //clock frequency division
- CLK->CLKDIV &= (~0x0Ful << 0);
- //enable clock of SPI3
- CLK->APBCLK |= 1ul << 15;
- SYS_LockReg(); // Lock protected registers
- }
- void SPI3_Config (void){
- SYS->GPD_MFP |= 1ul << 11; //1: PD11 is configured for alternative function
- SYS->GPD_MFP |= 1ul << 9; //1: PD9 is configured for alternative function
- SYS->GPD_MFP |= 1ul << 8; //1: PD8 is configured for alternative function
- SPI3->CNTRL &= ~(1ul << 23); //0: disable variable clock feature
- SPI3->CNTRL &= ~(1ul << 22); //0: disable two bits transfer mode
- SPI3->CNTRL &= ~(1ul << 18); //0: select Master mode
- SPI3->CNTRL &= ~(1ul << 17); //0: disable SPI interrupt
- SPI3->CNTRL |= 1ul << 11; //1: SPI clock idle high
- SPI3->CNTRL &= ~(1ul << 10); //0: MSB is sent first
- SPI3->CNTRL &= ~(3ul << 8); //00: one transmit/receive word will be executed in one data transfer
- SPI3->CNTRL &= ~(31ul << 3); //Transmit/Receive bit length
- SPI3->CNTRL |= 9ul << 3; //9: 9 bits transmitted/received per data transfer
- SPI3->CNTRL |= (1ul << 2); //1: Transmit at negative edge of SPI CLK
- SPI3->DIVIDER = 0; // SPI clock divider. SPI clock = HCLK / ((DIVIDER+1)*2). HCLK = 50 MHz
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement