Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. /*
  2. * CodedLockLED.c
  3. *
  4. * Created: 2020-01-13 12:30:17
  5. * Author : oem1
  6. */
  7.  
  8. #define F_CPU 16000000UL
  9. #include <avr/io.h>
  10. #include <util/delay.h>
  11.  
  12. #define LED_DIR DDRB /* define LED_DIR which allows us to decide on the direction of data */
  13. #define KEYS_DIR DDRA /* define KEYS_DIR which allows us to decide on the direction of data */
  14. #define LED_SUPPLY DDRD /* define LED_SUPPLY which allows us multiplex the 7 segment displays */
  15. #define LED_PORT PORTB /* define LED_PORT which sends the data about segments to light up */
  16. #define KEYS_PORT PORTA /* define KEYS_PORT */
  17. #define LIGHT_PORT PORTC /* define KEYS_PORT */
  18.  
  19. int code[] = {9,9,9,9}; /* code that will open our lock */
  20. int is_code_set = 0;
  21. char array[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
  22. // 0 1 2 3 4 5 6 7 8 9
  23. /* hex values for 7 segment display from 0 to 9 */
  24.  
  25. int button1 = 0, button2 = 0, button3 = 0, button4 = 0;
  26. int flag = 0;
  27. int elapsedTime = 0;
  28.  
  29. int main(void)
  30. {
  31. LED_DIR |= 0XFF; /* LED port direction is output */
  32. KEYS_DIR = 0x00; /* KEY port direction is input */
  33. KEYS_PORT = 0XFF;
  34. LED_SUPPLY = 0xFF;
  35. LED_PORT = array[0]; /* displaying all zeros on the display */
  36. DDRC = 0xFF;
  37. LIGHT_PORT = 0xFF;
  38.  
  39. while(1)
  40. {
  41. if(elapsedTime > 30) //we prevent the system from receiving too many inputs
  42. {
  43. elapsedTime = 0;
  44. flag = 0;
  45. }
  46.  
  47. //MULTIPLEXING THE 7 SEGMENT DISPLAY
  48. DDRD = 0x80; //DDRD = 0x80 only first on
  49. LED_PORT = array[button1];
  50. _delay_ms(4);
  51. DDRD = 0x40; //DDRD = 0x40 only second on
  52. LED_PORT = array[button2];
  53. _delay_ms(4);
  54. DDRD = 0x20; //DDRD = 0x20 only third on
  55. LED_PORT = array[button3];
  56. _delay_ms(4);
  57. DDRD = 0x10; //DDRD = 0x10 only fourth on
  58. LED_PORT = array[button4];
  59. _delay_ms(4);
  60.  
  61.  
  62. if(bit_is_clear(PINA,7) && flag == 0) //if button1 is pressed, adequate number on the display increases
  63. {
  64. button1++;
  65. flag = 1;
  66. if(button1>9) button1 = 0;
  67. }
  68.  
  69. if(bit_is_clear(PINA,6) && flag == 0) //if button2 is pressed, adequate number on the display increases
  70. {
  71. button2++;
  72. flag = 1;
  73. if(button2>9) button2 = 0;
  74. }
  75.  
  76. if(bit_is_clear(PINA,5) && flag == 0) //if button3 is pressed, adequate number on the display increases
  77. {
  78. button3++;
  79. flag = 1;
  80. if(button3>9) button3 = 0;
  81. }
  82.  
  83. if(bit_is_clear(PINA,4) && flag == 0) //if button4 is pressed, adequate number on the display increases
  84. {
  85. button4++;
  86. flag = 1;
  87. if(button4>9) button4 = 0;
  88. }
  89.  
  90. if(is_code_set == 0)
  91. {
  92. if(bit_is_clear(PINA,0))
  93. {
  94. code[0] = button1;
  95. code[1] = button2;
  96. code[2] = button3;
  97. code[3] = button4;
  98. is_code_set = 1;
  99. }
  100. }
  101.  
  102. else
  103. {
  104. 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
  105. { // the lights will turn on for 3 seconds indicating open door
  106. LIGHT_PORT = 0x00; //lights on
  107. DDRD = 0x00; //turning off the display
  108. _delay_ms(3000);
  109. LIGHT_PORT = 0xFF; //lights off
  110. button1 = button2 = button3 = button4 = 0; //resetting the values on screen
  111. }
  112.  
  113. 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
  114. { // the lights will flash 3 times
  115. DDRD = 0x00; //turning off the display
  116.  
  117. for(int a=0;a<3;a++)
  118. {
  119. _delay_ms(300);
  120. LIGHT_PORT = 0x00; //lights on
  121. _delay_ms(300);
  122. LIGHT_PORT = 0xFF; //lights off
  123. }
  124.  
  125. button1 = button2 = button3 = button4 = 0; //resetting the values on screen
  126. }
  127. }
  128.  
  129. elapsedTime++;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement