How to write a C Program Display a real time clock (HH:MM:SS) on the LCD in C Programming Language ?
Solution:
/*Real Time Clock (LCD Clock)
* Description: Display a real time clock (HH:MM:SS) on the LCD.
*/
#include <msp430.h>
#include <stdio.h>
#include "LCD.h"
#include "CLK.h"
// Port 1 defines
#define LED BIT0
#define DB4 BIT4
#define DB5 BIT5
#define DB6 BIT6
#define DB7 BIT7
// Port 2 defines
#define RS BIT0
#define RW BIT1
#define E BIT2
long int totalseconds = 1;
int seconds = 0;
int minutes = 0;
int hours = 0;
char OutString[10];
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
clk_set_16MHZ(); // set clock to 16MHz
P1DIR |= (LED + DB4 + DB5 + DB6 + DB7); // sets ports P1.0, P1.4-7 to outputs
P2DIR |= (RS + RW + E); // sets ports P2.0-2 to outputs (RS, R/W, E)
CCTL0 = CCIE; // CCR0 interrupt enabled
CCR0 = 32768;
TACTL = TASSEL_1 + MC_2; // ACLK, contmode
_enable_interrupts(); // enable interrupts
lcd_init_seq_nibble(); // initialize sequence for LCD nibble mode
// (2-line mode, 5x8 dots, cursor off, increment, no shifting)
int index = 0;
for(;;){
// if statement determines when to append a 0 and loads new time into OutString
if (hours < 10 & minutes < 10 & seconds < 10){
sprintf(OutString, "0%d:0%d:0%d", hours, minutes, seconds);
}
else if(hours < 10 & minutes < 10 & seconds >= 10){
sprintf(OutString, "0%d:0%d:%d", hours, minutes, seconds);
}
else if(hours < 10 & minutes >= 10 & seconds < 10){
sprintf(OutString, "0%d:%d:0%d", hours, minutes, seconds);
}
else if(hours < 10 & minutes >= 10 & seconds >= 10){
sprintf(OutString, "0%d:%d:%d", hours, minutes, seconds);
}
else if(hours >= 10 & minutes < 10 & seconds < 10){
sprintf(OutString, "%d:0%d:0%d", hours, minutes, seconds);
}
else if(hours >= 10 & minutes < 10 & seconds >= 10){
sprintf(OutString, "%d:0%d:%d", hours, minutes, seconds);
}
else if(hours >= 10 & minutes >= 10 & seconds < 10){
sprintf(OutString, "%d:%d:0%d", hours, minutes, seconds);
}
else{
sprintf(OutString, "%d:%d:%d", hours, minutes, seconds);
}
lcd_set_DDRAM_address(0x04); // Set DDRAM address to 4 (line 1)
// for loop increments through OutString and loads each character into DDRAM
for (index = 0; index < 8; index++){
lcd_wrt_data_nibble(OutString[index]); // Set DDRAM OutString[index]
}
}
}
// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
// if statement determines when to increment/reset hours, minutes, and seconds
if (totalseconds == 86400){ // 24hrs has passed, reset everything
hours = 0;
minutes = 0;
seconds = 0;
totalseconds = 1;
}
else if (totalseconds % 3600 == 0){ // 1hr has passed, increment hour, reset minutes and seconds
hours++;
minutes = 0;
seconds = 0;
totalseconds++;
}
else if (totalseconds % 60 == 0){ // 1min has passed, increment minute, reset seconds
minutes++;
seconds = 0;
totalseconds++;
}
else { // increment seconds
seconds++;
totalseconds++;
}
P1OUT ^= LED; // toggle P1.0
CCR0 += 32768; // extend timer by 1s
}
Learn More :
Display
- DISPLAY SOURCE CODE AS OUTPUT IN C PROGRAM
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program To Display The Number In A Specific Formats
- C Program that Display a IBM Logo
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- LED ON OFF For One Sec/Count and Display on the Attached Serial Monitor
- Pre Order, Post order, In order Implement Binary Tree using linked list
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C Program to accept n numbers & store all prime numbers in an array & display this result
- C program to display the transpose of given 3 X 3 matrix
- C Program to accept a string from user, delete all vowels from that string & display the result
- C Program To Create Two Singly Linked List and Perform Following Operation
- Create Two Singly Linked List Perform Differences Display It C Program
- C Program to Create, Display, Insert and Deletion of Queue Elements
- Creation and Display of Elements in Both Forward and Reverse Direction in a Doubly Linked List
- Program to Display Pie Chart Accepting User Input C Program
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
- Program to display the following pattern in C
- Display the Following Pattern * ** *** **** ***** C Program
HH%3AMM%3ASS
LCD Clock