如果紅外光電開關檢測到障礙物,蜂鳴器響,RGBLED 顯示紅色,如果沒有檢測到障礙物,蜂鳴器不響,RGBLED 顯示綠色
注意:添加一個 InfraredPSwitch.c 和 InfraredPSwitch.h 文件
InfraredPSwitch.h 文件裏面,對紅外光電開關使用的引腳和端口進行宏定義
InfraredPSwitch.c 文 件 裏 面 , 設 計 一 個 InfraredPSwitch_Init 函 數 , 一 個
InfraredPSwitch_Check()函數,返回值爲檢測到的狀態值
如有好的建議還望不吝賜教嚛
直接上代碼
InfrarePSwitch_H
#ifndef __InfrarePSwitch_H
#define __InfrarePSwitch_H
#include "sys.h"
#define sign GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_1)
void InfrarePSwitch_Init(void);
void InfrarePSwitch_Check(void);
#endif
InfrarePSwitch.c
#include "sys.h"
#include "delay.h"
#include "RGBLED.h"
#include "BEEP.h"
#include "usart.h"
#include "InfrarePSwitch.h"
//初始化PF1口讀取光電開關電平
void InfrarePSwitch_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_1);
}
void InfrarePSwitch_Check(void)
{
//GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_1);
if(sign == 0)
{
RGBLED_Display(RED, 1);
BEEP = 0;
}else
{
RGBLED_Display(GREEN, 1);
BEEP = 1;
}
}`
關於RGBLED_Display();還請參見第一篇筆記。
鏈接呈上 https://mp.csdn.net/mdeditor/90343426#
注意: 採用電源模塊爲紅外光電供電時,注意GND和VCC的連接。
將兩個線都接在電源模塊上;不要一根接在電源模塊,另一根接在開發板上。