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.

139 lines
3.3 KiB

#include "main.h"
#include "spi.h"
15 years ago
#include "muxer.h"
#include "i2c_simple.h"
#include "mcp_adc.h"
#include "debug.h"
#include <math.h>
#include "filter.h"
15 years ago
#include <util/delay.h>
uint16_t timertmp;
uint16_t temperatures[4];
15 years ago
/* pinout
* - i2c:
* C5
* C4
* - multiplexer:
* C3: inhibit
* C0-C2: muxer select
* - offset measurement
* D0-D3 offset compensation
15 years ago
*
*
* 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();
15 years ago
muxer_init();
offset_measure_init();
i2c_init();
15 years ago
sei();
}
void softinit() {
mcpadc_init(ADC_GAIN_2|ADC_CONV_CONT|ADC_BITS_16);
}
int __attribute__((noreturn)) main(void) {
hardinit();
softinit();
15 years ago
muxer_set(1);
15 years ago
int16_t data;
for(;;){
_delay_ms(4000);
dbgLog("loop iteration\n");
15 years ago
// measure temps 5*2 times
for(uint8_t i=0; i<5; i++) {
// dbgLog("starting temp measuring\n");
for(uint8_t active_sensor=0; active_sensor<2; active_sensor++) { // only measuring two probes atm
15 years ago
muxer_set(active_sensor);
while(!mcpadc_has_new_data()) _delay_ms(10);
15 years ago
mcpadc_get_data(); // first data after switch to trash
while(!mcpadc_has_new_data()) _delay_ms(10);
15 years ago
data = mcpadc_get_data();
process_thermocouple_value(data,active_sensor);
_delay_ms(1000);
}
dbgLog("a");
}
// dbgLog("now measuring offsets\n");
15 years ago
// measure 2 offsets
for(uint8_t active_sensor=0; active_sensor<2; active_sensor++) { // only measuring two offsets atm
// dbgLog("active sensor: %i",active_sensor);
15 years ago
muxer_set(active_sensor);
offset_measure_start(active_sensor);
while(!mcpadc_has_new_data()) _delay_ms(10);
15 years ago
mcpadc_get_data(); // first data after switch to trash
while(!mcpadc_has_new_data()) _delay_ms(10);
15 years ago
data = mcpadc_get_data();
// dbgLog("retrieved offset data %i\n", data);
15 years ago
// TODO: what to do with the offset?
offset_measure_stop();
}
// measure ambient
muxer_set(5);
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_ambient_value(data);
15 years ago
/* SOFTTIMER(1,8000) { // maybe measure coldjunction comp
15 years ago
/* // first measure ntc offset?! maybe? */
/* offset_measure_start(active_sensor); */
/* while(!mcpadc_has_new_data()) _delay_ms(1); */
/* mcpadc_get_data(); // first data after switch to trash */
/* while(!mcpadc_has_new_data()) _delay_ms(1); */
15 years ago
/* data = mcpadc_get_data(); */
/* offset_measure_stop(); */
15 years ago
/* // now measure temp-data */
/* while(!mcpadc_has_new_data()) _delay_ms(1); */
/* mcpadc_get_data(); // first data after switch to trash */
/* while(!mcpadc_has_new_data()) _delay_ms(1); */
15 years ago
/* data = mcpadc_get_data(); */
15 years ago
}
}
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;
}
}