|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#include "key.h"
#include "delay.h"
//°´¼ü³õʼ»¯º¯Êý
//PA0.15ºÍPC5 ÉèÖóÉÊäÈë
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);//ʹÄÜPORTA,PORTCʱÖÓ
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//¹Ø±Õjtag£¬Ê¹ÄÜSWD£¬¿ÉÒÔÓÃSWDģʽµ÷ÊÔ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;//PA15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉèÖóÉÉÏÀÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//PC5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉèÖóÉÉÏÀÊäÈë
GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯GPIOC5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PA0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0ÉèÖóÉÊäÈ룬ĬÈÏÏÂÀ
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.0
}
//°´¼ü´¦Àíº¯Êý
//·µ»Ø°´¼üÖµ
//mode:0,²»Ö§³ÖÁ¬Ðø°´;1,Ö§³ÖÁ¬Ðø°´;
//·µ»ØÖµ£º
//0£¬Ã»ÓÐÈκΰ´¼ü°´ÏÂ
//KEY0_PRES£¬KEY0°´ÏÂ
//KEY1_PRES£¬KEY1°´ÏÂ
//WKUP_PRES£¬WK_UP°´ÏÂ
//×¢Òâ´Ëº¯ÊýÓÐÏìÓ¦ÓÅÏȼ¶,KEY0>KEY1>WK_UP!!
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;//°´¼ü°´ËÉ¿ª±êÖ¾
if(mode)key_up=1; //Ö§³ÖÁ¬°´
if(key_up&&(KEY0==0||KEY1==0||WK_UP==1))
{
delay_ms(10);//È¥¶¶¶¯
key_up=0;
if(KEY0==0)return KEY0_PRES;
else if(KEY1==0)return KEY1_PRES;
else if(WK_UP==1)return WKUP_PRES;
}else if(KEY0==1&&KEY1==1&&WK_UP==0)key_up=1;
return 0;// ÎÞ°´¼ü°´ÏÂ
}
//#define KEY0 PCin(5)
//#define KEY1 PAin(15)
//#define WK_UP PAin(0)
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#define KEY0 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)//¶ÁÈ¡°´¼ü0
#define KEY1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)//¶ÁÈ¡°´¼ü1
#define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)//¶ÁÈ¡°´¼ü2
#define KEY0_PRES 1 //KEY0
#define KEY1_PRES 2 //KEY1
#define WKUP_PRES 3 //WK_UP
void KEY_Init(void);//IO³õʼ»¯
u8 KEY_Scan(u8 mode); //°´¼üɨÃ躯Êý
#endif
#include "iwdg.h"
#include "led.h"
void IWDG_Init(u8 prer,u16 rlr)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); //ʹÄܶԼĴæÆ÷IWDG_PRºÍIWDG_RLRµÄд²Ù×÷
IWDG_SetPrescaler(prer); //ÉèÖÃIWDGÔ¤·ÖƵֵ:ÉèÖÃIWDGÔ¤·ÖƵֵΪ64
IWDG_SetReload(rlr); //ÉèÖÃIWDGÖØ×°ÔØÖµ
IWDG_ReloadCounter(); //°´ÕÕIWDGÖØ×°ÔؼĴæÆ÷µÄÖµÖØ×°ÔØIWDG¼ÆÊýÆ÷
IWDG_Enable(); //ʹÄÜIWDG
}
//ι¶ÀÁ¢¿´ÃŹ·
void IWDG_Feed(void)
{
IWDG_ReloadCounter();
}
#ifndef __WDG_H
#define __WDG_H
#include "sys.h"
void IWDG_Init(u8 prer,u16 rlr);void IWDG_Feed(void);
#endif
#include "delay.h"
#include "sys.h"
#include "key.h"
#include "usart.h"
#include "iwdg.h"
int main(void)
{
delay_init(); //ÑÓʱº¯Êý³õʼ»¯
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// ÉèÖÃÖжÏÓÅÏȼ¶·Ö×é2
uart_init(9600); //´®¿Ú³õʼ»¯Îª9600
LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
KEY_Init(); //°´¼ü³õʼ»¯
delay_ms(300); //ÈÃÈË¿´µÃµ½Ãð
IWDG_Init(4,625); //Óë·ÖƵÊýΪ64,ÖØÔØֵΪ625,Òç³öʱ¼äΪ1s
LED0=0; //µãÁÁLED0
while(1)
{
if(KEY_Scan(0)==WKUP_PRES)IWDG_Feed();//Èç¹ûWK_UP°´ÏÂ,Ôòι¹·
delay_ms(10);
};
} |
|