Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
00001 #include "mbed.h" 00002 #define N 100000 00003 #define RELEASED 1 00004 #define PRESSED 0 00005 DigitalOut red_led(D7); //CountUp is in its critical section 00006 DigitalOut yellow_led(D6); //CountDown is in its critical section 00007 DigitalOut green_led(D5); //counter != 0 00008 DigitalIn button(USER_BUTTON); 00009 00010 //Shared mutable state 00011 volatile long long counter = 0; //Volatile means it must be stored in memory 00012 00013 //Increment the shared variable 00014 void countUp() 00015 { 00016 //RED MEANS THE COUNT UP FUNCTION IS IN ITS CRITICAL SECTION 00017 red_led = 1; 00018 for (int n=0; n<N; n++) { 00019 counter++; 00020 counter++; 00021 counter++; 00022 counter++; 00023 counter++; 00024 counter++; 00025 counter++; 00026 counter++; 00027 counter++; 00028 counter++; 00029 } 00030 red_led = 0; 00031 00032 //Last to finish turnes out the green light 00033 if (counter == 0) { 00034 green_led = 0; 00035 } 00036 } 00037 00038 //Decrement the shared variable 00039 void countDown() 00040 { 00041 //YELLOW MEANS THE COUNT DOWN FUNCTION IS IN ITS CRITICAL SECTION 00042 yellow_led = 1; 00043 for (int n=0; n<N; n++) { 00044 counter--; 00045 counter--; 00046 counter--; 00047 counter--; 00048 counter--; 00049 counter--; 00050 counter--; 00051 counter--; 00052 counter--; 00053 counter--; 00054 } 00055 yellow_led = 0; 00056 00057 //Last to finish turns out the green light 00058 if (counter == 0) { 00059 green_led = 0; 00060 } 00061 } 00062 int main() { 00063 00064 green_led = 1; 00065 Timeout t1; 00066 00067 // TRY EACH OF THESE LINES IN TURN. 00068 // ALL IT DOES IS CHANGE THE TIMING OF THE ISR, NOT THE FUNCTION 00069 00070 if (button == PRESSED) { 00071 //VERSION 2: short delay allowing main to be preempted - you might want to tweak this value 00072 t1.attach_us(&countDown, 15); 00073 } else { 00074 //VERSION 1: 2s - ENOUGH TIME FOR COUNTUP TO FINISH 00075 t1.attach(&countDown, 2); 00076 } 00077 00078 //Run count up on the main thread 00079 countUp(); 00080 00081 //Now spin-lock for ever 00082 while(1) { 00083 asm("nop"); 00084 }; 00085 } 00086
Generated on Fri Sep 9 2022 15:30:24 by
1.7.2