24小(xiǎo)时联系電(diàn)话:18217114652、13661815404

中文(wén)

您当前的位置:
首页>
電(diàn)子资讯>
技术专题>
有(yǒu)关于UCOSIII无法进...

技术专题

有(yǒu)关于UCOSIII无法进入软件计时器的某些函数且不报错的情况


发生这种情况的原因是由于定义的ucosiii自带的软件单次计时器错误的时间设置导致的,如下所示:

图中有(yǒu)两个OS_TICK类型,单次计时器的定时时间应设置在第一个OS_TICK上。上面的是初始定时时间设置,可(kě)用(yòng)于单次计时器周期计时器中;下面的是重装载时间设置,用(yòng)于仅周期定时器中。(之后在周期定时器的设置时将两个参数都设置為(wèi)0也无法正确触发)

//错误代码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,//定时器回调函数

            (void    *)0, //参数為(wèi)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,//定时器回调函数

            (void    *)0, //参数為(wèi)0

            (OS_ERR    *)&err); //返回的错误码

以下是源代码中关于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().************************************************************************************************************************

如果还有(yǒu)疑问可(kě)以参考详细的关于定时器用(yòng)法的中文(wén)描述:
UCOSIII软件计时器的使用(yòng)

2.有(yǒu)关于UCOSIII同一计时器的不同line的pwm输出不能(néng)共存的情况

发生这种情况的原因是由于定义初始化函数时加入了TIM_DeInit()函数导致的,如下所示:

GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;


TIM_DeInit(TIM4);//把TIM4的设置清空


RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 


//设置该引脚為(wèi)复用(yòng)输出功能(néng),输出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; //设置在下一个更新(xīn)事件装入活动的自动重装载寄存器周期

TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用(yòng)来作為(wèi)TIMx时钟频率除数的预分(fēn)频值

TIM_TimeBaseStructure.TIM_ClockDivision = 0; 

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式

TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); 

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM模式1

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能(néng)

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性:TIM输出比较极性高

TIM_OC3Init(TIM4, &TIM_OCInitStructure); //初始化外设TIM4

TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);//CH3预装载使能(néng)

TIM_Cmd(TIM4, ENABLE); //使能(néng)TIM3

TIM_SetCompare3(TIM4, 0);*****************以下為(wèi)部分(fēn)TIM_DeInit()函数代码**********************************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)初始化其他(tā)初始化函数中对于TIM4的设置清空,导致我们设置的其他(tā)通道的PWM设置被清空,因此无法在TIM4中输出2个PWM信号。

经过此次问题,我明白了每一个函数都要好好看清楚具體(tǐ)的功能(néng)作用(yòng),不能(néng)一抄了之,写初始化函数的时候需要保证每一句都明白它的功能(néng)作用(yòng),不然会在后续的调试过程中产生隐患。

 

请输入搜索关键字

确定