You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.2 KiB

#include "main.h"
#include "spi.h"
#include "muxer.h"
#include "i2c_simple.h"
#include "mcp_adc.h"
#include "debug.h"
#include "filter.h"
#include "offset.h"
#include <math.h>
#include <util/delay.h>
#include <stdio.h>
uint16_t timertmp;
uint16_t temperatures[4];
/* pinout
* - i2c:
* C5
* C4
* - multiplexer:
* C3: inhibit
* C0-C2: muxer select
*
* amp 0 is on muxer channel 2
*/
/* initializes the hardware */
void hardinit() {
// enable softtimer isr
TCCR1A = _BV(WGM10);
TCCR1B = _BV(WGM12) | _BV(CS11); // clk/8 prescaler, 4kHz PWM-freq.
TIMSK1 |= _BV(TOIE1);
spi_init();
muxer_init();
i2c_init();
sei();
}
void softinit() {
dbg_init();
stdout = &mystdout;
printf("======= starting logging\n");
mcpadc_init(ADC_GAIN_2|ADC_CONV_CONT|ADC_BITS_16);
// offset_init();
sensordata[0].offset = -400;
sensordata[1].offset = +478;
}
int __attribute__((noreturn)) main(void) {
hardinit();
softinit();
// muxer_set(4);
// for(;;);
int16_t data;
for(;;){
// measure temps 5*2 times
for(uint8_t i=0; i<5; i++) {
for(uint8_t active_sensor=0; active_sensor<2; active_sensor++) { // only measuring two probes atm
// printf("=== active sensor: %i\n",active_sensor);
muxer_set(active_sensor);
for(uint8_t j=0; j<4; ++j){
while(!mcpadc_has_new_data()) _delay_ms(10);
mcpadc_get_data(); // first data after switch to trash
}
while(!mcpadc_has_new_data()) _delay_ms(10);
data = mcpadc_get_data();
process_thermocouple_value(data,active_sensor);
}
}
/*
// measure 2 offsets
for(uint8_t active_sensor=0; active_sensor<2; active_sensor++)
if(offset_measure[active_sensor]) {
printf("=== measuring offset, sensor: %i\n",active_sensor);
muxer_set(active_sensor);
while(!mcpadc_has_new_data()) _delay_ms(10);
mcpadc_get_data(); // first data after switch to trash
while(!mcpadc_has_new_data()) _delay_ms(10);
offset_val[active_sensor] = mcpadc_get_data();
while(offset_measure[active_sensor]) {
offset_val[active_sensor] += mcpadc_get_data();
offset_count[active_sensor]++;
}
}
*/
// measure ambient
printf("====== now measuring ambient\n");
muxer_set(4);
while(!mcpadc_has_new_data()) _delay_ms(10);
mcpadc_get_data(); // first data after switch to trash
// while(!mcpadc_has_new_data()) _delay_ms(10);
// mcpadc_get_data(); // first data after switch to trash
_delay_ms(100);
while(!mcpadc_has_new_data()) _delay_ms(10);
data = mcpadc_get_data();
process_ambient_value(data);
}
}
ISR(TIMER1_OVF_vect,ISR_NOBLOCK){
timertmp=timer1_acc;
timertmp++;
/* the ATOMIC is acutally only needed if timer1_acc is never read from an ISR, which
* is probably the case.
* ATOMIC_FORCEON: the ISR_NOBLOCK sets sei() a few cycles before.
*/
ATOMIC_BLOCK(ATOMIC_FORCEON){
timer1_acc=timertmp;
}
}