各位專家您好,我在使用CH32V307的時(shí)候希望通過(guò)軟件觸發(fā)輸出單脈沖信號(hào),我的意思先初始化單脈沖,然后每隔一毫秒在PWM1,PWM2,PWM3,PWM4,輸出一個(gè)脈沖信號(hào),現(xiàn)在無(wú)法輸出脈沖信號(hào),煩請(qǐng)各位老師百忙之中指導(dǎo),看看問(wèn)題在哪哈。
我是用的TIMER1,然后需要IO口重定向到 PE14,PE13,PE11,PE9上面,
我的程序如下:
準(zhǔn)備通過(guò)軟件觸發(fā),網(wǎng)上說(shuō)的是軟件觸發(fā)只需要再次使能Timer1即可。
? ?
//初始化TIMER1單脈沖信號(hào)輸出 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; ????//設(shè)置時(shí)鐘為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); } //觸發(fā)一個(gè)單脈沖 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路初始化默認(rèn)低電平 ??stepper_one_pulse_init(0x0000); ??while(1) ??{ ?????//延時(shí)1ms ?????Delay(1); ?????//4路PWM全部輸出一個(gè)脈沖脈沖 ?????stepper_one_pulse_start(1000); ??} }