Implementation

The software consists of two subroutines, a FUSES section and one Interrupt Service Routine (ISR).

The init() routine configures the device to the needs of this application.

The other sub routine corrects the date for leap years. The routine is described in detail in the flow diagram Figure 2.

The FUSES section modifies the device fuses to make it run from the internal calibrated RC oscillator, with a prescaler of two. This results in a clocking speed of 4MHz.

The main program controls the power down sequence. The sleep_mode() macro will place the controller in sleep mode. To make sure the interrupt is cleared upon wakeup, the main routine also makes sure the device never re-enters sleep mode within less than one TOSC cycle.

Timer/Counter0 Overflow Interrupt Service Routine

The interrupt service routine is executed every time TCC0 overflows. The interrupt request wakes the MCU to update the timer variables. An interrupt routine cannot return or accept any variables. A global structure with timer variables are declared to keep track of time: “second”, “minute”, “hour”, “date”, “month”, and “year”. Since the time required to complete one timer overflow is known, second will be incremented by a fixed number for every interrupt. Once it reaches 60, minute is incremented by 1 and second is set to 0.

Figure 1. Flow Chart, TCC0 Overflow ISR

Leap Year Check Subroutine

This routine performs a leap year check. It returns false for a leap year, and true for a non-leap year. A year is considered to be leap if both of the following conditions are met:

  1. 1.The year is divisible with four, and
  2. 2.If the year is divisible with 100, it also has to be divisible by 400.
Figure 2. Flow Chart, Leap Year Check Subroutine