Let's sort out the ADC conversion function of AVR. The digital-to-analog converter in AVR is a 10-bit successive approximation ADC. It provides 8 single-ended input channels. The single-ended voltage is based on 0V. It also has 7 differential input channels. These seven differential channels share a common negative terminal (ADC). The input voltage range is 0~VCC. Its conversion time is 65~260uS. The phenomenon of the following program is that the serial port sends a command to start the ADC and sends the result to the PC through the serial port. Any character is turned on
#include #include < macros.h > #define uchar unsigned char uchar UcAdcData; //ADC conversion result uchar AdcConCom = 0; //ADC conversion completion flag uchar admuxx = 0x67; //External reference source data storage left alignment //AD conversion initialization void AdcInit(void) //AD conversion interrupt processing function #pragma interrupt_handler adc_isr : 15 { //Serial port initialization void Uart0Init(void) //Serial port interrupt processing function #pragma interrupt_handler UartRecvData:12 //Serial port sends data void UartTransmitData(void) // Initialize various information void InitDevices(void) MCUCR = 0x00; //Main function void main ( void ) { InitDevices ( ) ; //Serial port initialization }
{
ADCSR = 0x00; //ADC invalid
ADMUX = admuxx; //Select ADC channel 7
/*--REFS1---REFS0---ADLAR---0--------------Reference power supply selection------Alignment--------*/
/*--0000-- 0001-- 0010-- 0011-- 0100-- 0101-- 0110-- 0111-- 1110--- --1111---*/
/*--Channel 0--Channel 1--Channel 2--Channel 3--Channel 4--Channel 5--Channel 6-Channel 7--1.23V(VBG)-0V(GND)*/
ACSR = 0x80; //Analog comparator control and status register ACSR
//ACD is set to 1 to disable the analog comparator
/*--ACD--ACBG--ACO--ACI---ACIE---ACIC---ACIS1---ACIS0------------------------*/
/*-Analog comparator disable--Analog comparator energy gap reference source--Analog comparator output--Analog comparator interrupt flag*/
/*-Analog comparator interrupt enable--Analog comparator input capture enable--Analog comparator interrupt mode selection-------- */
ADCSR = 0x8B; //Analog comparator control and status register
/*-----------ADEN---ADSC---ADFR---ADIF---ADIE---ADPS2---ADPS1---ADPS0--------*/
//Enable analog-to-digital conversion ADC starts conversion ADC continuous conversion ADC interrupt enable 128 division
}
void adc_isr ( void )
UcAdcData = ADCH; //Read ADC conversion result
AdcConCom = 1; //Set ADC conversion completion flag
}
{
UCSRB = 0x00; //Close the serial port
UCSRA = 0x02; //Speed mode M16 Chinese P151
UCSRC = BIT(URSEL) | 0x06; //Write UCSRC register, set 8 bits
UBRR = 0x000c; //Set the serial port baud rate to 9600
UCSRB = 0x98; //Open the serial port
}
void UartRecvData(void) //Serial port interrupt processing function, that is, PC sends a start signal to AVR
{
uchar temp;
temp = UDR;
ADMUX |= 0x07;
ADCSR |= 0x40; //Start an ADC conversion
}
{
while(!(UCSRA&0x20)); //Judge whether the data register is empty
UDR = UcAdcData;
}
{
CLI ( ); //Disable all interrupts
Uart0Init ( ) ;
AdcInit ( ) ;
SEI ( ) ; // Enable all interrupts
}
while ( 1 )
{
if ( AdcConCom ) //AD completion flag starts processing data
{
AdcConCom = 0; //Clear ADC conversion completion flag
UartTransmitData ( ) ; //Send conversion results
}
}
Previous article:AVR series PWM test
Next article:Summer vacation study summary - AVR series (I)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Does anyone have a simulation circuit diagram of the LMD18200 chip?
- Digital Circuit Design with Verilog HDL.pdf
- 46% of open source maintainers are unpaid, and 26% earn more than $1,000 per year
- How to start learning analog electronics
- Why are all villains so stupid?
- Pipeline Leakage Monitor Based on Sound Waves
- [Mil MYS-8MMX] Mil MYS-8MMQ6-8E2D-180-C Unpacking Report 2 - Power on
- MSP430 MCU timer TA interrupt program
- Free review: Domestic FPGA Anlu SparkRoad development board
- Replaying MicroPython on STM32F7DISC - DCExpert