本帖最后由 lustao 于 2020-6-1 09:03 编辑
继前文
在MM32L3xx_n_Lib_Samples_V1.39_SCBoards_MM32L3xx_nEVBoardL373PS_n_LibRTCRTC_real timeKEIL_PRJ路径下加载EVBoard_RTC_realtime. UVprojx文件。
原来是另一个板的带lcd程序,准备在此上改led,加按键。
原led.c
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE); //ʹÄÜPA,PC¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //LD2-->PA.8 ¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯GPIOA.8
GPIO_SetBits(GPIOA, GPIO_Pin_8); //PA,8 Êä³ö¸ß
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //LD1-->PC.13 ¶Ë¿ÚÅäÖÃ, ÍÆÍìÊä³ö
GPIO_Init(GPIOC, &GPIO_InitStructure); //ÍÆÍìÊä³ö £¬IO¿ÚËÙ¶ÈΪ50MHz
GPIO_SetBits(GPIOC, GPIO_Pin_13); //PA.15 Êä³ö¸ß
}
新
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //¿ªÆôGPIOA,GPIOBʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//¿ªÆô¸´ÓÃʱÖÓ£¬Õâ¸öÒ»¶¨Òª¿ª
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//ʧÄÜJTAG£¬Ê¹ÄÜSW
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
LED1=1;
LED2=1;
LED3=1;
LED4=1;
} 原头led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
//////////////////////////////////////////////////////////////////////////////////
//¿ª·¢°å
//LEDÇý¶¯´úÂë
//////////////////////////////////////////////////////////////////////////////////
#define LED2 PAout(8) // PA8
#define LED1 PCout(13) // PC13
void LED_Init(void);//³õʼ»¯
#endif
新
#ifndef __LED_H
#define __LED_H
#include "sys.h"
//////////////////////////////////////////////////////////////////////////////////
//¿ª·¢°å
//LEDÇý¶¯´úÂë
//////////////////////////////////////////////////////////////////////////////////
#define LED1 PAout(15) // PA15
#define LED2 PBout(3) // PB3
#define LED3 PBout(4) // PB4
#define LED4 PBout(5) // PB5
void LED_Init(void);//³õʼ»¯
#endif
加按键key.c。
#include "key.h"
- #include "delay.h"
- //////////////////////////////////////////////////////////////////////////////////
- //¿ª·¢°å
- //°´¼üÊäÈë Çý¶¯´úÂë
- //////////////////////////////////////////////////////////////////////////////////
- //°´¼ü³õʼ»¯º¯Êý
- //PA15ºÍPC5 ÉèÖóÉÊäÈë
- void KEY_Init(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//ʹÄÜGPIOA,GPIOB,GPIOCʱÖÓ
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//¹Ø±Õjtag£¬Ê¹ÄÜSWD£¬¿ÉÒÔÓÃSWDģʽµ÷ÊÔ
- //
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;//PC13£¬K1
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉèÖóÉÉÏÀÊäÈë
- // GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯GPIOC13
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;//K1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÏÂÀÊäÈë
- GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIOB
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2| GPIO_Pin_10|GPIO_Pin_11;//PB2,K2£¨WK_UP£©PB10,PB11,K3,K4
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉèÖóÉÉÏÀÊäÈë
- GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIOB
- //
- // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;//PB10,PB11,K3,K4
- //// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PA0ÉèÖóÉÉÏÀÊäÈë
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉèÖóÉÏÂÀÊäÈë
- // GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIOB.10,11
-
- }
- //°´¼ü´¦Àíº¯Êý
- //·µ»Ø°´¼üÖµ
- //mode:0,²»Ö§³ÖÁ¬Ðø°´;1,Ö§³ÖÁ¬Ðø°´;
- //·µ»ØÖµ£º
- //0£¬Ã»ÓÐÈκΰ´¼ü°´ÏÂ
- //KEY1_PRES£¬KEY1°´ÏÂ
- //WKUP_PRES£¬WK_UP°´ÏÂ
- //KEY3_PRES£¬KEY3°´ÏÂ
- //KEY4_PRES£¬KEY4°´ÏÂ
- u8 KEY_Scan(u8 mode)
- {
- // print("Scan key");
- static u8 key_up=1;//°´¼ü°´ËÉ¿ª±êÖ¾
- if(mode)key_up=1; //Ö§³ÖÁ¬°´
- if(key_up&&(KEY1==1||WK_UP==0||KEY3==0||KEY4==0))
- {
- delay_ms(10);//È¥¶¶¶¯
- key_up=0;
- if(KEY1==1)return KEY1_PRES;
- else if(WK_UP==0)return WKUP_PRES;
- else if(KEY3==0)return KEY3_PRES;
- else if(KEY4==0)return KEY4_PRES;
- }else if(KEY1==0&&KEY3==1&&KEY4==1&&WK_UP==1)key_up=1;
- return 0;// ÎÞ°´¼ü°´ÏÂ
- }
复制代码
加按键key.h。
- #ifndef __KEY_H
- #define __KEY_H
- #include "sys.h"
- //////////////////////////////////////////////////////////////////////////////////
- //¿ª·¢°å
- //°´¼üÇý¶¯´úÂë
- //////////////////////////////////////////////////////////////////////////////////
- #define KEY1 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)//¶ÁÈ¡°´¼ü1
- #define WK_UP GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)//¶ÁÈ¡°´¼ü2
- #define KEY3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10)//¶ÁÈ¡°´¼ü3
- #define KEY4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)//¶ÁÈ¡°´¼ü4
- #define KEY1_PRES 61 //KEY1
- #define WKUP_PRES 62 //WK_UP
- #define KEY3_PRES 63 //KEY3
- #define KEY4_PRES 64 //KEY4
- void KEY_Init(void);//IO³õʼ»¯
- u8 KEY_Scan(u8 mode); //°´¼üɨÃ躯Êý
- #endif
复制代码
原来main
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "uart.h"
- #include "lcdc.h"
- #include "rtc.h"
- ////////////////////////////////////////////////////////////////////////////////
- int main(void)
- {
- u8 t;
- char str[20];
- delay_init(); //ÑÓʱº¯Êý³õʼ»¯
- uart_initwBaudRate(9600); //´®¿Ú³õʼ»¯Îª9600
- LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
- initLCD();
- putStr(110, 20, 2, 1, "MM32 Dev Board");
- putStr(110, 40, 2, 1, "RTC TEST");
- putStr(110, 60, 2, 1, "@MindMotion");
- putStr(110, 80, 2, 1, "2016/09/02");
- while(RTC_Init()) //RTC³õʼ»¯ £¬Ò»¶¨Òª³õʼ»¯³É¹¦
- {
- putStr(110, 100, 2, 1, "RTC ERROR! ");
- delay_ms(800);
- putStr(110, 100, 2, 1, "RTC Trying...");
- }
- putStr(110, 100, 2, 1, " - - ");
- putStr(110, 140, 2, 1, " : : ");
- while(1)
- {
- if(t != calendar.sec)
- {
- t = calendar.sec;
- sprintf(str, "%d", calendar.w_year);
- putStr(110, 100, 2, 1, str);
- sprintf(str, "%d", calendar.w_month);
- putStr(150, 100, 2, 1, str);
- sprintf(str, "%d", calendar.w_date);
- putStr(170, 100, 2, 1, str);
- switch(calendar.week)
- {
- case 0:
- putStr(110, 120, 2, 1, "Sunday ");
- break;
- case 1:
- putStr(110, 120, 2, 1, "Monday ");
- break;
- case 2:
- putStr(110, 120, 2, 1, "Tuesday ");
- break;
- case 3:
- putStr(110, 120, 2, 1, "Wednesday");
- break;
- case 4:
- putStr(110, 120, 2, 1, "Thursday ");
- break;
- case 5:
- putStr(110, 120, 2, 1, "Friday ");
- break;
- case 6:
- putStr(110, 120, 2, 1, "Saturday ");
- break;
- }
- sprintf(str, "%d", calendar.hour);
- putStr(110, 140, 2, 1, str);
- sprintf(str, "%d", calendar.min);
- putStr(135, 140, 2, 1, str);
- sprintf(str, "%d", calendar.sec);
- putStr(160, 140, 2, 1, str);
- LED1 = !LED1;
- }
- delay_ms(10);
- }
- while(1);
- }
复制代码
新
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "uart.h"
#include "tsensor.h"
#include "rtc.h"
#include "key.h"
//#include "exti.h"
void time_scr(void)
{
char str[20];
sprintf(str,"%d",calendar.w_year);
// putStr(110, 100,2, 1,str);
printf(str,"");
printf("_year");
sprintf(str,"%d",calendar.w_month);
// putStr(150, 100,2, 1,str);
printf(str,"");
printf("_month");
sprintf(str,"%d",calendar.w_date);
// putStr(170, 100,2, 1,str);
printf(str,"");
printf("_date");
switch(calendar.week)
{
case 0:
// putStr(110, 120,2, 1,"Sunday ");
printf(" Sunday ");
break;
case 1:
// putStr(110, 120,2, 1,"Monday ");
printf(" Monday ");
break;
case 2:
// putStr(110, 120,2, 1,"Tuesday ");
printf(" Tuesday ");
break;
case 3:
// putStr(110, 120,2, 1,"Wednesday");
printf(" Wednesday ");
break;
case 4:
// putStr(110, 120,2, 1,"Thursday ");
printf(" Thursday ");
break;
case 5:
// putStr(110, 120,2, 1,"Friday ");
printf(" Friday ");
break;
case 6:
// putStr(110, 120,2, 1,"Saturday ");
printf(" Saturday ");
break;
}
sprintf(str,"%d",calendar.hour);
printf(str,"");
printf("_hour:");
// putStr(110, 140,2, 1,str);
sprintf(str,"%d",calendar.min);
printf(str,"");
printf("min:");
// putStr(135, 140,2, 1,str);
sprintf(str,"%d",calendar.sec);
printf(str,"");
printf("sec");
printf("rnrn");//²åÈë»»ÐÐ
// putStr(160, 140,2, 1,str);
}
void time_i(void)
{
char str[20];
u8 t,tymdhm=0,tmo=0,td=0,th=0,tmi=0,tend=0;
u16 ty=0;
printf( "time set:");
printf("rnrn");//²åÈë»»ÐÐ
printf( "KEY1_PRES:print timernrn");
printf( "KEY2_PRES:switch set year,mon,day,hour,minrnrn");
printf( "KEY3_PRES:plusrnrn");
printf( "KEY4_PRES:set timernrn");
time_scr();
printf( "set time:");
switch(tymdhm)
{
case 0://K1ĬÈϲ»Á¬PC13,ĬÈÏÁ¬½Óreset¸´Î»°´¼ü£¬ËùÒÔ°´ÏÂK1»á¸´Î»
sprintf(str,"%d",calendar.w_year);
printf(str,"");
printf( "yearrn");
break;
case 1:
sprintf(str,"%d",calendar.w_month);
printf(str,"");
printf("monthrn");
break;
case 2:
sprintf(str,"%d",calendar.w_date);
printf(str,"");
printf("datern");
break;
case 3:
sprintf(str,"%d",calendar.hour);
printf(str,"");
printf( "hourrn");
break;
case 4:
sprintf(str,"%d",calendar.min);
printf(str,"");
printf( "minrn");
break;
case 5:
sprintf(str,"%d",calendar.sec);
printf(str,"");
printf( "secrn");
break;
// default:
// delay_ms(1);
}
while(1){
t=KEY_Scan(0); //µÃµ½¼üÖµ
switch(t)
{
case KEY1_PRES://K1ĬÈϲ»Á¬PC13,ĬÈÏÁ¬½Óreset¸´Î»°´¼ü£¬ËùÒÔ°´ÏÂK1»á¸´Î»
tymdhm=0,ty=0,tmo=0,td=0,th=0,tmi=0;
time_scr();
break;
case WKUP_PRES:
LED2=!LED2;
tymdhm+=1;
if (tymdhm==5 )tymdhm=0;
printf( "set time:");
switch(tymdhm)
{
case 0://K1ĬÈϲ»Á¬PC13,ĬÈÏÁ¬½Óreset¸´Î»°´¼ü£¬ËùÒÔ°´ÏÂK1»á¸´Î»
sprintf(str,"%d",calendar.w_year);
printf(str,"");
printf( "yearrn");
break;
case 1:
sprintf(str,"%d",calendar.w_month);
printf(str,"");
printf("monthrn");
break;
case 2:
sprintf(str,"%d",calendar.w_date);
printf(str,"");
printf("datern");
break;
case 3:
sprintf(str,"%d",calendar.hour);
printf(str,"");
printf( "hourrn");
break;
case 4:
sprintf(str,"%d",calendar.min);
printf(str,"");
printf( "minrn");
break;
case 5:
sprintf(str,"%d",calendar.sec);
printf(str,"");
printf( "secrn");
break;
// default:
// delay_ms(1);
}
break;
case KEY3_PRES:
LED3=!LED3;
switch(tymdhm)
{
case 0://K1ĬÈϲ»Á¬PC13,ĬÈÏÁ¬½Óreset¸´Î»°´¼ü£¬ËùÒÔ°´ÏÂK1»á¸´Î»
ty+=1;
sprintf(str,"%d",calendar.w_year+ty);
printf( str,"year");
printf("year");
break;
case 1:
tmo+=1;
sprintf(str,"%d",calendar.w_month+tmo);
printf( str,"month");
printf( "month");
break;
case 2:
td+=1;
sprintf(str,"%d",calendar.w_date+td);
printf( str,"date");
printf( "date");
break;
case 3:
th+=1;
sprintf(str,"%d",calendar.hour+th);
printf( str,"hour");
printf( "hour");
break;
case 4:
tmi+=1;
sprintf(str,"%d",calendar.min+tmi);
printf( str,"min");
printf( ":min:");
break;
case 5:
sprintf(str,"%d",calendar.sec);
printf( str,"sec");
break;
// default:
// delay_ms(1);
}
break;
case KEY4_PRES:
ty+=calendar.w_year;
tmo+=calendar.w_month;
while((tmo-12)>0)tmo-=12;
td+=calendar.w_date;
switch(tmo)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12://K1ĬÈϲ»Á¬PC13,ĬÈÏÁ¬½Óreset¸´Î»°´¼ü£¬ËùÒÔ°´ÏÂK1»á¸´Î»
while((td-31)>0)td-=31;
break;
case 4:case 6:case 9:case 11:
while((td-30)>0)td-=30;
break;
case 2:
while((td-28)>0)td-=28;
break;
// default:
// delay_ms(1);
}
th+=calendar.hour;
while((th-24)>0)th-=24;
tmi+=calendar.min;
while((tmi-60)>0)tmi-=60;
RTC_Set(ty,tmo,td,th,tmi,0);
tend=1;
// return 0;
break;
// default:
// delay_ms(1);
}
if(tend==1)break;
}
}
//¿ª·¢°å·¶Àý´úÂë3
//´®¿ÚʵÑé
void KEY2_main(void)
{
u16 average=0;
float temperate;
u8 i;
unsigned int uiADData=0;
// unsigned char tem[20];
// initLCD();
//
// printf( "MM32 Board");
// printf("rnrn");//²åÈë»»ÐÐ
printf( "Temperature TEST");
printf("rnrn");//²åÈë»»ÐÐ
printf( "@MindMotion");
printf("rnrn");//²åÈë»»ÐÐ
printf( "TEMP_VAL:");
printf( "C");
printf("rnrn");//²åÈë»»ÐÐ
for(i=0;i<10;i++)
{
T_Get_Adc_Average(ADC_Channel_8,&uiADData);
average+=uiADData;
if(i==9)
average=average/10;
}
temperate=(float)average*(3.3/4096); //µçѹֵ
temperate=(temperate-1.5)/0.00478+25; //ת»»ÎªÎ¶ÈÖµ
printf( "TEMP_VAL:");
printf("%.2f",temperate);
printf("rnrn");//²åÈë»»ÐÐ
// putStr(185, 120,2,1,tem);
// delay_ms(1250);
// average=0;
}
////////////////////////////////////////////////////////////////////////////////
int main(void)
{
u8 t,t1;
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
uart_initwBaudRate(9600); //´®¿Ú³õʼ»¯Îª9600
LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
KEY_Init(); //³õʼ»¯Óë°´¼üÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
T_Adc_Init(); //ADC³õʼ»¯
// initLCD();
//
// putStr(110, 20,2, 1, "MM32 Dev Board");
// putStr(110, 40,2, 1, "RTC TEST");
// putStr(110, 60,2, 1, "@MindMotion");
// putStr(110, 80,2, 1, "2016/09/02");
printf( "MM32 Board");
printf("rn");//²åÈë»»ÐÐ
while(RTC_Init()) //RTC³õʼ»¯ £¬Ò»¶¨Òª³õʼ»¯³É¹¦
{
// putStr(110, 100,2, 1,"RTC ERROR! ");
delay_ms(800);
printf("RTC Trying...");
// putStr(110, 100,2, 1,"RTC Trying...");
}
// EXTIX_Init();
printf("rn");//²åÈë»»ÐÐ
// putStr(110, 100,2, 1," - - ");
// putStr(110, 140,2, 1," : : ");
while(1)
{
t=KEY_Scan(0); //µÃµ½¼üÖµ
// if (t==0){}else{printf("%d",t);}//²åÈë»»ÐÐ }
// printf(" KEY down rnrn");
switch(t)
{
case KEY1_PRES://K1ĬÈϲ»Á¬PC13,ĬÈÏÁ¬½Óreset¸´Î»°´¼ü£¬ËùÒÔ°´ÏÂK1»á¸´Î»
printf("KEY1_PRES down rn");
printf("KEY1_PRES ");
time_i();
break;
case WKUP_PRES:
printf("KEY2_PRES down rn");
KEY2_main();
LED2=!LED2;
break;
case KEY3_PRES:
printf("KEY3_PRES down rn");
LED3=!LED3;
break;
case KEY4_PRES:
printf("KEY4_PRES down rn");
LED4=!LED4;
break;
// default:
// delay_ms(1);
}
if(t1!=calendar.sec)
{
t1=calendar.sec;
time_scr();
LED1=!LED1;
}
// delay_ms(1);
}
// while(1);
}
运行sscom32,图:(起动后,)
key2显示内部温度
key1进入设置
key4设置完成
0
|
|
|
|