Skip to main content

06 Experiment

Intensity variation of LED based on potentiometer value

Source and description

http://sincgrid.in/avr-tutorials/avr-tutorial-experiment-6/

http://sincgrid.in/avr-tutorials/avr-tutorial-experiment-6/
#include <avr/io.h>
#define F_CPU 16000000UL
#include <avr/delay.h>

int adc_value; //Variable used to store the value read from the ADC converter

int main(void){

// unsigned char i=0;

DDRD|= _BV(DDD6); //Set our pwm pin as an output

//Timer configuration
TCCR0A = ((1<<COM0A1)|(1<<WGM01)|(1<<WGM00)); //Enable pwm mode in pin PD6 and set the WGM bits to Fast pwm mode
TCCR0B = ((1<<CS01)|(1<<CS00)); //Set prescaler to 32

ADCSRA |= ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)); //Prescaler at 128 so we have an 125Khz clock source
ADMUX |= (1<<REFS0);
ADMUX &= ~(1<<REFS1); //Avcc(+5v) as voltage reference
ADCSRB &= ~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)); //ADC in free-running mode
ADCSRA |= (1<<ADATE); //Signal source, in this case is the free-running
ADCSRA |= (1<<ADEN); //Power up the ADC
ADCSRA |= (1<<ADSC); //Start converting

for(;;)
{

adc_value = ADCW;
OCR0A = adc_value/4 - 24;
_delay_ms(1000);

}

return 0;
}