728x90

728x90

Friday, January 30, 2026

ATMega644P Analog to Digital Converter (ADC) Example

Overview

The Analog to Digital Converter (ADC) convert analog voltage value to a digital representation that the micro-processor could understand. Most of modern micro-controller has at least one built-in ADC module that can be use by software configuration. The most common ADC resolution is between 8-bit and 12-bit with an acceptable conversion rate.

ATMega644P Analog to Digital Converter (ADC) Example

 

The resolution give more precise digital data for the micro-controller. However its reference voltage and lower clock also effect the precision of the analog input value. Some 32-bit micro-controller especially the STM32 micro-controller series has an ADC module with the resolution of 12-bit with the conversion rate up to 2MSPS. Some 16-bit dsPIC micro-controller series has an ADC module with the resolution of 10-bit and 1~2MSPS conversion rate.

Commonly we use the ADC to read temperature value, flex sensor and strain gauge etc.

ATMega644P ADC Programming Example

The ADC of the ATMega644P has a 10-bit analog to digital converter with the maximum conversion rate up to 15kSPS. It has up to 8 analog input channel (ADC7:ADC0) via PortA. The conversion triggers by user software (set ADC to start) or by any interrupt sources.

ATMega644P Analog to Digital Converter (ADC) Example
Analog-to-digital Converter Block Schematic

 

The software configuration of the ADC module of AVR micro-controller is simpler than other micro-controllers. It has a few registers to configure. 

I don't write its detail here since the ADC process of the ATMega644P is very similar to the ATMega32

ADC and LED Example 1

In this introductory example the ATMega644P read the analog input from ADC0(PA0). The 10-bit ADC result will converts to 8-bit by shifting right that will display on PORTB.

C Source Code:

  1. /*
  2. * 9-ADC_LED.c
  3. *
  4. * Created: 1/30/2026 3:28:06 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>

  8. int main(void)
  9. {
  10. DDRB=0xFF;
  11. //PA0 or ADC0 as an analog input
  12. DDRA=0;
  13. //Turn on the ADC module
  14. ADCSRA=(1<<ADEN);
  15. uint16_t temp;
  16. while (1)
  17. {
  18. //Start the conversion
  19. ADCSRA|=(1<<ADSC);
  20. //Wait for the completion
  21. while((ADCSRA&(1<<ADSC))==1);
  22. //Read the result
  23. temp=ADCL+(ADCH<<8);
  24. PORTB=temp>>2;
  25. }
  26. }
  27.  

Proteus Simulation:

ATMega644P Analog to Digital Converter (ADC) Example
Proteus Simulation #1

AVR Prototype Board Test:

ATMega644P Analog to Digital Converter (ADC) Example

ATMega644P Analog to Digital Converter (ADC) Example

 

ADC and LED Example 2

I use this prototype board's LED and POT to make a LED shifting using the ADC voltage level. It's very similar to a ring counter. However I need to convert between ADC value counter value.

Source Code:

  1. /*
  2. * 9-ADC_BarGraph.c
  3. *
  4. * Created: 1/30/2026 4:21:13 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>

  8. int main(void)
  9. {
  10. DDRB=0xFF;
  11. //PA0 or ADC0 as an analog input
  12. DDRA=0;
  13. //Turn on the ADC module
  14. ADCSRA=(1<<ADEN);
  15. long temp,div;
  16. while (1)
  17. {
  18. //Start the conversion
  19. ADCSRA|=(1<<ADSC);
  20. //Wait for the completion
  21. while((ADCSRA&(1<<ADSC))==1);
  22. //Read the result
  23. temp=ADCL+(ADCH<<8);
  24. div=(temp*8)/1024;
  25. PORTB=(1<<div);
  26. }
  27. }


Proteus Schematic and Simulation:

ATMega644P Analog to Digital Converter (ADC) Example
Proteus Schematic and Simulation

AVR Prototype Board Test: 

ATMega644P Analog to Digital Converter (ADC) Example

ATMega644P Analog to Digital Converter (ADC) Example

 

ADC and LED Example 2

There are two on-board ADC inputs, POT and LM35 temperature sensor. The LCD will display the analog input from POT(ADC0) and LM35(ADC1). The LM35 temperature (in degree Celsius) is equivalent to 10mV that's 2 in digital value (+5.0VDC voltage reference).

ATMega32 Interfaces To LM35 And 16x2 Character LCD
LM35DZ in TO-92 Package

 

Source Code:

  1. /*
  2. * 9-ADC_LCD.c
  3. *
  4. * Created: 1/30/2026 4:47:25 PM
  5. * Author : Admin
  6. */

  7. #include <avr/io.h>
  8. #include <stdio.h>
  9. #include <util/delay.h>
  10. #define F_CPU 16000000UL

  11. void adc_init(void){
  12. /*Set PA0 and PA1 to input*/
  13. DDRA&=~(1<<0);
  14. DDRA&=~(1<<1);
  15. ADCSRA=(1<<ADEN);
  16. /*Select AD0*/
  17. ADMUX=0x03;
  18. }

  19. unsigned int read_adc(char channel){
  20. //Select Channel
  21. ADMUX=channel;
  22. //Start Convsersion
  23. ADCSRA|=(1<<ADSC);
  24. //Wait for Completion
  25. while(ADCSRA&(1<<ADSC));
  26. //Get the 10-bit values
  27. return (ADCL+(ADCH<<8));
  28. }
  29. int main(void)
  30. {
  31. /* Replace with your application code */
  32. lcd_init();
  33. lcd_text("ATMega644P ADC");
  34. lcd_xy(1,2);
  35. lcd_text("POT AND LM35");
  36. adc_init();
  37. _delay_ms(1000);
  38. lcd_clear();
  39. unsigned int temp;
  40. float voltage;
  41. char msg[16];
  42. lcd_command(0x0C);
  43. while (1)
  44. {
  45. //Read POT
  46. temp=read_adc(0);
  47. //Convert To Voltage
  48. voltage=temp*5.0/1023;
  49. sprintf(msg,"ADC0: %4d %.2fV",temp,voltage);
  50. lcd_xy(1,1); lcd_text(msg);
  51. //Read LM35
  52. temp=read_adc(1);
  53. voltage=temp*5.0/1023;
  54. voltage*=100;
  55. sprintf(msg,"Temp: %0.1f%cC",voltage,223);
  56. lcd_xy(1,2); lcd_text(msg);
  57. _delay_ms(1000);
  58. }
  59. }

Proteus Schematic:

ATMega644P Analog to Digital Converter (ADC) Example
Proteus Schematic

AVR Prototype Board: 

ATMega644P Analog to Digital Converter (ADC) Example
Program Tested on my AVR Prototype Board

Unfortunately I bough some fake LM35DZ due to their local availability and low cost, around 0.50USD. The original LM35DZ temperature chip costs around 1USD. However some online store also sell this chip in bulk price at very low cost.

I have been using PCBWay for many years now. PCBWay fabricate PCBs at low cost, fast processing time for only 24 hours, and fast delivery time using any carrier options. This double side 10cmx10cm can be fabricate at only 5USD for 5 to 10pcs by PCBWay. It's a standard PCB with silk screen and solder mask.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
10 PCBs for only 5USD
 

For different size of PCB we can instantly quote on PCBWay website using a zip PCB Gerber file without account.

A DIY dsPIC30F2010 and dsPIC30F1010 Prototype Board with Programmer
PCBWay Instant Quote

 


 

 

 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90