Search code examples
catmelstudio

How can i display a digital clock on a powertip pc1602f b 16x2 LCD display module?


I kindly need help with displaying a digital clock on a powertip pc1602f b 16x2 LCD display module, the code works well but I need to be able to run it atmel studio 7... I am using STK300 AVR Board and the time displayed on the lcd screen. The program should ideally be in c/embedded c

/*
 * A1.c
 *
 * Created: 19/10/2018 11:51:29
 * Author : mk3101f
 */ 


#include <stdio.h>
#include <time.h> //for sleep() function

int main(void)
{
    int hour, minute, second;

    hour=minute=second=0;

    while (1) 
    {
        //clear output screen
        system("clear");

        //print time in HH : MM : SS format
        printf("%02d : %02d : %02d ",hour,minute,second);

        //clear output buffer in gcc
        fflush(stdout);

        //increase second
        second++;

        //update hour, minute and second
        if(second==60){
            minute+=1;
            second=0;
        }
        if(minute==60){
            hour+=1;
            minute=0;
        }
        if(hour==24){
            hour=0;
            minute=0;
            second=0;
        }

    sleep(1);   //wait till 1 second
    }

  return 0;
}

Solution

  • I need to be able to run it atmel studio 7... I am using STK300 AVR Board

    Fundamentally, you need:

    • a way to measure time reliably
    • a way to send output to the display

    Sleeping for 1 second and then adding a second to the current time is not a reliable way to keep track of time. Unless the chip you're using includes a real time clock (RTC), there's bound to be some error in the chip's clock, and even small errors will cause significant drift over the course of a few hours or a day. If this is a clock that needs to be accurate, look for a RTC module that you can read the time from.

    Displaying information on an LCD module generally involves writing data to some output pins according to the protocol that the display requires. If you need to write all the code yourself, you'll have to dig into the documentation for the display and find out what needs to be done. You haven't mentioned what display you're using, so it's impossible to know whether it has an I2C interface, a parallel interface, etc.

    The AVR platform is incredibly popular, and there are many, many libraries available for it that support add-on modules like clocks and displays. As long as you don't have to write all the code yourself, figure out what display you have and then Google for an AVR library that supports it.

    The same goes for the RTC, by the way -- if you use a clock module, there's likely a library that supports that module and makes it easy to use. Then the loop in your code will look something like:

    while (true) {
        time = read_the_clock();
        write_time_to_display(time);
        sleep(1);
    }
    

    That'll give you a much more accurate clock, since the time reported by the RTC won't suffer from the kind of drift that your controller will.