From: Mikko Rasa Date: Mon, 21 Oct 2013 23:15:31 +0000 (+0300) Subject: Rewrite ADC code to cause less timing interference X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=0ea90da32943b1fae28a3e872f0a9a41b75de4ae;p=model-railway-devices.git Rewrite ADC code to cause less timing interference Doing divisions in ISR is bad, mkay? The new code uses a different method which avoids divisions altogether and is much more accurate too. --- diff --git a/arducontrol/monitor.c b/arducontrol/monitor.c index c88a071..679c62a 100644 --- a/arducontrol/monitor.c +++ b/arducontrol/monitor.c @@ -7,7 +7,7 @@ uint16_t track_current_samples[16] = { 0 }; uint8_t track_current_head = 0; volatile uint16_t track_current_sum = 0; -uint16_t overcurrent_limit = 1000<<4; +uint16_t overcurrent_limit = 8796; uint8_t overcurrent_sent = 0; uint16_t input_voltage_samples[16] = { 0 }; @@ -15,6 +15,10 @@ uint8_t input_voltage_head = 0; volatile uint16_t input_voltage_sum = 0; volatile uint8_t adc_state = 0; +volatile uint16_t adc_value = 0; + +uint16_t track_current_milliamps(); +uint16_t input_voltage_millivolts(); void monitor_init() { @@ -25,22 +29,41 @@ void monitor_check() { if(!(adc_state&1)) { - ++adc_state; - adc_read_async(adc_state>>1); - } + uint16_t value = adc_value; - if(track_current_sum>overcurrent_limit) - { - output_set_power(0); - if(!overcurrent_sent) + if(adc_state==2) + { + uint8_t i = track_current_head; + track_current_sum -= track_current_samples[i]; + track_current_samples[i] = value; + track_current_sum += value; + track_current_head = (i+1)&15; + + if(track_current_sum>overcurrent_limit) + { + output_set_power(0); + if(!overcurrent_sent) + { + overcurrent_sent = 1; + serial_write(0xFE); + serial_write(OVERCURRENT); + } + } + else + overcurrent_sent = 0; + } + else if(adc_state==4) { - overcurrent_sent = 1; - serial_write(0xFE); - serial_write(OVERCURRENT); + uint8_t i = input_voltage_head; + input_voltage_sum -= input_voltage_samples[i]; + input_voltage_samples[i] = value; + input_voltage_sum += value; + input_voltage_head = (i+1)&15; } + + adc_state = (adc_state+1)&3; + adc_read_async(adc_state>>1); } - else - overcurrent_sent = 0; } uint8_t monitor_command() @@ -52,7 +75,7 @@ uint8_t monitor_command() serial_write(0xFC); serial_write(TRACK_CURRENT); - uint16_t value = track_current_sum>>4; + uint16_t value = track_current_milliamps(); serial_write(value>>8); serial_write(value); } @@ -61,10 +84,14 @@ uint8_t monitor_command() if(cmd_length!=3) return LENGTH_ERROR; - if(cmd_buf[1]&0xF0) + uint16_t value = (cmd_buf[1]<<8) | cmd_buf[2]; + if(value>4000) // Safe maximum value return INVALID_VALUE; - overcurrent_limit = (cmd_buf[1]<<12) | (cmd_buf[2]<<4); + // Convert from milliamps: (512+v/1000*0.185/5*1024)*16 + // multiply by 16384*0.185/5000 = 0.1001101100110b + uint16_t v_3 = value*3; + overcurrent_limit = 8192+(value>>1)+(v_3>>5)+(v_3>>8)+(v_3>>12); } else if(cmd_buf[0]==READ_INPUT_VOLTAGE) { @@ -73,7 +100,7 @@ uint8_t monitor_command() serial_write(0xFC); serial_write(INPUT_VOLTAGE); - uint16_t value = (input_voltage_sum>>3)*5; + uint16_t value = input_voltage_millivolts(); serial_write(value>>8); serial_write(value); } @@ -83,39 +110,36 @@ uint8_t monitor_command() return COMMAND_OK; } -static inline void adc_complete(uint16_t value) +uint16_t track_current_milliamps() { - if(adc_state==1) - { - // Convert to milliamps: (v*5/1024-2.5)*1000/0.185 - if(value<512) // Ignore negative current readings - value = 0; - else if(value>663) // Limit range so averaging won't overflow - value = 4000; - else - value = (value-512)*132/5; - - uint8_t i = track_current_head; - track_current_sum -= track_current_samples[i]; - track_current_samples[i] = value; - track_current_sum += value; - track_current_head = (i+1)&15; - } - else if(adc_state==3) + uint16_t value = track_current_sum; + + // Convert to milliamps: (v/16*5/1024-2.5)*1000/0.185 + if(value<8192) // Ignore negative current readings + return 0; + else { - // Convert to centivolts: (v*5/1024)*100*11 - if(value>744) // Limit range so averaging won't overflow - value = 4000; - else - value = value*43/8; - - uint8_t i = input_voltage_head; - input_voltage_sum -= input_voltage_samples[i]; - input_voltage_samples[i] = value; - input_voltage_sum += value; - input_voltage_head = (i+1)&15; + value -= 8192; + + // multiply by 5000/0.185/16384 = 1.1010011001001b + int16_t v_3 = value*3; + return (v_3>>1)+(value>>3)+(v_3>>7)+(value>>10)+(v_3>>13); } +} +uint16_t input_voltage_millivolts() +{ + uint16_t value = input_voltage_sum; + + // Convert to millivolts: (v/16*5/1024)*1000*11 + // multiply by 55000/16384 = 11.0101101101100b + uint16_t v_3 = value*3; + return v_3+(value>>2)+(v_3>>5)+(v_3>>8)+(v_3>>11); +} + +static inline void adc_complete(uint16_t value) +{ + adc_value = value; ++adc_state; }