EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / ADC

ADC

|

Microcontroller › AVR › ADC

  • This topic has 1 reply, 2 voices, and was last updated 12 years, 9 months ago by Jason Jordon.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • April 12, 2013 at 5:35 pm #2262
    neena
    Participant

    Hello,

     

    I am using atmega128 board. I wanted a  C code for adc anolog values range from 0-5V. And also how to check the output on avr studio 4.

     

    Please help.

    April 19, 2013 at 4:50 am #9519
    Jason Jordon
    Participant

    I found this code:

     

    #include <avr/io.h> // include I/O definitions (port names, pin names, etc) 
    #include “global.h” // include our global settings 
    // 
    //


    Defines


     
    #define BV(bit) (1<<(bit)) // Byte Value => converts bit into a byte value. One at bit location. 
    #define cbi(reg, bit) reg &= ~(BV(bit)) // Clears the corresponding bit in register reg 
    #define sbi(reg, bit) reg |= (BV(bit)) // Sets the corresponding bit in register reg 
    // 
    //


    Function Prototypes


     
    void adc_init(void); // Will set up the registers for A/D conversion 
    //


    Begin Code


     
    int main(void) 
    { 
     unsigned short adc_result; // Just a variable to hold the result 
     adc_init(); // Call the init function 
     DDRF = 0x00; // configure a2d port (PORTF) as input so we can receive analog signals 
     PORTF = 0x00; // make sure pull-up resistors are turned off (else we’ll just read 0xCFF) 
     while(1) 
     { 
     sbi(ADCSRA,ADSC); // start a conversion by writing a one to the ADSC bit (bit 6) 
     while(ADCSRA & 0b01000000); // wait for conversion to complete (bit 6 will change to 0) 
     adc_result = ((ADCL) | ((ADCH)<<8)); // 10-bit conversion for channel 0 (PF0) 
     } 
     return 0; 
    } // end main() 
    // 
    void adc_init(void) 
    { 
     sbi(ADCSRA,ADEN); // enables ADC by setting bit 7 (ADEN) in the ADCSRA 
     cbi(ADCSRA,ADFR); // single sample conversion by clearing bit 5 (ADFR) in the ADCSRA 
     ADCSRA = ((ADCSRA & 0b11111000) | 0b00000110); // selects div by 64 clock prescaler 
     ADMUX = ((ADMUX & 0b00111111) | 0b01000000); // selects AVCC as Vref 
     cbi(ADMUX,ADLAR); // selects right adjust of ADC result 
     ADMUX &= 0b11100000; // selects single-ended conversion on PF0 
    } 
  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Buffer design? February 9, 2026
  • ANOTHER OLD PROJECT REDO February 9, 2026
  • BPF February 9, 2026
  • To couple or decouple February 9, 2026
  • Op amp shock February 9, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise