Advertisement
Guest User

callCoreTimerServiceNowTest

a guest
Jun 4th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. /* Program to test callCoreTimerServiceNow() function.
  2. We set up a callback that shedules itself every 10ms to run. But what if we need it to run sooner than the next
  3. scheduled time? We use callCoreTimerServiceNow().
  4.  
  5. The code below sets up a callback that just incriments a global value every 10ms. We print out the global value
  6. every 1ms using the delay(1) function, and we see that for the first four ms, the RunCount only incriments once
  7. or so. But then we start calling callCoreTimerServiceNow() and we see that the RunCount is incrimented for
  8. every call. If callCoreTimerServiceNow() is not working, the RunCount will only be incrimented every 10ms.
  9.  
  10. The code should print out:
  11.  0ms : 1
  12.  1ms : 1
  13.  2ms : 1
  14.  3ms : 1
  15.  4ms : 2
  16.  5ms : 3
  17.  6ms : 9
  18.  
  19. This example code is in the public domain. */
  20.  
  21. volatile int RunCount = 0;
  22.  
  23. void setup() {  
  24.   Serial.begin(115200);
  25.   delay(2000);  // Wait for slow human to get serial monitor running
  26. }
  27.  
  28. void loop() {
  29.   delay(1);
  30.   attachCoreTimerService(MyCallback);
  31.   Serial.print(" 0ms : ");
  32.   Serial.println(RunCount);
  33.   delay(1);
  34.   Serial.print(" 1ms : ");
  35.   Serial.println(RunCount);
  36.   delay(1);
  37.   Serial.print(" 2ms : ");
  38.   Serial.println(RunCount);
  39.   delay(1);
  40.   Serial.print(" 3ms : ");
  41.   Serial.println(RunCount);
  42.   callCoreTimerServiceNow(MyCallback);
  43.   delay(1);
  44.   Serial.print(" 4ms : ");
  45.   Serial.println(RunCount);
  46.   callCoreTimerServiceNow(MyCallback);
  47.   delay(1);
  48.   Serial.print(" 5ms : ");
  49.   Serial.println(RunCount);
  50.   callCoreTimerServiceNow(MyCallback);
  51.   callCoreTimerServiceNow(MyCallback);
  52.   callCoreTimerServiceNow(MyCallback);
  53.   callCoreTimerServiceNow(MyCallback);
  54.   callCoreTimerServiceNow(MyCallback);
  55.   callCoreTimerServiceNow(MyCallback);
  56.   delay(1);
  57.   Serial.print(" 6ms : ");
  58.   Serial.println(RunCount);
  59.   while(1);
  60. }
  61.  
  62. // For the core timer callback, just toggle the output high and low
  63. // and schedule us for another 100uS in the future. CORE_TICK_RATE
  64. // is the number of core timer counts in 1 millisecond. So if we
  65. // want this callback to be called every 100uS, we just divide
  66. // the CORE_TICK_RATE by 10, and add it to the current time.
  67. uint32_t MyCallback(uint32_t currentTime) {
  68.   RunCount++;
  69.   return (currentTime + (CORE_TICK_RATE*10));
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement