Advertisement
dzieciol

Untitled

Dec 15th, 2017
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/delay.h>
  3. #include <stdint.h>
  4. #include <math.h>
  5. #include <avr/interrupt.h>
  6.  
  7. #define RS 0
  8. #define EN 1
  9. #define D4 4
  10. #define D5 5
  11. #define D6 6
  12. #define D7 7
  13. #define LCD PORTA
  14. #define KL_PORT PORTC
  15.  
  16. #define CMD (uint8_t)0
  17. #define DATA (uint8_t)1
  18.  
  19. #define _nop() asm volatile("nop")
  20.  
  21. volatile uint16_t now = 0;
  22. volatile int c = 0;
  23. volatile int tryb = 0;
  24.  
  25.  
  26. void send(const uint8_t data, const uint8_t type) {
  27. if (type == 0) {
  28. LCD &= ~_BV(RS); // rejestr
  29. } else if (type == 1) {
  30. LCD |= _BV(RS); // dane
  31. }
  32.  
  33. LCD |= _BV(EN);
  34. LCD = (LCD & 0x0F) | (data & 0xF0);
  35. LCD &= ~_BV(EN);
  36.  
  37. _nop();
  38.  
  39. LCD |= _BV(EN);
  40. LCD = (LCD & 0x0F) | (data << 4);
  41. LCD &= ~_BV(EN);
  42.  
  43. _delay_ms(2);
  44. }
  45.  
  46. void init() {
  47. DDRA = 0xFF;
  48. LCD = 0;
  49.  
  50. _delay_ms(20);
  51.  
  52. send(0b00000010, CMD);
  53. send(0b00101000, CMD);
  54. send(0b00001100, CMD);
  55. send(0b00000001, CMD);
  56.  
  57. _delay_ms(20);
  58. }
  59. void lcd_clear() {
  60. send(0b00000001, CMD); // clear
  61. }
  62.  
  63. void string(const uint8_t* data) {
  64. uint8_t line = 0;
  65. uint8_t i = 0;
  66. while (data[i])
  67. {
  68. if (i > 15 && line == 0) {
  69. send(0xC0, CMD);
  70. line = 1;
  71. }
  72.  
  73. send(data[i], DATA);
  74. i++;
  75. }
  76. }
  77. ISR(TIMER0_OVF_vect){
  78. if(c == 10){
  79. if(tryb == 1) now--;
  80. if(tryb == 0) now++;
  81. c = 0;
  82. }
  83. TCNT0 = 155;
  84. c++;
  85.  
  86. }
  87.  
  88.  
  89.  
  90. void update_time() {
  91. if(tryb == 2){
  92. lcd_clear();
  93. string("twoj czas minął");
  94. }else{
  95. if(tryb == 1 && now == 0){
  96. buzz();
  97. tryb = 2;
  98. }
  99. uint8_t hours = (uint8_t)floor(now / (60 * 60));
  100.  
  101. uint8_t divisor_for_minutes = (uint8_t)(now % (60 * 60));
  102. uint8_t minutes = floor(divisor_for_minutes / 60);
  103.  
  104. uint8_t divisor_for_seconds = (uint8_t)(divisor_for_minutes % 60);
  105. uint8_t seconds = (uint8_t)ceil(divisor_for_seconds);
  106.  
  107.  
  108. char buf[32];
  109.  
  110. sprintf(buf, "%.2d:%.2d:%.2d", hours, minutes, seconds);
  111.  
  112. lcd_clear();
  113. string(buf);
  114. }
  115. }
  116. void buzz(){
  117. PORTB = 1;
  118. _delay_ms(10);
  119. PORTB = 0;
  120. }
  121. void ogarnij_wyswietlacz(){
  122. if(tryb!=3){
  123. update_time();
  124. }
  125. }
  126.  
  127. int main() {
  128.  
  129. DDRB = 255;
  130. PORTB = 0;
  131.  
  132. tryb = 0;
  133. init();
  134.  
  135. TCCR0 |= (1<<CS00) | (1<<CS02);
  136. TIMSK |= (1<<TOIE0);
  137. TCNT0 = 55;
  138. sei();
  139.  
  140. //klawiatura
  141. DDRC = 0xF0;
  142. KL_PORT = 0xFF;
  143. char x, kl;
  144.  
  145. while(1){
  146. ogarnij_wyswietlacz();
  147.  
  148.  
  149. x = (PINC & 0x0f);
  150. if(x == 0b00001110)tryb = 1;
  151. if(x == 0b00001101)tryb = 0;
  152. if(x == 0b00001011)tryb = 3;
  153. if(x == 0b00000111)kl = 4;
  154. }
  155. return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement