uint8_t measurement_step;
uint8_t current_channel = 0;
-
+uint8_t num_connected_sensors = 8;
//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
return result;
}
+void set_inhibit_signals(int on_off) {
+ PORTC = (PORTC & 0x3F) | (on_off << 6);
+ }
+
+void select_channel(void) {
+ if (current_channel == 8) {
+ // temperature measurement
+ set_inhibit_signals(0);
+ }
+ else {
+ // update the select outputs
+ PORTA = (PORTA & 0x8F) | (current_channel << 4);
+ // clear inhibit signals
+ set_inhibit_signals(0);
+ }
+ }
+
+
+char SPI_transceive(unsigned char cData){
+ SPDR = cData;
+ while (!(SPSR & (1<<SPIF)))
+ ;
+ return SPDR;
+ }
+
+uint16_t SPI_transceive_16bit(uint16_t data){
+ char high, low;
+ high = SPI_transceive(data>>8);
+ low = SPI_transceive(data&0xFF);
+ return (high << 8) | low;
+ }
//measurement_active gets set once per second
//35 send ADC temperature
void do_measurement_step(void) {
- if(measurement_step < (CONNECTED_SENSORS*4) && measurement_step & 0x3 == 0x0) {
+ if(measurement_step < (CONNECTED_SENSORS*4) && (measurement_step & 0x3) == 0x0) {
LED1_ON();
select_channel();
}
- else if(measurement_step < (CONNECTED_SENSORS*4) && measurement_step & 0x3 == 0x1) {
+ else if(measurement_step < (CONNECTED_SENSORS*4) && (measurement_step & 0x3) == 0x1) {
ADCSRA |= (1<<ADSC); //start ADC conversion
uint16_t conf_register = (ADS_MODE_S | ADS_GAIN_025V |
ADS_SConv | ADS_WRITE_CONFIG | ADS_PULLUP_DRDY | ADS_1600SPS);
conf_register |= ADS_MUX_AIN0_AIN3;
SPI_transceive_16bit(conf_register);
}
- else if(measurement_step < (CONNECTED_SENSORS*4) && measurement_step & 0x3 == 0x2) {
+ else if(measurement_step < (CONNECTED_SENSORS*4) && (measurement_step & 0x3) == 0x2) {
set_inhibit_signals(1);
uint16_t x;
// read on / off
if (x < 58112) {
// read value
x = SPI_transceive_16bit(0x0000);
- x = res_to_temp(adc_to_res(x));
- void send_answer_hex(0, 0, x);
+ x = res_to_temp(adc_to_res(x, current_channel));
+ send_answer_hex(0, 0, x);
}
else {
- void send_answer_hex(0, 0, (1<< (5*4)));
+ num_connected_sensors = 8;
+ uint32_t nc_msg = 0x100000;
+ send_answer_hex(0, 0, nc_msg);
}
- else if(measurement_step < (CONNECTED_SENSORS*4) && measurement_step & 0x3 == 0x3) {
- selected_channel += 1;
+ }
+ else if(measurement_step < (CONNECTED_SENSORS*4) && (measurement_step & 0x3) == 0x3) {
+ current_channel += 1;
}
measurement_step += 1;
if(measurement_step == 36)
measurement_step = 0;
- selected_channel = 0
+ current_channel = 0;
LED1_OFF();
measurement_active = 0;
}
-
-void set_inhibit_signals(int on_off) {
- PORTC = (PORTC & 0x3F) | (on_off << 6);
- }
-
-void select_channel() {
- if (current_channel == 8) {
- // temperature measurement
- set_inhibit_signals(0);
- }
- else {
- // update the select outputs
- PORTA = (PORTA & 0x8F) | (current_channel << 4);
- // clear inhibit signals
- set_inhibit_signals(0);
- }
- }
-
-uint16_t SPI_transceive_16bit(uint16_t data){
- char high, low;
- high = SPI_transceive(data>>8);
- low = SPI_transceive(data&0xFF);
- return (high << 8) | low;
- }
-
-char SPI_transceive(unsigned char cData){
- SPDR = cData;
- while (!(SPSR & (1<<SPIF)))
- ;
- return SPDR;
- }
\ No newline at end of file