1.总结串口的发送和接收功能使用到的函数(见思维导图)
2.总结DMA的作用,和DMA+空闲中断的使用方式(见思维导图)
3.使用PWM+ADC光敏电阻完成光控灯的实验
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_TIM1_Init();MX_TIM2_Init();MX_USART1_UART_Init();MX_ADC_Init();MX_TIM3_Init();/* USER CODE BEGIN 2 */HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_3);//打开定时器的PWM通道3//TIM3->CCR3 = 0;//改变CCR的值,范围在0-999,CCR的值不能超过重装载值ARR的值int adc_val = 0;//float volt = 0;/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */HAL_ADC_Start(&hadc);//开启ADC采样adc_val = HAL_ADC_GetValue(&hadc) - 1000;//获取ADC采样的值printf("adc_val = %d\r\n",adc_val);if(adc_val > 0 && adc_val < 999){//volt = (adc_val /4095.0f * 3.3f);TIM3->CCR3 = adc_val;//改变CCR的值,控制灯的亮度}else{TIM3->CCR3 = 0;}}/* USER CODE END 3 */
}
效果视频:
QQ视频20240401200042