STM32——不同的按鍵對應實作不同功能的燈閃爍
- 功能介紹
- 程式解讀
- 總結
功能介紹
本程式使用STM32F103VE板子
實作功能:
按鍵1:實作單個燈閃爍
按鍵2:實作全部燈閃爍
按鍵3:實作流水燈
按鍵4:實作跑馬燈
程式解讀
#include "stm32f10x.h" // 相當于51單片機中的 #include <reg51.h>
void InitScan(void);
void InitLed(void);
void Delay_us(unsigned long time);
void Delay_ms(unsigned long time);
void InitLedStatus();
void ControlSingleLed(GPIO_TypeDef* GPIO_Scan,u16 GPIO_Pin_ScanKey,GPIO_TypeDef* GPIO_LED,u16 GPIO_Pin_Led);
void ControlAllLed(GPIO_TypeDef* GPIO_Scan,u16 GPIO_Pin_ScanKey);
void ControlHorseRaceLed(GPIO_TypeDef* GPIO_Scan,u16 GPIO_Pin_ScanKey);
void ControlWaterLed(GPIO_TypeDef* GPIO_Scan,u16 GPIO_Pin_ScanKey);
/*四個標志位分別用于控制單個燈閃爍,全部燈閃爍,流水燈,跑馬燈按鍵掃描*/
int Flag_SingleLed = 0;
int Flag_AllLed = 0;
int Flag_WaterLed = 0;
int Flag_HorseRaceLed = 0;
int main(void)
{
InitLedStatus();
InitScan();
// 來到這里的時候,系統的時鐘已經被配置成72M,
InitScan();
InitLed();
while(1){
/*LED0*/
ControlSingleLed(GPIOC,GPIO_Pin_0,GPIOA,GPIO_Pin_0); //控制燈閃爍
/*LED1*/
ControlAllLed(GPIOC,GPIO_Pin_1);//控制全部燈亮與滅
/*LED2*/
ControlWaterLed(GPIOC,GPIO_Pin_2);//實作流水燈
/*LED3*/
ControlHorseRaceLed(GPIOC,GPIO_Pin_3);//實作跑馬燈
}
}
void Delay_us(unsigned long time){
unsigned long i;
while(time--){
i = 8;
while(i--);
}
}
void Delay_ms(unsigned long time){
while(time--)
Delay_us(1100);
}
/**
*@param void
*Function : 對四個按鍵進行時鐘配置,并配置引腳,模式為上拉輸入,速度
*/
void InitScan(void){
GPIO_InitTypeDef GPIO_Scan;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_Scan.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_Scan.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_Scan);
}
void InitLedStatus(){
GPIO_SetBits(GPIOA,GPIO_Pin_All);
}
/**
*@param void
*Function : 對四個LED鄧進行時鐘配置,并配置引腳,模式為推挽輸出,速度
*/
void InitLed(void){
GPIO_InitTypeDef GPIO_Led;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_Led.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_Led.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Led.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_Led);
}
/**
*@ControlSingleLed ControlSingleLed
*@param GPIO_Scan 按鍵埠
*@param GPIO_Pin_ScanKey 按鍵引腳
*@param GPIO_LED 燈埠
*@param GPIO_Pin_Led 燈引腳
*Function : 按下按鍵,對應的LED燈亮
*/
void ControlSingleLed(GPIO_TypeDef* GPIO_Scan,u16 GPIO_Pin_ScanKey,GPIO_TypeDef* GPIO_LED,u16 GPIO_Pin_Led){
if(GPIO_ReadInputDataBit(GPIO_Scan,GPIO_Pin_ScanKey) == 0){
Delay_ms(50);
if(GPIO_ReadInputDataBit(GPIO_Scan,GPIO_Pin_ScanKey) == 0)
Flag_SingleLed = !Flag_SingleLed;
if(Flag_SingleLed == 1){
InitLedStatus();
GPIO_ResetBits(GPIO_LED,GPIO_Pin_Led);
}else{
GPIO_SetBits(GPIO_LED,GPIO_Pin_Led);
}
}
}
/**
*@ControlAllLed ControlAllLed
*@param GPIO_Scan 按鍵埠
*@param GPIO_Pin_ScanKey 按鍵引腳
*@param GPIO_LED 燈埠
*@param GPIO_Pin_Led 燈引腳
*Function : 按下按鍵所有的燈亮
*/
void ControlAllLed(GPIO_TypeDef* GPIO_Scan,u16 GPIO_Pin_ScanKey){
if(GPIO_ReadInputDataBit(GPIO_Scan,GPIO_Pin_ScanKey) == 0){
Delay_ms(50);
if(GPIO_ReadInputDataBit(GPIO_Scan,GPIO_Pin_ScanKey) == 0)
Flag_AllLed = !Flag_AllLed;
if(Flag_AllLed == 1){
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
}else{
GPIO_SetBits(GPIOA,GPIO_Pin_0);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
GPIO_SetBits(GPIOA,GPIO_Pin_2);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
}
}
}
/**
*@MethodName ControlWaterLed
*@param GPIO_Scan 按鍵埠
*@param GPIO_Pin_ScanKey 按鍵引腳
*@param GPIO_LED 燈埠
*@param GPIO_Pin_Led 燈引腳
*Function : 按下按鍵,實作流水燈
*/
void ControlWaterLed(GPIO_TypeDef* GPIO_Scan,u16 GPIO_Pin_ScanKey){
if(GPIO_ReadInputDataBit(GPIO_Scan,GPIO_Pin_ScanKey) == 0){
Delay_ms(50);
if(GPIO_ReadInputDataBit(GPIO_Scan,GPIO_Pin_ScanKey) == 0)
Flag_WaterLed = !Flag_WaterLed;
if(Flag_WaterLed == 1){
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
Delay_ms(300);
GPIO_SetBits(GPIOA,GPIO_Pin_0);
Delay_ms(300);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
Delay_ms(300);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
Delay_ms(300);
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
Delay_ms(300);
GPIO_SetBits(GPIOA,GPIO_Pin_2);
Delay_ms(300);
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
Delay_ms(300);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
Delay_ms(300);
}else{
InitLedStatus();
}
}
}
/**
*@MethodName ControlHorseRaceLed
*@param GPIO_Scan 按鍵埠
*@param GPIO_Pin_ScanKey 按鍵引腳
*@param GPIO_LED 燈埠
*@param GPIO_Pin_Led 燈引腳
*Function : 按下按鍵,實作跑馬燈
*/
void ControlHorseRaceLed(GPIO_TypeDef* GPIO_Scan,u16 GPIO_Pin_ScanKey){
if(GPIO_ReadInputDataBit(GPIO_Scan,GPIO_Pin_ScanKey) == 0){
Delay_ms(50);
if(GPIO_ReadInputDataBit(GPIO_Scan,GPIO_Pin_ScanKey) == 0)
Flag_HorseRaceLed = !Flag_HorseRaceLed;
if(Flag_HorseRaceLed == 1){
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
Delay_ms(300);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
Delay_ms(300);
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
Delay_ms(300);
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
Delay_ms(300);
InitLedStatus();
}else{
InitLedStatus();
}
}
}
總結
上面程式所用到的4個燈,4個按鍵在電路圖上的位置

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/275540.html
標籤:其他
上一篇:TimeLine的使用
