Sei sulla pagina 1di 2

/****************************************************************** Course: Embedded System Development using AVR microcontrollers Project: Description: Author: Company: Date: Web: Controlling

PWM using a potentiometer Read ADC and use the value read to control PWM duty cycle. Anirban Basak Li2 Innovations Pvt. Ltd. 30 Nov 2011 www.li2.in

*******************************************************************/ #include <avr/io.h> #define L_PORT PORTC ters #define L_DDR DDRC #define L_PIN PINC #define ADC_PORT PORTA ters #define ADC_DDR DDRA #define ADC_PIN PINA // Define LED port regis

// Define ADC port regis

void adc_init(void) ation function { // Enable ADC, set ADC clock speed as 16MHz/128 ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); ADMUX = (1<<REFS0)|(1<<ADLAR); // ADC left adjust result for 8-bit } uint8_t adc_output(void) { uint8_t adc_result; ADCSRA |= (1<<ADSC); while((ADCSRA & (1<<ADIF)) == 0); adc_result = ADCH; sult return adc_result; sult } int main() program { L_DDR = 0xFF; for LED and ADC ports ADC_DDR = 0x00;

// ADC initializ

// Set Reference as AVCC and

// Function to read ADC0 // Start conversion // Wait for end of conversion // Read 8-bit re // Return the re

// Main // Set direction

DDRD |= (1<<7); output adc_init(); alize ADC OCR2 = 0; nitial PWM duty cycle

// Set pin D7 as // Initi // Set i

// PWM frequency as ~1953Hz, Fast PWM in non-inverting mode and start. TCCR2 = (1<<CS21)|(1<<CS20)|(1<<WGM21)|(1<<WGM20)|(1<<COM21); // Set PWM frequency as 15625 Hz while(1) ite loop { L_PORT = adc_output(); to LEDs OCR2 = L_PORT; e accordingly } return 0; } // Set duty cycl // Infin // ADC read and display

Potrebbero piacerti anche