CH32V307 定時(shí)器的單脈沖輸出問(wèn)題求教

各位專家您好,我在使用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);
??}
}


你的AFIO時(shí)鐘似乎沒(méi)開(kāi)


謝謝老師回復(fù),我現(xiàn)在把初始化函數(shù)修改為如下:增加了AFIO的時(shí)鐘使能,但是還是沒(méi)有看到IO口的變化,由于我手頭沒(méi)有調(diào)試器,我也不知道是TIMER沒(méi)有跑起來(lái)還是PWM沒(méi)有成功,能否繼續(xù)指導(dǎ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_TimeBaseInitTypeDef?TIM_TimeBaseInitStructure={0};

????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_GPIOE,?ENABLE?);
????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_TIM1,?ENABLE?);
????//使能remap時(shí)鐘
????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_AFIO,?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?);
????AFIO->PCFR1|=(0x3<<6);
????TIM_TimeBaseInitStructure.TIM_Period?=?arr;
????//設(shè)置TIMER時(shí)鐘為8M
????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);

}



我又修改了下,初始化程序增加了失能影子寄存器,還是沒(méi)有效果,請(qǐng)老師務(wù)必在百忙之中指導(dǎo)下哈,萬(wàn)分感謝。

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_TimeBaseInitTypeDef?TIM_TimeBaseInitStructure={0};

????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_GPIOE,?ENABLE?);
????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_TIM1,?ENABLE?);
????RCC_APB2PeriphClockCmd(?RCC_APB2Periph_AFIO,?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?);
????AFIO->PCFR1|=(0x3<<6);
????GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE);
????TIM_TimeBaseInitStructure.TIM_Period?=?arr;
????//TIM_TimeBaseInitStructure.TIM_Prescaler?=?psc;
????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_OCInitStructure.TIM_OCMode?=?TIM_OCMode_PWM1;
????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_ARRPreloadConfig(TIM1,?DISABLE);
????TIM_OC1PreloadConfig(TIM1,?DISABLE);
????TIM_OC2PreloadConfig(TIM1,?DISABLE);
????TIM_OC3PreloadConfig(TIM1,?DISABLE);
????TIM_OC4PreloadConfig(TIM1,?DISABLE);
????TIM_SelectOnePulseMode(?TIM1,TIM_OPMode_Single);

}



您好,根據(jù)你的描述,若需要每隔1ms輸出一個(gè)脈沖信號(hào),感覺(jué)你可以直接配置使用PWM輸出,低電平的脈寬直接設(shè)置為1ms即可,若需要脈沖方式,可以參考一下附件例程看一下。后續(xù)若有問(wèn)題,可郵箱(lzs@wch.cn)溝通。

icon_rar.gifCH32V307 脈沖輸出.zip



感謝回復(fù),我需要的是任意時(shí)候,我輸出一個(gè)脈沖只能是一個(gè),而且周期也是可以變化的,目前只有單脈沖能滿足我的需求。這個(gè)1ms我是為了方便老師理解和測(cè)試設(shè)計(jì)的,我實(shí)際上脈沖不是固定周期的,也不是固定寬度的


喏,調(diào)用一次OnePulse(var);就產(chǎn)生一個(gè)脈沖,參數(shù)是脈沖寬度,單位是1ms

icon_rar.gifOnePulse.zip



改脈寬時(shí)間直接改ccr就行,arr最好設(shè)置成ccr+1


老師您好, 我們硬件已經(jīng)做好了,對(duì)應(yīng)的是PE8-PE15相關(guān)的引腳,只能使用TIM1,且必須是remap相關(guān)的IO引腳,能否再指導(dǎo)下這個(gè)TIM2跟我的TIM1有啥區(qū)別呢, 我的TIM1就是按照這個(gè)修改的啊

另外,如下這幾句話是通過(guò)通道2觸發(fā)單脈沖,通道2的引腳此時(shí)作為輸入;

我這里由于不需要通道2觸發(fā),且需要將通道2作為輸出引腳也產(chǎn)生脈沖,我沒(méi)有加上這幾句話。

TIM_ICStructInit( &TIM_ICInitStructure );

TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

TIM_ICInitStructure.TIM_ICFilter = 0x00;

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;



只有登錄才能回復(fù),可以選擇微信賬號(hào)登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频