如何在 powertip pc1602f b 16x2 LCD 显示模块上显示数字时钟?

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

我需要帮助在 powertip pc1602f b 16x2 LCD 显示模块上显示数字时钟,代码运行良好,但我需要能够 运行它是 atmel studio 7...我正在使用 STK300 AVR 板,时间显示在液晶屏上。该程序最好在 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;
}

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

从根本上说,您需要:

  • 一种可靠地测量时间的方法
  • 一种将输出发送到显示器的方法

休眠 1 秒然后在当前时间上加一秒不是记录时间的可靠方法。除非您使用的芯片包含实时时钟 (RTC),否则芯片的时钟肯定会出现一些错误,即使是很小的错误也会在几个小时或一天的过程中造成显着的漂移。如果这是一个需要准确的时钟,请寻找可以读取时间的 RTC 模块。

在LCD模块上显示信息一般涉及到根据显示器要求的协议向一些输出引脚写入数据。如果您需要自己编写所有代码,则必须深入研究显示的文档并找出需要完成的工作。你没有提到你用的是什么显示器,所以不可能知道它是否有I2C接口,并行接口等

AVR 平台非常受欢迎,有很多库支持时钟和显示器等附加模块。只要你不必自己编写所有代码,弄清楚你有什么显示器,然后 Google 寻找支持它的 AVR 库。

顺便说一句,RTC 也是如此——如果您使用时钟模块,则可能有一个库支持该模块并使其易于使用。然后你的代码中的循环看起来像:

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

这会给你一个更准确的时钟,因为 RTC 报告的时间不会像你的控制器那样受到漂移的影响。