// This file lists the source code for setting and reading the real-time clock on an eZ80Acclaim! // device. It is a companion file to the ZAP Note titled "Using a Real-Time Clock in eZ80Acclaim! // Microcontrollers" (ZAP0013). // Define Months and Days #define JAN 1 #define FEB 2 #define MAR 3 #define APR 4 #define MAY 5 #define JUN 6 #define JUL 7 #define AUG 8 #define SEP 9 #define OCT 0x10 #define NOV 0x11 #define DEC 0x12 #define MO 1 #define TU 2 #define WE 3 #define TH 4 #define FR 5 #define SA 6 #define SU 7 int rtc_buff[20]; // RTC ascii buff //********************************************************* // Make sure you run your hardware/software init. routines // before entering main // Note: The Get and Put serial port routines are not included // in this list. See ZiLOGÕs eZ80 Flash Loader source code for // listing. // void main(void) { int c; int am_flag; int baudrate; RTC_CTRL = 0x20; // Make sure real time clock is off am_flag=0; // Reset real time clock am/pm flag baudrate=0x36; // 57600 50M/(16*57600)= 54 = 36H init_com0(baudrate); // Init com 0 delay(); // Just a little delay _ei(); // Turn on all interrupts puts("\r\nReset\r\n"); // Output to com0 ascii message delay(); // Delay a little display_help(); prompt(); //********************************************************* //Get command from com0 and run routines // do { do { c = getchar(); } while(c < 0); if(c == '\n' || c == ' ') continue; putchar(c); // Get a Char. For the serial port // See ZiLOGÕs flash loader software for // Source code of this routine process_char(tolower(c)); } while(1); }// end of main /*************************************************************/ // This parses the commands received from main. Will sort // through, determine what type of command it is, then call // the appropriate routine to handle it. void process_char(int c) { switch( tolower(c) ) { case '?': display_help(); break; case 's': RTC_CTRL = 0x21; // unlock RTC Ð you must do this before // you can write to the RTC registers get_user_input(); // Get users input from Serial port RTC_DOW = rtc_buff[0]; // Move data to RTC registers RTC_DOM = rtc_buff[1]; RTC_MON = rtc_buff[2]; RTC_YR = rtc_buff[3]; RTC_HRS = rtc_buff[4]; RTC_MIN = rtc_buff[5]; RTC_SEC = rtc_buff[6]; RTC_CTRL = 0x20; // lock RTC to start clock running again break; case 'r': Display_RTC(); // Output the RTC values on the console break; default: puts("\r\nUnknown Command"); break; } prompt(); } // end process_char /************************************************************ * This will display the list of valid commands. ************************************************************/ void display_help(void) { puts("\r\n"); puts("\r\n ?\tDisplay this help menu"); puts("\r\n s\tSet real time clock"); puts("\r\n r\tRead RTC value\r\n"); return ; }/** end of display_help() */ /************************************************************ * This displays the system prompt ************************************************************/ void prompt(void) { puts("\r\nAcclaim>"); return ; }/** end of prompt() */ /************************************************************ * Get user input for RTC * Note: No error checking on input, if error just re-enter ************************************************************/ void get_user_input(void) { char c; puts("\r\n"); puts("\r\nSet Real time Clock"); puts("\r\n"); puts( "\r\nEnter Day of the Week (Monday = 1, Sunday = 7)" ) ; puts("\r\nDay of Week: "); c=getchar(); putchar(c); // Save Day of the Week rtc_buff[0] = c -0x30; puts( "\r\nEnter Month (Jan = 01, Dec = 12)" ) ; puts("\r\nMonth: "); c=getchar(); putchar(c); rtc_buff[2] = ((c-0x30) << 4); c=getchar(); putchar(c); //Save Month rtc_buff[2] = rtc_buff[2] | (c-0x30); puts( "\r\nEnter Day (01-31)" ) ; puts("\r\nDay: "); c=getchar(); putchar(c); rtc_buff[1] = ((c-0x30) << 4); c=getchar(); putchar(c); //Save Day rtc_buff[1] = rtc_buff[1] | (c-0x30); puts( "\r\nEnter Year (01-99)" ) ; puts("\r\nYear: "); c=getchar(); putchar(c); rtc_buff[3] = ((c-0x30) << 4); c=getchar(); putchar(c); //Save Year rtc_buff[3] = rtc_buff[3] | (c-0x30); puts( "\r\nHours (01-24)" ) ; puts("\r\nHours: "); c=getchar(); putchar(c); rtc_buff[4] = ((c-0x30) << 4); c=getchar(); putchar(c); //Save Current Hour rtc_buff[4] = rtc_buff[4] | (c-0x30); puts( "\r\nMins (00-59)" ) ; puts("\r\nMins: "); c=getchar(); putchar(c); rtc_buff[5] = ((c-0x30) << 4); c=getchar(); putchar(c); //Save Current Mins. rtc_buff[5] = rtc_buff[5] | (c-0x30); //Just Reset Seconds. rtc_buff[6] = 0; puts("\r\n"); return ; } /************************************************************ * This displays Real time Clock on console ************************************************************/ void Display_RTC(void) { int c; char buffx[3]; c = RTC_DOW; switch (c) { case MON: puts ("MON"); break; case TUE: puts ("TUE"); break; case WED: puts ("WED"); break; case THU: puts ("THU"); break; case FRI: puts ("FRI"); break; case SAT: puts ("SAT"); break; case SUN: puts ("SUN"); break; default: break; } puts(" "); c = RTC_MON; switch (c) { case JAN: puts("JAN"); break; case FEB: puts ("FEB"); break; case MAR: puts ("MAR"); break; case APR: puts ("APR"); break; case MAY: puts ("MAY"); break; case JUN: puts ("JUN"); break; case JUL: puts ("JUL"); break; case AUG: puts ("AUG"); break; case SEP: puts ("SEP"); break; case OCT: puts ("OCT"); break; case NOV: puts ("NOV"); break; case DEC: puts ("DEC"); break; default: break; } puts("-"); *buffx='\0'; c = RTC_DOM; bin_to_ascii(c, buffx); puts(buffx); puts("-"); *buffx='\0'; c = RTC_YR; bin_to_ascii(c, buffx); puts(buffx); puts(" "); *buffx='\0'; c = RTC_HRS; if (c > 0x12) { am_flag=0; if (c == 0x20) { c= 0x08; } else if (c == 0x21) { c= 0x09; } else { c=c-0x12; } } else { am_flag=1; } bin_to_ascii(c, buffx); puts(buffx); puts(":"); *buffx='\0'; c = RTC_MIN; bin_to_ascii(c, buffx); puts(buffx); puts(":"); *buffx='\0'; c = RTC_SEC; bin_to_ascii(c, buffx); puts(buffx); } /*************************************************************/ // This will convert the binary value c into it's ascii // representation in hex. It will append the two ascii characters at the end of *buff // If you want to save it at the start of buff, make // buff[0]='\0'; This function will also null terminate // the string. void bin_to_ascii(unsigned char c, char *buff) { while(*buff) { buff++; } if( ((c >> 4) & 0x0f) <= 9) { *buff++ = ((c >> 4) & 0x0f) + '0'; } else { *buff++ = ((c >> 4) & 0x0f) + 'A' - 10; } if( (c & 0x0f) <= 9) { *buff++ = (c & 0x0f) + '0'; } else { *buff++ = (c & 0x0f) + 'A' - 10; } *buff='\0'; } /*************************************************************/