用
STM32F103C8写了一个
tiM4 PWM输出 输出通道为CH4,但不出PWM,不知寄存器哪里设置不对,求高手指点,
系统时钟为72MHz
TIM4_PWMOut_Init(999,71);
void TIM4_PWMOut_Init(u16 arr,u16 psc)
{
RCC->APB1ENR|=1<<2; //TIM4时钟使能
RCC->APB2ENR|=1<<3;//使能B端口时钟
TIM4->PSC=psc; //72000000/72=1Mh
TIM4->ARR=arr; //1000000/1000=1000 PWM频率为1K
TIM4->CCR4=500;
TIM4->CCMR2|=(6<<12);//PWM模式样
TIM4->CCMR2|=(1<<11);//开启预装载功能
TIM4->CCER|=(1<<12);//CH4 开启输出对应管脚
TIM4->CCER|=0<<13;//高电平有效
TIM4->CR1|=0<<4;//向上计数
TIM4->CR1|=(1<<7);//自动重装允许
TIM4->SR=0X00;
TIM4->DIER|=1<<0;//允许更新
TIM4->DIER|=1<<4;//允许CH4中断
TIM4->EGR|=1<<4;
TIM4->CR1|=0x01; //使能计数器
MY_NVIC_Init(1,3,TIM4_IRQn,2);//中断
}
void TIM4_IRQHandler(void)
{
if(TIM4->SR&0X0010)
{
TIM4->SR&=~(1<<4);
}
}
main.c:
MX_TIM2_Init(200,1000);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
config:
/* TIM2 init function */
void MX_TIM2_Init(int pulse,int period)
{
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = period;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = pulse;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim2);
}
一周热门 更多>