Script voor het weergeven van 1 EMG signaal met behulp van een OLIMEX shield. Let hierbij goed op dat je de juiste pinnetjes op het OLIMEX shield verbonden hebt, in dit geval A0 als uitgang. Verder maken we gebruik van 3,3 Volt!
Fork of EMG by
main.cpp@6:80c13d99aa55, 2013-10-03 (annotated)
- Committer:
- vsluiter
- Date:
- Thu Oct 03 13:47:49 2013 +0000
- Revision:
- 6:80c13d99aa55
- Parent:
- 5:4dacb7b72109
- Child:
- 7:3396c3e33928
Added some measures to prevent overflow; Data rate is still too high, though
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:32bb76391d89 | 1 | #include "mbed.h" |
vsluiter | 6:80c13d99aa55 | 2 | #include "MODSERIAL.h" |
vsluiter | 0:32bb76391d89 | 3 | |
vsluiter | 4:8b298dfada81 | 4 | //Define objects |
ArvidKeemink | 1:db54d9412d18 | 5 | AnalogIn emg0(PTB0); //Analog input |
ArvidKeemink | 1:db54d9412d18 | 6 | PwmOut red(LED_RED); //PWM output |
vsluiter | 2:e314bb3b2d99 | 7 | Ticker timer; |
vsluiter | 6:80c13d99aa55 | 8 | MODSERIAL pc(USBTX,USBRX,64,1024); |
vsluiter | 2:e314bb3b2d99 | 9 | |
vsluiter | 4:8b298dfada81 | 10 | /** Looper function |
vsluiter | 4:8b298dfada81 | 11 | * functions used for Ticker and Timeout should be of type void <name>(void) |
vsluiter | 4:8b298dfada81 | 12 | * i.e. no input arguments, no output arguments. |
vsluiter | 4:8b298dfada81 | 13 | * if you want to change a variable that you use in other places (for example in main) |
vsluiter | 4:8b298dfada81 | 14 | * you will have to make that variable global in order to be able to reach it both from |
vsluiter | 4:8b298dfada81 | 15 | * the function called at interrupt time, and in the main function. |
vsluiter | 4:8b298dfada81 | 16 | * To make a variable global, define it under the includes. |
vsluiter | 4:8b298dfada81 | 17 | * variables that are changed in the interrupt routine (written to) should be made |
vsluiter | 4:8b298dfada81 | 18 | * 'volatile' to let the compiler know that those values may change outside the current context. |
vsluiter | 4:8b298dfada81 | 19 | * i.e.: "volatile float emg_value;" instead of "float emg_value" |
vsluiter | 4:8b298dfada81 | 20 | * in the example below, the variable is not re-used in the main function, and is thus declared |
vsluiter | 4:8b298dfada81 | 21 | * local in the looper function only. |
vsluiter | 4:8b298dfada81 | 22 | **/ |
vsluiter | 2:e314bb3b2d99 | 23 | void looper() |
vsluiter | 2:e314bb3b2d99 | 24 | { |
vsluiter | 4:8b298dfada81 | 25 | /*variable to store value in*/ |
vsluiter | 4:8b298dfada81 | 26 | float emg_value; |
vsluiter | 4:8b298dfada81 | 27 | /*put raw emg value both in red and in emg_value*/ |
vsluiter | 4:8b298dfada81 | 28 | red = emg_value = emg0.read(); |
vsluiter | 5:4dacb7b72109 | 29 | /*send value to PC. use 6 digits after decimal sign*/ |
vsluiter | 6:80c13d99aa55 | 30 | if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) |
vsluiter | 6:80c13d99aa55 | 31 | pc.printf("%.6f\n",emg_value); |
vsluiter | 5:4dacb7b72109 | 32 | /**When not using the LED, the above could also have been done this way: |
vsluiter | 5:4dacb7b72109 | 33 | * pc.printf("%.6\n", emg0.read()); |
vsluiter | 5:4dacb7b72109 | 34 | */ |
vsluiter | 2:e314bb3b2d99 | 35 | } |
vsluiter | 0:32bb76391d89 | 36 | |
vsluiter | 0:32bb76391d89 | 37 | int main() |
vsluiter | 0:32bb76391d89 | 38 | { |
vsluiter | 4:8b298dfada81 | 39 | /*setup baudrate. Choose the same in your program on PC side*/ |
vsluiter | 2:e314bb3b2d99 | 40 | pc.baud(115200); |
vsluiter | 4:8b298dfada81 | 41 | /*set the period for the PWM to the red LED*/ |
vsluiter | 2:e314bb3b2d99 | 42 | red.period_ms(2); |
vsluiter | 4:8b298dfada81 | 43 | /**Here you attach the 'void looper(void)' function to the Ticker object |
vsluiter | 4:8b298dfada81 | 44 | * The looper() function will be called every 0.001 seconds. |
vsluiter | 4:8b298dfada81 | 45 | * Please mind that the parentheses after looper are omitted when using attach. |
vsluiter | 4:8b298dfada81 | 46 | */ |
vsluiter | 2:e314bb3b2d99 | 47 | timer.attach(looper, 0.001); |
ArvidKeemink | 1:db54d9412d18 | 48 | while(1) //Loop |
vsluiter | 0:32bb76391d89 | 49 | { |
vsluiter | 4:8b298dfada81 | 50 | /*Empty!*/ |
vsluiter | 4:8b298dfada81 | 51 | /*Everything is handled by the interrupt routine now!*/ |
vsluiter | 0:32bb76391d89 | 52 | } |
vsluiter | 0:32bb76391d89 | 53 | } |