varli_ketanpl

SMI_LAB6_EX2

Nov 6th, 2023
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. #include <util/delay.h>
  2. #include <LiquidCrystal.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. LiquidCrystal lcd(12, NULL, 11, 9, 8, 7, 6);
  7. volatile int secunda = 0;
  8. volatile int minut = 0;
  9. volatile int ora = 0;
  10. volatile int zi = 1;
  11. volatile int luna = 1;
  12. volatile int an = 2013;
  13. volatile int zile_luna[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31,
  14.                              30, 31 };
  15. String weekDays[7] = {"Duminica", "Luni", "Marti", "Miercuri", "Joi", "Vineri", "Sambata"};
  16. const int backlight = 13;
  17. volatile boolean backlight_on;
  18. volatile byte set_mode;
  19.  
  20. void setup() {
  21.   TIMSK1 = (1 << TOIE1);  // activare timer overflow
  22.   TCNT1 = 0x0BDC;
  23.   TCCR1A = 0;
  24.   TCCR1B = (1 << CS12);  // timer start
  25.   //activare int0 si int1
  26.   EIMSK |= (1 << INT0);
  27.   EIMSK |= (1 << INT1);
  28.   //activare PCI2 (buton 3 - PCINT20 - PCI2)
  29.   PCICR |= (1 << PCIE2);
  30.   PCMSK2 |= (1 << PCINT20);
  31.   //la pornire lumina de fundal a afisajului este oprita
  32.   pinMode(backlight, OUTPUT);
  33.   digitalWrite(backlight, LOW);
  34.   backlight_on = false;
  35.   //setare mod functionare normala
  36.   set_mode = 0;
  37.   //configurare LCD
  38.   lcd.begin(16, 2);
  39.   lcd.noCursor();
  40.   Serial.begin(9600);
  41. }
  42.  
  43. void loop() {
  44.   lcd.setCursor(0, 0);
  45.   if (zi < 10) lcd.print("0");
  46.   lcd.print(zi);
  47.   lcd.setCursor(2, 0);
  48.   lcd.print("/");
  49.   if (luna < 10) lcd.print("0");
  50.   lcd.print(luna);
  51.   lcd.setCursor(5, 0);
  52.   lcd.print("/");
  53.   lcd.print(an);
  54.   lcd.setCursor(0, 1);
  55.   if (ora < 10) lcd.print("0");
  56.   lcd.print(ora);
  57.   lcd.setCursor(2, 1);
  58.   lcd.print(":");
  59.   if (minut < 10) lcd.print("0");
  60.   lcd.print(minut);
  61.   lcd.setCursor(5, 1);
  62.   lcd.print(":");
  63.   if (secunda < 10) lcd.print("0");
  64.   lcd.print(secunda);
  65.   lcd.print(" ");
  66.  
  67.   lcd.print(weekDays[dayOfWeek(an, luna, zi)]);
  68.   lcd.print(" ");
  69.  
  70.   if (set_mode != 0) {
  71.     lcd.setCursor(11, 1);
  72.     lcd.print("SET ");
  73.     switch (set_mode) {
  74.       case 1:
  75.         lcd.print("Z");
  76.         break;
  77.       case 2:
  78.         lcd.print("L");
  79.         break;
  80.       case 3:
  81.         lcd.print("A");
  82.         break;
  83.       case 4:
  84.         lcd.print("O");
  85.         break;
  86.       case 5:
  87.         lcd.print("M");
  88.         break;
  89.       case 6:
  90.         lcd.print("S");
  91.         break;
  92.     }
  93.   }
  94.   delay(500);
  95.   lcd.clear();
  96. }
  97.  
  98. #define LEAP_YEAR(Y)     ( (Y>0) && !(Y%4) && ( (Y%100) || !(Y%400) ))     // from time-lib
  99.  
  100. int dayOfWeek(uint16_t year, uint8_t month, uint8_t day)
  101. {
  102.   uint16_t months[] =
  103.   {
  104.     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 // days until 1st of month
  105.   };  
  106.  
  107.   uint32_t days = year * 365;   // days until year
  108.   for (uint16_t i = 4; i < year; i+=4)
  109.   {
  110.     if (LEAP_YEAR(i)) days++;  // adjust leap years
  111.   }
  112.  
  113.   days += months[month-1] + day;
  114.   if ((month > 2) && LEAP_YEAR(year)) days++;
  115.   return days % 7;
  116. }
  117.  
  118. ISR(TIMER1_OVF_vect) {
  119.   TCNT1 = 0x0BDC;
  120.   secunda++;
  121.   if (secunda == 60) {
  122.     secunda = 0;
  123.     minut++;
  124.     if (minut >= 60) {
  125.       minut = 0;
  126.       ora++;
  127.       if (ora >= 24) {
  128.         ora = 0;
  129.         zi++;
  130.         if (zi > zile_luna[luna - 1]) {
  131.           zi = 1;
  132.           luna++;
  133.           if (luna > 12) {
  134.             luna = 1;
  135.             an++;
  136.             if (an % 4 == 0) zile_luna[1] = 29;
  137.             else zile_luna[1] = 28;
  138.           }
  139.         }
  140.       }
  141.     }
  142.   }
  143. }
  144. ISR(INT0_vect) {
  145.   set_mode++;
  146.   if (set_mode == 7) set_mode = 0;
  147.   _delay_ms(400);
  148. }
  149. ISR(INT1_vect) {
  150.   if (set_mode != 0)
  151.     switch (set_mode) {
  152.       case 1:
  153.         zi++;
  154.         if (zi == zile_luna[luna - 1]) zi = 1;
  155.         break;
  156.       case 2:
  157.         luna++;
  158.         if (luna == 12) luna = 1;
  159.         if (zi > zile_luna[luna - 1]) zi = zile_luna[luna - 1];
  160.         break;
  161.       case 3:
  162.         an++;
  163.         if (an == 2024) an = 2013;
  164.         if (an % 4 == 0) zile_luna[1] = 29;
  165.         else zile_luna[1] = 28;
  166.         if (zi > zile_luna[luna - 1]) zi = zile_luna[luna - 1];
  167.         break;
  168.       case 4:
  169.         ora++;
  170.         if (ora == 24) ora = 0;
  171.         break;
  172.       case 5:
  173.         minut++;
  174.         if (minut == 60) minut = 0;
  175.         break;
  176.       case 6:
  177.         secunda++;
  178.         if (secunda == 60) secunda = 0;
  179.         break;
  180.     }
  181.   _delay_ms(400);
  182. }
  183. ISR(PCINT2_vect) {
  184.   _delay_ms(20);
  185.   if (!(PIND & 0x10)) {
  186.     if (backlight_on) {
  187.       digitalWrite(backlight, LOW);
  188.       backlight_on = false;
  189.     } else {
  190.       digitalWrite(backlight, HIGH);
  191.       backlight_on = true;
  192.     }
  193.   }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment