初级会员

- 积分
- 143
- 金钱
- 143
- 注册时间
- 2012-7-11
- 在线时间
- 0 小时
|
原子哥你好 ,我将您的版本改成库函数,同时将KEY_Scan()函数整合到main函数中,可是当我烧录到板子里的时候,只有key0按键可以正常使用,key1和wk_up两个俺按键不同正常使用,可能要按好几次才能让灯实现亮暗转换,代码如下:
#include "stm32f10x.h"
#include "stdio.h"
#define KEY0 GPIO_Pin_13
#define KEY1 GPIO_Pin_15
#define WK_UP GPIO_Pin_0
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void TIM2_Configuration(void);
void Delay_ms(vu32 nCount);
int main()
{
static int key_up = 1;
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
TIM2_Configuration();
while(1)
{
if(key_up&&(GPIO_ReadInputDataBit(GPIOA,KEY0) == 0)||(GPIO_ReadInputDataBit(GPIOA,KEY1)==0)
||(GPIO_ReadInputDataBit(GPIOA,WK_UP) == 1))
{
Delay_ms(10); //去抖动
key_up=0;
if(GPIO_ReadInputDataBit(GPIOA,KEY0)==0)
{
GPIO_WriteBit(GPIOA,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8)));
}
else if(GPIO_ReadInputDataBit(GPIOA,KEY1)==0)
{
GPIO_WriteBit(GPIOD,GPIO_Pin_2,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_2)));
}
else if(GPIO_ReadInputDataBit(GPIOA,WK_UP)==1)
{
GPIO_WriteBit(GPIOA,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8)));
GPIO_WriteBit(GPIOD,GPIO_Pin_2,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_2)));
}
}
else if((GPIO_ReadInputDataBit(GPIOA,KEY0)==1&&GPIO_ReadInputDataBit(GPIOA,KEY1) ==1
&&GPIO_ReadInputDataBit(GPIOA,WK_UP)==0))
key_up= 1;
}
}
void RCC_Configuration(void)
{
/* 定义枚举类型变量 HSEStartUpStatus */
ErrorStatus HSEStartUpStatus;
/* 复位系统时钟设置 */
RCC_DeInit();
/* 开启 HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* 等待 HSE 起振并稳定 */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
/* 判断 HSE 起是否振成功,是则进入if()内部 */
if(HSEStartUpStatus == SUCCESS)
{
/* 选择 HCLK(AHB)时钟源为SYSCLK 1分频 */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* 选择 PCLK2 时钟源为 HCLK(AHB) 1分频 */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* 选择 PCLK1 时钟源为 HCLK(AHB) 2分频 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* 设置 FLASH 延时周期数为2 */
FLASH_SetLatency(FLASH_Latency_2);
/* 使能 FLASH 预取缓存 */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* 使能 PLL */
RCC_PLLCmd(ENABLE);
/* 等待 PLL 输出稳定 */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* 选择 SYSCLK 时钟源为 PLL */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* 等待 PLL 成为 SYSCLK 时钟源 */
while(RCC_GetSYSCLKSource() != 0x08);
}
/* 打开 GPIOA,GPIOB,USART1 和 SPI1 时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
}
void GPIO_Configuration(void)
{
/* 定义 GPIO 初始化结构体 GPIO_InitStructure */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD , &GPIO_InitStructure);
/* 设置 USART1 的Tx脚(PA.9)为第二功能推挽输出功能 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitStructure);
/* 设置 USART1 的Rx脚(PA.10)为浮空输入脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA , &GPIO_InitStructure);
}
void TIM2_Configuration()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 1;
TIM_TimeBaseStructure.TIM_Prescaler = 7200;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
}
void Delay_ms(vu32 nCount)
{
u16 TIMCounter = nCount;
TIM_Cmd(TIM2, ENABLE);
TIM_SetCounter(TIM2, TIMCounter);
while(TIMCounter>1)
{
TIMCounter = TIM_GetCounter(TIM2);
}
TIM_Cmd(TIM2,DISABLE);
}
void USART_Configuration(void)
{
/* 定义 USART 初始化结构体 USART_InitStructure */
USART_InitTypeDef USART_InitStructure;
/* 波特率为115200bps;
* 8位数据长度;
* 1个停止位,无校验;
* 禁用硬件流控制;
* 禁止USART时钟;
* 时钟极性低;
* 在第2个边沿捕获数据
* 最后一位数据的时钟脉冲不从 SCLK 输出;
*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1 , &USART_InitStructure);
/* 使能 USART1 */
USART_Cmd(USART1 , ENABLE);
}
/*******************************************************************************
* 函数名 : fputc
* 函数描述 : 将printf函数重定位到USATR1
* 输入参数 : 无
* 输出结果 : 无
* 返回值 : 无
*******************************************************************************/
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (u8) ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return ch;
}
|
|