Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3.  
  4.  
  5. #define F_CPU 1600000UL
  6. #define RS 6
  7. #define E 5
  8.  
  9. void send_a_command (unsigned char command);
  10. void send_a_character(unsigned char character);
  11. void send_a_number(int a);
  12. void send_a_string(char *a);
  13.  
  14.  
  15.  
  16. void avr_setup(void)
  17. {
  18. DDRA = 0xFF; //all outputs
  19. DDRB = 0x00; //PORTB input for buttons
  20. PORTB = 0xFF; //pullups all on
  21. DDRA = 0xFF;
  22. DDRD = 0xFF;
  23. }
  24.  
  25.  
  26. int main(void)
  27. {
  28. int a=0;
  29. int b=0;
  30. int c=0;
  31. int d=0;
  32. _delay_ms(50);
  33. send_a_command(0x01);// sending all clear command
  34. send_a_command(0x38);// 16*2 line LCD
  35. send_a_command(0x0E);// screen and cursor ON
  36. print_screen(a%10,b%10,c%10,d%10);// printing 4 digits
  37. avr_setup();
  38.  
  39. while(1)
  40. {
  41.  
  42. _delay_ms(1000);
  43. if (!(PINB & (1<<PB0)))
  44. {
  45. a +=1;
  46. print_screen(a%10,b%10,c%10,d%10);
  47. }
  48. if (!(PINB & (1<<PB1)))
  49. {
  50. b +=1;
  51. print_screen(a%10,b%10,c%10,d%10);
  52. }
  53. if (!(PINB & (1<<PB2)))
  54. {
  55. c +=1;
  56. print_screen(a%10,b%10,c%10,d%10);
  57. }
  58. if (!(PINB & (1<<PB3)))
  59. {
  60. d +=1;
  61. print_screen(a%10,b%10,c%10,d%10);
  62. }
  63. if(a%10==1&&b%10==2&&c%10==3&&d%10==4)
  64. {
  65. send_a_command(0x01);
  66. send_a_string("Open");
  67. }
  68.  
  69. }
  70. }
  71.  
  72. void send_a_string(char *a)
  73. {
  74. for (int i=0;a[i]!='\0';i++)
  75. {
  76. send_a_character(a[i]);
  77. }
  78. }
  79. void send_a_command (unsigned char command)
  80. {
  81. PORTA=command;
  82. PORTD&= ~(1<<RS);
  83. PORTD|= (1<<E);
  84. _delay_ms(50);
  85. PORTD&= ~(1<<E);
  86. PORTA =0;
  87. }
  88.  
  89. void send_a_number(int a)
  90. {
  91. char character = a + '0';
  92. PORTA=character;
  93. PORTD|= (1<<RS);
  94. PORTD|= (1<<E);
  95. _delay_ms(50);
  96. PORTD&= ~(1<<E);
  97. PORTA =0;
  98. }
  99.  
  100. void send_a_character (unsigned char character)
  101. {
  102. PORTA=character;
  103. PORTD|= (1<<RS);
  104. PORTD|= (1<<E);
  105. _delay_ms(50);
  106. PORTD&= ~(1<<E);
  107. PORTA =0;
  108. }
  109. void send_a_result(int a,int b,int c,int d)
  110. {
  111. char aa=a+'0';
  112. char bb=b+'0';
  113. char cc=c+'0';
  114. char dd=d+'0';
  115.  
  116. char result[4]= { aa,bb,cc,dd, };
  117. send_a_string(result);
  118. }
  119. void print_screen(int a,int b,int c,int d)
  120. {
  121. send_a_command(0x01);// sending all clear command
  122. send_a_string("Enter Code:");
  123. send_a_result( a, b, c, d);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement