Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MAIN:
- #include <ioavr.h>
- #include <inavr.h>
- #include <stdio.h>
- #include <pgmspace.h>
- #include "rtc_simple.h"
- #include "hd44780.h"
- #include "queue.h"
- #include "keyb_drv.h"
- #define CLOCK_INC 0x03
- typedef union TSysRq {
- unsigned char msg;
- struct {
- unsigned char rq_data : 4;
- unsigned char rq_id : 4;
- };
- } TSysRq;
- //---------------------------------------------------------
- // Constants declaration
- //---------------------------------------------------------
- static const char __flash LCDUserChar[] = {
- 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F,
- 0x1F, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x1F,
- 0x1F, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x1F,
- 0x1F, 0x00, 0x1C, 0x1C, 0x1C, 0x1C, 0x00, 0x1F,
- 0x1F, 0x00, 0x1E, 0x1E, 0x1E, 0x1E, 0x00, 0x1F,
- 0x1F, 0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x1F,
- 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03,
- 0x18, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x18 };
- static const char __flash sSun[] = " ";
- static char const __flash sMon[] = " ";
- static char const __flash sTue[] = " ";
- static char const __flash sWed[] = " ";
- static char const __flash sThu[] = " ";
- static char const __flash sFri[] = " ";
- static char const __flash sSat[] = " ";
- static const char __flash sCl[] = "%2d:%02d:%02d %s";
- __flash const char __flash* sWDay[] = {
- sSun, sMon, sTue, sWed, sThu, sFri, sSat
- };
- //---------------------------------------------------------
- // Global variables declaration
- //---------------------------------------------------------
- TClock rtc;
- TClock myTime;
- TQueue event_queue;
- unsigned char ev_q_buf[16];
- char tmp_buf[4];
- TSysRq rq;
- char timer[3]={0, 1, 30};
- const char endingMes[] =
- "END END END END";
- //This function is used by printf function to transfer data to STDIO device
- int putchar(int ch)
- {
- LCDPutChar(ch);
- return ch;
- }
- void onClockInc()
- {
- LCDGoTo(LINE_0 + 1);
- printf_P(
- sCl,
- rtc.hr,
- rtc.min,
- rtc.sec,
- memcpy_P(tmp_buf, sWDay[rtc.wday], sizeof(sSun)));
- }
- void onClockInc2(){
- LCDGoTo(LINE_0 + 21);
- printf_P(
- sCl,
- myTime.hr,
- myTime.min,
- myTime.sec,
- memcpy_P(tmp_buf, sWDay[rtc.wday], sizeof(sSun)));
- }
- void progressBar(TClock rtc){
- int score = (rtcToSec(rtc)/(timer[0]*3600+timer[1]*60+timer[2])*10);
- LCDGoTo(LINE_1 + 1);
- LCDPutChar('|');
- LCDGoTo(LINE_1 + 12);
- LCDPutChar('|');
- for(int i=0; i<score; i++){
- LCDGoTo(LINE_1 + 2+ i);
- LCDPutChar('X');
- }
- }
- void finish()
- {
- LCDGoTo(LINE_1 + 21);
- LCDPuts(endingMes);
- }
- static void InitDevices()
- {
- LCDInit();
- LCDLoadUserCharP(LCDUserChar, 0, sizeof(LCDUserChar));
- LCDClear();
- //T0 start
- TCNT0 = 0;
- OCR0 = 71;
- rtcInit(&myTime, timer[0], timer[1], timer[2]);
- TCCR0 = (1 << WGM01) | (1 << CS02) | (1 << CS01) | (1 << CS00);
- //dvt = 50;
- //Timers interrupt mask
- TIMSK = (1 << OCIE0);
- __enable_interrupt();
- }
- #pragma vector = TIMER0_COMP_vect
- __interrupt void ISR_OCR0()
- {
- static unsigned char pre_dv = 100;
- kbService(&event_queue);
- if(--pre_dv) return;
- pre_dv = 100;
- if(!rtcZero(myTime)){
- rtcInc(&rtc);
- rtcDec(&myTime);
- }
- if(rtcZero(myTime)){
- finish();
- }
- qAdd(&event_queue, MSG(CLOCK_INC, 0));
- }
- void main()
- {
- InitDevices();
- qInit(&event_queue, ev_q_buf, sizeof(ev_q_buf));
- qAdd(&event_queue, MSG(CLOCK_INC, 0));
- for(;;)
- {
- while(!qGet(&event_queue, &rq.msg));
- switch(rq.rq_id) {
- case CLOCK_INC:
- onClockInc();
- onClockInc2();
- progressBar(rtc);
- break;
- }
- }
- }
- %%%%%%
- LIBRARY
- "rtc_simple.h"
- #include "rtc_simple.h"
- void rtcInc(TClock *rtc)
- {
- if((++rtc->sec) != 60) return;
- rtc->sec = 0;
- if((++rtc->min) != 60) return;
- rtc->min = 0;
- if((++rtc->hr) != 24) return;
- rtc->hr = 0;
- if((++rtc->wday) != 7) return;
- rtc->wday = 0;
- }
- void rtcDec(TClock *rtc)
- {
- if((--rtc->sec) != 255) return;
- rtc->sec = 59;
- if((--rtc->min) != 255) return;
- rtc->min = 59;
- if((--rtc->hr) != 255 ) return;
- rtc->hr = 0;
- if((--rtc->wday) != 7) return;
- rtc->wday = 0;
- }
- void rtcInit(TClock *rtc, char t_hr, char t_min, char t_sec)
- {
- rtc->sec = t_sec;
- rtc->min = t_min;
- rtc->hr = t_hr;
- }
- int rtcZero(TClock rtc){
- if(rtc.hr==0 && rtc.min==0 && rtc.sec==0)
- return 1;
- else
- return 0;
- }
- double rtcToSec(TClock rtc){
- double sum = 0;
- sum=rtc.sec+rtc.min*60+rtc.hr*3600;
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement