一、按鍵控制原理圖
按鍵:button
電阻:res
二、按鍵代碼
1. led.c文件的代碼
先配置LED燈的GPIO
//LED 初始化函數(shù)
void Led_Init(void){
//聲明一個(gè)結(jié)構(gòu)體,名字是GPIO_InitStructure
GPIO_InitTypeDef GPIO_InitStructure;
//使能GPIOA的時(shí)鐘,ENABLE代表使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//GPIOA
//設(shè)置引腳為推挽輸出Out_PP
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
//定義引腳為 0號(hào)引腳 和 1號(hào)引腳
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;
//設(shè)置引腳的速度50MHz
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//初始化GPIO,初始化哪個(gè)引腳就對(duì)應(yīng)哪個(gè)
GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化GPIOA,所以引腳對(duì)應(yīng)PA0和PA1
//初始化時(shí)LED應(yīng)為熄滅狀態(tài),所以要拉高LED引腳的電平
GPIO_SetBits(GPIOA,GPIO_Pin_0 | GPIO_Pin_1); //PA0 和 PA1引腳拉高電平
}
編寫(xiě)LED亮滅的函數(shù)
//LED燈狀態(tài)函數(shù)
void Ledx_state(int x , int t){ //x表示第幾個(gè)LED,t==1表示點(diǎn)亮,t==0表示熄滅
if(x==1){ //x==1對(duì)應(yīng)LED1
if(t==1)GPIO_ResetBits(GPIOA,GPIO_Pin_0); //LED1點(diǎn)亮
if(t==0)GPIO_SetBits(GPIOA,GPIO_Pin_0); //LED1熄滅
}
if(x==2){ //x==2對(duì)應(yīng)LED2
if(x==1)GPIO_ResetBits(GPIOA,GPIO_Pin_1); //LED2點(diǎn)亮
if(x==0)GPIO_SetBits(GPIOA,GPIO_Pin_1); //LED2熄滅
}
}
實(shí)現(xiàn)按下按鍵LED燈的狀態(tài)取反
//***** 實(shí)現(xiàn)按下按鍵LED燈的狀態(tài)取反 *****//
void Led1_Turn(void){
//如果PA0的輸出寄存器的值為0,證明LED1為點(diǎn)亮狀態(tài)
if(GPIO_ReadOutputDataBit(GPIOA , GPIO_Pin_0)==0){
//取反,PA0置高電平,LED1熄滅
GPIO_SetBits(GPIOA , GPIO_Pin_0);
}
else{ //PA0的輸出寄存器的值為1,證明LED1為熄滅狀態(tài)
//取反,PA0置低電平,LED1點(diǎn)亮
GPIO_ResetBits(GPIOA , GPIO_Pin_0);
}
}
void Led2_Turn(void){
//如果PA1的輸出寄存器的值為0,證明LED2為點(diǎn)亮狀態(tài)
if(GPIO_ReadOutputDataBit(GPIOA , GPIO_Pin_1)==0){
//取反,PA1置高電平,LED2熄滅
GPIO_SetBits(GPIOA , GPIO_Pin_1);
}
else{ //PA1的輸出寄存器的值為1,證明LED2為熄滅狀態(tài)
//取反,PA1置低電平,LED2點(diǎn)亮
GPIO_ResetBits(GPIOA , GPIO_Pin_1);
}
}
led.c總代碼
#include "led.h"
#include "stm32f10x.h"
void Led_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//GPIOA
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1);
}
//LED燈狀態(tài)函數(shù)
void Ledx_state(int x , int t){ //x表示第幾個(gè)LED,t==1表示點(diǎn)亮,t==0表示熄滅
if(x==1){
if(t==1)GPIO_ResetBits(GPIOA,GPIO_Pin_0);
if(t==0)GPIO_SetBits(GPIOA,GPIO_Pin_0);
}
if(x==2){
if(t==1)GPIO_ResetBits(GPIOA,GPIO_Pin_1);
if(t==0)GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
}
//***** 實(shí)現(xiàn)按下按鍵LED燈的狀態(tài)取反 *****//
void Led1_Turn(void){
//如果PA0的輸出寄存器的值為0,證明LED1為點(diǎn)亮狀態(tài)
if(GPIO_ReadOutputDataBit(GPIOA , GPIO_Pin_0)==0){
//取反,PA0置高電平,LED1熄滅
GPIO_SetBits(GPIOA , GPIO_Pin_0);
}
else{ //PA0的輸出寄存器的值為1,證明LED1為熄滅狀態(tài)
//取反,PA0置低電平,LED1點(diǎn)亮
GPIO_ResetBits(GPIOA , GPIO_Pin_0);
}
}
void Led2_Turn(void){
//如果PA1的輸出寄存器的值為0,證明LED2為點(diǎn)亮狀態(tài)
if(GPIO_ReadOutputDataBit(GPIOA , GPIO_Pin_1)==0){
//取反,PA1置高電平,LED2熄滅
GPIO_SetBits(GPIOA , GPIO_Pin_1);
}
else{ //PA1的輸出寄存器的值為1,證明LED2為熄滅狀態(tài)
//取反,PA1置低電平,LED2點(diǎn)亮
GPIO_ResetBits(GPIOA , GPIO_Pin_1);
}
}
2. led.h頭文件的代碼
#ifndef __LED_H
#define __LED_H
void Led_Init(void); //LED初始化
void Ledx_state(int i , int x); //LED燈狀態(tài)函數(shù)
#endif
3. key.c文件的代碼
先配置按鍵的GPIO
//按鍵 初始化函數(shù)
void Key_Init(void){
//聲明一個(gè)結(jié)構(gòu)體,名字是GPIO_InitStructure
GPIO_InitTypeDef GPIO_InitStructure;
//使能GPIOA的時(shí)鐘,ENABLE代表使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//GPIOB
//按鍵需要設(shè)置引腳模式為上拉模式GPIO_Mode_IPU
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; //上拉模式GPIO_Mode_IPU
//定義引腳為 0號(hào)引腳 和 1號(hào)引腳
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;
//設(shè)置引腳的速度50MHz
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//初始化GPIO,初始化哪個(gè)引腳就對(duì)應(yīng)哪個(gè)
GPIO_Init(GPIOB,&GPIO_InitStructure);//初始化GPIOB,所以引腳對(duì)應(yīng)PA0和PA1
//初始化時(shí)LED應(yīng)為熄滅狀態(tài),所以要拉高LED引腳的電平
GPIO_SetBits(GPIOB,GPIO_Pin_0 | GPIO_Pin_1); //PB0 和 PB1引腳拉高電平
}
獲取當(dāng)前按鍵鍵值的函數(shù)
uint8_t Key_GetNum(void){ //獲取當(dāng)前按鍵鍵值
uint8_t KeyNum = 0;
//檢測(cè)PB0引腳是否為低電平,按鍵按下時(shí)為低電平
if(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_0)==0){ //PB0為低電平,按鍵1已按下
delay_ms(20); //延時(shí),消抖
//消抖,等待PB0重新變成高電平,如果一直為低電平則一直進(jìn)入死循環(huán)
while(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_0)==0);
delay_ms(20); //延時(shí),消抖
KeyNum=1; //鍵值賦值為1,代表按鍵1已按下
}
//檢測(cè)PB1引腳是否為低電平,按鍵按下時(shí)為低電平
if(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_1)==0){ //PB1為低電平,按鍵2已按下
delay_ms(20); //延時(shí),消抖
//消抖,等待PB1重新變成高電平,如果一直為低電平則一直進(jìn)入死循環(huán)
while(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_1)==0);
delay_ms(20); //延時(shí),消抖
KeyNum=2; //鍵值賦值為2,代表按鍵1已按下
}
return KeyNum; //返回鍵值
}
key.c總代碼
#include "key.h"
void Key_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//GPIOB
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //設(shè)置上拉模式 GPIO_Mode_IPU
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_0|GPIO_Pin_1);
}
uint8_t Key_GetNum(void){
uint8_t KeyNum = 0;
if(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_0)==0){
delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_0)==0);
delay_ms(20);
KeyNum=1;
}
if(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_1)==0){
delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB , GPIO_Pin_1)==0);
delay_ms(20);
KeyNum=2;
}
return KeyNum;
}
4. key.h頭文件的代碼
#ifndef __KEY_H
#define __KEY_
#include "stm32f10x.h" // Device header
#include "delay.h"
void Key_Init(void); //按鍵初始化
uint8_t Key_GetNum(void); //獲取當(dāng)前按鍵按下的鍵值
#endif
5. main.c文件的代碼
5.1 按鍵按下實(shí)現(xiàn)LED亮或者滅
#include "stm32f10x.h"
#include "led.h"
#include "key.h"
#include "delay.h"
uint8_t KeyNum = 0;
int main(void)
{
delay_init();
Led_Init();
Key_Init();
while(1){
KeyNum = Key_GetNum(); //獲取鍵值
if(KeyNum == 1){
//按下按鍵1,LED1燈亮,LED2燈滅
Ledx_state(1 , 1);
Ledx_state(2 , 0);
}
if(KeyNum == 2){
//按下按鍵2,LED2燈亮,LED1燈滅
Ledx_state(1 , 0);
Ledx_state(2 , 1);
}
}
仿真效果圖:
5.2 按鍵按下實(shí)現(xiàn)LED狀態(tài)取反
#include "stm32f10x.h"
#include "led.h"
#include "key.h"
#include "delay.h"
uint8_t KeyNum = 0;
int main(void)
{
delay_init();
Led_Init();
Key_Init();
while(1){ //按下按鍵,LED燈取反
KeyNum = Key_GetNum(); //獲取鍵值
if(KeyNum == 1){
Led1_Turn(); //按下按鍵1,LED1燈狀態(tài)取反
}
if(KeyNum == 2){
Led2_Turn(); //按下按鍵2,LED2燈狀態(tài)取反
}
}
仿真效果圖:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-728050.html
三、項(xiàng)目(代碼+仿真)分享鏈接
百度網(wǎng)盤(pán)
鏈接:https://pan.baidu.com/s/1pcVtAcER2mAwnQnyRL3aXQ
提取碼:p8q4文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-728050.html
到了這里,關(guān)于Stm32f103c8t6(proteus仿真)學(xué)習(xí)——3.按鍵控制LED燈的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!