mcpadc working a little, still some unknown bug

master
Paul Goeser 15 years ago committed by Dario Ernst
parent 01470cf054
commit 5c9b6ceefc

@ -49,17 +49,12 @@ void hardinit() {
}
void softinit() {
// TODO: to prevent evil random access and stuff?
temperatures[0] = 0;
temperatures[1] = 0;
temperatures[2] = 0;
temperatures[3] = 0;
//test values
foo = 0x87;
bar= 0xfafa;
mcpadc_init(ADC_GAIN_2|ADC_CONV_SINGLE|ADC_BITS_16);
mcpadc_init(ADC_GAIN_2|ADC_CONV_CONT|ADC_BITS_16);
}
@ -71,11 +66,11 @@ int __attribute__((noreturn)) main(void) {
softinit();
muxer_set(MUXER_CHANNEL_0);
temperatures[2]=33;
temperatures[2]=0;
for(;;){
SOFTTIMER(1,100) {
SOFTTIMER(1,10) {
if(mcpadc_has_new_data()) {
temperatures[0] = mcpadc_get_data();
temperatures[2]++;

@ -30,16 +30,25 @@
uint8_t r[4];
uint8_t curmode;
uint8_t unread_data_available;
void mcpadc_init(uint8_t mode)
{
i2c_write(ADC_ADDR, 1, &mode);
int written = i2c_write(ADC_ADDR, 1, &mode);
if(written!=1){
for(;;); //TODO: error reporting
}
curmode = mode;
}
uint8_t mcpadc_has_new_data(void)
{
/* this assumes that the first RDY bit read after the sample data indicates the old/new state of the sample we just read */
if(unread_data_available){
// we already have new data, don't overwrite that by reading more
return unread_data_available;
}
#if ADC_ENABLE_18_BIT_MODE
if(curmode & ADC_BITS_MASK == ADC_BITS_18)
{
@ -47,18 +56,24 @@ uint8_t mcpadc_has_new_data(void)
curmode = r[3];
}
else
#endif
#else
{
i2c_read(ADC_ADDR, 3, r);
curmode = r[2];
}
if(~curmode & ADC_NEW_SAMPLE) {curmode &= ADC_READ_DATA;}
return ~curmode & ADC_READ_DATA;
#endif
if(!(curmode & ADC_NEW_SAMPLE)){ // if the /RDY bit reads 0, we just read a new sample
unread_data_available = 1;
}
return unread_data_available;
}
#if ADC_ENABLE_18_BIT_MODE
int32_t mcpadc_get_data(void)
{
if(!unread_data_available){
return 0;
}
int32_t value = 0;
if(r[0] & 0x80) {value = 0xffff;}
value = (value << 16) | (r[0] << 8) | r[1];
@ -66,14 +81,19 @@ int32_t mcpadc_get_data(void)
{
value = (value << 8) | r[2];
}
curmode |= (ADC_NEW_SAMPLE & ADC_READ_DATA);
curmode |= (ADC_NEW_SAMPLE);
unread_data_available = 0;
return value;
}
#else
int16_t mcpadc_get_data(void)
{
if(!unread_data_available){
return 0;
}
int16_t value = (r[0] << 8) | r[1];
curmode |= (ADC_NEW_SAMPLE & ADC_READ_DATA);
unread_data_available = 0;
return value;
}
#endif
@ -82,5 +102,6 @@ void mcpadc_start_conv(void)
{
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 */
// curmode |= (ADC_NEW_SAMPLE & ADC_READ_DATA);/* you asked for a new sample - no you won't get the old one */
unread_data_available = 0;
}

Loading…
Cancel
Save