emg
Fork of EMG by
main.cpp@4:8b298dfada81, 2013-10-02 (annotated)
- Committer:
- vsluiter
- Date:
- Wed Oct 02 09:40:41 2013 +0000
- Revision:
- 4:8b298dfada81
- Parent:
- 3:1296e996026a
- Child:
- 5:4dacb7b72109
Added comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
vsluiter | 0:32bb76391d89 | 2 | |
vsluiter | 4:8b298dfada81 | 3 | //Define objects |
ArvidKeemink | 1:db54d9412d18 | 4 | AnalogIn emg0(PTB0); //Analog input |
ArvidKeemink | 1:db54d9412d18 | 5 | PwmOut red(LED_RED); //PWM output |
vsluiter | 2:e314bb3b2d99 | 6 | Ticker timer; |
vsluiter | 2:e314bb3b2d99 | 7 | Serial pc(USBTX,USBRX); |
vsluiter | 2:e314bb3b2d99 | 8 | |
vsluiter | 4:8b298dfada81 | 9 | /** Looper function |
vsluiter | 4:8b298dfada81 | 10 | * functions used for Ticker and Timeout should be of type void <name>(void) |
vsluiter | 4:8b298dfada81 | 11 | * i.e. no input arguments, no output arguments. |
vsluiter | 4:8b298dfada81 | 12 | * if you want to change a variable that you use in other places (for example in main) |
vsluiter | 4:8b298dfada81 | 13 | * you will have to make that variable global in order to be able to reach it both from |
vsluiter | 4:8b298dfada81 | 14 | * the function called at interrupt time, and in the main function. |
vsluiter | 4:8b298dfada81 | 15 | * To make a variable global, define it under the includes. |
vsluiter | 4:8b298dfada81 | 16 | * variables that are changed in the interrupt routine (written to) should be made |
vsluiter | 4:8b298dfada81 | 17 | * 'volatile' to let the compiler know that those values may change outside the current context. |
vsluiter | 4:8b298dfada81 | 18 | * i.e.: "volatile float emg_value;" instead of "float emg_value" |
vsluiter | 4:8b298dfada81 | 19 | * in the example below, the variable is not re-used in the main function, and is thus declared |
vsluiter | 4:8b298dfada81 | 20 | * local in the looper function only. |
vsluiter | 4:8b298dfada81 | 21 | **/ |
vsluiter | 2:e314bb3b2d99 | 22 | void looper() |
vsluiter | 2:e314bb3b2d99 | 23 | { |
vsluiter | 4:8b298dfada81 | 24 | /*variable to store value in*/ |
vsluiter | 4:8b298dfada81 | 25 | float emg_value; |
vsluiter | 4:8b298dfada81 | 26 | /*put raw emg value both in red and in emg_value*/ |
vsluiter | 4:8b298dfada81 | 27 | red = emg_value = emg0.read(); |
vsluiter | 4:8b298dfada81 | 28 | /*send value to PC. use 6 digits precision*/ |
vsluiter | 4:8b298dfada81 | 29 | pc.printf("%.6f\n",emg_value); |
vsluiter | 2:e314bb3b2d99 | 30 | } |
vsluiter | 0:32bb76391d89 | 31 | |
vsluiter | 0:32bb76391d89 | 32 | int main() |
vsluiter | 0:32bb76391d89 | 33 | { |
vsluiter | 4:8b298dfada81 | 34 | /*setup baudrate. Choose the same in your program on PC side*/ |
vsluiter | 2:e314bb3b2d99 | 35 | pc.baud(115200); |
vsluiter | 4:8b298dfada81 | 36 | /*set the period for the PWM to the red LED*/ |
vsluiter | 2:e314bb3b2d99 | 37 | red.period_ms(2); |
vsluiter | 4:8b298dfada81 | 38 | /**Here you attach the 'void looper(void)' function to the Ticker object |
vsluiter | 4:8b298dfada81 | 39 | * The looper() function will be called every 0.001 seconds. |
vsluiter | 4:8b298dfada81 | 40 | * Please mind that the parentheses after looper are omitted when using attach. |
vsluiter | 4:8b298dfada81 | 41 | */ |
vsluiter | 2:e314bb3b2d99 | 42 | timer.attach(looper, 0.001); |
ArvidKeemink | 1:db54d9412d18 | 43 | while(1) //Loop |
vsluiter | 0:32bb76391d89 | 44 | { |
vsluiter | 4:8b298dfada81 | 45 | /*Empty!*/ |
vsluiter | 4:8b298dfada81 | 46 | /*Everything is handled by the interrupt routine now!*/ |
vsluiter | 0:32bb76391d89 | 47 | } |
vsluiter | 0:32bb76391d89 | 48 | } |