技能梳理21@stm32+oled+声音强度检测+舵机

132 阅读1分钟

检测声音强度,显示大概的分贝数在oled上,不同强度下舵机转不同的角度

1、项目简介

在这里插入图片描述

2、实现逻辑

#通过STM32的adc检测大概声音强度
#在oled上显示声音强度
#舵机根据声音大小转动

3、应用场景

#益智小玩具(听到声音自动转头的小猫模型)

4、核心代码梳理

int main(void)
{
    /* USER CODE BEGIN 1 */
    uint32_t  clk;
    uint8_t pwm;
    /* USER CODE END 1 */

    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_I2C1_Init();
    MX_ADC1_Init();
    MX_TIM1_Init();
    /* USER CODE BEGIN 2 */
    OLED_Init();			//初始化OLED
    OLED_Clear();
    OLED_ShowString(0,3,"SOUND:    .   DB",16);

//    OLED_ShowNum(52,3,123,3,16);
//		OLED_ShowNum(88,3,13,2,16);
    HAL_ADC_Start_IT(&hadc1);

    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 90);
    HAL_Delay(500);



    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
        HAL_Delay(2);
        HAL_ADC_Start_IT(&hadc1);

        if(clk%250 == 0)
        {
            HAL_GPIO_TogglePin(GPIOC, LED_Pin);

            db = 0;
            for(uint8_t i=0; i<50; i++)
            {
                db += (ADC_temp[i]/10);
            }

            db = db/5;

            if(DB < 30)
                DB = db*0.5;
            if(DB<40)
                DB = db*1.5;
            else if(DB < 50)
                DB = db*1.5;
            else DB = db*2;

            DB = DB*1.5;
            if((DB >= 0) && (DB <= 200))
            {
                intDB = (int)DB;
                float DB_tmp = DB - intDB;
                DB_tmp *= 100;
                decDB = DB_tmp;

                OLED_ShowNum(52,3,intDB,3,16);
                OLED_ShowNum(88,3,decDB,2,16);

                if(DB >= 60)
                {
                    if(pwm < 110)
                    {
                        for(uint8_t i=0; i<30; i++)
                        {
                            pwm = 90 + i;
                            __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pwm);
                            HAL_Delay(8);//2400/30=8
                        }
                        HAL_Delay(5000);
                    }
                }
                else
                {
                    if(pwm > 100)
                    {
                        for(uint8_t i=0; i<30; i++)
                        {
                            pwm = 120-i;
                            __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, pwm);
                            HAL_Delay(8);//1000/25=4
                        }
                    }
                }

            }


        }
        clk++;

    }
    /* USER CODE END 3 */
}

5、部分参考资料

6、注意事项

#舵机选360度的
#声音检测模块是现成的,不过不是特别准,而且感觉只对某个频率范围内的声音有检测

完整可运行项目地址