6
Programming Code //####### Prototype Area ######### #include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> #define F_CPU 1000000UL //Clock Frequency 1MHz //####### Functions############### void InitADC() // This routine initializes AVR internal ADC module to work with RC clock . Clock determines the time period necessary for performing AD conversion { ADMUX=(1<<REFS0)|(1<<REFS1); // Set (REF Voltage & Input channel) … AREF=internal 2.56v ADCSRA=(1<<ADEN)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2); //ADC Enabling (ADEN ..Start Conversion) & Set pre-scalar setting } uint16_t ReadADC() { //Select ADC Channel ADMUX = 0xE0; //Start Single conversion ADCSRA|=(1<<ADSC); //Wait for conversion to complete while(!(ADCSRA & (1<<ADIF))); //Clear ADIF by writing one to it ADCSRA|=(1<<ADIF); return(ADCH); //The digital value stored in the ADCH register } int main(void) { char segment [10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //7-Segment Numbers Codes in hexadecimal DDRB=0xFF; //PORT B Output DDRD=0xFF; //PORT D Output sei(); // Enable Global Interrupts InitADC(); // Enabling the Conversion while(1) // Infinite Loop { // Temperature Calculations uint16_t adc_value=ReadADC() ; //Select .. Start .. Wait .. Clear uint8_t temp= adc_value / 4; //Getting the temp value uint8_t first = temp/10; // int (25/10) = 2 >> First uint8_t second= temp-first*10; // 25 – (2*10) = 5 >> Second PORTB=segment[first]; // send first digit to 7-segment PORTD=segment[second]; // send last digit to 7-segment _delay_ms(1000); //wait 1 sec ADCSRA |= 1<<ADSC; //Start ADC Conversion Again } }

AVR ADC

Embed Size (px)

DESCRIPTION

avr adc

Citation preview

  • Programming Code //####### Prototype Area ######### #include #include #include #define F_CPU 1000000UL //Clock Frequency 1MHz //####### Functions###############

    void InitADC() // This routine initializes AVR internal ADC module to work with RC clock. Clock determines the time period necessary for performing AD conversion { ADMUX=(1

  • AVR ADC

    FUNCTION CODE Start Conversion ADCSRA |= 1

  • Registers Description

    Bit 5 ADLAR: ADC Left Adjust Result The ADLAR bit affects the presentation of the ADC conversion result in the ADC Data Register. Write one to ADLAR to left adjust the result. Otherwise, the result is right adjusted. Changing the ADLAR bit will affect the ADC Data Register immediately, regardless of any ongoing conversions.

  • When an ADC conversion is complete, the result is found in these two registers. When ADCL is read, the ADC Data Register is not updated until ADCH is read. Consequently, if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read ADCH. Otherwise, ADCL must be read first, then ADCH. The ADLAR bit in ADMUX, and the MUXn bits in ADMUX affect the way the result is read from the registers. If ADLAR is set, the result is left adjusted. If ADLAR is cleared (default), the result is right adjusted. Bits 3:0 MUX3:0: Analog Channel Selection Bits The value of these bits selects which analog inputs are connected to the ADC. If these bits are changed during a conversion, the change will not go in effect until this conversion is complete (ADIF in ADCSRA is set).

  • ADC Conversion Result After the conversion is complete (ADIF is high), the conversion result can be found in the ADC Result Registers (ADCL, ADCH). For single ended conversion the result is :

    1024*ref

    in

    VV

    ADC

    LM35 Calculations : We choose 2.56v as a Reference voltage , that mean the maximum value that can be read from the sensor is 2.56v. So: 1 C = 10 mV ( Analog ) = 4 ADC >> Linear Relation

    Temp = 4

    ADC