之前程序(原始程序来自于按键矩阵基础实验)
main函数
#include "led1.h"
#include "system.h"
#include "SysTick.h"
#include "key.h"
#include "smg.h"
#include "beep.h"
#include "rtc.h"
#include "usart.h"
int main()
{
//u8 data=0x01;
u8 i=0;
//u8 j=0;
ST_Init(72);
LED_Init();
KEY_Matrix_Init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //????????2?
USART1_Init(115200);
BEEP_Init();
RTC_Init();
SMG_Init();
KEY_Init();
while(1)
{Keychoose();
smgshow();
if(ALRflag)
{
if((calendar.sec-tim)==5)//ÄÖÖÓÏìÎåÃë
{
ALRflag=0;
}
BEEP_Alarm(50,255);
}
if(i%100==0)
LED0=!LED0;
delay_ms(1);
}
}
rtc函数
#include "rtc.h"
#include "SysTick.h"
#include "usart.h"
_calendar calendar;//ʱÖӽṹÌå
TypeTime MyTime;
int ALRflag;//ÄÖÖÓ·äÃùÆ÷Ïì±ê־λ
static void RTC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; //RTCÈ«¾ÖÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÏÈÕ¼ÓÅÏȼ¶1λ,´ÓÓÅÏȼ¶3λ
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //ÏÈÕ¼ÓÅÏȼ¶0λ,´ÓÓÅÏȼ¶4λ
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄܸÃͨµÀÖжÏ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷
}
u8 Is_Leap_Year(u16 year)
{
if(year%4==0) //±ØÐëÄܱ»4Õû³ý
{
if(year%100==0)
{
if(year%400==0)return 1;//Èç¹ûÒÔ00½áβ,»¹ÒªÄܱ»400Õû³ý
else return 0;
}else return 1;
}else return 0;
}
//Ô·ÝÊý¾Ý±í
u8 const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //ÔÂÐÞÕýÊý¾Ý±í
//ƽÄêµÄÔ·ÝÈÕÆÚ±í
const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};
u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
{
u16 t;
u32 seccount=0;
if(syear<1970||syear>2099)return 1;
for(t=1970;t<syear;t++) //°ÑËùÓÐÄê·ÝµÄÃëÖÓÏà¼Ó
{
if(Is_Leap_Year(t))seccount+=31622400;//ÈòÄêµÄÃëÖÓÊý
else seccount+=31536000; //ƽÄêµÄÃëÖÓÊý
}
smon-=1;
for(t=0;t<smon;t++) //°ÑÇ°ÃæÔ·ݵÄÃëÖÓÊýÏà¼Ó
{
seccount+=(u32)mon_table[t]*86400;//Ô·ÝÃëÖÓÊýÏà¼Ó
if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//ÈòÄê2Ô·ÝÔö¼ÓÒ»ÌìµÄÃëÖÓÊý
}
seccount+=(u32)(sday-1)*86400;//°ÑÇ°ÃæÈÕÆÚµÄÃëÖÓÊýÏà¼Ó
seccount+=(u32)hour*3600;//СʱÃëÖÓÊý
seccount+=(u32)min*60; //·ÖÖÓÃëÖÓÊý
seccount+=sec;//×îºóµÄÃëÖÓ¼ÓÉÏÈ¥
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //ʹÄÜPWRºÍBKPÍâÉèʱÖÓ
PWR_BackupAccessCmd(ENABLE); //ʹÄÜRTCºÍºó±¸¼Ä´æÆ÷·ÃÎÊ
RTC_SetCounter(seccount); //ÉèÖÃRTC¼ÆÊýÆ÷µÄÖµ
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
return 0;
}
//³õʼ»¯ÄÖÖÓ
//ÒÔ1970Äê1ÔÂ1ÈÕΪ»ù×¼
//1970~2099ÄêΪºÏ·¨Äê·Ý
//syear,smon,sday,hour,min,sec£ºÄÖÖÓµÄÄêÔÂÈÕʱ·ÖÃë
//·µ»ØÖµ:0,³É¹¦;ÆäËû:´íÎó´úÂë.
u8 RTC_Alarm_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
{
u16 t;
u32 seccount=0;
if(syear<1970||syear>2099)return 1;
for(t=1970;t<syear;t++) //°ÑËùÓÐÄê·ÝµÄÃëÖÓÏà¼Ó
{
if(Is_Leap_Year(t))seccount+=31622400;//ÈòÄêµÄÃëÖÓÊý
else seccount+=31536000; //ƽÄêµÄÃëÖÓÊý
}
smon-=1;
for(t=0;t<smon;t++) //°ÑÇ°ÃæÔ·ݵÄÃëÖÓÊýÏà¼Ó
{
seccount+=(u32)mon_table[t]*86400;//Ô·ÝÃëÖÓÊýÏà¼Ó
if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//ÈòÄê2Ô·ÝÔö¼ÓÒ»ÌìµÄÃëÖÓÊý
}
seccount+=(u32)(sday-1)*86400;//°ÑÇ°ÃæÈÕÆÚµÄÃëÖÓÊýÏà¼Ó
seccount+=(u32)hour*3600;//СʱÃëÖÓÊý
seccount+=(u32)min*60; //·ÖÖÓÃëÖÓÊý
seccount+=sec;//×îºóµÄÃëÖÓ¼ÓÉÏÈ¥
//ÉèÖÃʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //ʹÄÜPWRºÍBKPÍâÉèʱÖÓ
PWR_BackupAccessCmd(ENABLE); //ʹÄܺ󱸼ĴæÆ÷·ÃÎÊ
//ÉÏÃæÈý²½ÊDZØÐëµÄ!
RTC_SetAlarm(seccount);
RTC_WaitForLastTask(); //µÈ´ý×î½üÒ»´Î¶ÔRTC¼Ä´æÆ÷µÄд²Ù×÷Íê³É
return 0;
}
_calendar calendar;
u8 RTC_Get(void)
{
static u16 daycnt=0;
u32 timecount=0;
u32 temp=0;
u16 temp1=0;
timecount=RTC_GetCounter();
temp=timecount/86400; //µÃµ½ÌìÊý(ÃëÖÓÊý¶ÔÓ¦µÄ)
if(daycnt!=temp)//³¬¹ýÒ»ÌìÁË
{
daycnt=temp;
temp1=1970; //´Ó1970Ä꿪ʼ
while(temp>=365)
{
if(Is_Leap_Year(temp1))//ÊÇÈòÄê
{
if(temp>=366)temp-=366;//ÈòÄêµÄÃëÖÓÊý
else {temp1++;break;}
}
else temp-=365; //ƽÄê
temp1++;
}
calendar.w_year=temp1;//µÃµ½Äê·Ý
temp1=0;
while(temp>=28)//³¬¹ýÁËÒ»¸öÔÂ
{
if(Is_Leap_Year(calendar.w_year)&&temp1==1)//µ±ÄêÊDz»ÊÇÈòÄê/2Ô·Ý
{
if(temp>=29)temp-=29;//ÈòÄêµÄÃëÖÓÊý
else break;
}
else
{
if(temp>=mon_table[temp1])temp-=mon_table[temp1];//ƽÄê
else break;
}
temp1++;
}
calendar.w_month=temp1+1; //µÃµ½Ô·Ý
calendar.w_date=temp+1; //µÃµ½ÈÕÆÚ
}
temp=timecount%86400; //µÃµ½ÃëÖÓÊý
calendar.hour=temp/3600; //Сʱ
calendar.min=(temp%3600)/60; //·ÖÖÓ
calendar.sec=(temp%3600)%60; //ÃëÖÓ
calendar.week=RTC_Get_Week(calendar.w_year,calendar.w_month,calendar.w_date);//»ñÈ¡ÐÇÆÚ
return 0;
}
u8 RTC_Init(void)
{
u8 temp=0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP|RCC_APB1Periph_PWR,ENABLE);
PWR_BackupAccessCmd(ENABLE);
if(BKP_ReadBackupRegister(BKP_DR1)!=0xa0a0)
{
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET&& temp<250)
{
temp++;
delay_ms(10);
}
if(temp>=250)return 1;
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForLastTask();
RTC_WaitForSynchro();
RTC_ITConfig(RTC_IT_SEC,ENABLE);
RTC_WaitForLastTask();
RTC_EnterConfigMode();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
RTC_Set(2023,8,18,17,34,55);
RTC_ExitConfigMode();
BKP_WriteBackupRegister(BKP_DR1,0xa0a0);
}
else
{
RTC_WaitForSynchro();
RTC_ITConfig(RTC_IT_SEC,ENABLE);
RTC_WaitForLastTask();
}
RTC_NVIC_Config();
RTC_Get();
return 0;
}
//²ÌÀÕ¹«Ê½W=[C/4]-2C+y+[y/4]+[26(m+1)/10]+d-1 £¨ÆäÖÐ[ ]ΪȡÕû·ûºÅ£©
//»ñµÃÏÖÔÚÊÇÐÇÆÚ¼¸
//¹¦ÄÜÃèÊö:ÊäÈ빫ÀúÈÕÆڵõ½ÐÇÆÚ(Ö»ÔÊÐí1901-2099Äê)
//ÊäÈë²ÎÊý£º¹«ÀúÄêÔÂÈÕ
//·µ»ØÖµ£ºÐÇÆÚºÅ
u8 RTC_Get_Week(u16 year,u8 month,u8 day)
{
u8 yearH,yearL;
u8 week=0;
yearH=year/100;//»ñÈ¡µ½¹«ÔªÄê·ÝÇ°2λÊý
yearL=year%100;//»ñÈ¡µ½¹«ÔªÄê·Ýºó2λÊý
//ËùÇóµÄÔ·ÝÈç¹ûÊÇ1Ô»ò2ÔÂ,ÔòÓ¦ÊÓΪǰһÄêµÄ13Ô»ò14ÔÂ
if(month<3)
{
yearL-=1;
if(month==1)
month=13;
else if(month==2)
month=14;
}
week=yearH/4-2*yearH+yearL+yearL/4+(26*(month+1))/10+day-1;
return week%7;
}
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)//ÃëÖÓÖжÏ
{
RTC_Get();
printf("RTC Time:%d-%d-%d %d:%d:%d\r\n",calendar.w_year,calendar.w_month,calendar.w_date,calendar.hour,calendar.min,calendar.sec);//Êä³öÄÖÁåʱ¼ä
printf("Week:%d\r\n",calendar.week);
}
if(RTC_GetITStatus(RTC_IT_ALR)!= RESET)//ÄÖÖÓÖжÏ
{
RTC_ClearITPendingBit(RTC_IT_ALR); //ÇåÄÖÖÓÖжÏ
RTC_Get(); //¸üÐÂʱ¼ä
printf("Alarm Time:%d-%d-%d %d:%d:%d\r\n",calendar.w_year,calendar.w_month,calendar.w_date,calendar.hour,calendar.min,calendar.sec);//Êä³öÄÖÁåʱ¼ä
}
RTC_ClearITPendingBit(RTC_IT_SEC|RTC_IT_OW); //ÇåÄÖÖÓÖжÏ
RTC_WaitForLastTask();
}
key函数1
#include "key.h"
#include "SysTick.h"
#include "rtc.h"
#include "usart.h"
#include "stdio.h"
#include "smg.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //???????
RCC_APB2PeriphClockCmd(KEY1_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY2_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY3_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY4_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE);
GPIO_InitStructure.GPIO_Pin=KEY1_PIN;//???????IO?
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//????
GPIO_Init(KEY1_PORT,&GPIO_InitStructure);/* ???GPIO */
GPIO_InitStructure.GPIO_Pin=KEY2_PIN;
GPIO_Init(KEY2_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY3_PIN;
GPIO_Init(KEY3_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY4_PIN;
GPIO_Init(KEY4_PORT,&GPIO_InitStructure);
}
u8 KEY_Scan(u8 mode)
{
static u8 key=1;
if(mode==1) //??????
key=1;
if(key==1&&(KEY1==0||KEY2==0||KEY3==0||KEY4==0)) //????????
{
delay_ms(10); //??
key=0;
if(KEY1==0)
return KEY1_PRESS;
else if(KEY2==0)
return KEY2_PRESS;
else if(KEY3==0)
return KEY3_PRESS;
else if(KEY4==0)
return KEY4_PRESS;
}
else if(KEY1==1&&KEY2==1&&KEY3==1&&KEY4==1) //?????
key=1;
return 0;
}
void KEY_Matrix_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //???????
RCC_APB2PeriphClockCmd(KEY_MATRIX_H1_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY_MATRIX_H2_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY_MATRIX_H3_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY_MATRIX_H4_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY_MATRIX_L1_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY_MATRIX_L2_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY_MATRIX_L3_PORT_RCC,ENABLE);
RCC_APB2PeriphClockCmd(KEY_MATRIX_L4_PORT_RCC,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin=KEY_MATRIX_L1_PIN;
GPIO_Init(KEY_MATRIX_L1_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_MATRIX_L2_PIN;
GPIO_Init(KEY_MATRIX_L2_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_MATRIX_L3_PIN;
GPIO_Init(KEY_MATRIX_L3_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_MATRIX_L4_PIN;
GPIO_Init(KEY_MATRIX_L4_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz ;
GPIO_InitStructure.GPIO_Pin=KEY_MATRIX_H1_PIN;
GPIO_Init(KEY_MATRIX_H1_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_MATRIX_H2_PIN;
GPIO_Init(KEY_MATRIX_H2_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_MATRIX_H3_PIN;
GPIO_Init(KEY_MATRIX_H3_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_MATRIX_H4_PIN;
GPIO_Init(KEY_MATRIX_H4_PORT,&GPIO_InitStructure);
}
u8 KEY_Matrix_Scan(void)
{
u8 col1,col2,col3,col4;
u8 key_value=0;
GPIO_SetBits(KEY_MATRIX_H1_PORT,KEY_MATRIX_H1_PIN);
GPIO_SetBits(KEY_MATRIX_H2_PORT,KEY_MATRIX_H2_PIN);
GPIO_SetBits(KEY_MATRIX_H3_PORT,KEY_MATRIX_H3_PIN);
GPIO_SetBits(KEY_MATRIX_H4_PORT,KEY_MATRIX_H4_PIN);
if((GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN))==0)
return 0; //??X1?X4??????????
else
{
delay_ms(10);
if((GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN))==0)
return 0;
}
GPIO_SetBits(KEY_MATRIX_H1_PORT,KEY_MATRIX_H1_PIN);
GPIO_ResetBits(KEY_MATRIX_H2_PORT,KEY_MATRIX_H2_PIN);
GPIO_ResetBits(KEY_MATRIX_H3_PORT,KEY_MATRIX_H3_PIN);
GPIO_ResetBits(KEY_MATRIX_H4_PORT,KEY_MATRIX_H4_PIN);
col1=GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN);
col2=GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN);
col3=GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN);
col4=GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN);
if(col1==1&&col2==0&&col3==0&&col4==0)
key_value=1;
if(col1==0&&col2==1&&col3==0&&col4==0)
key_value=2;
if(col1==0&&col2==0&&col3==1&&col4==0)
key_value=3;
if(col1==0&&col2==0&&col3==0&&col4==1)
key_value=4;
while((GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN))>0);
GPIO_ResetBits(KEY_MATRIX_H1_PORT,KEY_MATRIX_H1_PIN);
GPIO_SetBits(KEY_MATRIX_H2_PORT,KEY_MATRIX_H2_PIN);
GPIO_ResetBits(KEY_MATRIX_H3_PORT,KEY_MATRIX_H3_PIN);
GPIO_ResetBits(KEY_MATRIX_H4_PORT,KEY_MATRIX_H4_PIN);
col1=GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN);
col2=GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN);
col3=GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN);
col4=GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN);
if(col1==1&&col2==0&&col3==0&&col4==0)
key_value=5;
if(col1==0&&col2==1&&col3==0&&col4==0)
key_value=6;
if(col1==0&&col2==0&&col3==1&&col4==0)
key_value=7;
if(col1==0&&col2==0&&col3==0&&col4==1)
key_value=8;
while((GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN))>0);
GPIO_ResetBits(KEY_MATRIX_H1_PORT,KEY_MATRIX_H1_PIN);
GPIO_ResetBits(KEY_MATRIX_H2_PORT,KEY_MATRIX_H2_PIN);
GPIO_SetBits(KEY_MATRIX_H3_PORT,KEY_MATRIX_H3_PIN);
GPIO_ResetBits(KEY_MATRIX_H4_PORT,KEY_MATRIX_H4_PIN);
col1=GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN);
col2=GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN);
col3=GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN);
col4=GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN);
if(col1==1&&col2==0&&col3==0&&col4==0)
key_value=9;
if(col1==0&&col2==1&&col3==0&&col4==0)
key_value=10;
if(col1==0&&col2==0&&col3==1&&col4==0)
key_value=11;
if(col1==0&&col2==0&&col3==0&&col4==1)
key_value=12;
while((GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN))>0);
GPIO_ResetBits(KEY_MATRIX_H1_PORT,KEY_MATRIX_H1_PIN);
GPIO_ResetBits(KEY_MATRIX_H2_PORT,KEY_MATRIX_H2_PIN);
GPIO_ResetBits(KEY_MATRIX_H3_PORT,KEY_MATRIX_H3_PIN);
GPIO_SetBits(KEY_MATRIX_H4_PORT,KEY_MATRIX_H4_PIN);
col1=GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN);
col2=GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN);
col3=GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN);
col4=GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN);
if(col1==1&&col2==0&&col3==0&&col4==0)
key_value=13;
if(col1==0&&col2==1&&col3==0&&col4==0)
key_value=14;
if(col1==0&&col2==0&&col3==1&&col4==0)
key_value=15;
if(col1==0&&col2==0&&col3==0&&col4==1)
key_value=16;
while((GPIO_ReadInputDataBit(KEY_MATRIX_L1_PORT,KEY_MATRIX_L1_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L2_PORT,KEY_MATRIX_L2_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L3_PORT,KEY_MATRIX_L3_PIN)|
GPIO_ReadInputDataBit(KEY_MATRIX_L4_PORT,KEY_MATRIX_L4_PIN))>0);
return key_value;
}
u16 tim;
TypeFlag Myflag;
加入按键控制选择Keychoose();
void Keychoose(void)
{
u8 key=0;
u8 keynum=0;
key=KEY_Matrix_Scan();
keynum=KEY_Scan(0);
switch(key)
{
case 1: Myflag.Bitflag=0;break;//Ñ¡ÔñµÚÒ»¸öλ
case 2:Myflag.Bitflag=1;break;
case 3:Myflag.Bitflag=2;break;
case 4:Myflag.Bitflag=3;break;
case 5:Myflag.Bitflag=4;break;
case 6:Myflag.Bitflag=5;break;
case 7:Myflag.Bitflag=6;break;
case 8:Myflag.Bitflag=7;break;
case 9: break;
case 10:break;
case 11:break;
case 12:break;
case 13:Myflag.Timeflag=0;break;//Õý³£Ê±¼ä½çÃæ(S13)
case 14:Myflag.Timeflag=1;break;//ÉèÖÃʱ¼ä½çÃæ(S14)
case 15:Myflag.Timeflag=2;break;//ÄÖÖÓʱ¼ä½çÃæ(S15)
case 16:Myflag.Dateflag=!Myflag.Dateflag;
break;//Ñ¡ÔñÏÔʾʱ·ÖÃë½çÃæ»òÕßÄêÔÂÈÕ½çÃæ(S16)
}
switch(keynum)/*Ëĸö¶ÀÁ¢°´¼üµÄÅжÏ*/
{
case KEY1_PRESS://ÉèÖÃʱ¼ä++(K1)
Myflag.addadd=1;
break;
case KEY2_PRESS://ÉèÖÃʱ¼ä--(K2)
Myflag.addadd=2;
break;
case KEY3_PRESS://(K3)
if(Myflag.Bitflag<=2)
{
Myflag.Bitflag=7;
}
else
{
Myflag.Bitflag-=3;
}
break;
case KEY4_PRESS://È·ÈÏÉèÖÃʱ¼ä(K4)
if(Myflag.Timeflag==1)
{
RTC_Set(MyTime.w_year,MyTime.w_month,MyTime.w_date,MyTime.hour,MyTime.min,MyTime.sec); //ÉèÖÃʱ¼ä
}
else if(Myflag.Timeflag==2)
{
RTC_Alarm_Set(MyTime.w_year,MyTime.w_month,MyTime.w_date,MyTime.hour,MyTime.min,MyTime.sec);//ÉèÖÃÄÖÖÓ
tim=MyTime.sec;
}
Myflag.Timeflag=0;
break;
}
}
加入数码管显示函数
共有6种情况,即有三个状态(正常显示,设置时间,设置闹钟),每一个有两个部分,年月日和时分秒。所以共有6种情况。
void smgshow(void)
{
int i=0;
if(Myflag.Timeflag==0)
{add();
MyTime.hour=calendar.hour;
MyTime.min=calendar.min;
MyTime.sec=calendar.sec;
MyTime.w_year=calendar.w_year;
MyTime.w_month=calendar.w_month;
MyTime.w_date=calendar.w_date;
if(Myflag.Dateflag==1)
{
buf[0]=gsmg_code[calendar.w_year/1000];
buf[1]=gsmg_code[calendar.w_year/100%10];
buf[2]=gsmg_code[calendar.w_year/10%10];
buf[3]=gsmg_code[calendar.w_year%10];
buf[4]=gsmg_code[calendar.w_month/10];
buf[5]=gsmg_code[calendar.w_month%10];
buf[6]=gsmg_code[calendar.w_date/10];
buf[7]=gsmg_code[calendar.w_date%10];
}
else
{ buf[0]=gsmg_code[calendar.hour/10];
buf[1]=gsmg_code[calendar.hour%10];
buf[2]=0x40;
buf[3]=gsmg_code[calendar.min/10];
buf[4]=gsmg_code[calendar.min%10];
buf[5]=0x40;
buf[6]=gsmg_code[calendar.sec/10];
buf[7]=gsmg_code[calendar.sec%10];
}
}
else if(Myflag.Timeflag==1)
{
//jiahs
MyTime.shi=MyTime.hour;
MyTime.fen=MyTime.min;
MyTime.miao=MyTime.sec;
MyTime.nian=MyTime.w_year;
MyTime.yue=MyTime.w_month;
MyTime.ri=MyTime.w_date;
if(Myflag.Dateflag==1)
{
if(i>=DELAY)
{
buf[0]=gsmg_code[MyTime.nian/1000]|(1<<7);
buf[1]=gsmg_code[MyTime.nian/100%10]|(1<<7);
buf[2]=gsmg_code[MyTime.nian/10%10]|(1<<7);
buf[3]=gsmg_code[MyTime.nian%10]|(1<<7);
buf[4]=gsmg_code[MyTime.yue/10]|(1<<7);
buf[5]=gsmg_code[MyTime.yue%10]|(1<<7);
buf[6]=gsmg_code[MyTime.ri/10]|(1<<7);
buf[7]=gsmg_code[MyTime.ri%10]|(1<<7);
}
else
{
buf[0]=gsmg_code[MyTime.nian/1000];
buf[1]=gsmg_code[MyTime.nian/100%10];
buf[2]=gsmg_code[MyTime.nian/10%10];
buf[3]=gsmg_code[MyTime.nian%10];
buf[4]=gsmg_code[MyTime.yue/10];
buf[5]=gsmg_code[MyTime.yue%10];
buf[6]=gsmg_code[MyTime.ri/10];
buf[7]=gsmg_code[MyTime.ri%10];
jump1();
}
}
else
{
if(i>=DELAY)
{
buf[0]=gsmg_code[MyTime.shi/10]|(1<<7);
buf[1]=gsmg_code[MyTime.shi%10]|(1<<7);
buf[2]=0x40;
buf[3]=gsmg_code[MyTime.fen/10]|(1<<7);
buf[4]=gsmg_code[MyTime.fen%10]|(1<<7);
buf[5]=0x40;
buf[6]=gsmg_code[MyTime.miao/10]|(1<<7);
buf[7]=gsmg_code[MyTime.miao%10]|(1<<7);
}
else
{
buf[0]=gsmg_code[MyTime.shi/10];
buf[1]=gsmg_code[MyTime.shi%10];
buf[2]=0x40;
buf[3]=gsmg_code[MyTime.fen/10];
buf[4]=gsmg_code[MyTime.fen%10];
buf[5]=0x40;
buf[6]=gsmg_code[MyTime.miao/10];
buf[7]=gsmg_code[MyTime.miao%10];
jump1();
}
}
}
else if(Myflag.Timeflag==2)
{
//jiahs
add();
MyTime.shi=MyTime.hour;
MyTime.fen=MyTime.min;
MyTime.miao=MyTime.sec;
MyTime.nian=MyTime.w_year;
MyTime.yue=MyTime.w_month;
MyTime.ri=MyTime.w_date;
if(Myflag.Dateflag==1)
{
if(i>=DELAY)
{
buf[0]=gsmg_code[MyTime.nian/1000]|(1<<7);
buf[1]=gsmg_code[MyTime.nian/100%10]|(1<<7);
buf[2]=gsmg_code[MyTime.nian/10%10]|(1<<7);
buf[3]=gsmg_code[MyTime.nian%10]|(1<<7);
buf[4]=gsmg_code[MyTime.yue/10]|(1<<7);
buf[5]=gsmg_code[MyTime.yue%10]|(1<<7);
buf[6]=gsmg_code[MyTime.ri/10]|(1<<7);
buf[7]=gsmg_code[MyTime.ri%10]|(1<<7);
}
else
{
buf[0]=gsmg_code[MyTime.nian/1000];
buf[1]=gsmg_code[MyTime.nian/100%10];
buf[2]=gsmg_code[MyTime.nian/10%10];
buf[3]=gsmg_code[MyTime.nian%10];
buf[4]=gsmg_code[MyTime.yue/10];
buf[5]=gsmg_code[MyTime.yue%10];
buf[6]=gsmg_code[MyTime.ri/10];
buf[7]=gsmg_code[MyTime.ri%10];
//sztiaod
jump1();
}
}
else
{
if(i>=DELAY)
{
buf[0]=gsmg_code[MyTime.shi/10]|(1<<7);
buf[1]=gsmg_code[MyTime.shi%10]|(1<<7);
buf[2]=0x40;
buf[3]=gsmg_code[MyTime.fen/10]|(1<<7);
buf[4]=gsmg_code[MyTime.fen%10]|(1<<7);
buf[5]=0x40;
buf[6]=gsmg_code[MyTime.miao/10]|(1<<7);
buf[7]=gsmg_code[MyTime.miao%10]|(1<<7);
}
else
{
buf[0]=gsmg_code[MyTime.shi/10];
buf[1]=gsmg_code[MyTime.shi%10];
buf[2]=0x40;
buf[3]=gsmg_code[MyTime.fen/10];
buf[4]=gsmg_code[MyTime.fen%10];
buf[5]=0x40;
buf[6]=gsmg_code[MyTime.miao/10];
buf[7]=gsmg_code[MyTime.miao%10];
jump1();
}
}
}
SMG_Display(buf,1);
if(i++==2*DELAY)i=0;
}
为了在设置时间的时候跳动
使用jump1()函数
void jump1(void)
{
if(Myflag.Dateflag==0)
{
switch(Myflag.Bitflag)
{case 0:
case 1:buf[0]=0;buf[1]=0;break;
case 2:break;
case 3:
case 4:buf[3]=0;buf[4]=0;break;
case 5:break;
case 6:
case 7:buf[6]=0;buf[7]=0;break;
}
}
else
{
switch(Myflag.Bitflag)
{case 0:
case 1:
case 2:
case 3:buf[0]=0;buf[1]=0;buf[3]=0;break;
case 4:
case 5:buf[4]=0;buf[5]=0;break;
case 6:
case 7:buf[6]=0;buf[7]=0;break;
}
}
}
加减时间使用add()
其中yflag.addadd==1是加
yflag.addadd==2是减
小时的范围是0-23
分秒的范围是0-59
年的范围是2023-9999
月的范围是1-12
日的范围是0-31(大月),0-30(小月),0-29(闰年2月),0-28(平年2月)
先写判断闰年平年函数
int cheak_year(int year)
{
if((year%4==0)&&((year%100!=0)||(year%400==0)))
{
return 1;
}
else
{
return 0;
}
}
再写计算加减函数
void add(void)
{
if(Myflag.Dateflag==0)
{
switch(Myflag.Bitflag)
{case 0:
case 1:
if(Myflag.addadd==1)
{
Myflag.addadd=0;
if(MyTime.hour<23)MyTime.hour++;
else MyTime.hour=0;
}
else if(Myflag.addadd==2)
{
Myflag.addadd=0;
if(MyTime.hour>0)MyTime.hour--;
else MyTime.hour=23;
}
break;
case 2:break;
case 3:
case 4:if(Myflag.addadd==1)
{
Myflag.addadd=0;
if(MyTime.min<59)MyTime.min++;
else MyTime.min=0;
}
else if(Myflag.addadd==2)
{
Myflag.addadd=0;
if(MyTime.min>0)MyTime.min--;
else MyTime.min=23;
}break;
case 5:break;
case 6:
case 7:if(Myflag.addadd==1)
{
Myflag.addadd=0;
if(MyTime.sec<59)MyTime.sec++;
else MyTime.sec=0;
}
else if(Myflag.addadd==2)
{
Myflag.addadd=0;
if(MyTime.sec>0)MyTime.sec--;
else MyTime.sec=23;
}break;
}
}
else
{
switch(Myflag.Bitflag)
{case 0:
case 1:
case 2:
case 3:if(Myflag.addadd==1)
{
Myflag.addadd=0;
if(MyTime.w_year<9999)MyTime.w_year++;
else MyTime.w_year=0;
}
else if(Myflag.addadd==2)
{
Myflag.addadd=0;
if(MyTime.w_year>0)MyTime.w_year--;
else MyTime.w_year=2023;
}break;
case 4:
case 5:if(Myflag.addadd==1)
{
Myflag.addadd=0;
if(MyTime.w_month<12)MyTime.w_month++;
else MyTime.w_month=1;
}
else if(Myflag.addadd==2)
{
Myflag.addadd=0;
if(MyTime.w_month>0)MyTime.w_month--;
else MyTime.w_month=12;
}break;
case 6:
case 7: switch(MyTime.w_month)
{
case 1://´óÔÂ31Ìì
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(Myflag.addadd==1)
{
Myflag.addadd=0;
if(MyTime.w_date<31)MyTime.w_date++;
else MyTime.w_date=1;
}
else if(Myflag.addadd==2)
{
Myflag.addadd=0;
if(MyTime.w_date>1)MyTime.w_date--;
else MyTime.w_date=31;
}
break;
case 4://СÔÂ30Ìì
case 6:
case 9:
case 11:
if(Myflag.addadd==1)
{
Myflag.addadd=0;
if(MyTime.w_date<30)MyTime.w_date++;
else MyTime.w_date=1;
}
else if(Myflag.addadd==2)
{
Myflag.addadd=0;
if(MyTime.w_date>1)MyTime.w_date--;
else MyTime.w_date=30;
}
break;
case 2://ÌØÊâÔÂ
if(cheak_year(MyTime.w_year))//ÈòÄê
{
if(Myflag.addadd==1)
{
Myflag.addadd=0;
if(MyTime.w_date<29)MyTime.w_date++;
else MyTime.w_date=1;
}
else if(Myflag.addadd==2)
{
Myflag.addadd=0;
if(MyTime.w_date>1)MyTime.w_date--;
else MyTime.w_date=29;
}
}
else//ƽÄê
{
if(Myflag.addadd==1)
{
Myflag.addadd=0;
if(MyTime.w_date<28)MyTime.w_date++;
else MyTime.w_date=1;
}
else if(Myflag.addadd==2)
{
Myflag.addadd=0;
if(MyTime.w_date>1)MyTime.w_date--;
else MyTime.w_date=28;
}
}
break;
}break;
}
}
}