The Clock

This is a twelve hour clock that uses a crystal oscillator to keep time. The time is displayed on a seven segment display that is driven by a PIC16f882. Also, the pulse width modulation (PWM) peripheral of the chip is used to drive a speaker that acts as an alarm. Two buttons can be used to enter a setup mode and increment the program time, select an alarm time, and turn the alarm on or off.
Assembling the Clock
Part List
-
28 pin IC socket: http://www.futurlec.com/Sockets/ICS28.shtml
-
Seven segment display with four numbers and colon:http://www.futurlec.com/LEDDisp.html
-
Crystal Oscillator freq= 32.768kHz: http://futurlec.com/ICcrystals.html
-
Five 2.2kOhm resistors
-
Eight 100Ohm resistors
-
Three 10kOhm resistors
- 100kOhm resistor
-
0.1µF ceramic capacitor
-
Two 36pF ceramic capacitors
-
Six 2N3904 NPN general purpose transistors: http://www.futurlec.com/TransGen2N.shtml
-
One IRL540N mosfet N channel transistor : http://www.futurlec.com/TransMosIRF.shtml
-
2”x1.5” proto-board: http://futurlec.com/ProtoBoards.shtml
-
Male and female header pins: http://futurlec.com/ConnHead.html
-
Small 0.5W Speaker: http://futurlec.com/Speakers.html
-
Two square push buttons: http://futurlec.com/SwPush.html
-
ProjectBox 4.0" x 2.6" x 1.4" : http://www.futurlec.com/ProjectBoxes.html
-
4.8V AC to DC transformer wall socket
-
22 AWG wire of varying colours
The Protoboard and Box
The chip and female ports are mounted onto the proto-board, with female pins to connect the display, buttons, speaker, and program lines. It is placed inside the project box, along with the two buttons, seven segment display and speaker.
The ProtoBoard with all the components soldered on



The Project box with all the display buttons and speaker (protoboard removed)

The Protoboard mounted inside the project box
The Proto-Board
Schematic:
The Code
All the Source Code can be found here: AlarmClockCode.asm
Displaying to the 7 Segment:
Displaying to the 7 segement is done by cycling through each segment and using a lookup table to pass the number to the display. This code is in the Main and is cycle through.
Timing:
Taking advantage of the external oscilator function and hardware interupts, the program counts how many times the oscilator has pulsed. Since the value of the oscilator's frequency is 37.768kHz, in one second the oscilator would have counted 37768 times. This is equal to (2^15) which allows us to determine when one second has elapsed. The timer1 perihpheral is used to handle the oscliator counting since it is split up into two 8 bit registers and can use hardward interupts. A one is placed in the bit 16 of the counting register (bit 8 of TMR1H) so that the register will over flow after one second. The overflow trips a hardware interupt, and inside the interupt service routine (ISR) a register that counts the number of seconds in the first decimal place is incremented by one, and the flags are cleared. A subroutine "CheckAll" is called which compared the registers holding the clock value and sees if the other registers need to be incremented.
Setup:
In order to enter the setup mode, a check for a button press exists inside the "Main" code. It is passed through when displaying to the LEDs. If a button is pressed the SetMinute subroutine is called. This then waits for other buttons presses to either increment the value of the minute, or change to the SetHour subroutine. This continues through subroutines to set the alarm, and turn the alarm on and off. Since the clock trips the ISR there is no worries that the clock will stop during this process.
Alarms and PWM:
To generate an alarm tone through the speaker, the pulse width modulation hardware peripheral of the chip was used. This feature allows for the generation of a square wave with a particular frequency, and duty cycle. Since the duty cycle for audio is irrelevant it was set to 50%. The frequency was set by placing certian values inside the setup registers PR2, CCPR1L, and CPP1CON. A good explanation of how to setup the PWM can be found in the 16F882 DataSheet (see external resources section). In order to sound the alarm, the TMR2ON bit of the T2CON register must be set. This is done inside a block of code in the main which is only entered if a flag, TEST,1 , is set. By placing the alarm code in the Main as opposed to the ISR, allows the clock to continue running, and the current time to be displayed, while the alarm is sounding.
Putting it all Together:
In order to make sure all the code flows well, the Main and subroutines need to be layed out in a certain way. Also playing one tone as an alarm would be dull, so the alarm is oscilated by turning the PWM on and off. This is better seen by looking at the actual code which shows the flow of the code. In the end you have a functional 12 hour clock with a built in alarm that can be turned on and off.
External Resources:
PIC16f882 DataSheet: http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf
