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.

101 lines
1.8 KiB

#include "main.h"
#include "spi.h"
15 years ago
#include "muxer.h"
#include "i2c_simple.h"
#include "mcp_adc.h"
#include "debug.h"
uint16_t timertmp;
uint16_t temperatures[4];
uint8_t sensor_active[4];
int32_t temp_avg_cumul[4];
int16_t temp_avg_count[4];
15 years ago
/* pinout
* - i2c:
* C5
* C4
* - multiplexer:
* C3: inhibit
* C0-C2: muxer select
*
*
* amp 0 is on muxer channel 2
*/
#define MUXER_CHANNEL_0 2
#define MUXER_CHANNEL_1 0
#define MUXER_CHANNEL_2 1
#define MUXER_CHANNEL_3 3
/* 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();
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();
muxer_set(MUXER_CHANNEL_0);
for(;;){
SOFTTIMER(1,10) {
for(int i=0; i<4; i++) {
if(sensor_active[i] && mcpadc_has_new_data(i)) {
temperatures[i] = mcpadc_get_data(i);
temp_avg_cumul[i] += temperatures[i];
temp_avg_count[i] += 1;
}
}
}
SOFTTIMER(2,500){
for(int i=0; i<4; i++) {
if(sensor_active[i]) {
int32_t temp = temp_avg_cumul[i];
temp /= temp_avg_count[i];
temp_avg_count[i] = 0; temp_avg_cumul[i] = 0;
int32_t nV = (temp * 625);
int32_t mK = nV/39;
dbgLog("temp-%i: %6li (%li µV, %li mK)\n",i,temp,nV/1000, mK);
}
}
}
}
}
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;
}
}