杭州市住房与城乡建设部网站2018怎么做网站淘宝客

当前位置: 首页 > news >正文

杭州市住房与城乡建设部网站,2018怎么做网站淘宝客,谷歌推广怎么做,工会网站平台建设PWMPWM#xff0c;英文名Pulse Width Modulation#xff0c;是脉冲宽度调制缩写#xff0c;它是通过对一系列脉冲的宽度进行调制#xff0c;等效出所需要的波形#xff08;包含形状以及幅值#xff09;#xff0c;对模拟信号电平进行数字编码#xff0c;也就是说通过调…PWMPWM英文名Pulse Width Modulation是脉冲宽度调制缩写它是通过对一系列脉冲的宽度进行调制等效出所需要的波形包含形状以及幅值对模拟信号电平进行数字编码也就是说通过调节占空比的变化来调节信号、能量等的变化占空比就是指在一个周期内信号处于有效电平的时间占据整个信号周期的百分比。PWM是脉冲宽度调制。有效电平持续的时间占整个周期的百分比称为占空比。PWM的输出模式可以修改TIMx_CCRx寄存器的值来修改占空比。PWM模式1在向上计数时一旦CNTCCRx 时输出为有效电平否则为无效电平。在向下计数时一旦CNTCCRx 时输出为无效电平否则为有效电平。PWM模式2在向上计数时一旦CNTCCRx 时输出为无效电平否则为有效电平。在向下计数时一旦CNTCCRx 时输出为有效电平否则为无效电平。STM32F103C8T6的PWM资源高级定时器TIM1:7路的PWM通用定时器TIM2~TIM4:每个定时器各4路的PWMPWM的周期和频率周期是频率的倒数如驱动sg90舵机时PWM信号的频率大概为50HZ即周期为20msTout也就是定时时间PWM实现呼吸灯利用调节PWM的占空比大小来实现呼吸灯PWM周期为0.5ms即频率为2000HZ使用STM32CubeMX创建工程配置SYS配置RCC配置PWM翻看使用手册查看LED1使用哪个PWM选择定时器4打开时钟来源选择中间时钟选择通道三输出PWM配置定时方式定时时间为0.5ms即PSC为71ARR为499PWM的相关信息选择PWM模式1由于需要手动改变PWM的占空比所以设置为0由于点亮LED1的有效电平为低电平所以PWM的极性选择LOW配置工程名称、工程路径选择固件库生成工程main函数编写在main函数中打开某个PWMHAL_TIM_PWM_Start(htim4,TIM_CHANNEL_3);修改某个PWM的占空比即修改TIMx_CCRx寄存器的值HAL_TIM_SetCompare(htim4,TIM_CHANNEL_3,pwmVal);/* USER CODE BEGIN Header / /******************************************************************************** file : main.c* brief : Main program body******************************************************************************* attention** Copyright © 2023 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.******************************************************************************/ /* USER CODE END Header / / Includes ——————————————————————/ #include main.h #include tim.h #include gpio.h/ Private includes ———————————————————-/ / USER CODE BEGIN Includes // USER CODE END Includes // Private typedef ———————————————————–/ / USER CODE BEGIN PTD // USER CODE END PTD // Private define ————————————————————/ / USER CODE BEGIN PD / / USER CODE END PD // Private macro ————————————————————-/ / USER CODE BEGIN PM // USER CODE END PM // Private variables ———————————————————// USER CODE BEGIN PV // USER CODE END PV // Private function prototypes ———————————————–/ void SystemClock_Config(void); / USER CODE BEGIN PFP // USER CODE END PFP // Private user code ———————————————————/ / USER CODE BEGIN 0 // USER CODE END 0 *//* brief The application entry point.* retval int/ int main(void) {/ USER CODE BEGIN 1 /uint16_t pwmVal 0; //占空比大小(CCRx的大小)uint8_t direction 1; //呼吸灯方向 1. 越来越亮 2. 越来越暗/ 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_TIM4_Init();/ USER CODE BEGIN 2 ///初始化之后打开引脚PB8的PWM,即定时器4通道三的PWMHAL_TIM_PWM_Start(htim4,TIM_CHANNEL_3);/ USER CODE END 2 // Infinite loop // USER CODE BEGIN WHILE /while (1){/ USER CODE END WHILE // USER CODE BEGIN 3 */HAL_Delay(3); //控制呼吸灯呼吸速率//控制占空比大小即修改CCRx的大小if(direction){pwmVal;}else{pwmVal–;}//设置了ARR为499因此每计数500为一个周期if(pwmVal 500){ direction 0; //改变呼吸灯方向}else if(pwmVal 0){direction 1; //改变呼吸灯方向}//修改定时器4通道三的PWM的占空比HAL_TIM_SetCompare(htim4,TIM_CHANNEL_3,pwmVal);}/* USER CODE END 3 / }/** brief System Clock Configuration* retval None/ void SystemClock_Config(void) {RCC_OscInitTypeDef RCC_OscInitStruct {0};RCC_ClkInitTypeDef RCC_ClkInitStruct {0};/** Initializes the RCC Oscillators according to the specified parameters in the RCC_OscInitTypeDef structure./RCC_OscInitStruct.OscillatorType RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState RCC_HSE_ON;RCC_OscInitStruct.HSEPredivValue RCC_HSE_PREDIV_DIV1;RCC_OscInitStruct.HSIState RCC_HSI_ON;RCC_OscInitStruct.PLL.PLLState RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLMUL RCC_PLL_MUL9;if (HAL_RCC_OscConfig(RCC_OscInitStruct) ! HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks/RCC_ClkInitStruct.ClockType RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider RCC_HCLK_DIV2;RCC_ClkInitStruct.APB2CLKDivider RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(RCC_ClkInitStruct, FLASH_LATENCY_2) ! HAL_OK){Error_Handler();} }/* USER CODE BEGIN 4 // USER CODE END 4 //** brief This function is executed in case of error occurrence.* retval None/ void Error_Handler(void) {/ USER CODE BEGIN Error_Handler_Debug // User can add his own implementation to report the HAL error return state /__disable_irq();while (1){}/ USER CODE END Error_Handler_Debug / }#ifdef USE_FULL_ASSERT /** brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* param file: pointer to the source file name* param line: assert_param error line source number* retval None*/ void assert_failed(uint8_t file, uint32_t line) {/ USER CODE BEGIN 6 // User can add his own implementation to report the file name and line number,ex: printf(Wrong parameters value: file %s on line %d\r\n, file, line) // USER CODE END 6 / } #endif / USE_FULL_ASSERT */