Timer.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "cmsis_os.h" // CMSIS RTOS header file
  2. /*----------------------------------------------------------------------------
  3. * Timer: Sample timer functions
  4. *---------------------------------------------------------------------------*/
  5. /*----- One-Shoot Timer Example -----*/
  6. static void Timer1_Callback (void const *arg); // prototype for timer callback function
  7. static osTimerId id1; // timer id
  8. static uint32_t exec1; // argument for the timer call back function
  9. static osTimerDef (Timer1, Timer1_Callback); // define timers
  10. // One-Shoot Timer Function
  11. static void Timer1_Callback (void const *arg) {
  12. // add user code here
  13. }
  14. /*----- Periodic Timer Example -----*/
  15. static void Timer2_Callback (void const *arg); // prototype for timer callback function
  16. static osTimerId id2; // timer id
  17. static uint32_t exec2; // argument for the timer call back function
  18. static osTimerDef (Timer2, Timer2_Callback);
  19. // Periodic Timer Example
  20. static void Timer2_Callback (void const *arg) {
  21. // add user code here
  22. }
  23. // Example: Create and Start timers
  24. void Init_Timers (void) {
  25. osStatus status; // function return status
  26. // Create one-shoot timer
  27. exec1 = 1;
  28. id1 = osTimerCreate (osTimer(Timer1), osTimerOnce, &exec1);
  29. if (id1 != NULL) { // One-shot timer created
  30. // start timer with delay 100ms
  31. status = osTimerStart (id1, 100);
  32. if (status != osOK) {
  33. // Timer could not be started
  34. }
  35. }
  36. // Create periodic timer
  37. exec2 = 2;
  38. id2 = osTimerCreate (osTimer(Timer2), osTimerPeriodic, &exec2);
  39. if (id2 != NULL) { // Periodic timer created
  40. // start timer with periodic 1000ms interval
  41. status = osTimerStart (id2, 1000);
  42. if (status != osOK) {
  43. // Timer could not be started
  44. }
  45. }
  46. }