目录
资料下载地址:基于STM32风速风向检测仿真
一、项目描述
获取风速风向值并在OLED液晶上显示。
二、仿真
系统未上电
系统上电
更改风速风向
三、程序
主程序
#include "main.h"
#include "stm32f1xx.h"
#include "./usart/bsp_debug_usart.h"
#include <stdlib.h>
#include "./adc/bsp_adc.h"
#include "string.h"
#include "oled.h"
uint16_t value[5];
char strvalue[10];
uint8_t Wind_Direc[10][10] =
{
"north ", //北风
"northeast", //东北风
"east ", //东风
"southeast", //东南风
"south ", //南风
"southwest", //西南风
"west ", //西风
"northwest", //西北风
};
int main(void)
{
SystemClock_Config();//配置系统时钟为72 MHz
DEBUG_USART_Config(); //初始化USART1 配置模式为 115200 8-N-1
DisPlay_Init();//配置OLED液晶屏
ADCx_Init();//AD功能初始化
HAL_ADCEx_Calibration_Start(&hadcx);//启用ADC校准
HAL_ADC_Start(&hadcx );//开始采集
delay_ms(10);//等待AD采集
while (1)
{
Get_Pressure_Gas_Value();
}
}
void Get_Pressure_Gas_Value(void)
{
static int idx = 0;
value[0] = ADC_Get_Average(0,1);
Current_Voltage = value[0] * 100 / 4095;//采集电位计电压
sprintf(strvalue,"%d ",Current_Voltage);//整型数据转化为字符串
OLED_ShowString(45,2,strvalue);//显示风速
value[1] = ADC_Get_Average(1,1);
if(value[1] < 511 )
{
idx = 0;
}
else if(value[1] < 1022 && value[1] >= 511)
{
idx = 1;
}
else if(value[1] < 1533 && value[1] >= 1022)
{
idx = 2;
}
else if(value[1] < 2044 && value[1] >= 1533)
{
idx = 3;
}
else if(value[1] < 2555 && value[1] >= 2044)
{
idx = 4;
}
else if(value[1] < 3066 && value[1] >= 2555)
{
idx = 5;
}
else if(value[1] < 3577 && value[1] >= 3066)
{
idx = 6;
}
else if( value[1] >= 3577)
{
idx = 7;
}
OLED_ShowString(45,4,Wind_Direc[idx]);//显示
printf("WindSpeed = %d\r\nWindDirect = %s\r\n" ,Current_Voltage,Wind_Direc[idx]);
HAL_Delay(10);
}
/**
* @brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 72000000
* HCLK(Hz) = 72000000
* AHB Prescaler = 1
* APB1 Prescaler = 2
* APB2 Prescaler = 1
* HSE Frequency(Hz) = 8000000
* HSE PREDIV1 = 1
* PLLMUL = 9
* Flash Latency(WS) = 2
* @param None
* @retval None
*/
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef clkinitstruct = {0};
RCC_OscInitTypeDef oscinitstruct = {0};
/* Enable HSE Oscillator and activate PLL with HSE as source */
oscinitstruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
oscinitstruct.HSEState = RCC_HSE_ON;
oscinitstruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
oscinitstruct.PLL.PLLState = RCC_PLL_ON;
oscinitstruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
oscinitstruct.PLL.PLLMUL = RCC_PLL_MUL5;
if (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)
{
/* Initialization Error */
while(1);
}
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;
clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2)!= HAL_OK)
{
/* Initialization Error */
while(1);
}
}