Lab Three

Lab Three - Lab Three - AVR Analog-to-Digital Conversion...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Lab Three - AVR Analog-to-Digital Conversion Wallace Kerrigan, 1541317 wallace.kerrigan@uconn.edu 10 October 2011 Part One
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Figure 1. Flow Chart for Analog-to-Digital Conversion with LED Partition Code #include <avr/io.h> //************************************************************************* //InitADC: initialize analog-to-digital converter //************************************************************************* void InitADC( void) { ADMUX = 0; //Select channel 0 //Set ADC module prescalar to 8 //critical for accurate ADC results // this is equivalent to ADCSRA = (1<<ADEN) | //(1<<ADSC)| (3<<ADPS0) while (!(ADCSRA & 0x10)); //Check if conversation is ready ADCSRA |= 0x10; //Clear conv rdy flag - set the bit
Background image of page 2
} //************************************************************************* //ReadADC: read analog voltage from analog-to-digital converter - the //desired channel for conversion is passed in as an unsigned character //variable. The result is returned as a left justified, 10 bit binary //result. The ADC prescalar must be set to 8 to slow down the ADC clock at //higher external clock frequencies (10 MHz) to obtain accurate results. //************************************************************************* unsigned int ReadADC(unsigned char channel) { unsigned int binary_weighted_voltage, binary_weighted_voltage_low; unsigned int binary_weighted_voltage_high; //weighted binary voltage ADMUX = channel; //Select channel ADCSRA |= 0x43; //Start conversion by setting ADSC //Set ADC module prescalar to 8 //critical for accurate ADC results while (!(ADCSRA & 0x10)); //Check if conversion is ready ADCSRA |= 0x10; //Clear Conv rdy flag - set the bit binary_weighted_voltage_low = ADCL; //Read 8 low bits first (important) binary_weighted_voltage_high = ((unsigned int)(ADCH << 8)); //Read 2 high bits, multiply by 256 binary_weighted_voltage = binary_weighted_voltage_low | binary_weighted_voltage_high; return binary_weighted_voltage; //ADCH:ADCL } //************************************************************************* int main(void) { DDRD = 0x5C; PORTD = 0x5C; InitADC(); // Initialize the Analog-to-Digital Converter for(;;) { ADCSRB = 0x00; // Free-running mode { PORTD = 0x00; // Binary conversion: 0b00000000 } else { PORTD = 0x40; // Binary conversion: 0b01000000
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
} else { PORTD = 0x50; // Binary conversion: 0b01010000 } else { PORTD = 0x58; // Binary conversion: 0b01011000 } else if (ReadADC(0x41) >= 1020) { PORTD = 0x5C; // Binary conversion: 0b01011100 } else { PORTD = 0x48; // Binary conversion: 0b01001000 } } } Explanation The sample code was implemented and used mainly as a base for the program. The LEDs from port D were established as the output. “InitADC” was used to initialize the ADC converter.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

Lab Three - Lab Three - AVR Analog-to-Digital Conversion...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online