使用stm32cubemx配置外設,代碼使用HAL stm32f1 v1.3.1庫。
用的是stm32l152c開發(fā)板,時鐘頻率32MHZ。
這里,沒有配置中斷。
上圖的意思是,TI2收到1給正脈沖,觸發(fā)TIM1開始計數(shù),經(jīng)過 tDelay后,OC1輸出低,經(jīng)過一個tPulse后,OC1又恢復為高。
The OPM waveform is defined by writing the compare registers (taking into account theclock frequency and the counter prescaler).
? The tDELAY is defined by the value written in the TIMx_CCR1 register.
? The tPULSEis defined by the difference between the auto-reload value and the comparevalue (TIMx_ARR - TIMx_CCR + 1).
Let’s say you want to build a waveform with a transition from ‘0 to ‘1 when a comparematch occurs and a transition from ‘1 to ‘0 when the counter reaches the auto-reloadvalue.
To do this you enable PWM mode 2 by writing OC1M=111 in the TIMx_CCMR1register. You can optionally enable the preload registers by writing OC1PE=1 in the
TIMx_CCMR1 register and ARPE in the TIMx_CR1 register. In thiscase you have towrite the compare value in the TIMx_CCR1 register, the auto-reload value in the
TIMx_ARR register, generate an update by setting the UG bit and wait for externaltrigger event on TI2. CC1P is written to ‘0 in this example.
向比較寄存器寫入數(shù)值(考慮時鐘頻率和計數(shù)分頻來計算)來定義OPM波形。
1)tDelay值是TIMx_CCR1寄存器值
2)tPulse的值是 自動重裝值減掉比較值:(TIMx_ARR - TIMx_CCR + 1)
假如,當比較相符發(fā)生時,你希望得到從0到1的波形,而當計數(shù)器達到自動重裝值時,波形又從1變到0.
這種情況,使用PWM模式2,TIMx_CCMR1 寄存器的OC1M要為111??蛇x:使能重裝寄存器。 在此例子中,你需要把比較值寫到TIMx_CCR1 寄存器,自動重裝值寫到TIMx_ARR寄存器,設置UG位產(chǎn)生一個更新事件,并且等待TI2的外部觸發(fā)事件。CC1P被寫為 0.
看了手冊上面的描述,就明白了:
我按下按鈕,延時2秒,點亮綠燈,停1秒,綠燈滅。
如果是控制可控硅,就是:
檢測到零點,延時x微秒,觸發(fā)可控硅,停1毫秒,關掉可控硅。 生成觸發(fā)脈沖。
可以讓硬件自己處理,不用中斷。
開發(fā)板實現(xiàn)描述1.
while(1)
{
/*USERCODEENDWHILE*/
/*USERCODEBEGIN3*/
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_SET){
HAL_Delay(100);
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_SET){
__HAL_TIM_ENABLE(&htim4);
HAL_TIM_OnePulse_Start(&htim4,TIM_CHANNEL_2);
}
}
}
/*USERCODEEND3*/
HAL_TIM_OnePulse_Start函數(shù)有問題,它總是使能1、2兩個通道,而且它不啟用定時器的計數(shù)。
因此,在它之前要使用宏__HAL_TIM_ENABLE,置位TIMx_CR1的CEN。
需要注意到,單脈沖功能,只能在1、2通道上做。