标题: stm32温湿度-超声波-LCD1602结合项目 附Proteus仿真程序 [打印本页]
作者: 1244986336 时间: 2020-7-10 10:23
标题: stm32温湿度-超声波-LCD1602结合项目 附Proteus仿真程序
程序实现功能:
程序基于stm32芯片实现了控制LED灯亮灭、按键控制、串口通信、电机控制、温湿度数据采集、超声波测距、LCD显示屏显示内容这几个功能,并用proteus8进行仿真。
1.电路图 1、我设计的电路图如下所示:
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
2. 程序功能介绍程序总共包括控制LED灯亮灭、按键控制、串口通信、电机控制、温湿度数据采集、超声波测距、LCD显示屏显示内容这几个功能,以下是这些功能的介绍:
2.1. LED灯亮灭与按键控制程序默认运行时,两个LED灯会被点亮。当按下按钮后,两个LED灯会闪烁。
2.2. 串口通信程序运行时,虚拟终端接串口通信用到的接收端和发送端,通过配置波特率、传输的奇偶校验位、停止位、字长以及重定向,将printf函数打印的内容打印到虚拟终端上。
2.3. 电机的控制通过L298芯片,改变功率,来控制电机的转动。
2.4. 温湿度数据采集用DHT11温湿度传感器来采集温湿度信息。
通过了解DHT11的工作时序,设计好对应的延时函数,进行数据采集,同时通过循环将每次采集的数据打印在虚拟终端上。
2.5. 超声波测距通过超声波测距模块来进行测距。
通过了解超声波测距模块的时序,利用定时器,采集测到的距离,并且通过循环打印在虚拟终端上。
2.6. LCD液晶显示器显示数据客户端可以通过发送“database”字符串进入到数据库的相关服务,在选择相应功能执行。如下图所示:
通过了解LM16016l中各引脚功能,相关控制指令以及写时序和读时序,在程序运行时,在显示屏上打印“hello”。
3.具体代码如下:
* 文件名 : UltrasonicWave.c
* 描述 :超声波测距模块,UltrasonicWave_Configuration()函数
初始化超声模块,UltrasonicWave_StartMeasure()函数
启动测距,并将测得的数据通过串口1打印出来
* 实验平台:野火STM32开发板
* 硬件连接:------------------
* | PC8 - TRIG |
* | PC9 - ECHO |
* ------------------
* 库版本 :ST3.5.0
UltrasonicWave.H
- #ifndef __UltrasonicWave_H
- #define __UltrasonicWave_H
- void UltrasonicWave_Configuration(void); //对超声波模块初始化
- void UltrasonicWave_StartMeasure(void); //开始测距,发送一个>10us的脉冲,然后测量返回的高电平时间
- #endif /* __UltrasonicWave_H */
复制代码
UltrasonicWave.c
*********************************************************************************/
#include "./Wave/UltrasonicWave.h"
#include "./usart/bsp_usart.h"
#include "./Tim2/TIM2.h"
#define TRIG_PORT GPIOC //TRIG
#define ECHO_PORT GPIOC //ECHO
#define TRIG_PIN GPIO_Pin_8 //TRIG
#define ECHO_PIN GPIO_Pin_9 //ECHO
unsigned short int UltrasonicWave_Distance; //计算出的距离
/*
* 函数名:DelayTime_us
* 描述 :1us延时函数
* 输入 :Time 延时的时间 US
* 输出 :无
*/
void DelayTime_us(int Time)
{
unsigned char i;
for ( ; Time>0; Time--)
for ( i = 0; i < 72; i++ );
}
/*
* 函数名:UltrasonicWave_Configuration
* 描述 :超声波模块的初始化
* 输入 :无
* 输出 :无
*/
void UltrasonicWave_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = TRIG_PIN; //PC8接TRIG
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设为推挽输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(TRIG_PORT, &GPIO_InitStructure); //初始化外设GPIO
GPIO_InitStructure.GPIO_Pin = ECHO_PIN; //PC9接ECH0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设为输入
GPIO_Init(ECHO_PORT,&GPIO_InitStructure); //初始化GPIOA
}
/*
* 函数名:UltrasonicWave_CalculateTime
* 描述 :计算距离
* 输入 :无
* 输出 :无
*/
void UltrasonicWave_CalculateTime(void)
{
UltrasonicWave_Distance=TIM_GetCounter(TIM2)*5*34/2000;
}
/*
* 函数名:UltrasonicWave_StartMeasure
* 描述 :开始测距,发送一个>10us的脉冲,然后测量返回的高电平时间
* 输入 :无
* 输出 :无
*/
void UltrasonicWave_StartMeasure(void)
{
GPIO_SetBits(TRIG_PORT,TRIG_PIN); //送>10US的高电平
DelayTime_us(20); //延时20US
GPIO_ResetBits(TRIG_PORT,TRIG_PIN);
while(!GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); //等待高电平
TIM_Cmd(TIM2, ENABLE); //开启时钟
while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); //等待低电平
TIM_Cmd(TIM2, DISABLE); //定时器2失能
UltrasonicWave_CalculateTime(); //计算距离
TIM_SetCounter(TIM2,0);
printf("\r\ndistance:%d%d cm\r\n",UltrasonicWave_Distance/256,UltrasonicWave_Distance%256);
}
motor.h
- #ifndef __MOTOR_H
- #define __MOTOR_H
- #include "stm32f10x.h"
- #define DEBUG_MOTOR_CLK RCC_APB2Periph_GPIOB
- #define MOTOR_GPIO GPIOB
- #define Motor_Pin_1 GPIO_Pin_13
- #define Motor_Pin_2 GPIO_Pin_14
- void motor_init(void);
- void motor_stop(void);
- #endif
复制代码 motor.c:
- #include <./MOTOR/motor.h>
- //初始化电机,让电机跑起来
- void motor_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = Motor_Pin_1 | Motor_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(MOTOR_GPIO, &GPIO_InitStructure);
- GPIO_SetBits(MOTOR_GPIO,Motor_Pin_1);
- GPIO_ResetBits(MOTOR_GPIO,Motor_Pin_2);
- }
- //让电机停止
- void motor_stop(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = Motor_Pin_1 | Motor_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(MOTOR_GPIO, &GPIO_InitStructure);
- GPIO_SetBits(MOTOR_GPIO,Motor_Pin_1);
- GPIO_SetBits(MOTOR_GPIO,Motor_Pin_2);
- }
复制代码 温湿度.H
- #ifndef __DHT11_H
- #define __DHT11_H
- #include "stm32f10x.h"
- /************************** DHT11 数据类型定义********************************/
- typedef struct
- {
- uint8_t humi_int; //湿度的整数部分
- uint8_t humi_deci; //湿度的小数部分
- uint8_t temp_int; //温度的整数部分
- uint8_t temp_deci; //温度的小数部分
- uint8_t check_sum; //校验和
-
- } DHT11_Data_TypeDef;
- /************************** DHT11 连接引脚定义********************************/
- #define DHT11_Dout_SCK_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define DHT11_Dout_GPIO_CLK RCC_APB2Periph_GPIOC
- #define DHT11_Dout_GPIO_PORT GPIOC
- #define DHT11_Dout_GPIO_PIN GPIO_Pin_15
- /************************** DHT11 函数宏定义********************************/
- #define DHT11_Dout_0 GPIO_ResetBits ( DHT11_Dout_GPIO_PORT, DHT11_Dout_GPIO_PIN )
- #define DHT11_Dout_1 GPIO_SetBits ( DHT11_Dout_GPIO_PORT, DHT11_Dout_GPIO_PIN )
- #define DHT11_Dout_IN() GPIO_ReadInputDataBit ( DHT11_Dout_GPIO_PORT, DHT11_Dout_GPIO_PIN )
- /************************** DHT11 函数声明 ********************************/
- void DHT11_Init ( void );
- uint8_t DHT11_Read_TempAndHumidity ( DHT11_Data_TypeDef * DHT11_Data );
- void dht11_delay_ms(int32_t time);
- void dht11_delay_us(int32_t time);
- #endif /* __DHT11_H */
复制代码 温湿度.c
- /**
- ******************************************************************************
- * @file bsp_dht11.c
- * @author fire
- * @version V1.0
- * @date 2015-xx-xx
- * @brief 温湿度传感器应用函数接口
- ******************************************************************************
- */
- #include "./dht11/bsp_dht11.h"
- #include "./systick/bsp_SysTick.h"
复制代码
LCD.H
- #ifndef __LCD_H
- #define __LCD_H
- #include "stm32f10x.h"
- /************************** LCD连接引脚定义********************************/
- #define LCD_Dout_SCK_APBxClock_FUN RCC_APB2PeriphClockCmd
- #define LCD_Dout_GPIO_CLK RCC_APB2Periph_GPIOC
- /************************** LCD函数********************************/
- void LcdWriteCom(uint8_t com);
- void LcdGpioInit(void);
- void LcdWriteDate(uint8_t date);
- void LCD1602Init(void);
- void LCD1602WriteCommand(uint8_t comm);
- #endif /* __LCD_H */
复制代码 LCD.C
- #include "./LCD/bsp_lcd.h"
- #include "./systick/bsp_SysTick.h"
- uint8_t const table1[]="hello";
- /*初始化用到的引脚*/
- void LcdGpioInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
-
- GPIO_WriteBit(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12,Bit_RESET);
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init ( GPIOC, &GPIO_InitStruct);
- }
- /*******************************************************************************
- * 函 数 名 :write_com
- * 函数功能 :LCD1602 写指令
- * 输 入 :无
- * 输 出 :无
- *******************************************************************************/
- void LcdWriteCom(uint8_t com)
- {
- Delay_us(20);
- GPIOC->BSRR = 0x00ff0000;
- GPIOC->BSRR = (com);
- GPIO_WriteBit(GPIOC,GPIO_Pin_10,Bit_RESET); //LCDRS
- GPIO_WriteBit(GPIOC,GPIO_Pin_11,Bit_RESET); //LCDRW
- GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_RESET); //LCDEN
- Delay_us(10);
- GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_SET); //LCDEN
- Delay_us(10);
- GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_RESET); //LCDEN
- Delay_us(10);
- }
- /*******************************************************************************
- * 函 数 名 :write_Date
- * 函数功能 :LCD1602 写数据
- * 输 入 :无
- * 输 出 :无
- *******************************************************************************/
- void LcdWriteDate(uint8_t date)
- {
- Delay_us(20);
- GPIOC->BSRR = 0x00ff0000;
- GPIOC->BSRR = (date);
- GPIO_WriteBit(GPIOC,GPIO_Pin_10,Bit_SET); //LCDRS
- GPIO_WriteBit(GPIOC,GPIO_Pin_11,Bit_RESET); //LCDRW
- GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_RESET); //LCDEN
- Delay_us(10);
- GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_SET); //LCDEN
- Delay_us(10);
- GPIO_WriteBit(GPIOC,GPIO_Pin_12,Bit_RESET); //LCDEN
- Delay_us(10);
- }
- /*******************************************************************************
- * 函 数 名 :LCD1602Init
- * 函数功能 :LCD1602初始化
- * 输 入 :无
- * 输 出 :无
- *******************************************************************************/
- void LCD1602Init(void)
- {
- uint8_t index=0;
- Delay_ms(10);
- LcdWriteCom(0x38); //设置16*2显示,8位数据接口
- LcdWriteCom(0x0c); //开显示,显示光标且闪烁
- LcdWriteCom(0x06);//写一个指针自动加一
- LcdWriteCom(0x01);//清屏
- Delay_ms(10);//延时一段时间时间,等待LCD1602稳定
-
- LcdWriteCom(0x80);//设置第一行 数据地址指针
- for(index=0;index<13;index++)
- LcdWriteDate(table1[index]); //写入数据
-
- // LcdWriteCom(0xc0);//设置第二行 数据地址指针
- // for(index=0;index<7;index++)
- // LcdWriteDate(table2[index]); //写入数据
- }
- /*******************************************************************************
- * 函 数 名 :LCD1602WriteCommand
- * 函数功能 :显示指令到屏幕 U D L R S
- * 输 入 :comm 字符格式
- * 输 出 :无
- *******************************************************************************/
- void LCD1602WriteCommand(uint8_t comm)
- {
- LcdWriteCom(0xc0 + 14);
- LcdWriteDate(comm); //写入数据
- }
复制代码 main.c
- /**
- ******************************************************************************
- * @file main.c
- * @author fire
- * @version V1.0
- * @date 2020-xx-xx
- * @brief dht11温湿度传感器测试实验
- ******************************************************************************
- */
-
- #include "stm32f10x.h"
- #include "./systick/bsp_SysTick.h"
- #include "./dht11/bsp_dht11.h"
- #include "./usart/bsp_usart.h"
- #include "./Key/bsp_key.h"
- #include "./Led/bsp_led.h"
- #include "./LCD/bsp_lcd.h"
- #include "./MOTOR/motor.h"
- #include "./Tim2/TIM2.h"
- #include "./Wave/UltrasonicWave.h"
- /**
- * @brief 主函数
- * @param 无
- * @retval 无
- */
- int main(void)
- {
-
- DHT11_Data_TypeDef DHT11_Data;
- RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
-
-
- /* 配置SysTick 为1us中断一次 */
- SysTick_Init();
- LED_GPIO_Config();
- //LED1_ON;
- LED2_ON;
- LED3_ON;
- motor_init();
- //NVIC_Configuration();
- TIM2_Configuration();
- UltrasonicWave_Configuration();
- LcdGpioInit();
- LCD1602Init();
- USART_Config();//初始化串口1
- NVIC_Configuration();
- printf("\r\n***秉火STM32 dht11 温湿度传感器实验***\r\n");
- /*初始化DTT11的引脚*/
- DHT11_Init();
- //printf("22\n");
- UltrasonicWave_StartMeasure();
- dht11_delay_ms(100);
- while(1)
- {
- //调用DHT11_Read_TempAndHumidity读取温湿度,若成功则输出该信息
- if( DHT11_Read_TempAndHumidity ( & DHT11_Data ) == SUCCESS)
- {
-
- printf("\r\n读取DHT11成功!\r\n\r\n湿度为%d.%d %RH ,温度为 %d.%d℃ \r\n",DHT11_Data.humi_int,DHT11_Data.humi_deci,DHT11_Data.temp_int,DHT11_Data.temp_deci);
- }
- else
- {
- printf("Read DHT11 ERROR!\r\n");
- }
- Delay_ms(100);
- UltrasonicWave_StartMeasure();
- dht11_delay_ms(100);
-
-
-
-
- if( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON )
- {
- /*LED2关闭*/
- LED2_OFF;
- motor_init();
-
-
- }
- if( Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON )
- {
- /*LED3关闭*/
- LED3_OFF;
- }
-
-
-
-
-
- }
- return 0;
-
- }
- /*********************************************END OF FILE**********************/
复制代码
全部资料51hei下载地址:
stm32温湿度-超声波-LCD结合项目.7z
(540.61 KB, 下载次数: 773)
作者: 51hei电控200522 时间: 2020-7-16 23:44
这个是牛的,给楼主点个赞
作者: Mars_ 时间: 2020-7-30 00:23
看到能仿真成功,我也试试
作者: 撞南墙 时间: 2021-6-14 19:04
为什么keil5显示
..\..\Output\DHT11.axf: error: L6002U: Could not open file ..\..\output\core_cm3.o: No such file or directory
Finished: 0 information, 0 warning, 0 error and 1 fatal error messages.
"..\..\Output\DHT11.axf" - 1 Error(s), 5 Warning(s).
Target not created.
Build Time Elapsed: 00:00:10
作者: 文明233 时间: 2021-6-15 09:16
LCD液晶显示器怎么修改显示
作者: 撞南墙 时间: 2021-6-17 11:20
撞南墙 发表于 2021-6-14 19:04
为什么keil5显示
..\..\Output\DHT11.axf: error: L6002U: Could not open file ..\..\output\core_cm3. ...
把if 改成else if 好了
作者: 撞南墙 时间: 2021-6-17 11:22
撞南墙 发表于 2021-6-14 19:04
为什么keil5显示
..\..\Output\DHT11.axf: error: L6002U: Could not open file ..\..\output\core_cm3. ...
因为用户名是中文的原因
作者: xjj850205 时间: 2022-3-14 12:55
看到能仿真成功,我也试试
作者: dghfhjtugu 时间: 2022-5-25 19:07
在仿真中
作者: dghfhjtugu 时间: 2022-5-25 19:16
*** Using Compiler 'V5.06 update 1 (build 61)', folder: 'E:\keil\ARM\ARMCC\Bin'
Build target 'Target 1'
compiling UltrasonicWave.c...
UltrasonicWave.c(1): error: #5: cannot open source input file "./Wave/UltrasonicWave.h": No such file or directory
#include "./Wave/UltrasonicWave.h"
UltrasonicWave.c: 0 warnings, 1 error
compiling w.c...
w.c(11): error: #5: cannot open source input file "./dht11/bsp_dht11.h": No such file or directory
#include "./dht11/bsp_dht11.h"
w.c: 0 warnings, 1 error
compiling LCD.c...
LCD.c(1): error: #5: cannot open source input file "LCD/bsp_LCD.h": No such file or directory
#include "LCD/bsp_LCD.h"
LCD.c: 0 warnings, 1 error
compiling main.c...
E:\keil\ARM\PACK\Keil\STM32F1xx_DFP\1.0.5\Device\Include\stm32f10x.h(96): error: #35: #error directive: "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"
#error "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"
main.c: 0 warnings, 1 error
".\Objects\1.axf" - 4 Error(s), 0 Warning(s).
Target not created.
Build Time Elapsed: 00:00:00
作者: dghfhjtugu 时间: 2022-5-25 19:36
#error "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"
作者: dghfhjtugu 时间: 2022-5-25 21:35
撞南墙 发表于 2021-6-14 19:04
为什么keil5显示
..\..\Output\DHT11.axf: error: L6002U: Could not open file ..\..\output\core_cm3. ...
你这个好像是目标没有创建
作者: zhenghuixing 时间: 2022-5-29 10:56
试了一下能仿真成功,牛!
作者: yhfgdgr 时间: 2023-4-15 22:25
测量出来的距离误差有点大,把UltrasonicWave_Distance=TIM_GetCounter(TIM2)*5*34/2000;
改成UltrasonicWave_Distance=TIM_GetCounter(TIM2)*0.79;
会好很多
作者: da'da'l'k 时间: 2023-4-17 18:41
看到能仿真成功,我也试试,不知道8.8能不能行
作者: cclucycc 时间: 2025-3-7 20:53
仿真不了啊。。LCD和串口都没反应。。
作者: 1763333333 时间: 2025-6-9 11:01
牛牛牛
作者: 1763333333 时间: 2025-6-11 16:41
左侧找到lcd.c uint8_t const table1[]=
这里改
欢迎光临 (http://www.51hei.com/bbs/) |
Powered by Discuz! X3.1 |