AVR analog-to-digital (AD) conversion example program

Publisher:bullfishLatest update time:2011-11-08 Keywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Development Language

This example was developed using WinAVR/GCC 20050214 version

2. Example Description

This program simply demonstrates how to use the ADC analog-to-digital converter of ATMEGA16. Common single-ended input, differential input and calibration, reference voltage calibration, query mode, interrupt mode, data format conversion. In order to simplify the program, various data are not output to the outside. It is recommended to use the JTAG ICE hardware emulator when learning.


3. Circuit diagram design:

To simplify the circuit design, the ATmega16 function board from this website was used.

In the example, the internal 2.56V voltage reference is selected as Vref, and the differential channel is magnified 10 times
. The single-ended voltage measurement range is 02.56V, resolution is 2.5mV.
The differential voltage measurement range is +/- 256mV, resolution is 0.5mV.
Current resolution = 50uA@10 ohm current sampling resistor
Current resolution = 500uA@1 ohm current sampling resistor
In the program, the measured reference voltage needs to be substituted into the constant Vref to obtain a more accurate result
The sample I have is measured to be 2.556V@Vcc=5.0V
2.550V@Vcc=3.3V
This circuit is for reference only and does not consider the requirements for anti-interference.

.

4. Code design and description:

/******************************************************** ADC (analog-to-digital conversion) usage example ******* ******* Planning, organization and testing: Armok ***
**** Code design: HJJourAVR ******* Compiler: WINAVR20050214 ******* www.OurAVR.com 2005.8.31 ****************************************************/#include #include #include #include /*The usage of the macro INTERRUPT is similar to that of SIGNAL, the difference is that when SIGNAL is executed, the global interrupt trigger bit is cleared and other interrupts are disabled; when INTERRUPT is executed, the global interrupt trigger bit is set and other interrupts can be nested. In addition, avr-libc provides two API functions for setting and clearing the global interrupt trigger bit, which are frequently used. They are: void sei(void) and void cli(void) defined by interrupt.h*/
//Pin definition
#define in_Single 0 //PA0(ADC0)
#define in_Diff_P 3 //PA3(ADC3)
#define in_Diff_N 2 //PA2(ADC2)

//Constant definition
//Single-ended channel, no amplification
#define AD_SE_ADC0 0x00 //ADC0
#define AD_SE_ADC1 0x01 //ADC1
#define AD_SE_ADC2 0x02 //ADC2
#define AD_SE_ADC3 0x03 //ADC3
#define AD_SE_ADC4 0x04 //ADC4
#define AD_SE_ADC5 0x05 //ADC5
#define AD_SE_ADC6 0x06 //ADC6
#define AD_SE_ADC7 0x07 //ADC7

//Differential channel ADC0 as negative terminal, 10/200 times magnification
#define AD_Diff0_0_10x 0x08 //ADC0+ ADC0-, 10 times magnification, for calibration
#define AD_Diff1_0_10x 0x09 //ADC1+ ADC0-, 10 times magnification
#define AD_Diff0_0_200x 0x0A //ADC0+ ADC0-, 200 times magnification, for calibration
#define AD_Diff1_0_200x 0x0B //ADC1+ ADC0-, 200 times magnification

//Differential channel ADC2 as negative terminal, 10/200 times magnification
#define AD_Diff2_2_10x 0x0C //ADC2+ ADC2-, 10 times magnification, for calibration
#define AD_Diff3_2_10x 0x0D //ADC3+ ADC2-, 10 times magnification
#define AD_Diff2_2_200x 0x0E //ADC2+ ADC2-, 200 times magnification, for calibration
#define AD_Diff3_2_200x 0x0F //ADC3+ ADC2-, 200 times magnification

//Differential channel ADC1 is used as the negative terminal and is not amplified
#define AD_Diff0_1_1x 0x10 //ADC0+ ADC1-
#define AD_Diff1_1_1x 0x11 //ADC1+ ADC1-, for calibration
#define AD_Diff2_1_1x 0x12 //ADC2+ ADC1-
#define AD_Diff3_1_1x 0x13 //ADC3+ ADC1-
#define AD_Diff4_1_1x 0x14 //ADC4+ ADC1-
#define AD_Diff5_1_1x 0x15 //ADC5+ ADC1-
#define AD_Diff6_1_1x 0x16 //ADC6+ ADC1-
#define AD_Diff7_1_1x 0x17 //ADC7+ ADC1-

//Differential channel ADC2 is used as the negative terminal and is not amplified
#define AD_Diff0_2_1x 0x18 //ADC0+ ADC2-
#define AD_Diff1_2_1x 0x19 //ADC1+ ADC2-
#define AD_Diff2_2_1x 0x1A //ADC2+ ADC2-, for calibration
#define AD_Diff3_2_1x 0x1B //ADC3+ ADC2-
#define AD_Diff4_2_1x 0x1C //ADC4+ ADC2-
#define AD_Diff5_2_1x 0x1D //ADC5+ ADC2-

//Single-ended channel, no amplification
#define AD_SE_VBG 0x1E //VBG internal bandgap 1.22V voltage reference, for calibration
#define AD_SE_GND 0x1F //Ground calibration

//Note:
//Differential channels, if using 1x or 10x gain, 8-bit resolution is available. If using 200x gain, 7-bit resolution is available.
//Differential input channel devices in PDIP package are not tested. Devices are only guaranteed to work properly in TQFP and MLF packages.

#define Vref 2556 //mV measured Vref pin voltage @5.0V power supply
//#define Vref 2550 //mV measured Vref pin voltage @3.3V power supply

//Global variables
unsigned int ADC_SingleEnded; //ADC value of single-ended input
int ADC_Diff; //ADC value of differential input
volatile unsigned int ADC_INT_SE; //ADC value of single-ended input used in interrupt mode, will be modified in interrupt service routine,
//must be qualified by volatile
volatile unsigned char ADC_OK; //ADC status, will be modified in interrupt service routine, must be qualified by volatile
unsigned int LED_Volt; //Converted voltage mV
int LED_Curr; //Conversion

Keywords:AVR Reference address:AVR analog-to-digital (AD) conversion example program

Previous article:AVR Studio Quick Start
Next article:AVR MCU JTAG Debugging Quick Start

Recommended ReadingLatest update time:2024-11-16 19:35

Sharing of PT2262 software decoding program based on AVR microcontroller
    Many PT2262 software decoding programs use timers to measure pulse width. They take up too many system resources. My own program does not use timers or interrupts, and has the least code. But it consumes CPU. The transmitter is a PT2262 3.3M resistor and the receiver is an M8 internal 1M RC oscillator. After
[Microcontroller]
Sharing of PT2262 software decoding program based on AVR microcontroller
Design of LED lighting control system based on AVR
  introduction   LED lighting has entered the home users. Compared with traditional lighting equipment (such as incandescent lamps and fluorescent lamps), it has the advantages of high purity of light source, diverse colors, high efficiency, and adjustable light intensity. In view of the problem that the brightness of
[Microcontroller]
Design of LED lighting control system based on AVR
AVR EEPROM read and write experiment
iccavr tutorial   ATMEGA8 has 1024 bytes of SRAM, 8096 bytes of FLASH and 512 bytes of EEPROM. The difference between them is that FLASH is used to store your program code. During the program running, the program itself cannot modify its content. The content will not be lost when the power is off. It is only modifie
[Microcontroller]
Design of museum anti-theft protection system based on AVR microcontroller
introduction The collections in the museum are often threatened by human theft, and changes in the surrounding environment can also cause damage to them. Therefore, a very reliable anti-theft system and a complete air-conditioning system are required. The purpose of this design is to realize the intelligent anti-theft
[Microcontroller]
Design of museum anti-theft protection system based on AVR microcontroller
AVR Timer Compare Interrupt
Recently I plan to make a Bluetooth receiver myself and connect it to the quadcopter, so I study how to output 4-channel PWM. The plan is to use the timer interrupt, calculate the duty cycle during the interrupt, and then switch the specified PIN pin between high and low levels. It took me a long time to understand th
[Microcontroller]
Design of a simple fire-fighting robot based on AVR microcontroller
Introduction In recent years, with the rapid development of basic industries such as petrochemicals, the use of flammable, explosive and highly toxic chemicals in the production process has increased dramatically. Due to equipment and management reasons, the number of accidents involving leakage, combustio
[Microcontroller]
Design of a simple fire-fighting robot based on AVR microcontroller
AVR I2C (TWI) Program
/************************************************************************            Purpose: Create an I2C operation library Target system: Based on AVR microcontroller                                                  Application software: ICCAVR                                                       Version: Version
[Microcontroller]
Several points to note when using the I/O port of AVR microcontroller
Introduction: Several precautions for using the I/O port of AVR microcontroller 1. Correctly set the DDRx direction register before performing read and write operations on the I/O port. 2. The initial state of the I/O port after reset is all input working mode, the internal pull-up resistor is invalid, and the ext
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号