初级会员

- 积分
- 59
- 金钱
- 59
- 注册时间
- 2020-3-17
- 在线时间
- 15 小时
|
1金钱
求助一下,这个程序是抄书的,KEY引脚和LED引脚配置没有错误,现在程序没有报错,能下载到我的板子上
我的问题是:
1.我看程序似乎是说按键之后,不同LED灯会亮,但是现在只是一直一个灯亮,哪里不对了,还是说我理解错了,他只是配置了,没有调用,主函数如何改。
2.main中的ucLed >>=1是什么意思呢 右移一位吗?
下面我放了文字的程序,附件也有,求解答,打扰了,在此谢谢了。
——————————————————————————————————————————————————————————————————
main.c
#include "stm32f10x.h"
#include "led.h"
#include "key.h"
unsigned char ucLed=1 ;
unsigned long ulTick_ms, ulKey_Time;
uint8_t ucKey_Long;
void KEY_Proc(void);
int main()
{
SysTick_Config(72000);
LED_Init();
KEY_Init();
// BUZ_Init();
while(1)
{
KEY_Proc();
LED_Disp(ucLed);
}
}
void KEY_Proc(void)
{
unsigned char ucKey_Val;
ucKey_Val = KEY_Scan();
if(ucKey_Val != ucKey_Long)
{
ucKey_Long = ucKey_Val;
ulKey_Time = ulTick_ms;
}
else
ucKey_Val = 0;
if(ucKey_Val == 1) //B1短按
{
ucLed <<= 1;
if(ucLed ==0) ucLed = 1;
}
if(ucKey_Val == 2) //B2短按
{
ucLed >>= 1;
if(ucLed ==0) ucLed = 0x80;
}
if(ucKey_Long == 1) //B1长按
{
if(ulKey_Time-ulKey_Time > 800)
{
ulKey_Time = ulTick_ms;
ucLed <<= 2;
if(ucLed == 0)ucLed = 1;
}
}
if(ucKey_Long == 2) //B2长按
{
if(ulKey_Time-ulKey_Time > 800)
{
ulKey_Time = ulTick_ms;
ucLed >>= 2;
if(ucLed == 0)ucLed = 0x80;
}
}
if(ucKey_Long == 3) //B3长按
GPIO_ResetBits(GPIOB,GPIO_Pin_4);
else
GPIO_ResetBits(GPIOB,GPIO_Pin_4);
}
void Systick_Handler(void)
{
ulTick_ms++;
}
_____________________________________________
key.h
#ifndef __KEY_H
#define __KEY_H
#include "stm32f10x.h"
void KEY_Init(void);
void Delay_KEY(unsigned int ms);
unsigned char KEY_Scan(void);
#endif
key.c
#include "key.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
unsigned char KEY_Scan(void)
{
unsigned char ucKey_Val = 0;
if(~GPIO_ReadInputData(GPIOA) & 0X101)
{
Delay_KEY(10);
if(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0))
ucKey_Val = 1;
if(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8))
ucKey_Val = 2;
}
else if(~GPIO_ReadInputData(GPIOB) & 6)
{
Delay_KEY(10);
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1))
ucKey_Val = 3;
if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2))
ucKey_Val = 4;
}
return ucKey_Val;
}
void Delay_KEY(unsigned int ms)
{
uint32_t i,j;
for(i=0;i<ms;i++)
for(j=0;j<7992;j++); //SYSCLK = 72MHz
// for(j=0;j<1598,j++); //SYSCLK = 8MHz
}
______________________________________________________________________________________
led.h
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
void LED_Init(void);
void LED_Disp(unsigned char ucLed);
void BUZ_Init(void);
#endif
led.c
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
//LED引脚配置,PC08~PC15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//74HC573锁存引脚配置,PD2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void LED_Disp(unsigned char ucLed)
{
GPIO_Write(GPIOC,~ucLed << 8 );
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
void BUZ_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
|
|