SHARE
TWEET

EXAMTIMER

a guest Dec 9th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ioavr.h>
  2. #include <inavr.h>
  3. #include <stdio.h>
  4. #include <pgmspace.h>
  5. #include "rtc_simple.h"
  6. #include "hd44780.h"
  7. #include "queue.h"
  8. #include "keyb_drv.h"
  9. #define CLOCK_INC   0x03
  10.  
  11. typedef union TSysRq {
  12.     unsigned char msg;
  13.     struct {
  14.         unsigned char rq_data   : 4;
  15.         unsigned char rq_id     : 4;
  16.     };
  17. } TSysRq;
  18.  
  19. //---------------------------------------------------------
  20. // Constants declaration
  21. //---------------------------------------------------------
  22.  
  23. static const char __flash LCDUserChar[] = {
  24.     0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F,
  25.     0x1F, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x1F,
  26.     0x1F, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x1F,
  27.     0x1F, 0x00, 0x1C, 0x1C, 0x1C, 0x1C, 0x00, 0x1F,
  28.     0x1F, 0x00, 0x1E, 0x1E, 0x1E, 0x1E, 0x00, 0x1F,
  29.     0x1F, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x1F,
  30.     0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03,
  31.     0x18, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x18 };
  32.  
  33. static const char __flash sSun[] = " ";
  34. static char const __flash sMon[] = " ";
  35. static char const __flash sTue[] = " ";
  36. static char const __flash sWed[] = " ";
  37. static char const __flash sThu[] = " ";
  38. static char const __flash sFri[] = " ";
  39. static char const __flash sSat[] = " ";
  40. static const char __flash sCl[] = "%2d:%02d:%02d %s";
  41.  
  42. __flash const char __flash* sWDay[] = {
  43.     sSun, sMon, sTue, sWed, sThu, sFri, sSat
  44.  };
  45.  
  46. //---------------------------------------------------------
  47. // Global variables declaration
  48. //---------------------------------------------------------
  49. TClock rtc;
  50. TClock myTime;
  51. TQueue event_queue;
  52. unsigned char ev_q_buf[16];
  53. char tmp_buf[4];
  54. TSysRq rq;
  55. int timer[3]={0, 4, 20};
  56. rtcInit(&myTime, timer[0], timer[1], timer[2]);
  57.  
  58. //This function is used by printf function to transfer data to STDIO device
  59. int putchar(int ch)
  60. {
  61.     LCDPutChar(ch);
  62.     return ch;
  63. }
  64.  
  65.  
  66.  
  67.  
  68. void onClockInc()
  69. {
  70.     LCDGoTo(LINE_0 + 2);
  71.     printf_P(
  72.         sCl,
  73.         rtc.hr,
  74.         rtc.min,
  75.         rtc.sec,
  76.         memcpy_P(tmp_buf, sWDay[rtc.wday], sizeof(sSun)));
  77. }
  78.  
  79. void onClockInc2()
  80. {
  81.     LCDGoTo(LINE_1 + 2);
  82.     printf_P(
  83.         sCl,
  84.         myTime.hr,
  85.         myTime.min,
  86.         myTime.sec,
  87.         memcpy_P(tmp_buf, sWDay[rtc.wday], sizeof(sSun)));
  88. }
  89.  
  90.  
  91. static void InitDevices()
  92. {
  93.     LCDInit();
  94.     LCDLoadUserCharP(LCDUserChar, 0, sizeof(LCDUserChar));
  95.     LCDClear();
  96.     //T0 start
  97.     TCNT0 = 0;
  98.     OCR0 = 71;
  99.  
  100.     TCCR0 = (1 << WGM01) | (1 << CS02) | (1 << CS01) | (1 << CS00);
  101.     //dvt = 50;
  102.     //Timers interrupt mask
  103.     TIMSK = (1 << OCIE0);
  104.     __enable_interrupt();  
  105. }
  106.  
  107. #pragma vector = TIMER0_COMP_vect
  108. __interrupt void ISR_OCR0()
  109. {
  110. static unsigned char pre_dv = 100;
  111.     kbService(&event_queue);
  112.     if(--pre_dv) return;
  113.     pre_dv = 100;
  114.     rtcInc(&rtc);  
  115.         rtcDec(&myTime);   
  116.     qAdd(&event_queue, MSG(CLOCK_INC, 0));
  117. }
  118.  
  119. void main()
  120. {  
  121.     InitDevices();
  122.     qInit(&event_queue, ev_q_buf, sizeof(ev_q_buf));
  123.     qAdd(&event_queue, MSG(CLOCK_INC, 0));
  124.     for(;;)
  125.     {
  126.         while(!qGet(&event_queue, &rq.msg));
  127.         switch(rq.rq_id) {
  128.        
  129.         case CLOCK_INC:
  130.             onClockInc();
  131.                         onClockInc2();
  132.             break;
  133.         }
  134.     }
  135. }
  136.  
  137. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  138.  
  139. #include "rtc_simple.h"
  140.  
  141. void rtcInc(TClock *rtc)
  142. {
  143.     if((++rtc->sec) != 60) return;
  144.     rtc->sec = 0;
  145.     if((++rtc->min) != 60) return;
  146.     rtc->min = 0;
  147.     if((++rtc->hr) != 24) return;
  148.     rtc->hr = 0;
  149.     if((++rtc->wday) != 7) return;
  150.     rtc->wday = 0;
  151. }
  152.  
  153.  
  154. void rtcDec(TClock *rtc)
  155. {
  156.     if((--rtc->sec) != 255) return;
  157.     rtc->sec = 59;
  158.     if((--rtc->min) != 255) return;
  159.     rtc->min = 59;
  160.     if((--rtc->hr) != 255 ) return;
  161.     rtc->hr = 0;
  162.     if((--rtc->wday) != 7) return;
  163.     rtc->wday = 0;
  164. }
  165.  
  166. void rtcInit(TClock *rtc, int hr, int min, int sec)
  167. {
  168.     rtc->sec = sec;
  169.     rtc->min = min;
  170.     rtc->hr = hr;
  171.    
  172. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top