Yoichi Nagashima
/
CQ_nagasm_05_detection
NucleoF401RE A/D Envelope detection with Moving average, transfer via XBww
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "sub.hpp" 00003 00004 float mean_sum, ad_data[201]; 00005 int average_mode, max_count, ad_pointer[5]; 00006 00007 void sum_clear(){ 00008 int i; 00009 for (i=0; i<5; i++) ad_pointer[i] = 0; 00010 for (i=0; i<201; i++) ad_data[i] = 0; 00011 mean_sum = 0; 00012 } 00013 00014 float move_mean_calc(float data){ 00015 mean_sum = mean_sum - ad_data[ad_pointer[0]] + data; 00016 ad_data[ad_pointer[0]] = data; 00017 ad_pointer[0]++; 00018 if(ad_pointer[0] == max_count) ad_pointer[0] = 0; 00019 return(mean_sum / (float)max_count); 00020 } 00021 00022 int main(){ 00023 int i, detection, sum, gain; 00024 i = 0; 00025 detection = 0; 00026 gain = 1; 00027 average_mode = 0; 00028 for (i=0; i<5; i++) timer_value[i] = 0; 00029 sum_clear(); 00030 xbee.baud(38400); 00031 xbee.attach(&rx_fifoset, xbee.RxIrq); 00032 timer_setup.attach_us(&timer_interrupt, 50); // 50usec 00033 while(1){ 00034 if(timer_value[2] > 99){ // 5msec 00035 timer_value[2] = 0; 00036 float data = (float)gain * (analog_value3.read() - 0.5f); 00037 if(detection == 1){ 00038 if (data < 0) data = -data; 00039 } 00040 if(average_mode != 0) data = move_mean_calc(data); 00041 tx_message((uint16_t)((data + 1.0f) * 2047)<<4); 00042 } 00043 if(timer_value[0] > 9999){ // 500msec 00044 timer_value[0] = 0; 00045 myled = !myled; 00046 } 00047 tx_fifo_check(); 00048 if(rx_fifo_check() == 1){ 00049 sum = 0; 00050 for (i=0; i<6; i++) sum += conv_hex(raw_data[i])<<(4*(5-i)); 00051 tx_message(sum); /* Echo Back */ 00052 if(sum>>16 == 0x80){ 00053 switch((sum & 0xff00)>>8){ 00054 case 0x00: 00055 detection = sum & 0x01; 00056 break; 00057 case 0x01: 00058 gain = sum & 0x0f; 00059 break; 00060 case 0x02: 00061 average_mode = sum & 0x07; 00062 max_count = 5; 00063 switch(average_mode){ 00064 case(2): 00065 max_count = 10; 00066 break; 00067 case(3): 00068 max_count = 20; 00069 break; 00070 case(4): 00071 max_count = 50; 00072 break; 00073 case(5): 00074 max_count = 100; 00075 break; 00076 case(6): 00077 max_count = 200; 00078 break; 00079 } 00080 sum_clear(); 00081 break; 00082 } 00083 } 00084 } 00085 } 00086 }
Generated on Wed Jul 13 2022 06:08:59 by 1.7.2