Timer.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. static osTimerId_t 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. static osTimerId_t 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. void Init_Timers (void) {
  23. osStatus_t status; // function return status
  24. // Create one-shoot timer
  25. exec1 = 1;
  26. id1 = osTimerNew ((os_timer_func_t)&Timer1_Callback, osTimerOnce, &exec1, NULL);
  27. if (id1 != NULL) { // One-shot timer created
  28. // start timer with delay 100ms
  29. status = osTimerStart (id1, 100);
  30. if (status != osOK) {
  31. // Timer could not be started
  32. }
  33. }
  34. // Create periodic timer
  35. exec2 = 2;
  36. id2 = osTimerNew((os_timer_func_t)&Timer2_Callback, osTimerPeriodic, &exec2, NULL);
  37. if (id2 != NULL) { // Periodic timer created
  38. // start timer with periodic 1000ms interval
  39. status = osTimerStart (id2, 1000);
  40. if (status != osOK) {
  41. // Timer could not be started
  42. }
  43. }
  44. }