Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * main.c
- */
- void main(void)
- {
- WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
- set_DCO(CURRENT_FREQ);
- LCD_INIT();
- init_keypad();
- write_string("LOCKED", 0x00);
- write_string("ENTER KEY", 0x40);
- char key, index = 0;
- char key_str[4];
- char answer[4]="1234";
- while (1){
- //check initial key presses
- key=keypad_check();
- if (key != '\0'){
- if (key=='*'){ //clear screen and restart if * pressed
- index=0;
- clear_LCD();
- write_string("LOCKED", 0x00);
- write_string("ENTER KEY", 0x40);
- }
- else{ //otherwise print the character pressed and add to the answer string
- write_char_LCD(0x4A+index, key);
- key_str[index]=key;
- index++;
- delay_ms(500, CURRENT_FREQ);
- }
- }
- //once four characters have been entered check against answer string
- if (index== 4){
- index=0;
- //if code is correct, print hello world and wait for next input
- if (key_str[0]==answer[0] && key_str[1]==answer[1] && key_str[2]==answer[2] && key_str[3]==answer[3]) {
- clear_LCD();
- write_string("HELLO WORLD", 0x00);
- //once correct entered, wait for next key
- while (1){
- key=keypad_check();
- if (key != '\0'){
- //if next key is *, clear and go back to locked screen
- if (key=='*'){
- index=0;
- clear_LCD();
- write_string("LOCKED", 0x00);
- write_string("ENTER KEY", 0x40);
- break;
- }
- //if next key is #, prompt for new code and
- else if (key == '#'){
- clear_LCD();
- write_string("ENTER NEW CODE", 0x00);
- while (1) {
- key=keypad_check();
- if (key != '\0'){
- //if * is pressed, clear and restart new code entry
- if (key=='*'){
- index=0;
- clear_LCD();
- write_string("ENTER NEW CODE", 0x00);
- }
- //change code to next key pressed and exit once 4 digits entered
- else{
- write_char_LCD(0x40+index, key);
- answer[index]=key;
- index++;
- delay_ms(500, CURRENT_FREQ);
- if (index == 4) {
- index = 0;
- clear_LCD();
- write_string("LOCKED", 0x00);
- write_string("ENTER KEY", 0x40);
- break;
- }
- }
- }
- }
- break;
- }
- }
- }
- }
- //if code is incorrect return to locked screen
- else {
- clear_LCD();
- write_string("LOCKED", 0x00);
- write_string("ENTER KEY", 0x40);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement