Advertisement
lukicdarkoo

Interrupt based Clock for ATMega328p & KEL Shield

Jan 22nd, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 KB | None | 0 0
  1. #include "lib.h" // lib.h from http://pastebin.com/Bzz5PRcx
  2.  
  3. unsigned long timeInMinutes = 0;
  4. unsigned long long minutesCounter = 0;
  5. unsigned int dotCounter = 0;
  6.  
  7. unsigned char cursor = 0; // 0 - sati, 1 - minuti
  8.  
  9. unsigned char display[5] = {SEG7_1, SEG7_1, SEG7_1, SEG7_1};
  10.  
  11.  
  12.  
  13. void makeClock(unsigned char *display_buffer, int num) {
  14.     unsigned char numbers[] = { SEG7_0, SEG7_1, SEG7_2, SEG7_3, SEG7_4, SEG7_5,
  15.     SEG7_6, SEG7_7, SEG7_8, SEG7_9 };
  16.  
  17.     unsigned int minutes = num % 60;
  18.     unsigned int hours = (num / 60) % 24;
  19.  
  20.     display_buffer[3] = numbers[minutes % 10];
  21.     display_buffer[2] = numbers[minutes / 10];
  22.  
  23.     display_buffer[1] = numbers[hours % 10];
  24.     display_buffer[0] = numbers[hours / 10];
  25.  
  26.  
  27.     if (display_buffer[0] == SEG7_0) {
  28.         display_buffer[0] = SEG7_NULL;
  29.     }
  30. }
  31.  
  32. ISR(TIMER0_COMPA_vect) {
  33.     minutesCounter++;
  34.     dotCounter++;
  35.  
  36.     if (minutesCounter == 60000) {
  37.         timeInMinutes++;
  38.         makeClock(display, timeInMinutes);
  39.         minutesCounter = 0;
  40.     }
  41.     if (dotCounter == 500) {
  42.         display[1] &= 0b11111011;
  43.     } else if (dotCounter == 1000) {
  44.         display[1] |= 0b00000100;
  45.         dotCounter = 0;
  46.     }
  47. }
  48.  
  49. ISR(PCINT1_vect) {
  50.     // Interrupt
  51. }
  52.  
  53. int main() {
  54.     init7seg();
  55.     initTimer();
  56.     initButtons();
  57.  
  58.     makeClock(display, 0);
  59.  
  60.     while (1) {
  61.         if (readButton('l') == 1) {
  62.             cursor = 0;
  63.         }
  64.         if (readButton('r') == 1) {
  65.             cursor = 1;
  66.         }
  67.  
  68.         if (readButton('u') == 1) {
  69.             if (cursor == 1) {
  70.                 timeInMinutes += 1;
  71.             } else {
  72.                 timeInMinutes += 60;
  73.             }
  74.             makeClock(display, timeInMinutes);
  75.             while (readButton('u') == 1) print7segWord(display);
  76.         }
  77.  
  78.         if (readButton('d') == 1) {
  79.             if (cursor == 1) {
  80.                 timeInMinutes -= 1;
  81.             } else {
  82.                 timeInMinutes -= 60;
  83.             }
  84.             makeClock(display, timeInMinutes);
  85.             while (readButton('d') == 1) print7segWord(display);
  86.         }
  87.  
  88.         print7segWord(display);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement