STM32F103 實現 LCD顯示年月日時分秒星期 並可逐值修改的日期 小程序


@

前言

開發板:正點原子 STM32F103 精英版
語言:C語言
開發環境:Keil5
使用了 KEY LED LCD RTC
代碼下載碼雲 GitHub
在這里插入圖片描述

代碼參考:正點原子 源碼RTC實驗例程
功能介紹
1、LCD顯示年月日 時分秒 星期 信息。
2、KEY0 進入修改模式,分別可以修改 年月日時分秒(處於修改狀態下,對應值會變紅),最后退出修改模式。
3、在KEY0的修改模式下,按KEY1為數字+1,按KEY_UP為數字-1。

效果圖

在這里插入圖片描述

5M的動圖 給我畫質都壓成渣渣了

核心代碼

完整代碼下載碼雲 GitHub
main.c

#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "usmart.h"
#include "rtc.h"

/* 顯示時間,index特殊處理 */
void show_index_time( u8 index, _calendar_obj calendar_temp );

int main( void )
{
    /* 按鍵返回值 */
    u8 key = 0;
    /* 修改指向下標 */
    u8 index = 0;
    /* 日歷結構體 */
    _calendar_obj calendar_temp;
    /* 延時函數初始化 */
    delay_init();
    /* 設置中斷優先級分組為組2:2位搶占優先級,2位響應優先級 */
    NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2 );
    /* 串口初始化為115200 */
    uart_init( 115200 );
    /* LED端口初始化 */
    LED_Init();
    /* LCD初始化 */
    LCD_Init();
    /* 按鍵初始化 */
    KEY_Init();
    /* 初始化USMART */
    usmart_dev.init( SystemCoreClock / 1000000 );
    /* RTC初始化 */
    RTC_Init();
    /*
     * 顯示時間
     * 設置字體為藍色
     */
    POINT_COLOR = BLUE;
    LCD_ShowString( 60, 130, 200, 16, 16, "    -  -  " );
    LCD_ShowString( 60, 162, 200, 16, 16, "  :  :  " );
    calendar_temp = calendar;
    while ( 1 )
    {
        /* 根據index顯示不同日歷變量 */
        if ( 0 == index )
            show_index_time( index, calendar );
        else show_index_time( index, calendar_temp );
        /*
         * 鍵處理函數
         * 返回按鍵值
         * mode:0,不支持連續按;1,支持連續按;
         * 0,沒有任何按鍵按下
         * 1,KEY0按下
         * 2,KEY1按下
         * 3,KEY3按下 WK_UP
         */
        key = KEY_Scan( 0 );
        /* KEY0 進入修改模式,依次順序循環 */
        if ( 1 == key )
        {
            index++;
            index = index % 7;
            /* 進入修改 */
            if ( 1 == index )
            {
                calendar_temp = calendar;
            }
            /* 退出修改 */
            else if(0 == index)
            {
                calendar = calendar_temp;
                RTC_Set( calendar_temp.w_year, calendar_temp.w_month, calendar_temp.w_date, calendar_temp.hour, calendar_temp.min, calendar_temp.sec );
            }
        }
        /* KEY1 選中值+1 */
        else if ( 2 == key )
        {
            if ( 1 == index )
            {
                calendar_temp.w_year++;
            } else if ( 2 == index )
            {
                calendar_temp.w_month++;
                calendar_temp.w_month = calendar_temp.w_month > 12 ? 1 : calendar_temp.w_month;
            } else if ( 3 == index )
            {
                /* 判斷閏年 */
                if ( Is_Leap_Year( calendar_temp.w_year ) )
                {
                    calendar_temp.w_date++;
                    calendar_temp.w_date = calendar_temp.w_date > (calendar_temp.w_month == 2 ? (mon_table[calendar_temp.w_month - 1] + 1) : mon_table[calendar_temp.w_month - 1]) ? 1 : calendar_temp.w_date;
                } else {
                    calendar_temp.w_date++;
                    calendar_temp.w_date = calendar_temp.w_date > mon_table[calendar_temp.w_month - 1] ? 1 : calendar_temp.w_date;
                }
            } else if ( 4 == index )
            {
                calendar_temp.hour++;
                calendar_temp.hour = calendar_temp.hour > 23 ? 0 : calendar_temp.hour;
            } else if ( 5 == index )
            {
                calendar_temp.min++;
                calendar_temp.min = calendar_temp.min > 59 ? 0 : calendar_temp.min;
            } else if ( 6 == index )
            {
                calendar_temp.sec++;
                calendar_temp.sec = calendar_temp.sec > 59 ? 0 : calendar_temp.sec;
            }
        }
        /* KEY_UP 選中值-1 */
        else if ( 3 == key )
        {
            if ( 1 == index )
            {
                calendar_temp.w_year--;
                calendar_temp.w_year = calendar_temp.w_year > 0 ? calendar_temp.w_year : 0;
            } else if ( 2 == index )
            {
                calendar_temp.w_month--;
                calendar_temp.w_month = calendar_temp.w_month > 0 ? calendar_temp.w_month : 12;
            } else if ( 3 == index )
            {
                if ( Is_Leap_Year( calendar_temp.w_year ) )
                {
                    calendar_temp.w_date--;
                    calendar_temp.w_date = calendar_temp.w_date > 0 ? calendar_temp.w_date : (calendar_temp.w_month == 2 ? (mon_table[calendar_temp.w_month - 1] + 1) : mon_table[calendar_temp.w_month - 1]);
                } else {
                    calendar_temp.w_date--;
                    calendar_temp.w_date = calendar_temp.w_date > 0 ? calendar_temp.w_date : mon_table[calendar_temp.w_month - 1];
                }
            } else if ( 4 == index )
            {
                calendar_temp.hour = calendar_temp.hour == 0 ? 23 : calendar_temp.hour - 1;
            } else if ( 5 == index )
            {
                calendar_temp.min = calendar_temp.min == 0 ? 59 : calendar_temp.min - 1;
            } else if ( 6 == index )
            {
                calendar_temp.sec = calendar_temp.sec == 0 ? 59 : calendar_temp.sec - 1;
            }
        }
        delay_ms( 10 );
    }
}


/* 顯示時間,index特殊處理 */
void show_index_time( u8 index, _calendar_obj calendar_temp )
{
    POINT_COLOR = BLUE;
    if ( 1 == index )
        POINT_COLOR = RED;
    LCD_ShowNum( 60, 130, calendar_temp.w_year, 4, 16 );
    POINT_COLOR = BLUE;
    if ( 2 == index )
        POINT_COLOR = RED;
    LCD_ShowNum( 100, 130, calendar_temp.w_month, 2, 16 );
    POINT_COLOR = BLUE;
    if ( 3 == index )
        POINT_COLOR = RED;
    LCD_ShowNum( 124, 130, calendar_temp.w_date, 2, 16 );
    POINT_COLOR = BLUE;
    if ( 4 == index )
        POINT_COLOR = RED;
    LCD_ShowNum( 60, 162, calendar_temp.hour, 2, 16 );
    POINT_COLOR = BLUE;
    if ( 5 == index )
        POINT_COLOR = RED;
    LCD_ShowNum( 84, 162, calendar_temp.min, 2, 16 );
    POINT_COLOR = BLUE;
    if ( 6 == index )
        POINT_COLOR = RED;
    LCD_ShowNum( 108, 162, calendar_temp.sec, 2, 16 );
    POINT_COLOR = RED;
}

參考圖

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM