Measure EMG, publish on HID Scope
Dependencies: HIDScope mbed mbed-dsp
Fork of EMG by
main.cpp@12:768048d7f742, 2014-09-11 (annotated)
- Committer:
- vsluiter
- Date:
- Thu Sep 11 07:19:37 2014 +0000
- Revision:
- 12:768048d7f742
- Parent:
- 10:09b8424a7b39
- Child:
- 14:94a4a5863689
- Child:
- 15:d3a849ab3697
Added comment that 12-bit value is converted to be 16-bit full scale (raw value multiplied by 16 / shifted 4 bits)
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 |
vsluiter | 7:3396c3e33928 | 5 | AnalogIn emg0(PTB1); //Analog input |
ArvidKeemink | 1:db54d9412d18 | 6 | PwmOut red(LED_RED); //PWM output |
vsluiter | 8:8a17f65622b4 | 7 | Ticker log_timer; |
vsluiter | 8:8a17f65622b4 | 8 | MODSERIAL pc(USBTX,USBRX); |
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 | 8:8a17f65622b4 | 19 | * i.e.: "volatile uint16_t emg_value;" instead of "uint16_t 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 | 8:8a17f65622b4 | 26 | uint16_t emg_value; |
vsluiter | 4:8b298dfada81 | 27 | /*put raw emg value both in red and in emg_value*/ |
vsluiter | 10:09b8424a7b39 | 28 | red.write(emg0.read()); // read float value (0..1 = 0..3.3V) |
vsluiter | 12:768048d7f742 | 29 | emg_value = emg0.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V) |
vsluiter | 8:8a17f65622b4 | 30 | /*send value to PC. Line below is used to prevent buffer overrun */ |
vsluiter | 6:80c13d99aa55 | 31 | if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) |
vsluiter | 8:8a17f65622b4 | 32 | pc.printf("%u\n",emg_value); |
vsluiter | 5:4dacb7b72109 | 33 | /**When not using the LED, the above could also have been done this way: |
vsluiter | 8:8a17f65622b4 | 34 | * pc.printf("%u\n", emg0.read_u16()); |
vsluiter | 5:4dacb7b72109 | 35 | */ |
vsluiter | 2:e314bb3b2d99 | 36 | } |
vsluiter | 0:32bb76391d89 | 37 | |
vsluiter | 0:32bb76391d89 | 38 | int main() |
vsluiter | 0:32bb76391d89 | 39 | { |
vsluiter | 4:8b298dfada81 | 40 | /*setup baudrate. Choose the same in your program on PC side*/ |
vsluiter | 2:e314bb3b2d99 | 41 | pc.baud(115200); |
vsluiter | 4:8b298dfada81 | 42 | /*set the period for the PWM to the red LED*/ |
vsluiter | 2:e314bb3b2d99 | 43 | red.period_ms(2); |
vsluiter | 4:8b298dfada81 | 44 | /**Here you attach the 'void looper(void)' function to the Ticker object |
vsluiter | 9:d33e7b175ad7 | 45 | * The looper() function will be called every 0.01 seconds. |
vsluiter | 4:8b298dfada81 | 46 | * Please mind that the parentheses after looper are omitted when using attach. |
vsluiter | 4:8b298dfada81 | 47 | */ |
vsluiter | 8:8a17f65622b4 | 48 | log_timer.attach(looper, 0.01); |
ArvidKeemink | 1:db54d9412d18 | 49 | while(1) //Loop |
vsluiter | 0:32bb76391d89 | 50 | { |
vsluiter | 4:8b298dfada81 | 51 | /*Empty!*/ |
vsluiter | 4:8b298dfada81 | 52 | /*Everything is handled by the interrupt routine now!*/ |
vsluiter | 0:32bb76391d89 | 53 | } |
vsluiter | 0:32bb76391d89 | 54 | } |