一.系统概述
系统以STM32单片机作为核心控制器,采用超声波测距模块进行距离检测。测量到的距离数据会显示在屏幕上,另外,通过按键操作可以控制两个LED灯的开关状态。
二.仿真概述
- 距离测量与显示:
- 利用HC-SR04超声波测距传感器进行距离测量,并将结果显示在OLED12864显示屏上。
- LED控制:
- 通过按键操作实现对LED灯的开关控制。
三.程序设计
#include "stm32f10x.h" // Device header
#include "OLED.h"
#include "HCSR04.h"
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "timer.h"
uint16_t T;
uint8_t KeyNum;
int main(void)
{
OLED_Init();
HCSR04_Init();
Key_Init();
LED_Init();
Timer_Init();
OLED_ShowString(2, 1, "distance:");
OLED_ShowString(2, 13, "cm");
while (1)
{
KeyNum = Key_GetNum();
if (KeyNum == 1) //按键1按下
{
LED1_Turn(); //LED1翻转
}
if (KeyNum == 2) //按键2按下
{
LED2_Turn(); //LED2翻转
}
T = HCSR04_GetValue();
OLED_ShowNum(2, 10, T, 3);
Delay_ms(100);
}
}
24-32f-52