home *** CD-ROM | disk | FTP | other *** search
- /*****************************************/
- /* Time.c demo program for the EMUL51-PC */
- /* The program simulates a simple alarm */
- /* clock. The time delay uses timer0 */
- /* interrupt and assumes 12Mhz oscillator*/
- /*****************************************/
-
- /* create Franklin extended symbol file */
- #pragma code symbols debug objectextend
-
- /*****************************************/
- /* Include register definitions for the */
- /* 8051 and C string functions */
- /*****************************************/
-
- #include <reg51.h>
- #include <string.h>
- #include <stdio.h>
-
- /*****************************************/
- /* Definition of global variables */
- /*****************************************/
-
- typedef struct {
- unsigned char hour;
- unsigned char min;
- unsigned char sec;
- unsigned char tenth;
- unsigned char hunth;
- } TIME;
-
- bit alarmflg = 0, flipflop;
- xdata char status [10];
- char counter;
-
- void maritime_clock ( ) ;
- void timer0_delay ( ) ;
- void check_alarm ( ) ;
-
- /****************************************/
- /* Predefined timer values */
- /****************************************/
-
- TIME timer = { 10, 15, 30, 00, 00 };
- TIME alarmtime = { 10, 17, 00, 00, 00 }; /* 1 1/2 min catnap */
-
- /****************************************/
- /* Main function */
- /****************************************/
-
- main()
- {
-
- /* lets use timer0 in the 8 bit autoreload mode to get our */
- /* hundreths of a second interrupt - this could be done */
- /* by counting to 250 forty times or 10,000 cpu cycles */
- /* (assuming 12Mhz clock) */
-
- TH0 = 6; /* auto reload value goes in TH0 */
- /* (256-250) as 8051 timers increment and interrupt on overflow */
- counter = 0 ; /* used to count to forty */
-
- IE = 0x82; /* initialize interrupt register to enable timer0 */
- TMOD = 2; /* setup timer 0 in 8 bit autoreload mode */
- TR0 = 1; /* start the cycle clock */
-
- while(1) {
-
- maritime_clock();
- }
- }
-
- /****************************************/
- /* 1/100th sec delay function */
- /****************************************/
-
- void timer0() interrupt 1
-
- {
- TF0 = 0 ; /* disable further interrupts */
-
- if (++counter > 39) {
- counter = 0;
- timer.hunth++;
- }
-
- return;
-
- }
-
- /****************************************/
- /* Function for incrementing maritime */
- /* (24 hour) clock */
- /****************************************/
-
- void maritime_clock ( )
-
- {
- /* 100ths second */
-
- if (timer.hunth > 9) {
- timer.hunth = 0;
- timer.tenth++;
- }
- else return;
-
- /* 10ths second */
-
- if (timer.tenth > 9) {
- timer.tenth = 0;
- timer.sec++;
- }
- else return;
-
- /* seconds */
-
- flipflop = !flipflop; /* toggle bit */
-
- if (flipflop) strcpy (status, "TICK");
- else strcpy (status, "TOCK");
-
- if (alarmflg) {
- if (P1 == 255) P1 = 0;
- else P1 = 255;
- }
- else P1++; /* P1 as binary counter - works well on demo board */
-
- if ( timer.sec > 59 ){
- timer.sec = 0;
- timer.min ++;
- }
- else return;
-
- /* Minutes */
-
- if ( timer.min > 59 ){
- timer.min = 0;
- timer.hour ++;
- }
- else {
- check_alarm();
- return;
- }
-
- /* Hours */
-
- if ( timer.hour > 23 ) timer.hour = 0;
- check_alarm();
-
- }
-
- /*****************************************/
- /* Function for checking set time */
- /*****************************************/
-
- void check_alarm ( void )
- {
- if ( ( timer.min == alarmtime.min)
- & ( timer.hour == alarmtime.hour) ) {
- alarmflg = 1;
- strcpy (status,"RINGING");
- }
- }