#include "stm32f10x.h"
void RCC_Init(void) //GPIO时钟初始化
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD,ENABLE);
}
void Led_Init(void) //LED相关的GPIO初始化
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_DeInit(GPIOA);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(GPIOA,&GPIO_InitStruct); //PA_8引脚
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(GPIOD,&GPIO_InitStruct); //PD_2引脚初始化
}
void Key_Init(void) //按键初始化
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 ;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; //13上拉
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; //15上拉
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD; //下拉
GPIO_Init(GPIOA,&GPIO_InitStruct);
}
void Delay_ms(uint32_t nms)
{
uint32_t i;
for(;nms>0;--nms)
for(i=12000;i>0;--i) ;
}
uint8_t KeyScan(void)
{
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE); //关SW
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13) == 0X00 \
|| GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15) == 0X00 \
|| GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) == 0X01)
{
Delay_ms(1);
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) == 0X01) //wk_up按键判断
{
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST,ENABLE); //开SW
return 1;
}
else if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15) == 0X00) //KEY1按键判断
{
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST,ENABLE);
return 2;
}
else if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13) == 0X00) //KEY0按键判断
{
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST,ENABLE);
return 3;
}
}
else
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST,ENABLE);
return 0; //无按键按下
}
int main(void)
{
uint8_t temp;
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();
RCC_Init(); //外设时钟初始化
Led_Init(); //LED初始化
Key_Init(); // 按键初始化
while (1)
{
temp = KeyScan(); //判断是否有按键
if(temp)
{
switch(temp)
{
case 1: //WK_UP被按下
GPIO_ResetBits(GPIOA,GPIO_Pin_8); //红灯亮
GPIO_ResetBits(GPIOD,GPIO_Pin_2); //绿灯亮
break;
case 2: //KEY1按下
GPIO_SetBits(GPIOA,GPIO_Pin_8); //红灯灭
break;
case 3: //KEY0按下
GPIO_SetBits(GPIOD,GPIO_Pin_2); //绿灯灭
break;
}
}
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
上面是我的代码 |