C Program to Display a real time clock (HH:MM:SS) on the LCD

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 :