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;

	POINT_COLOR = BLUE;
	switch ( RTC_Get_Week( calendar_temp.w_year, calendar_temp.w_month, calendar_temp.w_date ) )
	{
		case 0:
			LCD_ShowString( 60, 180, 200, 16, 16, "Sunday   " );
			break;
		case 1:
			LCD_ShowString( 60, 180, 200, 16, 16, "Monday   " );
			break;
		case 2:
			LCD_ShowString( 60, 180, 200, 16, 16, "Tuesday  " );
			break;
		case 3:
			LCD_ShowString( 60, 180, 200, 16, 16, "Wednesday" );
			break;
		case 4:
			LCD_ShowString( 60, 180, 200, 16, 16, "Thursday " );
			break;
		case 5:
			LCD_ShowString( 60, 180, 200, 16, 16, "Friday   " );
			break;
		case 6:
			LCD_ShowString( 60, 180, 200, 16, 16, "Saturday " );
			break;
	}
}




参考图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

基于51单片机开发板,要实现利用DS18B20获取时间信息LCD1602上显示,包括年月日、星期以及时分秒信息,同时显示温度信息,且具有按键调节和光标闪烁功能,需要编写一系列的代码来完成这个项目。以下是一个简化的代码框架,用于指导你完成这个任务。请注意,这个代码只是一个示例,你可能需要根据你的硬件连接和实际需求进行调整。 ```c #include <reg51.h> // 包含51单片机寄存器定义 #include <intrins.h> // 包含一些内置函数定义,如nop() // DS18B20、LCD1602和按键的接口定义 // 请根据实际硬件连接情况定义以下宏 #define DS18B20_PIN ... #define LCD_RS ... #define LCD_RW ... #define LCD_EN ... #define LCD_DATA ... #define KEY_PIN ... // 函数声明 void SystemInit(); // 系统初始化 void DS18B20_Init(); // DS18B20初始化 float DS18B20_ReadTemperature(); // 读取温度 void LCD1602_Init(); // 初始化LCD1602 void LCD1602_WriteString(char x, char y, char *str); // 在LCD1602上写字符串 void Timer0_Init(); // 定时器初始化 void DisplayTimeAndTemperature(); // 显示时间和温度 void KeyScan(); // 按键扫描 // 全局变量 float temperature; // 温度变量 unsigned char year = 0; unsigned char month = 0; unsigned char day = 0; unsigned char week = 0; unsigned char hour = 0; unsigned char minute = 0; unsigned char second = 0; void main() { SystemInit(); // 系统初始化 Timer0_Init(); // 定时器初始化 DS18B20_Init(); // 初始化DS18B20 LCD1602_Init(); // 初始化LCD1602 while(1) { DisplayTimeAndTemperature(); // 显示时间和温度 KeyScan(); // 执行按键扫描 } } void SystemInit() { // 系统初始化代码 } void DS18B20_Init() { // DS18B20初始化代码 } float DS18B20_ReadTemperature() { // 读取温度代码 return temperature; } void LCD1602_Init() { // LCD1602初始化代码 } void LCD1602_WriteString(char x, char y, char *str) { // LCD1602写字符串代码 } void Timer0_Init() { // 定时器初始化代码 } void DisplayTimeAndTemperature() { // 显示时间和温度代码 } void KeyScan() { // 按键扫描代码 } ``` 这个代码框架涵盖了硬件初始化、DS18B20温度读取、LCD显示、定时器、按键扫描等基础功能。你需要填充具体的硬件操作代码,比如如何通过DS18B20读取温度、如何显示数据到LCD1602、如何处理按键输入等。此外,还需要设置定时器来维护时间和周期性地更新显示。光标闪烁的实现通常需要在LCD显示函数中增加切换显示和不显示字符的逻辑。
评论 41
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love丶伊卡洛斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值