04 基于 STM32 的时钟展示程序

前言

我们经常会看到 各个场合下面有 基于数码管 的时钟程序

比如 在车站, 教室, 办公室 等等 各个场合都有 

然后 这里就是做一个 简单的 时钟程序 展示程序 

 

 

测试用例

每一秒钟更新时间, 然后 迭代更新 天, 时, 分 等等 

然后 主流程 基于 天, 时分秒 渲染数码管 


#include "utils.h"

u8 emptyLedCols[8] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};

/**
 * main related
 */
long counter = 1;

long baseDay = 24;
long baseHour = 15;
long baseMinute = 30;
long baseSecond = 30;


/**
 * reset all
 */
void resetAll();

u32 genDateNumber();

void incrementSecond();


void main() {

    u32 dateNumber;

    resetAll();

    while (1) {

		dateNumber = genDateNumber();
        lightTubeByInt(dateNumber);
        // 计算和渲染貌似挺花时间, 这里 incrementSecond 的频率调整一下
        if(counter % 100 == 0) {
            incrementSecond();
        }

		delay_ms(5);
        counter++;

    }
}

void resetAll() {
    u8 i;
    printLedWord(8, emptyLedCols);
	BEEP = 1;
    for(i=0; i<8; i++) {
        ledCtl(i, 0);
    }
}

u32 genDateNumber() {
    u32 result = 0;
    result = baseDay;

    result = result * 100;
    result += baseHour;

    result = result * 100;
    result += baseMinute;

    result = result * 100;
    result += baseSecond;
    return result;
}

void incrementSecond() {
    baseSecond ++;
    if(baseSecond == 60) {
        baseSecond = 0;

        baseMinute++;
        if(baseMinute == 60) {
            baseMinute = 0;

            baseHour ++;
            if(baseHour == 24) {
                baseHour = 0;

                baseDay ++;
                if(baseDay == 31) {
                    baseDay = 1;
                }
            }
        }
    }
}


 

 

展示效果

当前状态 

 

下一秒

 

下一秒

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值