API Functions
Create
timerhandle_t xtimercreate (const char * const PCTIMERNAME,
const ticktype_t xtimerperiodinticks,
Const ubasetype_t Uxautoreload,
void * Const Pvtimerid,
timercallbackfunction_t pxcallbackfunction)
//Start
#define Xtimerstart (Xtimer, xtickstowait) Xtimergenericcommand ((Xtimer), Tmrcommand_start,
( Xtaskgettickcount ()), NULL, (xtickstowait))
basetype_t Xtimergenericcommand (timerhandle_t xtimer, const BASETYPE _t Xcommandid,
const ticktype_t xoptionalvalue, basetype_t * Const Pxhigherprioritytaskwoken,
const TICKTYPE _t xtickstowait)
//Stop
#define XTIMERSTOP (Xtimer, xtickstowait) Xtimergenericcommand ((Xtimer), Tmrcommand_ STOP,
0U, NULL, (xtickstowait))
Example
void Start_task (void *pvparameters) {//Create software cycle timers.
Cycle 1s (1000 clock beats), Cycle mode autoreloadtimer_handle=xtimercreate ((const char*) "Autoreloadtimer",
(ticktype_t), (ubasetype_t) Pdtrue, (void*) 1, (timercallbackfunction_t)
Autoreloadcallback); Create a one-time timer.
Cycle 2s (2000 clock beats), single-mode oneshottimer_handle=xtimercreate ((const char*) "Oneshottimer",
(ticktype_t), (ubasetype_t) Pdfalse, (void*) 2, (timercallbackfunction_t) Onesh
Otcallback);
}//Cycle timer callback function void Autoreloadcallback (timerhandle_t xtimer) {static U8 tmr1_num=0; tmr1_num++; Cycle timer execution times plus 1 printf ("Autoreloadcallback%d\r\n", tMr1_num);
}//single-timer callback function void Oneshotcallback (timerhandle_t xtimer) {static U8 Tmr2_num = 0; tmr2_num++;
Single timer execution times plus 1 printf ("Oneshotcallback%d\r\n", tmr2_num);
} void Timercontrol_task (void *pvparameters) {while (1) {key = Key_scan (0); Switch (key) {case Wkup_pres://When KEY_UP is pressed, turn on the cycle timer Xtimerstart (Autoreloadtimer_han dle,0);
Open Cycle Timer printf ("Turn on Timer 1\r\n");
Break Case Key0_pres://When KEY0 is pressed, a single timer xtimerstart (oneshottimer_handle,0) is opened;
Turn on single timer printf ("Turn on Timer 2\r\n");
Break Case Key1_pres://When Key1 is pressed, the timer xtimerstop (autoreloadtimer_handle,0) is turned off; Close Cycle Timer xtimerstop (oneshottimer_handle,0);
Turn off the single timer printf ("Off timer 1 and 2\r\n");
Break } vtaskdelay (10); Delay 10ms, i.e. 10 clock ticks}}
experimental phenomenon