0% found this document useful (0 votes)
90 views

ADC Initialization

The code segment initializes the ADC to read analog values from two channels, display them on an LCD, and control an LED based on threshold comparisons of the channel values. It sets the ADC reference voltage, enables the ADC, and configures the prescaler. A function reads an ADC value from a given channel by setting the multiplexer, starting a conversion, and waiting for it to complete. In main, it initializes the ADC and LCD display, then continuously reads both channels, displays the values, and turns an LED on if both values are below thresholds.

Uploaded by

PDT_VIJAYINFO
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

ADC Initialization

The code segment initializes the ADC to read analog values from two channels, display them on an LCD, and control an LED based on threshold comparisons of the channel values. It sets the ADC reference voltage, enables the ADC, and configures the prescaler. A function reads an ADC value from a given channel by setting the multiplexer, starting a conversion, and waiting for it to complete. In main, it initializes the ADC and LCD display, then continuously reads both channels, displays the values, and turns an LED on if both values are below thresholds.

Uploaded by

PDT_VIJAYINFO
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ADC Initialization

The following code segment initializes the ADC. 1 2void adc_init() 3{ // AREF = AVcc 4 ADMUX = (1<<REFS0); 5 6 // ADC Enable and prescaler of 128 7 // 16000000/128 = 125000 ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); 8 9

Reading ADC Value


uint16_t adc_read(uint8_t ch) { // select the corresponding channel 0~7 // ANDing with 7 will always keep the value // of ch between 0 and 7 ch &= 0b00000111; // AND operation with 7 ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing // start single convertion // write 1 to ADSC ADCSRA |= (1<<ADSC); // wait for conversion to complete // ADSC becomes 0 again // till then, run loop continuously while(ADCSRA & (1<<ADSC)); return (ADC); }

#include <avr/io.h> #include <util/delay.h> #include "lcd.h" #define LTHRES 500 #define RTHRES 500 // initialize adc void adc_init() { // AREF = AVcc ADMUX = (1<<REFS0); // ADC Enable and prescaler of 128 // 16000000/128 = 125000 ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); } // read adc value uint16_t adc_read(uint8_t ch) { // select the corresponding channel 0~7 // ANDing with '7' will always keep the value // of 'ch' between 0 and 7 ch &= 0b00000111; // AND operation with 7 ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing // start single conversion // write '1' to ADSC ADCSRA |= (1<<ADSC); // wait for conversion to complete // ADSC becomes '0' again // till then, run loop continuously while(ADCSRA & (1<<ADSC)); return (ADC); } int main() { uint16_t adc_result0, adc_result1; char int_buffer[10]; DDRC = 0x01; // to connect led to PC0 // initialize adc and lcd adc_init(); lcd_init(LCD_DISP_ON_CURSOR); // display the labels on LCD lcd_puts("left ADC = "); lcd_gotoxy(0,1);

lcd_puts("right ADC = "); _delay_ms(50); while(1) { adc_result0 = adc_read(0); adc_result1 = adc_read(1);

// read adc value at PA0 // read adc value at PA1

// condition for led to glow if (adc_result0 < LTHRES && adc_result1 < RTHRES) PORTC = 0x01; else PORTC = 0x00; // now display on lcd itoa(adc_result0, int_buffer, 10); lcd_gotoxy(12,0); lcd_puts(int_buffer); itoa(adc_result0, int_buffer, 10); lcd_gotoxy(12,1); lcd_puts(int_buffer); _delay_ms(50); } }

You might also like