程序實現功能:
程序基于stm32芯片實現了控制LED燈亮滅、按鍵控制、串口通信、電機控制、溫濕度數據采集、超聲波測距、LCD顯示屏顯示內容這幾個功能,并用proteus8進行仿真。
1.電路圖 1、我設計的電路圖如下所示:
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
圖片1.png (587.17 KB, 下載次數: 75)
下載附件
2020-7-10 09:54 上傳
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)
2020-7-10 15:21 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|