#AVRDUDE_VERBOSE = -v -v
AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
-AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER)
-
+AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER)
+#$(AVRDUDE_NO_VERIFY)
CC = avr-gcc
OBJCOPY = avr-objcopy
volatile uint8_t measurement_active = 0;
-uint32_t temperature[9];
+uint32_t temperature[17][9];
+uint8_t invalid[8];
+uint8_t make_update_lcd;
color_t col_background;
color_t col_font;
calib_settings.gain_current = eeprom_read(9);
calib_settings.period = eeprom_read(10);
calib_settings.uselcd = eeprom_read(11);
+ calib_settings.average = eeprom_read(12);
}
/******************
// send_answer_hex('I','I',DDRB);
for(uint8_t i = 0; i<8; i++){
- int32_t x = temperature[i];
+ int32_t x = temperature[16][i];
if (x < 0) {lcd_use_foreground(col_neg); x = -x;}
else {lcd_use_foreground(col_font);}
//Init ADC for presence measurement
- ADMUX = (3 << REFS0) | (1 << ADLAR) | (3 << MUX0); // internal2.56V ADC3
+ ADMUX = (1 << REFS0) | (1 << ADLAR) | (3 << MUX0); // internal2.56V ADC3
ADCSRA = (1 << ADEN) | (0 << ADIE) | (5 << ADPS0); //fcpu/32
DIDR0 = 0x08; //PA3 is analog only
ISR(TIMER0_COMPA_vect) {
if(++time == calib_settings.period) {
time = 0;
- send_information();
measurement_active = 1;
}
next_step = 1;
* Second ticks
*********************/
ISR(TIMER1_COMPA_vect) {
- next_second = 1;
+// next_second = 1;
+// send_information();
}
while(next_step==0);
next_step = 0;
if (measurement_active) {do_measurement_step();}
- if (next_second && !measurement_active) {
+
+ if(make_update_lcd) {
if(calib_settings.uselcd)
lcd_update();
- next_second = 0;
+ make_update_lcd = 0;
}
if(key_was_pressed(1<<KEY_1)) {}
if(key_was_pressed(1<<KEY_2)) {}
extern volatile uint16_t time;
extern volatile uint8_t measurement_active;
extern struct calib_t calib_settings;
-extern uint32_t temperature[9];
+
+extern uint32_t temperature[17][9];
+extern uint8_t invalid[8];
+extern uint8_t make_update_lcd;
struct calib_t {
uint16_t offset_res[8]; //precise offset in Milliohm minus nominal value
uint16_t nominal_offset; //approximate offset resistor, in Ohm, 100 as default
uint16_t gain_current; //2^29 / (gain * current[uA]) (about 53700)
uint16_t period; //measurement period in timer ticks (2ms)
- uint16_t uselcd; //set to 0 to disable LCD
+ uint16_t uselcd; //set to 0 to disable LCD (0xb)
+ uint16_t average; //how many samples to average (0xc)
};
uint8_t current_channel = 0;
uint8_t num_connected_sensors = 8;
uint16_t connect_threshold = 0xffff;
+uint8_t sample = 0;
//factors: gain_current has 2^29, ADC has 2^7 steps per mV
//hence shift by 36 necessary, 6 as part of multiplication by 1E6
static uint16_t con;
static int32_t adc_result = 0;
uint16_t conf_register;
+
if( (measurement_step < (CONNECTED_SENSORS*4)) && ((measurement_step & 0x3) == 0x0) ) {
select_channel();
adc_result = 0;
// read value
adc_result += SPI_transceive_16bit(0x0000);
adc_result /= 2;
-// send_answer_hex('C', current_channel+'0', adc_result & 0xffff);
int32_t y = res_to_temp(adc_to_res((uint16_t)adc_result, current_channel));
- temperature[current_channel] = y;
- send_answer_hex('T', current_channel+'0', y & 0xfffff);
+ temperature[sample][current_channel] = y;
}
else {
num_connected_sensors = 8;
- uint32_t nc_msg = 0x100000;
- temperature[current_channel] = 0;
- send_answer_hex('T', current_channel+'0', nc_msg);
+ temperature[sample][current_channel] = 0;
+ invalid[current_channel] = 1;
+ }
+
+ if(sample == calib_settings.average) {
+ if(invalid[current_channel] == 0) {
+ temperature[16][current_channel] = 0;
+ for(uint8_t i=0; i<=calib_settings.average;i++)
+ temperature[16][current_channel] += temperature[i][current_channel];
+ temperature[16][current_channel] /= calib_settings.average+1;
+ send_answer_hex('T', current_channel+'0', temperature[16][current_channel] & 0xfffff);
+ }
+ else {
+ uint32_t nc_msg = 0x100000;
+ temperature[16][current_channel] = 0;
+ send_answer_hex('T', current_channel+'0', nc_msg);
+ }
}
+
current_channel += 1;
}
else if(measurement_step == 33) {
else if(measurement_step == 34) {
con = ADCL;
con += (ADCH<<8);
- connect_threshold = con - 7000;
+ connect_threshold = con - 4000;
}
measurement_step += 1;
if(measurement_step == 36) {
measurement_step = 0;
current_channel = 0;
measurement_active = 0;
- }
+ if(sample == calib_settings.average) {
+ make_update_lcd = 1;
+ sample = 0;
+ for(uint8_t i = 0; i<8; i++)
+ invalid[i] = 0;
+ }
+ else {
+ sample++;
+ }
+ }
}
void forward_msg(uint8_t i) {
if(rxbuf[0] == 'A') rxbuf[2]++;
else rxbuf[2]--;
+ sei();
+ while(TX_BUSY());
memcpy ((uint8_t*)txbuf,(uint8_t*)rxbuf,i);
STARTTX(i);
}