Advertisement
Guest User

RELOJ

a guest
May 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #define F_CPU 16000000UL
  2. #include <avr/delay.h>
  3. #include <avr/io.h>
  4. #include <string.h>
  5. #include <avr/interrupt.h>
  6. #include <LiquidCrystal.h>
  7. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  8.  
  9.  
  10. const short buttonPin = 5;
  11. volatile unsigned char seconds;
  12. volatile unsigned char minutes;
  13. volatile unsigned char hours;
  14. void update_clock()
  15. {
  16. seconds++;
  17. if (seconds == 60)
  18. {
  19. seconds = 0;
  20. minutes++;
  21. }
  22. if(minutes==60)
  23. {
  24. minutes=0;
  25. hours++;
  26. }
  27. if(hours>23)
  28. {
  29. hours=0;
  30. }
  31.  
  32.  
  33. }
  34.  
  35. ISR(TIMER1_COMPA_vect)
  36. {
  37.  
  38. update_clock();
  39.  
  40. }
  41.  
  42. void setup()
  43. {
  44.  
  45.  
  46.  
  47. // initialize Timer1
  48. pinMode(A5, INPUT_PULLUP);
  49. cli(); // disable global interrupts
  50. TCCR1A = 0; // set entire TCCR1A register to 0
  51. TCCR1B = 0; // same for TCCR1B
  52.  
  53. // set compare match register to desired timer count:
  54. OCR1A = 15624;
  55. // turn on CTC mode:
  56. TCCR1B |= (1 << WGM12);
  57. // Set CS10 and CS12 bits for 1024 prescaler:
  58. TCCR1B |= (1 << CS10);
  59. TCCR1B |= (1 << CS12);
  60. // enable timer compare interrupt:
  61. TIMSK1 |= (1 << OCIE1A);
  62. sei(); // enable global interrupts
  63. lcd.begin(16, 2);
  64. lcd.print("HH:MM:SS");
  65. Serial.begin(9600);
  66.  
  67.  
  68. }
  69.  
  70. void display_on_lcd()
  71. {
  72.  
  73. lcd.setCursor(0, 1);
  74. lcd.print(hours);
  75. lcd.setCursor(2,1);
  76. lcd.print(":");
  77. lcd.setCursor(3,1);
  78. lcd.print(minutes);
  79. lcd.setCursor(5,1);
  80. lcd.print(":");
  81. lcd.setCursor(6,1);
  82. lcd.print(seconds);
  83.  
  84.  
  85. }
  86.  
  87.  
  88. void loop()
  89. {
  90.  
  91. display_on_lcd();
  92.  
  93. Serial.println(analogRead(5));
  94. if(analogRead(buttonPin)<1000)
  95. {
  96. if(minutes==59)
  97. {
  98. minutes=0;
  99. if(hours==23)
  100. hours=0;
  101. else
  102. hours++;
  103. }
  104. else
  105. {
  106. minutes++;
  107. }
  108. }
  109.  
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement