]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
adding configurable averaging, few bugfixes
authorJan Michel <j.michel@gsi.de>
Thu, 27 Aug 2015 18:20:36 +0000 (20:20 +0200)
committerJan Michel <j.michel@gsi.de>
Thu, 27 Aug 2015 18:20:36 +0000 (20:20 +0200)
pt100/Makefile
pt100/main.c
pt100/main.h
pt100/tempmeas.c
pt100/uart.c

index b246656b1adf68295e897df6ce8dc6963612bbc3..18dfd9675d6b97f413e2689796807ec705a41f6a 100644 (file)
@@ -109,8 +109,8 @@ AVRDUDE_NO_VERIFY = -V
 #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
index b192ea4d999fed8b7c8d7aee5cfdfd16f13c519a..d9afba6d1f0cc05ea76675e0d4d254688e95b313 100644 (file)
@@ -10,7 +10,9 @@ volatile uint8_t  next_second = 0;
 
 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;
@@ -41,6 +43,7 @@ void read_calib() {
   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);
   }
 
 /******************
@@ -73,7 +76,7 @@ void lcd_update(void) {
 //   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);}
     
@@ -123,7 +126,7 @@ void init(void) {
 
  
 //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
 
@@ -165,7 +168,6 @@ void init(void) {
 ISR(TIMER0_COMPA_vect) {
   if(++time == calib_settings.period) {
     time = 0;
-    send_information();
     measurement_active = 1;
     }
   next_step = 1;
@@ -176,7 +178,8 @@ ISR(TIMER0_COMPA_vect) {
  * Second ticks
  *********************/ 
 ISR(TIMER1_COMPA_vect) {
-  next_second = 1;
+//   next_second = 1;
+//   send_information();
 }
 
 
@@ -188,10 +191,11 @@ int main(void) {
     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)) {}
index 68bcc541b8375fea7e1d98f9dd0492f10384448e..7925b7adf78a177f92977fab3064d44ff31667c5 100644 (file)
@@ -53,12 +53,16 @@ extern uint8_t  num_connected_sensors;
 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)
   };
index 50ab0bfa037195a36db73268249c1e5c0fda992b..cadfc3c65e2e5158e03f3fc5bba2b5c67b28fc3b 100644 (file)
@@ -7,6 +7,7 @@ uint8_t measurement_step = 0;
 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
@@ -117,6 +118,7 @@ void do_measurement_step(void) {
   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;
@@ -146,17 +148,30 @@ void do_measurement_step(void) {
       // 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) {
@@ -165,13 +180,22 @@ void do_measurement_step(void) {
   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++;
+      }
+    }  
   }
 
index 81aa14e631c59885a3db1ca72b85e4f93e3e55f1..dd6994b6f3f2051f2c1192e44ea669af5afb9380 100644 (file)
@@ -49,6 +49,8 @@ uint8_t hex_to_byte(uint8_t* h) {
 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);
   }