I'm currently try to connect a DS18S20 temperature sensor to the atmega328p. I have wired AVCC and AGRD to the same power rail on my breadboard as normal VCC/GRD and AREF is not connected.
When I try to connect the data pin of the temperature sensor to the first channel of port A, the value changes to zero and stays zero. Before connecting the data pin, the value jumps around randomly. I have the following code:
volatile uint16_t vOutADCReading = 0U;
void setupAnalogDigitalConversion(){
ADMUX |= 1<<REFS0;
ADCSRA |= 1<<ADEN;
ADCSRA |= 1<<ADPS0 | 1<<ADPS1 | 1<<ADPS2;
ADCSRA |= ( 1 << ADSC );
}
void adc_read(uint8_t channel){
ADMUX &= 0xf0;
ADMUX |= channel;
ADCSRA |= (1<<ADSC);
while(ADCSRA & (1<<ADIF));
// Set voltage output
vOutADCReading = ADC;
// Clear ADIF flag
ADCSRA |= ( 1 << ADIF );
_delay_ms(1000);
}
void main(){
while(1){
adc_read(0);
displayOnSegmentDisplay(vOutADCReading);
_delay_ms(1000);
}
}
Are there any errors in this code?