Changed some stuff
Fork of EMG by
main.cpp@5:4dacb7b72109, 2013-10-02 (annotated)
- Committer:
- vsluiter
- Date:
- Wed Oct 02 09:45:01 2013 +0000
- Revision:
- 5:4dacb7b72109
- Parent:
- 4:8b298dfada81
- Child:
- 6:80c13d99aa55
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 | 5:4dacb7b72109 | 28 | /*send value to PC. use 6 digits after decimal sign*/ |
vsluiter | 4:8b298dfada81 | 29 | pc.printf("%.6f\n",emg_value); |
vsluiter | 5:4dacb7b72109 | 30 | /**When not using the LED, the above could also have been done this way: |
vsluiter | 5:4dacb7b72109 | 31 | * pc.printf("%.6\n", emg0.read()); |
vsluiter | 5:4dacb7b72109 | 32 | */ |
vsluiter | 2:e314bb3b2d99 | 33 | } |
vsluiter | 0:32bb76391d89 | 34 | |
vsluiter | 0:32bb76391d89 | 35 | int main() |
vsluiter | 0:32bb76391d89 | 36 | { |
vsluiter | 4:8b298dfada81 | 37 | /*setup baudrate. Choose the same in your program on PC side*/ |
vsluiter | 2:e314bb3b2d99 | 38 | pc.baud(115200); |
vsluiter | 4:8b298dfada81 | 39 | /*set the period for the PWM to the red LED*/ |
vsluiter | 2:e314bb3b2d99 | 40 | red.period_ms(2); |
vsluiter | 4:8b298dfada81 | 41 | /**Here you attach the 'void looper(void)' function to the Ticker object |
vsluiter | 4:8b298dfada81 | 42 | * The looper() function will be called every 0.001 seconds. |
vsluiter | 4:8b298dfada81 | 43 | * Please mind that the parentheses after looper are omitted when using attach. |
vsluiter | 4:8b298dfada81 | 44 | */ |
vsluiter | 2:e314bb3b2d99 | 45 | timer.attach(looper, 0.001); |
ArvidKeemink | 1:db54d9412d18 | 46 | while(1) //Loop |
vsluiter | 0:32bb76391d89 | 47 | { |
vsluiter | 4:8b298dfada81 | 48 | /*Empty!*/ |
vsluiter | 4:8b298dfada81 | 49 | /*Everything is handled by the interrupt routine now!*/ |
vsluiter | 0:32bb76391d89 | 50 | } |
vsluiter | 0:32bb76391d89 | 51 | } |