24小時聯(lián)系電話:18217114652、13661815404
中文
技術(shù)專題
有關(guān)于UCOSIII無法進(jìn)入軟件計時器的某些函數(shù)且不報錯的情況
發(fā)生這種情況的原因是由于定義的ucosiii自帶的軟件單次計時器錯誤的時間設(shè)置導(dǎo)致的,如下所示:
圖中有兩個OS_TICK類型,單次計時器的定時時間應(yīng)設(shè)置在第一個OS_TICK上。上面的是初始定時時間設(shè)置,可用于單次計時器與周期計時器中;下面的是重裝載時間設(shè)置,用于僅周期定時器中。(之后在周期定時器的設(shè)置時將兩個參數(shù)都設(shè)置為0也無法正確觸發(fā))
//錯誤代碼OSTmrCreate((OS_TMR *)&tmra_20s, //定時器tmra_20s
(CPU_CHAR *)"tmra_20s", //定時器名字
(OS_TICK )0, //0ms
(OS_TICK )200, //2000*10=20000ms=20s
(OS_OPT )OS_OPT_TMR_ONE_SHOT, //單次定時器
(OS_TMR_CALLBACK_PTR)tmra_20s_callback,//定時器回調(diào)函數(shù)
(void *)0, //參數(shù)為0
(OS_ERR *)&err); //返回的錯誤碼//正確代碼OSTmrCreate((OS_TMR *)&tmra_20s, //定時器tmra_20s
(CPU_CHAR *)"tmra_20s", //定時器名字
(OS_TICK )200, //2000*10=20000ms=20s
(OS_TICK )0, //0ms
(OS_OPT )OS_OPT_TMR_ONE_SHOT, //單次定時器
(OS_TMR_CALLBACK_PTR)tmra_20s_callback,//定時器回調(diào)函數(shù)
(void *)0, //參數(shù)為0
(OS_ERR *)&err); //返回的錯誤碼
以下是源代碼中關(guān)于ucosiii軟件計時器的描述。
************************************************************************************************************************* CREATE A TIMER** Description: This function is called by your application code to create a timer.** Arguments : p_tmr Is a pointer to a timer control block** p_name Is a pointer to an ASCII string that is used to name the timer. Names are useful for* debugging.** dly Initial delay.* If the timer is configured for ONE-SHOT mode, this is the timeout used* If the timer is configured for PERIODIC mode, this is the first timeout to wait for* before the timer starts entering periodic mode** period The 'period' being repeated for the timer.* If you specified 'OS_OPT_TMR_PERIODIC' as an option, when the timer expires, it will* automatically restart with the same period.** opt Specifies either:** OS_OPT_TMR_ONE_SHOT The timer counts down only once* OS_OPT_TMR_PERIODIC The timer counts down and then reloads itself** p_callback Is a pointer to a callback function that will be called when the timer expires. The* callback function must be declared as follows:** void MyCallback (OS_TMR *p_tmr, void *p_arg);** p_callback_arg Is an argument (a pointer) that is passed to the callback function when it is called.** p_err Is a pointer to an error code. '*p_err' will contain one of the following:** OS_ERR_NONE* OS_ERR_ILLEGAL_CREATE_RUN_TIME if you are trying to create the timer after you called* OSSafetyCriticalStart().* OS_ERR_OBJ_CREATED if the timer has already been created* OS_ERR_OBJ_PTR_NULL is 'p_tmr' is a NULL pointer* OS_ERR_OBJ_TYPE if the object type is invalid* OS_ERR_OPT_INVALID you specified an invalid option* OS_ERR_TMR_INVALID_DLY you specified an invalid delay* OS_ERR_TMR_INVALID_PERIOD you specified an invalid period* OS_ERR_TMR_ISR if the call was made from an ISR** Returns : none** Note(s) : 1) This function only creates the timer. In other words, the timer is not started when created. To* start the timer, call OSTmrStart().************************************************************************************************************************
如果還有疑問可以參考詳細(xì)的關(guān)于定時器用法的中文描述:
UCOSIII軟件計時器的使用
2.有關(guān)于UCOSIII同一計時器的不同line的pwm輸出不能共存的情況
發(fā)生這種情況的原因是由于定義初始化函數(shù)時加入了TIM_DeInit()函數(shù)導(dǎo)致的,如下所示:
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM4);//把TIM4的設(shè)置清空
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//設(shè)置該引腳為復(fù)用輸出功能,輸出TIM4,CH3的PWM脈沖波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期
TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//輸出極性:TIM輸出比較極性高
TIM_OC3Init(TIM4, &TIM_OCInitStructure); //初始化外設(shè)TIM4
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);//CH3預(yù)裝載使能
TIM_Cmd(TIM4, ENABLE); //使能TIM3
TIM_SetCompare3(TIM4, 0);*****************以下為部分TIM_DeInit()函數(shù)代碼**********************************void TIM_DeInit(TIM_TypeDef* TIMx){
/* Check the parameters */
assert_param(IS_TIM_ALL_PERIPH(TIMx));
if (TIMx == TIM1)
{
RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, DISABLE);
}
TIM_DeInit(TIM4)初始化其他初始化函數(shù)中對于TIM4的設(shè)置清空,導(dǎo)致我們設(shè)置的其他通道的PWM設(shè)置被清空,因此無法在TIM4中輸出2個PWM信號。
經(jīng)過此次問題,我明白了每一個函數(shù)都要好好看清楚具體的功能作用,不能一抄了之,寫初始化函數(shù)的時候需要保證每一句都明白它的功能作用,不然會在后續(xù)的調(diào)試過程中產(chǎn)生隱患。