SensorTile.box声音控制LED亮度
[复制链接]
本帖最后由 littleshrimp 于 2020-2-28 19:56 编辑
SensorTile.box声音控制LED亮度
原理比较简单使用ADC采集麦克风数据做峰峰值计算后控制PWM输出调节LED亮度
VID_20200228_160307.mp4
(5.6 MB, 下载次数: 15)
工程使用STM32CubeMX创建,核心代码如下:
/* USER CODE BEGIN 0 */
uint16_t vpp(uint16_t *buf,uint16_t len)
{
uint16_t max = 0;
uint16_t min = 4095;
for(int i=0;i<len;i++)
{
if(buf[i] > max)max = buf[i];
if(buf[i] < min)min = buf[i];
}
return max - min;
}
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
{
half_flag = 1;
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
cplt_flag = 1;
}
/* USER CODE END 0 */
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* 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_DMA_Init();
MX_ADC1_Init();
MX_DFSDM1_Init();
MX_I2C1_Init();
MX_I2C3_Init();
MX_RTC_Init();
MX_SDMMC1_SD_Init();
MX_SPI1_Init();
MX_SPI2_Init();
MX_SPI3_Init();
MX_TIM2_Init();
MX_TIM6_Init();
MX_TIM7_Init();
MX_TIM15_Init();
MX_FATFS_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_2);
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)adc_buf,ADC_BUF_COUNT);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(half_flag)
{
half_flag = 0;
dutyCycle = vpp(adc_buf, ADC_BUF_COUNT/2);
__HAL_TIM_SET_COMPARE(&htim15, TIM_CHANNEL_2, dutyCycle);
}
if(cplt_flag)
{
cplt_flag = 0;
dutyCycle = vpp(&adc_buf[ADC_BUF_COUNT/2], ADC_BUF_COUNT/2);
__HAL_TIM_SET_COMPARE(&htim15, TIM_CHANNEL_2, dutyCycle);
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
工程文件:
7.box_adc_pwm_led.rar
(10.98 MB, 下载次数: 52)
|