From c3538a31b9cca9f47383407156f03f41c0504778 Mon Sep 17 00:00:00 2001 From: Nidan Date: Fri, 10 Dec 2010 16:54:01 +0000 Subject: [PATCH] messing with the adc --- firmware/mcp_adc.c | 40 ++++++++++++++++++++++++++++------------ firmware/mcp_adc.h | 19 +++++-------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/firmware/mcp_adc.c b/firmware/mcp_adc.c index d6d04cb..9aaa43d 100644 --- a/firmware/mcp_adc.c +++ b/firmware/mcp_adc.c @@ -20,49 +20,65 @@ * */ + #define ADC_NEW_SAMPLE 0x80 +#define ADC_READ_DATA 0x40 + #define ADC_BITS_MASK 0x0c #define ADC_ADDR 0xD0 /* or 0x68 */ uint8_t r[4]; +uint8_t curmode; void mcpadc_init(uint8_t mode) { i2c_write(ADC_ADDR, 1, &mode); + curmode = mode; } -uint8_t mcpadc_has_new_data() +uint8_t mcpadc_has_new_data(void) { - i2c_read(ADC_ADDR, 4, r);/* 4 bytes are only needed in 18 bit mode */ - return(r[3] & ADC_NEW_SAMPLE); + /* this assumes that the first RDY bit read after the sample data indicates the old/new state of the sample we just read */ + if(curmode & ADC_BITS_MASK == ADC_BITS_18) + { + i2c_read(ADC_ADDR, 4, r); + curmode = r[4]; + } + else + { + i2c_read(ADC_ADDR, 3, r); + curmode = r[3]; + } + if(~curmode & ADC_NEW_SAMPLE) {curmode &= ADC_READ_DATA;} + return ~curmode & ADC_READ_DATA; } #if ADC_ENABLE_18_BIT_MODE -int32_t mcpadc_get_data() +int32_t mcpadc_get_data(void) { int32_t value = 0; - i2c_read(ADC_ADDR, 4, r);/* reading 4 bytes guarantees us one config byte */ if(r[0] & 0x80) {value = 0xffff;} - value = (value << 16) | (r[0] << 8) | r[1];/*endianess ???*/ + value = (value << 16) | (r[0] << 8) | r[1]; if((uint8_t) r[3] & ADC_BITS_MASK == ADC_BITS_18) { value = (value << 8) | r[2]; } + curmode |= (ADC_NEW_SAMPLE & ADC_READ_DATA); return value; } #else -int16_t mcpadc_get_data() +int16_t mcpadc_get_data(void) { - i2c_read(ADC_ADDR, 2, r);/* this will NOT work in 18 bit mode */ - int16_t value = (r[0] << 8) | r[1];/*endianess ???*/ + int16_t value = (r[0] << 8) | r[1]; + curmode |= (ADC_NEW_SAMPLE & ADC_READ_DATA); return value; } #endif -void mcpadc_start_conv() +void mcpadc_start_conv(void) { - i2c_read(ADC_ADDR, 4, r); - r[3] |= ADC_NEW_SAMPLE; + r[3] = curmode | ADC_NEW_SAMPLE; i2c_write(ADC_ADDR, 1, &(r[3])); + curmode |= (ADC_NEW_SAMPLE & ADC_READ_DATA);/* you asked for a new sample - no you won't get the old one */ } diff --git a/firmware/mcp_adc.h b/firmware/mcp_adc.h index 5ff341a..db90bcc 100644 --- a/firmware/mcp_adc.h +++ b/firmware/mcp_adc.h @@ -1,15 +1,6 @@ #ifndef MCPADC_H #define MCPADC_H -/* - -used i2c functions: - -i2c_read(uint8_t address, uint length, uint8_t *buf); -i2c_write(uint8_t address, uint length, uint8_t *buf); - -*/ - #define ADC_ENABLE_18_BIT_MODE 0 #define ADC_GAIN_1 0x00 @@ -30,25 +21,25 @@ i2c_write(uint8_t address, uint length, uint8_t *buf); /* sets the mcp adc to the specified mode * valid modes are obtained by oring one of each group (ADC_GAIN_*, ADC_BITS_* and ADC_CONV_*) together */ -void mcpadc_init(uint8_t const mode); +void mcpadc_init(uint8_t mode); /* tests if the adc has a new sample * returns nonzero if new data is aviable, 0 otherwise */ -uint8_t mcpadc_has_new_data(); +uint8_t mcpadc_has_new_data(void); /* returns the most recent sample result of the adc * !! return type depends on defines */ #if ADC_ENABLE_18_BIT_MODE -int32_t mcpadc_get_data(); +int32_t mcpadc_get_data(void); #else -int16_t mcpadc_get_data(); +int16_t mcpadc_get_data(void); #endif /* instructs the adc to take a new sample now * has no effect if the current mode includes ADC_CONV_CONT */ -void mcpadc_start_conv(); +void mcpadc_start_conv(void); #endif