帮我写一个基于stm32的声音传感器模块检测周围的分贝数并olde显示屏上显示的代码
时间: 2023-03-24 18:02:30 浏览: 104
好的,以下是基于stm32的声音传感器模块检测周围的分贝数并olde显示屏上显示的代码:
#include "stm32f10x.h"
#include "oled.h"
#define ADC1_DR_Address ((u32)x4001244C)
void ADC1_Init(void);
u16 Get_Adc(u8 ch);
u16 Get_Adc_Average(u8 ch, u8 times);
int main(void)
{
u16 adc_value;
float db_value;
char db_str[10];
ADC1_Init();
OLED_Init();
while(1)
{
adc_value = Get_Adc_Average(ADC_Channel_, 10);
db_value = (adc_value / 4096.) * 3.3 / .001;
sprintf(db_str, "%.2f dB", db_value);
OLED_ShowString(, , db_str);
}
}
void ADC1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
u16 Get_Adc(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
return ADC_GetConversionValue(ADC1);
}
u16 Get_Adc_Average(u8 ch, u8 times)
{
u32 temp_val = ;
for(u8 i = ; i < times; i++)
{
temp_val += Get_Adc(ch);
}
return temp_val / times;
}