N32G45X是类似STM32的一种国产方案,我想让PA15输出一组PWM波, 可以选用TIM2通道1(方案1),也可以选用TIM8的反向通道1(方案2)
以输出占空比为50%的方波波形为例: 方案1** 方案1代码 方法如下:
void GPIO_Config(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2,ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_SW_ENABLE,ENABLE);
GPIO_ConfigPinRemap(GPIO_PartialRemap1_TIM2,ENABLE);
GPIO_InitStructure.Pin = GPIO_PIN_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_PIN_15);
}
void TIM2_Init(u16 psc, u16 arr)
{
TIM_TimeBaseInitType TIM_TimeBaseStructure;
OCInitType TIM_OCInitStructure;
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
TIM_TimeBaseStructure.Period = arr;
TIM_TimeBaseStructure.Prescaler = psc;
TIM_TimeBaseStructure.ClkDiv=0;
TIM_TimeBaseStructure.RepetCnt = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIM2, &TIM_TimeBaseStructure);
#if 1
TIM_InitOcStruct(&TIM_OCInitStructure);
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
TIM_OCInitStructure.Pulse = 365;
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_LOW;
TIM_InitOc1(TIM2, &TIM_OCInitStructure);
TIM_ConfigOc1Preload(TIM2, TIM_OC_PRE_LOAD_ENABLE);
#endif
TIM_Enable(TIM2, ENABLE);
}
然后再main中调用之即可。 例如: int main(void) { GPIO_Config(); TIM2_Init(8,799); while(1) {} } 示波器读取PA15对地输出波形如下:
*******一个问题 有的初学者工程师会问:单独输出一路反向通道PWM波是否可以??答案是可以,查看方案2.
*******方案2 方案2代码如下:
void GPIO_Config(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB |RCC_APB2_PERIPH_GPIOC|
RCC_APB2_PERIPH_GPIOD|RCC_APB2_PERIPH_AFIO|RCC_APB2_PERIPH_TIM8, ENABLE);
GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_SW_ENABLE,ENABLE);
GPIO_ConfigPinRemap(GPIO_RMP3_TIM8,ENABLE);
GPIO_InitStructure.Pin = GPIO_PIN_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_PIN_15);
}
void TIM8_Init(u16 psc, u16 arr)
{
TIM_TimeBaseInitType TIM_TimeBaseStructure;
OCInitType TIM_OCInitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM8, ENABLE);
TIM_TimeBaseStructure.Period = arr;
TIM_TimeBaseStructure.Prescaler = psc;
TIM_TimeBaseStructure.ClkDiv=0;
TIM_TimeBaseStructure.RepetCnt = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIM8, &TIM_TimeBaseStructure);
#if 1
TIM_InitOcStruct(&TIM_OCInitStructure);
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;
TIM_OCInitStructure.Pulse = 365;
TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_LOW;
TIM_OCInitStructure.OcNIdleState = TIM_OCN_IDLE_STATE_RESET;
TIM_InitOc1(TIM8, &TIM_OCInitStructure);
TIM_ConfigOc1Preload(TIM8, TIM_OC_PRE_LOAD_ENABLE);
#endif
TIM_Enable(TIM8, ENABLE);
TIM_EnableCtrlPwmOutputs(TIM8,ENABLE);
}
然后再main中调用之即可。 例如: int main(void) { GPIO_Config(); TIM8_Init(8,799); while(1) {} } 示波器读取PA15对地输出波形如下: 拍的有点反光,另外我要说明一点,我设置的就是让它输出占空比为50%的方波。 各位亲们可以修改捕获值和ARR值来配置不同的PWM波。
|