各位专家您好,我在使用CH32V307的时候希望通过软件触发输出单脉冲信号,我的意思先初始化单脉冲,然后每隔一毫秒在PWM1,PWM2,PWM3,PWM4,输出一个脉冲信号,现在无法输出脉冲信号,烦请各位老师百忙之中指导,看看问题在哪哈。
我是用的TIMER1,然后需要IO口重定向到 PE14,PE13,PE11,PE9上面,
我的程序如下:
准备通过软件触发,网上说的是软件触发只需要再次使能Timer1即可。
//初始化TIMER1单脉冲信号输出 void stepper_one_pulse_init(uint16_t PolarityPins) { const uint16_t arr=0xffff; const uint16_t ccp=0xffff; GPIO_InitTypeDef GPIO_InitStructure={0}; TIM_OCInitTypeDef TIM_OCInitStructure={0}; TIM_ICInitTypeDef TIM_ICInitStructure={0}; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0}; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB2Periph_TIM1, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_11|GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOE, &GPIO_InitStructure ); //重定向TIMER1 AFIO->PCFR1|=(0x3<<6); TIM_TimeBaseInitStructure.TIM_Period = arr; //TIM_TimeBaseInitStructure.TIM_Prescaler = psc; //设置时钟为8M,TIMER_COUNTER_FREQ=8000000 TIM_TimeBaseInitStructure.TIM_Prescaler = (int) (((((float) SystemCoreClock) / TIMER_COUNTER_FREQ)) + 0.5f) - 1; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure); TIM_ARRPreloadConfig(TIM1, DISABLE); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = ccp; TIM_OCInitStructure.TIM_OCPolarity = (PolarityPins&0x08) ? TIM_OCPolarity_High:TIM_OCPolarity_Low; TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCPolarity = (PolarityPins&0x04) ? TIM_OCPolarity_High:TIM_OCPolarity_Low; TIM_OC2Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCPolarity = (PolarityPins&0x02) ? TIM_OCPolarity_High:TIM_OCPolarity_Low; TIM_OC3Init(TIM1, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCPolarity = (PolarityPins&0x01) ? TIM_OCPolarity_High:TIM_OCPolarity_Low; TIM_OC4Init(TIM1, &TIM_OCInitStructure); TIM_SelectOnePulseMode( TIM1,TIM_OPMode_Single); } //触发一个单脉冲 void stepper_one_pulse_start(uint16_t arr) { TIM_Cmd(TIM1, DISABLE); TIM1->CNT=0; TIM1->ATRLR=(arr<<1)+(arr<<2); TIM1->CH1CVR=TIM1->ATRLR>>1; TIM1->CH2CVR=TIM1->ATRLR>>1; TIM1->CH3CVR=TIM1->ATRLR>>1; TIM1->CH4CVR=TIM1->ATRLR>>1; TIM_Cmd(TIM1, ENABLE); } OneTimeTest() { //4路初始化默认低电平 stepper_one_pulse_init(0x0000); while(1) { //延时1ms Delay(1); //4路PWM全部输出一个脉冲脉冲 stepper_one_pulse_start(1000); } }