新手上路
- 积分
- 20
- 金钱
- 20
- 注册时间
- 2020-7-4
- 在线时间
- 6 小时
|
楼主 |
发表于 2022-12-7 21:29:30
|
显示全部楼层
本帖最后由 haoyue1995 于 2022-12-8 13:02 编辑
更新一下,我好像发现问题所在了
我把例程里串口初始化屏蔽了,例程就成我现在程序的样子,PB5常亮PWM不输出
我把例程里串口初始化解除屏蔽了,例程就能够正常输出PWM了,PB5亮灭发生了变化然后发现问题出在语句__HAL_RCC_AFIO_CLK_ENABLE();上,而且这个语句必须在PWM输出引脚重映射语句前执行,否则就不会执行PWM输出引脚重映射,PWM不会才PB5输出。
现在在我的程序中单独执行PWM输出,PB5就可以正常亮灭了
- //Main.c
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- void SystemClock_Config(void);
- void LED_Init(void);
- #define LED1_ON HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5, GPIO_PIN_RESET)
- #define LED1_OFF HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5, GPIO_PIN_SET)
- #define LED1_Toggle HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_5)
- #define LED2_ON HAL_GPIO_WritePin(GPIOE,GPIO_PIN_5, GPIO_PIN_RESET)
- #define LED2_OFF HAL_GPIO_WritePin(GPIOE,GPIO_PIN_5, GPIO_PIN_SET)
- #define LED2_Toggle HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_5)
- void Tim3_Init(void);
- void Tim3B_Init(void);
- TIM_HandleTypeDef TIM3_Handler; //定时器句柄
- TIM_OC_InitTypeDef TIM3_CH2Handler; //定时器3通道2句柄
- uint ret;
- int compare=0;
- int main(void)
- {
- HAL_Init();
- SystemClock_Config();
-
- // LED_Init();
- // LED1_OFF;
- // LED2_OFF;
-
- //Tim3B_Init();
-
- Tim3_Init();
-
- while (1)
- {
-
- }
- }
- /**
- * @brief System Clock Configuration
- * The system Clock is configured as follow :
- * System Clock source = PLL (HSE)
- * SYSCLK(Hz) = 72000000
- * HCLK(Hz) = 72000000
- * AHB Prescaler = 1
- * APB1 Prescaler = 2
- * APB2 Prescaler = 1
- * HSE Frequency(Hz) = 8000000
- * HSE PREDIV1 = 1
- * PLLMUL = 9
- * Flash Latency(WS) = 2
- * @param None
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_ClkInitTypeDef clkinitstruct = {0};
- RCC_OscInitTypeDef oscinitstruct = {0};
-
- /* Enable HSE Oscillator and activate PLL with HSE as source */
- oscinitstruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- oscinitstruct.HSEState = RCC_HSE_ON;
- oscinitstruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- oscinitstruct.PLL.PLLState = RCC_PLL_ON;
- oscinitstruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- oscinitstruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&oscinitstruct)!= HAL_OK)
- {
- /* Initialization Error */
- while(1);
- }
- /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
- clocks dividers */
- clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;
- clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2;
- if (HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2)!= HAL_OK)
- {
- /* Initialization Error */
- while(1);
- }
- }
- void LED_Init(void)
- {
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOE_CLK_ENABLE();
-
- GPIO_InitTypeDef GPIO_Init_Struct;
-
- GPIO_Init_Struct.Pin=GPIO_PIN_5;
- GPIO_Init_Struct.Mode=GPIO_MODE_OUTPUT_PP;
- GPIO_Init_Struct.Pull=GPIO_PULLUP;
- GPIO_Init_Struct.Speed=GPIO_SPEED_FREQ_HIGH;
-
- HAL_GPIO_Init(GPIOB, &GPIO_Init_Struct);
-
- HAL_GPIO_Init(GPIOE, &GPIO_Init_Struct);
- }
- void Tim3_Init(void)
- {
- TIM3_Handler.Instance=TIM3; //定时器3
- TIM3_Handler.Init.Prescaler=(7200-1); //定时器分频
- TIM3_Handler.Init.CounterMode=TIM_COUNTERMODE_UP;//向上计数模式
- TIM3_Handler.Init.Period=(50000-1); //自动重装载值
- TIM3_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
- HAL_TIM_PWM_Init(&TIM3_Handler); //初始化PWM
-
- TIM3_CH2Handler.OCMode=TIM_OCMODE_PWM1; //模式选择PWM1
- TIM3_CH2Handler.Pulse=(50000-1)/2; //设置比较值,此值用来确定占空比,默认比较值为自动重装载值的一半,即占空比为50%
- TIM3_CH2Handler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低
-
- HAL_TIM_PWM_ConfigChannel(&TIM3_Handler,&TIM3_CH2Handler,TIM_CHANNEL_2);//配置TIM3通道2
-
- HAL_TIM_PWM_Start(&TIM3_Handler,TIM_CHANNEL_2);//开启PWM通道2
- }
- void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
- {
- GPIO_InitTypeDef GPIO_Initure;
-
- if(htim->Instance==TIM3)
- {
- __HAL_RCC_TIM3_CLK_ENABLE(); //使能定时器3
- __HAL_RCC_AFIO_CLK_ENABLE();
- __HAL_AFIO_REMAP_TIM3_PARTIAL(); //TIM3通道引脚部分重映射使能
- __HAL_RCC_GPIOB_CLK_ENABLE(); //开启GPIOB时钟
-
-
- GPIO_Initure.Pin=GPIO_PIN_5; //PB5
- GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
- //GPIO_Initure.Pull=GPIO_PULLUP; //上拉
- GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;//高速
- HAL_GPIO_Init(GPIOB,&GPIO_Initure);
- }
- }
- void Tim3B_Init(void)
- {
- TIM3_Handler.Instance=TIM3; //定时器3
- TIM3_Handler.Init.Prescaler=(72-1); //定时器分频
- TIM3_Handler.Init.CounterMode=TIM_COUNTERMODE_UP;//向上计数模式
- TIM3_Handler.Init.Period=(50000-1); //自动重装载值
- TIM3_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
- HAL_TIM_Base_Init(&TIM3_Handler); //初始化PWM
-
- HAL_TIM_Base_Start_IT(&TIM3_Handler); //使能定时器3和定时器3更新中断:TIM_IT_UPDATE
- }
- void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance==TIM3)
- {
- __HAL_RCC_TIM3_CLK_ENABLE(); //使能TIM3时钟
- HAL_NVIC_SetPriority(TIM3_IRQn,1,3); //设置中断优先级,抢占优先级1,子优先级3
- HAL_NVIC_EnableIRQ(TIM3_IRQn); //开启ITM3中断
- }
- }
- //定时器3中断服务函数
- void TIM3_IRQHandler(void)
- {
- HAL_TIM_IRQHandler(&TIM3_Handler);
- }
- //回调函数,定时器中断服务函数调用
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim==(&TIM3_Handler))
- {
- LED1_Toggle; //LED1反转
- }
- }
复制代码
|
|