Timer.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "cmsis_os2.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. osTimerId_t tim_id1; // timer id
  8. static uint32_t exec1; // argument for the timer call back function
  9. // One-Shoot Timer Function
  10. static void Timer1_Callback (void const *arg) {
  11. // add user code here
  12. }
  13. /*----- Periodic Timer Example -----*/
  14. static void Timer2_Callback (void const *arg); // prototype for timer callback function
  15. osTimerId_t tim_id2; // timer id
  16. static uint32_t exec2; // argument for the timer call back function
  17. // Periodic Timer Example
  18. static void Timer2_Callback (void const *arg) {
  19. // add user code here
  20. }
  21. // Example: Create and Start timers
  22. int Init_Timers (void) {
  23. osStatus_t status; // function return status
  24. // Create one-shoot timer
  25. exec1 = 1;
  26. tim_id1 = osTimerNew ((osTimerFunc_t)&Timer1_Callback, osTimerOnce, &exec1, NULL);
  27. if (tim_id1 != NULL) { // One-shot timer created
  28. // start timer with delay 100ms
  29. status = osTimerStart (tim_id1, 100);
  30. if (status != osOK) {
  31. return -1;
  32. }
  33. }
  34. // Create periodic timer
  35. exec2 = 2;
  36. tim_id2 = osTimerNew((osTimerFunc_t)&Timer2_Callback, osTimerPeriodic, &exec2, NULL);
  37. if (tim_id2 != NULL) { // Periodic timer created
  38. // start timer with periodic 1000ms interval
  39. status = osTimerStart (tim_id2, 1000);
  40. if (status != osOK) {
  41. return -1;
  42. }
  43. }
  44. return NULL;
  45. }