Advertisement
Guest User

EXAMTIMER

a guest
Dec 16th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. MAIN:
  2. #include <ioavr.h>
  3. #include <inavr.h>
  4. #include <stdio.h>
  5. #include <pgmspace.h>
  6. #include "rtc_simple.h"
  7. #include "hd44780.h"
  8. #include "queue.h"
  9. #include "keyb_drv.h"
  10. #define CLOCK_INC   0x03
  11.  
  12. typedef union TSysRq {
  13.     unsigned char msg;
  14.     struct {
  15.         unsigned char rq_data   : 4;
  16.         unsigned char rq_id     : 4;
  17.     };
  18. } TSysRq;
  19.  
  20. //---------------------------------------------------------
  21. // Constants declaration
  22. //---------------------------------------------------------
  23.  
  24. static const char __flash LCDUserChar[] = {
  25.     0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F,
  26.     0x1F, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x1F,
  27.     0x1F, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x1F,
  28.     0x1F, 0x00, 0x1C, 0x1C, 0x1C, 0x1C, 0x00, 0x1F,
  29.     0x1F, 0x00, 0x1E, 0x1E, 0x1E, 0x1E, 0x00, 0x1F,
  30.     0x1F, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x1F,
  31.     0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03,
  32.     0x18, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x18 };
  33.  
  34. static const char __flash sSun[] = " ";
  35. static char const __flash sMon[] = " ";
  36. static char const __flash sTue[] = " ";
  37. static char const __flash sWed[] = " ";
  38. static char const __flash sThu[] = " ";
  39. static char const __flash sFri[] = " ";
  40. static char const __flash sSat[] = " ";
  41. static const char __flash sCl[] = "%2d:%02d:%02d %s";
  42.  
  43. __flash const char __flash* sWDay[] = {
  44.     sSun, sMon, sTue, sWed, sThu, sFri, sSat
  45.  };
  46.  
  47. //---------------------------------------------------------
  48. // Global variables declaration
  49. //---------------------------------------------------------
  50. TClock rtc;
  51. TClock myTime;
  52. TQueue event_queue;
  53. unsigned char ev_q_buf[16];
  54. char tmp_buf[4];
  55. TSysRq rq;
  56. char timer[3]={0, 1, 30};
  57.  
  58.  const char endingMes[] =
  59.     "END END END END";
  60.  
  61. //This function is used by printf function to transfer data to STDIO device
  62. int putchar(int ch)
  63. {
  64.     LCDPutChar(ch);
  65.     return ch;
  66. }
  67.  
  68.  
  69.  
  70.  
  71. void onClockInc()
  72. {
  73.     LCDGoTo(LINE_0 + 1);
  74.     printf_P(
  75.         sCl,
  76.         rtc.hr,
  77.         rtc.min,
  78.         rtc.sec,
  79.         memcpy_P(tmp_buf, sWDay[rtc.wday], sizeof(sSun)));
  80. }
  81.  
  82. void onClockInc2(){
  83.  
  84.   LCDGoTo(LINE_0 + 21);
  85.     printf_P(
  86.         sCl,
  87.         myTime.hr,
  88.         myTime.min,
  89.         myTime.sec,
  90.         memcpy_P(tmp_buf, sWDay[rtc.wday], sizeof(sSun)));
  91. }
  92.  
  93. void progressBar(TClock rtc){
  94.   int score = (rtcToSec(rtc)/(timer[0]*3600+timer[1]*60+timer[2])*10);
  95.   LCDGoTo(LINE_1 + 1);
  96.     LCDPutChar('|');
  97.    LCDGoTo(LINE_1 + 12);
  98.     LCDPutChar('|');
  99.   for(int i=0; i<score; i++){
  100.   LCDGoTo(LINE_1 + 2+ i);
  101.     LCDPutChar('X');
  102.        
  103.   }
  104. }
  105.  
  106.  
  107. void finish()
  108. {
  109.     LCDGoTo(LINE_1 + 21);
  110.     LCDPuts(endingMes);
  111. }
  112.  
  113.  
  114.  
  115. static void InitDevices()
  116. {
  117.     LCDInit();
  118.     LCDLoadUserCharP(LCDUserChar, 0, sizeof(LCDUserChar));
  119.     LCDClear();
  120.     //T0 start
  121.     TCNT0 = 0;
  122.     OCR0 = 71;
  123.      
  124.     rtcInit(&myTime, timer[0], timer[1], timer[2]);
  125.    
  126.     TCCR0 = (1 << WGM01) | (1 << CS02) | (1 << CS01) | (1 << CS00);
  127.     //dvt = 50;
  128.     //Timers interrupt mask
  129.     TIMSK = (1 << OCIE0);
  130.     __enable_interrupt();  
  131. }
  132.  
  133. #pragma vector = TIMER0_COMP_vect
  134. __interrupt void ISR_OCR0()
  135. {
  136. static unsigned char pre_dv = 100;
  137.     kbService(&event_queue);
  138.     if(--pre_dv) return;
  139.     pre_dv = 100;
  140.         if(!rtcZero(myTime)){
  141.     rtcInc(&rtc);  
  142.         rtcDec(&myTime);   
  143.         }
  144.         if(rtcZero(myTime)){
  145.          finish();
  146.  
  147.         }
  148.          
  149.     qAdd(&event_queue, MSG(CLOCK_INC, 0));
  150. }
  151.  
  152. void main()
  153. {  
  154.     InitDevices();
  155.     qInit(&event_queue, ev_q_buf, sizeof(ev_q_buf));
  156.     qAdd(&event_queue, MSG(CLOCK_INC, 0));
  157.     for(;;)
  158.     {
  159.         while(!qGet(&event_queue, &rq.msg));
  160.         switch(rq.rq_id) {
  161.        
  162.         case CLOCK_INC:
  163.             onClockInc();
  164.                         onClockInc2();
  165.                         progressBar(rtc);
  166.             break;
  167.         }
  168.     }
  169. }
  170.  
  171. %%%%%%
  172. LIBRARY
  173. "rtc_simple.h"
  174.  
  175. #include "rtc_simple.h"
  176.  
  177. void rtcInc(TClock *rtc)
  178. {
  179.     if((++rtc->sec) != 60) return;
  180.     rtc->sec = 0;
  181.     if((++rtc->min) != 60) return;
  182.     rtc->min = 0;
  183.     if((++rtc->hr) != 24) return;
  184.     rtc->hr = 0;
  185.     if((++rtc->wday) != 7) return;
  186.     rtc->wday = 0;
  187. }
  188.  
  189.  
  190. void rtcDec(TClock *rtc)
  191. {
  192.     if((--rtc->sec) != 255) return;
  193.     rtc->sec = 59;
  194.     if((--rtc->min) != 255) return;
  195.     rtc->min = 59;
  196.     if((--rtc->hr) != 255 ) return;
  197.     rtc->hr = 0;
  198.     if((--rtc->wday) != 7) return;
  199.     rtc->wday = 0;
  200. }
  201.  
  202.  
  203.  
  204. void rtcInit(TClock *rtc, char t_hr, char t_min, char t_sec)
  205. {
  206.     rtc->sec = t_sec;
  207.     rtc->min = t_min;
  208.     rtc->hr = t_hr;
  209. }
  210.  
  211. int rtcZero(TClock rtc){
  212.   if(rtc.hr==0 && rtc.min==0 && rtc.sec==0)
  213.     return 1;
  214.     else
  215.       return 0;
  216. }
  217.  
  218. double rtcToSec(TClock rtc){
  219. double sum = 0;
  220. sum=rtc.sec+rtc.min*60+rtc.hr*3600;
  221. return sum;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement