主函数刷新:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
key=scankey(); //扫按键
key_function(key); //执行按键功能
Interface_function(); //刷新界面显示
Alarm_function(); //执行闹钟功能
}
按键模块:
/*
函数功能:执行按键功能
入参:key:按键值
返回值:无
*/
void key_function(uint8_t key)
{
if(key==1) //B1
{
if(flag_Interface==0) //在时钟显示界面下,进入时钟设置界面
{
sprintf((char *)Line1_buf," RTC-SETTING ");
LCD_DisplayStringLine(Line1,Line1_buf);
__HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); //失能写保护
RTC->CR&=~(1<<8); //失能闹钟
__HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); //使能写保护
LED_state&=~(1<<1); //点亮LD2
LED_state|=(1<<0); //熄灭LD1
LED_ALL(LED_state); //改变LED状态
flag_Interface=1; //界面标志位改为时钟设置界面
flag_set_time=0; //时间设置变量小时
}
else if(flag_Interface==1) //在时钟设置界面下,返回时钟显示界面
{
set_time(GetTime.Hours,GetTime.Minutes,GetTime.Seconds); //设置时间
__HAL_RTC_WRITEPROTECTION_DISABLE(hrtc); //失能写保护
RTC->CR|=(1<<8); //使能闹钟
__HAL_RTC_WRITEPROTECTION_ENABLE(hrtc); //使能写保护
sprintf((char *)Line1_buf," MAIN ");
LCD_DisplayStringLine(Line1,Line1_buf);
USART_RTC_buf[8]=GetTime.Hours/10+48; USART_RTC_buf[9]=GetTime.Hours%10+48;
USART_RTC_buf[11]=GetTime.Minutes/10+48; USART_RTC_buf[12]=GetTime.Minutes%10+48;
USART_RTC_buf[14]=GetTime.Seconds/10+48; USART_RTC_buf[15]=GetTime.Seconds%10+48;
HAL_UART_Transmit(&huart1,USART_RTC_buf,sizeof(USART_RTC_buf),50); //串口发送设置时间
X24c02_buf[1]++; //时间设置次数加1
m24c02_write(X24c02_buf+1,DATA_ARR+1,1);HAL_Delay(5); //时间设置次数存入EEPROM
LED_state|=(1<<1); //熄灭LD2
LED_ALL(LED_state); //改变LED状态
flag_Interface=0; //界面标志位改为时钟显示界面
}
else if(flag_Interface==2) {if(++flag_set_time==3) {flag_set_time=0;}} //报警设置界面下,切换时分秒
}
else if(key==2) //B2
{
if(flag_Interface==0) //在时钟显示界面下,进入报警设置界面
{
sprintf((char *)Line1_buf," ALARM-SETTING ");
Line3_buf[2]='A';Line3_buf[3]='l';Line3_buf[4]='a';
Line3_buf[5]='r';Line3_buf[6]='m';
LCD_DisplayStringLine(Line1,Line1_buf); //刷新LCD第一行显示
LCD_DisplayStringLine2(Line3,2,Line3_buf+2,5); //刷新LCD第三行显示
flag_Interface=2;flag_set_time=0; //界面标志位改为报警设置界面
LED_state&=~(1<<2); //点亮LD3
LED_state|=(1<<0); //熄灭LD1
LED_ALL(LED_state); //改变LED状态
}
else if(flag_Interface==2) //在报警设置界面下,返回时钟显示界面
{
set_alarm(time_alarm_clock[0],time_alarm_clock[1],time_alarm_clock[2]); //设置闹钟
sprintf((char *)Line1_buf," MAIN ");
Line3_buf[2]=' ';Line3_buf[3]=' ';Line3_buf[4]='R';
Line3_buf[5]='T';Line3_buf[6]='C';
LCD_DisplayStringLine(Line1,Line1_buf); //刷新LCD第一行显示
LCD_DisplayStringLine2(Line3,2,Line3_buf+2,5); //刷新LCD第三行显示
USART_Alarm_buf[10]=time_alarm_clock[0]/10+48; USART_Alarm_buf[11]=time_alarm_clock[0]%10+48;
USART_Alarm_buf[13]=time_alarm_clock[1]/10+48; USART_Alarm_buf[14]=time_alarm_clock[1]%10+48;
USART_Alarm_buf[16]=time_alarm_clock[2]/10+48; USART_Alarm_buf[17]=time_alarm_clock[2]%10+48;
HAL_UART_Transmit(&huart1,USART_Alarm_buf,sizeof(USART_Alarm_buf),50); //串口发送报警时间
LED_state|=(1<<2); //熄灭LD3
LED_ALL(LED_state); //改变LED状态
X24c02_buf[2]++; //报警设置次数加1
X24c02_buf[3]=time_alarm_clock[0]; //报警 时
X24c02_buf[4]=time_alarm_clock[1]; //报警 分
X24c02_buf[5]=time_alarm_clock[2]; //报警 秒
m24c02_write(X24c02_buf+2,DATA_ARR+2,4);HAL_Delay(5); //报警设置次数与时间存入EEPROM
flag_Interface=0; //界面标志位改为时钟显示界面
}
else if(flag_Interface==1) {if(++flag_set_time==3) {flag_set_time=0;}} //时钟设置界面下,切换时分秒
}
else if(key==3) //B3
{
if(flag_Interface==1) //在时钟设置界面下,对时分秒进行加1
{
if(flag_set_time==0) {if(++GetTime.Hours>=24) {GetTime.Hours=0;}} //时
else if(flag_set_time==1) {if(++GetTime.Minutes>=60) {GetTime.Minutes=0;}} //分
else if(flag_set_time==2) {if(++GetTime.Seconds>=60) {GetTime.Seconds=0;}} //秒
}
else if(flag_Interface==2) //在报警设置界面下,对时分秒进行加1
{
if(flag_set_time==0) { if(++time_alarm_clock[flag_set_time]>=24) {time_alarm_clock[flag_set_time]=0;} } //时
else { if(++time_alarm_clock[flag_set_time]>=60) {time_alarm_clock[flag_set_time]=0;} } //分,秒
}
}
else if(key==4) //B4
{
if(flag_Interface==1) //在时钟设置界面下,对时分秒进行减1
{
if(flag_set_time==0) {if(--GetTime.Hours>=24) {GetTime.Hours=23;}} //时
else if(flag_set_time==1) {if(--GetTime.Minutes>=60) {GetTime.Minutes=59;}} //分
else if(flag_set_time==2) {if(--GetTime.Seconds>=60) {GetTime.Seconds=59;}} //秒
}
else if(flag_Interface==2) //在报警设置界面下,对时分秒进行减1
{
if(flag_set_time==0) { if(--time_alarm_clock[flag_set_time]>=24) {time_alarm_clock[flag_set_time]=23;} } //时
else { if(--time_alarm_clock[flag_set_time]>=60) {time_alarm_clock[flag_set_time]=59;} } //分,秒
}
}
}
LCD模块:
/*
函数功能:执行不同界面不同功能
入参:无
返回值:无
*/
void Interface_function(void)
{
static u8 last_time; //RTC上次时间
if(get_sub(TASK_TIMES.Interface_TIME)==0)
{
TASK_TIMES.Interface_TIME=get_time()+Interface_time;
if(flag_Interface==0) //时钟显示界面
{
HAL_RTC_GetTime(&hrtc,&GetTime,RTC_FORMAT_BIN); //获取RTC时间
HAL_RTC_GetDate(&hrtc,&GetDate,RTC_FORMAT_BIN); //获取RTC日期
Line3_buf[8]=GetTime.Hours/10+48; Line3_buf[9]=GetTime.Hours%10+48; //时
Line3_buf[11]=GetTime.Minutes/10+48; Line3_buf[12]=GetTime.Minutes%10+48; //分
Line3_buf[14]=GetTime.Seconds/10+48; Line3_buf[15]=GetTime.Seconds%10+48; //秒
}
else if(flag_Interface==1) //时钟设置界面
{
Line3_buf[8]=GetTime.Hours/10+48; Line3_buf[9]=GetTime.Hours%10+48;
Line3_buf[11]=GetTime.Minutes/10+48; Line3_buf[12]=GetTime.Minutes%10+48;
Line3_buf[14]=GetTime.Seconds/10+48; Line3_buf[15]=GetTime.Seconds%10+48;
}
else if(flag_Interface==2) //报警设置界面
{
Line3_buf[8]=time_alarm_clock[0]/10+48; Line3_buf[9]=time_alarm_clock[0]%10+48;
Line3_buf[11]=time_alarm_clock[1]/10+48; Line3_buf[12]=time_alarm_clock[1]%10+48;
Line3_buf[14]=time_alarm_clock[2]/10+48; Line3_buf[15]=time_alarm_clock[2]%10+48;
}
if((flag_Interface!=0)&&(flag_set_time==0)) {LCD_SetTextColor(Red);LCD_DisplayStringLine2(Line3,8,Line3_buf+8,2);LCD_SetTextColor(White); }
else { LCD_DisplayStringLine2(Line3,8,Line3_buf+8,2); } //时
if((flag_Interface!=0)&&(flag_set_time==1)) {LCD_SetTextColor(Red);LCD_DisplayStringLine2(Line3,11,Line3_buf+11,2);LCD_SetTextColor(White); }
else { LCD_DisplayStringLine2(Line3,11,Line3_buf+11,2); } //分
if((flag_Interface!=0)&&(flag_set_time==2)) {LCD_SetTextColor(Red);LCD_DisplayStringLine2(Line3,14,Line3_buf+14,2);LCD_SetTextColor(White); }
else { LCD_DisplayStringLine2(Line3,14,Line3_buf+14,2); } //秒
if(flag_Interface==0) //在时钟显示界面下,LD1每秒闪烁
{
if(last_time!=Line3_buf[15])
{
LED_state^=(1<<0);
LED_ALL(LED_state);
last_time=Line3_buf[15];
}
}
}
}
闹钟模块:
/*
函数功能:执行闹钟功能
入参:无
返回值:无
*/
void Alarm_function(void)
{
if(flag_sAlarm==1)
{
TASK_TIMES.sAlarm_TIME=get_time()+sAlarm_time;
flag_sAlarm=2;
LED_state^=(1<<7);
LED_ALL(LED_state); //点亮LD8
}
else if(flag_sAlarm==2)
{
if(get_sub(TASK_TIMES.sAlarm_TIME)==0) //5秒
{
LED_state^=(1<<7);
LED_ALL(LED_state); //熄灭LD8
flag_sAlarm=0; //闹钟标志位清零
}
}
}
下载链接:
https://pan.baidu.com/s/1LMECSy3GuABduu42YUJdSQ
提取码:qy2o