This post will help us to understand how to implement Digital Clock with time set control functions using microcontroller and LCD 16x2.
I am using MPLAB x IDE and C30 Compiler and Proteus Simulation on v8.0 (can be downloaded from downloads section at the bottom of this post). This code will work on PIC24, DsPIC33 and DsPIC30 (16bit microcontroller) by slightly changing code.
I supposed that you know following.
- How to blink an LED (if not then I suggest you to read this (link) first before continuing this post)
- Digital Clock using 7 segments. (If not then I suggest you to read this (link) first before continuing this post).
- How to use timer 1 interrupt. (If not then I suggest you to read this (link) first before continuing this post).
- How to interface LCD (If not then I suggest you to read this (link) first before continuing this post).
In this figure, as I used three push buttons one is to set, up and down. With PortA.
In the following figure. I first initialize LCD and then initialize digital clock inputs and outputs pins and print default time on LCD. In main function a while loop is scanning set button. If it is pressed the time set mode will activated and then you can set time through this first hours will be set and then press again set button to set minutes. Up and down buttons will help you to increase and decrease hours and minutes respectively. After you set hours and minutes press set again to start timer in order to deactivate timeset mode of clock. Once you entered in timeset mode clock will never run.
Timer 1 interrupt function which is configured to give interrupt after every 1 second and time will be updated on LCD.
|ISR for Timer1|
In the following figure, the time set function is shown which is responsible for setting time. In this when its state is hours which is set initially. When up button is pressed it will increment hours and when down button is pressed it will decrement hours. Same in the case of minutes. To change the state into set minutes press set again. Now you can edit minutes after that press set again this while loop will be broken and you time is set and clock will run normally.