时钟节拍(实现精确延时)
设置sysTICk。
OSIntEnter
Description
OSIntEnter() notifies µC/OS-III that an ISR is being processed. This allows µC/OS-III to keep
track of interrupt nesting. OSIntEnter() is used in conjunction with OSIntExit(). This function
is generally called at the beginning of ISRs. Note that on some CPU architectures, it must be
written in assembly language (shown below in pseudo code):
MyISR:
Save CPU registers;
OSIntEnter(); /* Or, OSIntNestingCtr++ */
:
Process ISR;
:
OSIntExit();
Restore CPU registers;
Return from interrupt;
OSIntExit
Description
OSIntExit() notifies µC/OS-III that an ISR is complete. This allows µC/OS-III to keep track of
interrupt nesting. OSIntExit() is used in conjunction with OSIntEnter(). When the last nested
interrupt completes, OSIntExit() determines if a higher priority task is ready-to-run. If so, the
interrupt returns to the higher priority task instead of the interrupted task.
This function is typically called at the end of ISRs as follows, and on some CPU architectures,
it must be written in assembly language (shown below in pseudo code):
MyISR:
Save CPU registers;
OSIntEnter();
:
Process ISR;
:
OSIntExit();
Restore CPU registers;
Return from interrupt;
OSTimeTick
Description
Notifies the kernel that a tick has just occurred, and that time delays and timeouts need to be
updated. This function must be called from the tick ISR.
#include "os_cfg_app.h"
void SysTick_Configuration(void)
{
while(SysTick_Config(SystemCoreClock/OS_CFG_TICK_RATE_HZ));//while(SysTick_Config(72000));
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
OSIntEnter();
OSTimeTick();
OSIntExit();
}
操作系统延时
OSTimeDly
Description
Allows a task to delay itself for an integral number of clock ticks. The delay can either be relative (delay from current time), periodic (delay occurs at fixed intervals) or absolute (delay
until we reach some time).
In relative mode, rescheduling always occurs when the number of clock ticks is greater than
zero. A delay of 0 means that the task is not delayed, and OSTimeDly() returns immediately to
the caller.
In periodic mode, you must specify a non-zero period otherwise the function returns
immediately with an appropriate error code. The period is specified in “ticks”.
In absolute mode, rescheduling always occurs since all delay values are valid.
The actual delay time depends on the tick rate (see OS_CFG_TICK_RATE_HZ if os_cfg_app.h).
void OSTimeDly(1000,OS_OPT_TIME_DLY,& err); // 设置ls的延时。
void OSTimeDly (OS_TICK dly,
OS_OPT opt,
OS_ERR *p_err)
dly
is the desired delay expressed in number of clock ticks. Depending on the value of the opt
field, delays can be relative or absolute.
opt
is used to indicate whether the delay is absolute or relative:
OS_OPT_TIME_DLY //设置一个相对与当前时间延时
Specifies a relative delay.
OS_OPT_TIME_TIMEOUT
Same as OS_OPT_TIME_DLY.
…
OSTimeDlyHMSM
Description
Allows a task to delay itself for a user-specified period that is specified in hours, minutes,
seconds, and milliseconds. This format is more convenient and natural than simply specifying
ticks as in OSTimeDly(). Rescheduling always occurs when at least one of the parameters is
non-zero. The delay is relative from the time this function is called.
µC/OS-III allows the user to specify nearly any value when indicating that this function is not
to be strict about the values being passed (opt == OS_OPT_TIME_HMSM_NON_STRICT). This is a
useful feature, for example, to delay a task for thousands of milliseconds.
void OSTimeDlyHMSM (CPU_INT16U hours,
CPU_INT16U minutes,
CPU_INT16U seconds,
CPU_INT32U milli,
OS_OPT opt,
OS_ERR *p_err)
opt
is the desired mode and can be either:
OS_OPT_TIME_HMSM_STRICT // 60 进
(see above)
OS_OPT_TIME_HMSM_NON_STRICT
(see above)