Software Clock
# Include "Global. H "// time base function struct typedef struct {uint16_t delaytime; uint16_t timebackup; uint16_t Init;} timer_t; static volatile every u16day; // daily static volatile uint16_t u16hour; // static volatile uint16_t u16minute; // static volatile uint16_t u16s; // static volatile uint16_t u16ms; // millisecond /************************************ **************************************** * ** # function: delayadd # descripti On: calculation time # parameter: nowtime current time * time base structure # Return: NULL # authoranddata: huangzhigang 20140716 ************************************** **************************************** */static void delayadd (uint16_t nowtime, timer_t * Time) {If (nowtime> (time-> timebackup) {uint16_t temp = nowtime-time-> timebackup; If (0 xFFFF-time-> delaytime)> = temp) {time-> delaytime + = temp;} Time-> timeback Up = nowtime;} void timeh (timer_t * Time) // Add a timer task in hours {If (! Time-> init) // start time initialization {time-> init = 1; time-> timebackup = u16hour;} else {delayadd (u16hour, time );}} void timem (timer_t * Time) // Add a timer task in minutes {If (! Time-> init) // start time initialization {time-> init = 1; time-> timebackup = u16minute;} else {delayadd (u16minute, time );}} void times (timer_t * Time) // Add a timer task in seconds {If (! Time-> init) // start time initialization {time-> init = 1; time-> timebackup = u16s;} else {delayadd (u16s, time );}} void timems (timer_t * Time) // Add a timing task in milliseconds {If (! Time-> init) // start time initialization {time-> init = 1; time-> timebackup = u16ms;} else {delayadd (u16ms, time );}} /*************************************** **************************************** # function: interrup_time # Description: The function called in the interrupt is called once every 1 ms to record the clock running # parameter: NULL # Return: NULL # authoranddata: huangzhigang 20140716 ************************************** **************************************** */void int Errup_time (void) {If (! (+++ U16ms % 1000) {If (! (++ U16s % 60) {u16ms = 0; // clear millisecond after 60000ms accumulators if (! (++ U16minute % 60) {If (! (++ U16hour % 24) {++ u16day;} If (u16s >=61200) {u16s = 0 ;}// second after 17 hours complete processors }}}}
Software Clock-use timer to simulate clock