I am trying to get a simple IR sensor to work on my an attiny85. To do this I first wrote some code to setup the ADC and the perform an operation based on the outcome. Because the code didn't function as expected I decided to test if some code where I simply do one ADC conversion and then turn on an LED works. This did however not work. The cause of this seems to be that the attiny stops executing code after the ADC conversion. I am running the attiny at internal 8Mhz clock and the ADC is hooked up to ground through a 200 ohm resistor.
The following code does not turn on the pin at PB0:
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
DDRB |= _BV(DDB0); //set PB0 to output
ADMUX = _BV(ADLAR) | _BV(MUX1); //set ADC 2, left adjust result
ADCSRA = _BV(ADPS1) | _BV(ADPS2); //prescaler 64
ADCSRA |= _BV(ADEN); //enable ADC
while(1) {
ADCSRA |= _BV(ADSC); //start conversion
while (ADCSRA & _BV(ADSC)); //wait for conversion to end
uint8_t adc_result = ADCH; //adc results (should be 0)
PORTB |= _BV(PORTB0); //set PB0 to high
}
return(0);
}
When I, however, put the "PORTB |= _BV(PORTB0);" line at the start of the loop, so before the ADC conversion, the line does execute and the LED at PB0 does turn on.
Also commenting out the line "ADCSRA |= _BV(ADSC);" makes the code run fine and makes PB0 go high. So I am pretty certain that the attiny somehow crashes when executing that line.
Disabling interrupts using "cli()" does not solve the problem either.