Yoichi Nagashima
/
CQ_nagasm_05_detection
NucleoF401RE A/D Envelope detection with Moving average, transfer via XBww
Diff: main.cpp
- Revision:
- 0:14c6afea35c7
diff -r 000000000000 -r 14c6afea35c7 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Dec 09 00:51:38 2014 +0000 @@ -0,0 +1,86 @@ +#include "mbed.h" +#include "sub.hpp" + +float mean_sum, ad_data[201]; +int average_mode, max_count, ad_pointer[5]; + +void sum_clear(){ + int i; + for (i=0; i<5; i++) ad_pointer[i] = 0; + for (i=0; i<201; i++) ad_data[i] = 0; + mean_sum = 0; +} + +float move_mean_calc(float data){ + mean_sum = mean_sum - ad_data[ad_pointer[0]] + data; + ad_data[ad_pointer[0]] = data; + ad_pointer[0]++; + if(ad_pointer[0] == max_count) ad_pointer[0] = 0; + return(mean_sum / (float)max_count); +} + +int main(){ + int i, detection, sum, gain; + i = 0; + detection = 0; + gain = 1; + average_mode = 0; + for (i=0; i<5; i++) timer_value[i] = 0; + sum_clear(); + xbee.baud(38400); + xbee.attach(&rx_fifoset, xbee.RxIrq); + timer_setup.attach_us(&timer_interrupt, 50); // 50usec + while(1){ + if(timer_value[2] > 99){ // 5msec + timer_value[2] = 0; + float data = (float)gain * (analog_value3.read() - 0.5f); + if(detection == 1){ + if (data < 0) data = -data; + } + if(average_mode != 0) data = move_mean_calc(data); + tx_message((uint16_t)((data + 1.0f) * 2047)<<4); + } + if(timer_value[0] > 9999){ // 500msec + timer_value[0] = 0; + myled = !myled; + } + tx_fifo_check(); + if(rx_fifo_check() == 1){ + sum = 0; + for (i=0; i<6; i++) sum += conv_hex(raw_data[i])<<(4*(5-i)); + tx_message(sum); /* Echo Back */ + if(sum>>16 == 0x80){ + switch((sum & 0xff00)>>8){ + case 0x00: + detection = sum & 0x01; + break; + case 0x01: + gain = sum & 0x0f; + break; + case 0x02: + average_mode = sum & 0x07; + max_count = 5; + switch(average_mode){ + case(2): + max_count = 10; + break; + case(3): + max_count = 20; + break; + case(4): + max_count = 50; + break; + case(5): + max_count = 100; + break; + case(6): + max_count = 200; + break; + } + sum_clear(); + break; + } + } + } + } +}