Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * CodedLockLED.c
- *
- * Created: 2020-01-13 12:30:17
- * Author : oem1
- */
- #define F_CPU 16000000UL
- #include <avr/io.h>
- #include <util/delay.h>
- #define LED_DIR DDRB /* define LED_DIR which allows us to decide on the direction of data */
- #define KEYS_DIR DDRA /* define KEYS_DIR which allows us to decide on the direction of data */
- #define LED_SUPPLY DDRD /* define LED_SUPPLY which allows us multiplex the 7 segment displays */
- #define LED_PORT PORTB /* define LED_PORT which sends the data about segments to light up */
- #define KEYS_PORT PORTA /* define KEYS_PORT */
- #define LIGHT_PORT PORTC /* define KEYS_PORT */
- int code[] = {9,9,9,9}; /* code that will open our lock */
- int is_code_set = 0;
- char array[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- // 0 1 2 3 4 5 6 7 8 9
- /* hex values for 7 segment display from 0 to 9 */
- int button1 = 0, button2 = 0, button3 = 0, button4 = 0;
- int flag = 0;
- int elapsedTime = 0;
- int main(void)
- {
- LED_DIR |= 0XFF; /* LED port direction is output */
- KEYS_DIR = 0x00; /* KEY port direction is input */
- KEYS_PORT = 0XFF;
- LED_SUPPLY = 0xFF;
- LED_PORT = array[0]; /* displaying all zeros on the display */
- DDRC = 0xFF;
- LIGHT_PORT = 0xFF;
- while(1)
- {
- if(elapsedTime > 30) //we prevent the system from receiving too many inputs
- {
- elapsedTime = 0;
- flag = 0;
- }
- //MULTIPLEXING THE 7 SEGMENT DISPLAY
- DDRD = 0x80; //DDRD = 0x80 only first on
- LED_PORT = array[button1];
- _delay_ms(4);
- DDRD = 0x40; //DDRD = 0x40 only second on
- LED_PORT = array[button2];
- _delay_ms(4);
- DDRD = 0x20; //DDRD = 0x20 only third on
- LED_PORT = array[button3];
- _delay_ms(4);
- DDRD = 0x10; //DDRD = 0x10 only fourth on
- LED_PORT = array[button4];
- _delay_ms(4);
- if(bit_is_clear(PINA,7) && flag == 0) //if button1 is pressed, adequate number on the display increases
- {
- button1++;
- flag = 1;
- if(button1>9) button1 = 0;
- }
- if(bit_is_clear(PINA,6) && flag == 0) //if button2 is pressed, adequate number on the display increases
- {
- button2++;
- flag = 1;
- if(button2>9) button2 = 0;
- }
- if(bit_is_clear(PINA,5) && flag == 0) //if button3 is pressed, adequate number on the display increases
- {
- button3++;
- flag = 1;
- if(button3>9) button3 = 0;
- }
- if(bit_is_clear(PINA,4) && flag == 0) //if button4 is pressed, adequate number on the display increases
- {
- button4++;
- flag = 1;
- if(button4>9) button4 = 0;
- }
- if(is_code_set == 0)
- {
- if(bit_is_clear(PINA,0))
- {
- code[0] = button1;
- code[1] = button2;
- code[2] = button3;
- code[3] = button4;
- is_code_set = 1;
- }
- }
- else
- {
- if(bit_is_clear(PINA,0) && button1 == code[0] && button2 == code[1] && button3 == code[2] && button4 == code[3] ) // if user presses the accept button and the code is correct
- { // the lights will turn on for 3 seconds indicating open door
- LIGHT_PORT = 0x00; //lights on
- DDRD = 0x00; //turning off the display
- _delay_ms(3000);
- LIGHT_PORT = 0xFF; //lights off
- button1 = button2 = button3 = button4 = 0; //resetting the values on screen
- }
- if(bit_is_clear(PINA,0) && (button1 != code[0] || button2 != code[1] || button3 != code[2] || button4 != code[3] )) //if user presses the accept button but the code is incorrect
- { // the lights will flash 3 times
- DDRD = 0x00; //turning off the display
- for(int a=0;a<3;a++)
- {
- _delay_ms(300);
- LIGHT_PORT = 0x00; //lights on
- _delay_ms(300);
- LIGHT_PORT = 0xFF; //lights off
- }
- button1 = button2 = button3 = button4 = 0; //resetting the values on screen
- }
- }
- elapsedTime++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement