21ic问答首页 - 用N32G4FR芯片的TIM1生成PWM波,发现波始终有毛刺和突起
用N32G4FR芯片的TIM1生成PWM波,发现波始终有毛刺和突起
#define TIM1_CLK SystemCoreClock/2*2 //定时器的时钟(128MHz-128000000)
#define TM1_FREQ 632000
#define TIM1_dutyratio 4
int main(void)
{
__enable_irq();
RCC_Configuration();
GPIO_Configuration();
Tim1Config(TM1_FREQ);
while(1)
{
}
}
void GPIO_Configuration(void)//IO口的配置
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
/* TIM1 : PA8->TIM1_CH1 */
GPIO_InitStructure.Pin = GPIO_PIN_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
}
void Tim1Config(uint32_t freq)
{
TIM_TimeBaseInitType TIM_TimeBaseStructure;
OCInitType TIM_OCInitStructure;
TIM_TimeBaseStructure.Period =(TIM1_CLK/freq)-1;
TIM_TimeBaseStructure.Prescaler = 0;
TIM_TimeBaseStructure.ClkDiv = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_TimeBaseStructure.RepetCnt = 0;
TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel 1 */
TIM_InitOcStruct(&TIM_OCInitStructure);
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.Pulse =(TIM1_CLK/freq/TIM1_dutyratio)-1;
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
TIM_InitOc1(TIM1, &TIM_OCInitStructure);
//预装载使能
TIM_ConfigOc1Preload(TIM1, TIM_OC_PRE_LOAD_ENABLE);
TIM_ConfigArPreload(TIM1, ENABLE);
/* TIM3 enable counter */
TIM_Enable(TIM1, ENABLE);
/* TIM1 Main Output Enable */
TIM_EnableCtrlPwmOutputs(TIM1, ENABLE);
}
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB| RCC_APB2_PERIPH_AFIO, ENABLE);
/* Enable USART1 Clock */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_USART1, ENABLE);
/* TIM3 clock enable */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
/* TIM1 clock enable */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
}
#define TM1_FREQ 632000
#define TIM1_dutyratio 4
int main(void)
{
__enable_irq();
RCC_Configuration();
GPIO_Configuration();
Tim1Config(TM1_FREQ);
while(1)
{
}
}
void GPIO_Configuration(void)//IO口的配置
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStruct(&GPIO_InitStructure);
/* TIM1 : PA8->TIM1_CH1 */
GPIO_InitStructure.Pin = GPIO_PIN_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
}
void Tim1Config(uint32_t freq)
{
TIM_TimeBaseInitType TIM_TimeBaseStructure;
OCInitType TIM_OCInitStructure;
TIM_TimeBaseStructure.Period =(TIM1_CLK/freq)-1;
TIM_TimeBaseStructure.Prescaler = 0;
TIM_TimeBaseStructure.ClkDiv = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_TimeBaseStructure.RepetCnt = 0;
TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel 1 */
TIM_InitOcStruct(&TIM_OCInitStructure);
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.Pulse =(TIM1_CLK/freq/TIM1_dutyratio)-1;
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
TIM_InitOc1(TIM1, &TIM_OCInitStructure);
//预装载使能
TIM_ConfigOc1Preload(TIM1, TIM_OC_PRE_LOAD_ENABLE);
TIM_ConfigArPreload(TIM1, ENABLE);
/* TIM3 enable counter */
TIM_Enable(TIM1, ENABLE);
/* TIM1 Main Output Enable */
TIM_EnableCtrlPwmOutputs(TIM1, ENABLE);
}
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB| RCC_APB2_PERIPH_AFIO, ENABLE);
/* Enable USART1 Clock */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_USART1, ENABLE);
/* TIM3 clock enable */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
/* TIM1 clock enable */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
}

问答
赞0
确实是示波器和探头的原因,但是不知道原理
评论
2025-11-24
赞0
评论
2025-11-24
您需要登录后才可以回复 登录 | 注册