The Ultimate Alarm Clock

Software Design

Our program starts by getting input for the time and date. After this it stays in a main loop which polls for button presses. A press of the set button causes the program to jump to the beginning and get input for the time and date again. If the alarm set button is pressed, a subroutine is called which gets input for the alarm time and turns the alarm on. If the alarm on/off button is pressed, the alarm is turned on or off.

The input for the time and date are gotten using the same method. The first value that is input is the hour. The hour is incremented and decremented using the up (#) and down (*)buttons. When the hour gets incremented 24, it is automatically set back to zero. Also, when it is decremented below zero, it is reset to 23. To end the setting of hours, the D button is pressed, and the setting goes to the minutes. The minutes work the same way as the hours but with a reset to zero at 60 and a reset to 59 when it is decremented below zero. When the D button is pressed, the setting goes to the month. This also works the same way as the minutes and hours. Next, the date is set. The maximum and minumum reset value for the date changes depending on the month. For months with 31 days, the day will reset to 1 when the date reaches 32. For months with 30 days, the day will reset to 1 when the date reaches 31. For February, the date will always reset to 1 when the day reaches 29. There is no leap year check since the year is set after the date. When the date is decremented below 1 it is reset to the maximum date for the set month. The value of the year that can be set ranges from 0 to 99 and are the last two digits of the current year.

The timing for the clock is kept using the timer 2 overflow interrupt. Timer 2 is set up to use an external clock input, a 32.768 KHz crystal. The prescale of timer 2 is 128 which causes the timer 2 interrupt to be entered every one second. The code for changing the time and date once the clock is running is done inside this interrupt. Also, the alarm buzzer and blinking second led are controlled here.

The seconds are incremented every time the timer 2 interrupt is entered. If they equal 60, they are reset to zero. Every time the seconds reach 60, the minutes are incremented and are reset to zero when they reach 60. The hours are incremented when the minutes reach 60 and are reset when they reach 24. The days are incremented when the hours are reset to zero. The days reset to 1 when they reach the maximum date for the month. This date can be 31, 30, 29, or 28 depending on the month and year. There is a leap year check if the month is February.

The alarm is turned on in the timer 2 interrupt. The alarm on flag is checked at the end of the interrupt, and if it is set, the time is compared to the time of the alarm. If they are equal the alarm will be sounded for five seconds or until the alarm on/off button is pressed (whichever happens first). Also, the blinking seconds led is reversed in the interrupt. A feature of the alarm clock is that when the alarm is buzzing, the bliking seconds led stays lit.

The timer zero overflow interrupt is entered every millisecond. It is used for a millisecond delay function for the lcd display and for the button debounce routine which is called every thirty milliseconds when looking for a button press.

The output to the led and the lcd is refreshed every 30 milliseconds with the current time and date. The data for the time and date to be placed on their respective displays are calculated in similar ways. The number of tens and ones in the hours, minutes, months, days, and years are calculated. For the date, a 0x30 is added to every digit to convert them to ascii values. The date is then put on the display with slashes in between the numbers. After the number of tens and ones in the minutes and hours are calculated, they are put onto the led display one digit at a time. Eight bits are sent to the led. The lower four bits are the binary value of the digit displayed. The first four bits select the digit to be displayed and are logic low. A zero in any of the first four bits means that the, first, second, third, and/or fourth digit will be display. Each digit is lit up on the display for four milliseconds.

All of the button presses are debounced using a four state debounce routine. The states switch every thirty milliseconds which is the amount of time between every debounce call. The keyboard scanner is called at each state switch. State 1 checks if the button is pressed. If it is, it sets press to zero and stays in state 1. If it is not pressed, it sets maybe to button and goes to state 2. State 2 checks if button still equals maybe. If not, it goes to state 1. If yes, it sets press to button and goes to state 3. State three checks if button equals press. If yes, it stays in state 3. If no, of goes to state 4. State 4 also checks if button equals press. If it does, the state goes back to 3. If not, press is set to zero and the state goes back to 1.

Webpage created by Joel Avrunin ('01) and Philip Weiss ('00).
Created May 4, 2000. Last updated May 6, 2000.