????🔎【博主簡介】🔎
????????🏅CSDN博客專家
????????🏅2021年博客之星物聯網與嵌入式開發TOP5
????????🏅2022年博客之星物聯網與嵌入式開發TOP4
????????🏅2021年2022年C站百大博主
????????🏅華為云開發者社區專家博主
????????🏅阿里云開發者社區專家博主
????????🏅掘金INFOQ騰訊云優秀博主📝《個人主頁》謓澤-CSDN博客
🥰《個人社區》QRS社區-CSDN社區云
👀《系列專欄》STM32-單片機
📣 點贊👍+ 收藏??+ 留言💬
⒈按鍵控制LED燈
概述?在五一單片機當中博主也有寫過一篇關于輕觸按鍵控制的文章,對輕觸按鍵不了解的話可以看看🔗【51單片機】獨立按鍵控制LED燈(四種形式)
說明?在下述圖當中是兩個按鍵控制兩個LED燈的面包板接線圖。
說明?博主并非使用面包板接線圖的,而是自己使用了洞洞板來焊接一個個模塊,最終合成一個開發板的。就像和普中51的A2開發板一樣。
示例要求如下👇
- 按下B1按鍵第一個LED點亮,再按一下B1按鍵LED燈熄滅。依次...
- 按下B11按鍵第二個LED點亮,再按一下B11按鍵LED燈熄滅。依次...
🎓拓展知識點如下?
- 快捷鍵[CTRL+ALT+空格]可以彈出代碼提示框。?
- 當我們把GPIO初始化配置好了之后單片機默認是低電平,所以我們需要再初始化完成之后再設置成高電平。
- 按鍵當中的初始化使用的Mode模式是上拉模式的。注:當然你也可以自己在外部電路上接一個上拉電阻,這樣模式的選擇也可以不用是上拉模式。
注意?按鍵會產生抖動,一般有兩種方法可以解決按鍵產生抖動的問題。
⒈軟件消抖、定時器掃描或延時函數。
⒉硬件消抖、小電容103并聯接地。
第一個程序代碼
示例代碼如下?
main.c
#include "stm32f10x.h" // Device header #include "Delay.h" #include "LED.h" #include "Key.h" #include "stdint.h" uint8_t Ret;int main(void) {LED_Init();Key_Init();while (1){Ret=KeyNum();if(Ret==1){ LED1_Ture();}else if(Ret==2){LED2_Ture();}else{LED3_Ture();}} }
LED.c
#include "stm32f10x.h" // Device header/* 概述:LED的初始化函數。 ㈠使用RCC開啟GPIO的時鐘 [RCC-即復位與時鐘控制,主要是通過寄存器配置時鐘源]㈡使用GPIO_Init函數初始化GPIO口。㈢使用輸出或者輸入函數控制GPIO口。 說明:快捷鍵[CTRL+ALT+空格]可以彈出代碼提示框。 */ void LED_Init() {RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_15 | GPIO_Pin_14;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_Init(GPIOC, &GPIO_InitStructure);GPIO_SetBits(GPIOA, GPIO_Pin_0);//GPIO_Pin_x為高電平GPIO_SetBits(GPIOC, GPIO_Pin_15 | GPIO_Pin_14); //GPIO_Pin_x為高電平 } //ON:打開 OFF:關閉 void LED1_ON() {GPIO_SetBits(GPIOA,GPIO_Pin_1); }void LED1_OFF() {GPIO_ResetBits(GPIOA,GPIO_Pin_1); }void LED2_ON() {GPIO_SetBits(GPIOC,GPIO_Pin_15); }void LED2_OFF() {GPIO_ResetBits(GPIOC,GPIO_Pin_15); } //IO電平翻轉 void LED1_Ture() {if(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0) == 0){//如果PA0的輸出寄存器=0GPIO_SetBits(GPIOA, GPIO_Pin_0);//PA1=1}else//如果PA0的輸出寄存器=1{GPIO_ResetBits(GPIOA,GPIO_Pin_0);//PA1=0} }void LED2_Ture() {if(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_15) == 0){GPIO_SetBits(GPIOC, GPIO_Pin_15);}else{GPIO_ResetBits(GPIOC,GPIO_Pin_15);} }void LED3_Ture() {if(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_14) == 0){GPIO_SetBits(GPIOC, GPIO_Pin_14);}else{GPIO_ResetBits(GPIOC,GPIO_Pin_14);} }
LED.h
#ifndef __LED_H #define __LED_Hextern void LED_Init(void);extern void LED1_ON(void); extern void LED1_OFF(void); extern void LED2_ON(void); extern void LED2_OFF(void);extern void LED1_Ture(void); extern void LED2_Ture(void); extern void LED3_Ture(void); #endif
Key.c
#include "stm32f10x.h" // Device header #include "Delay.h"void Key_Init(void) {RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure); }uint8_t Key_GetNum(void) {uint8_t KeyNum = 0;if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0){Delay_ms(20);while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);Delay_ms(20);KeyNum = 1;}if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0){Delay_ms(20);while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);Delay_ms(20);KeyNum = 2;}return KeyNum; }
Key.h
#ifndef __Key_H #define __Key_Hextern void Key_Init(void); extern unsigned char KeyNum(void);#endif
說明?以上便是獨立按鍵控制LED的全部代碼。
注意?在這里對應的按鍵是可以控制對應LED燈的以及翻轉狀態,當按鍵按下的時候對應的LED燈點亮、當按鍵松手的時候對應的LED燈熄滅。
//大多數運用場景在IO口電平翻轉 GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0)
⒉光敏傳感器控制蜂鳴器
概述?第二個示例講的是用光敏電阻傳感器控制蜂鳴器。如果你對蜂鳴器不是很理解的話,推薦看看博主寫的這篇文章。🔗
說明?在下述圖當中是兩個按鍵控制兩個LED燈的面包板接線圖。
重要知識點?在這里還是主要介紹下光敏傳感器到底是啥玩意。
說明?在上述主要是由光敏電阻、電位器、電阻、LED燈、LM393組合而成的光敏傳感器的模塊,其主要功能是?光線越強,光敏電阻的阻值就會越小,信號輸出低電平(燈亮),當遮擋光敏電阻的時候,信號輸出為高電平(燈滅)
引腳?分別有四個引腳,1.Vcc、2.Gnd、3.AO、4.DO?
注意?AO是ADC模數轉換的引腳。
檢測距離調節:順時針調節電位器,檢測距離增加;逆時針調節電位器,檢測距離減少。
第二個程序代碼
示例代碼如下?
main.h
#include "stm32f10x.h" // Device header #include "Delay.h" #include "Buzzer.h" #include "LightSensor.h"int main(void) {Buzzer_Init();LightSensor_Init();while (1){if (LightSensor_Get() == 1){Buzzer_ON();}else{Buzzer_OFF();}} }
Key.c
#include "stm32f10x.h" // Device header #include "Delay.h"void Key_Init(void) {RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure); }uint8_t Key_GetNum(void) {uint8_t KeyNum = 0;if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0){Delay_ms(20);while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);Delay_ms(20);KeyNum = 1;}if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0){Delay_ms(20);while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);Delay_ms(20);KeyNum = 2;}return KeyNum; }
Key.h
#ifndef __KEY_H #define __KEY_Hvoid Key_Init(void); uint8_t Key_GetNum(void);#endif
Buzzer.c
#include "stm32f10x.h" // Device headervoid Buzzer_Init(void) {RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);GPIO_SetBits(GPIOB, GPIO_Pin_12); }void Buzzer_ON(void) {GPIO_ResetBits(GPIOB, GPIO_Pin_12); }void Buzzer_OFF(void) {GPIO_SetBits(GPIOB, GPIO_Pin_12); }void Buzzer_Turn(void) {if (GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12) == 0){GPIO_SetBits(GPIOB, GPIO_Pin_12);}else{GPIO_ResetBits(GPIOB, GPIO_Pin_12);} }
Buzzer.h
#ifndef __BUZZER_H #define __BUZZER_Hvoid Buzzer_Init(void); void Buzzer_ON(void); void Buzzer_OFF(void); void Buzzer_Turn(void);#endif
LightSensor.c
#include "stm32f10x.h" // Device headervoid LightSensor_Init(void) {RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure); }uint8_t LightSensor_Get(void) {return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13); }
LightSensor.h
#ifndef __LIGHT_SENSOR_H #define __LIGHT_SENSOR_Hvoid LightSensor_Init(void); uint8_t LightSensor_Get(void);#endif
說明?以上便是第二個示例代碼的全部程序內容。