[STC MCU learning] Lesson 16: AD and DA conversion

[Ms. Zhu Course Summary, Invasion and Deletion] The

first part, chapter list

1.16.1. AD conversion and related background knowledge

1.16.2. Schematic and data sheet

1.16.3. Analyze timing with routines

1.16.4. Code Practice

1.16.5. The serial port directly displays the voltage value

1.16.6.DA conversion


The second part, chapter introduction

1.16.1. AD conversion and related background knowledge 1
    This section introduces the whole course, and mainly talks about the two concepts of analog quantity and digital quantity and the related concepts of AD conversion,
    mainly the range, accuracy, resolution, bit Count, conversion speed, etc., and illustrate the connection of these courses.
1.16.2. Schematic diagram and data manual In
    this section, look at the schematic diagram, determine the wiring, and check the key points in the data manual.
1.16.3. Analyze the timing sequence combined with routines
    This section focuses on analyzing the timing diagrams in the data manual, and compares learning by comparing with the official routines.
1.16.4. Code Practice
    This section refers to official routines to build projects, write timing codes, collect potentiometer voltage values ​​through ADC conversion circuit, and display the collected and converted AD values ​​through the serial port.
1.16.5. The serial port directly displays the voltage value.
    This section adjusts the serial port output to make it directly display the voltage value. This method can make debugging and output more intuitive
1.16.6.
    DA conversion This section explains the concept of DA conversion, and Demonstration principle and example code of DA conversion in the schematic diagram of the development board.

Part Three, Classroom Record

1.16.1. AD conversion and related background knowledge

1.16.1.1 Basic concepts
(1) What is AD conversion? A (A, analog, analog, D, digital, digital <binary>)

  • Analog quantity: The real world is analog, continuously distributed, and cannot be divided into limited parts; for example, sound, voltage, temperature, current, etc.
  • Digital quantity: the physical quantity that a computer can handle. The computer world is digital, discretely distributed, and can be divided into limited parts;
  • AD conversion is the conversion from analog to digital. For example: voltage is a continuous quantity in reality, and a digital quantity in a computer, but the accuracy is different!
  • The following picture introduces AD and DA conversion
  • The circuit that implements AD conversion is called an analog -digital converter , which is also an ADC ( Analog-digital converter )

(3) Under what circumstances do you need AD conversion?
That is when our research data is analog, for example: gravity, temperature, sound, etc.

1.16.1.2, the main concepts in AD conversion

(1) Digital digits: how many binary digits are used to represent the digital value converted from analog quantity. The more digits, the higher the precision, and the smaller the digits, the lower the precision.
(2) Range: analog input range. The analog range is a parameter of the AD chip itself. The voltage signal to AD cannot exceed this voltage range during actual work.
(3) Resolution. The more digits, the smaller the voltage value represented by each grid, and the more accurate the analog voltage value calculated in the future, and the higher the resolution.

  • Resolution = range/(2^digits)

(3) Accuracy, a simple understanding is how accurate the conversion is. This is related to the number of digits. The higher the accuracy, the closer the digital quantity after conversion is to the analog quantity.

(5) Conversion rate, AD conversion time
Take a chestnut: the
input voltage range is 0-5V, assuming that the number of AD conversion output digits is 10 (divide the entire range into 1024 grids), the accuracy is 0.1V , and the range is 0 -5V , the resolution is: (5-0)/1024= 0.00488V.
For example, the digital quantity obtained after one AD conversion is 1010101010(682), then the corresponding voltage value is: 0.00488*682=3.32816V, which is after considering the accuracy 3.3V

1.16.1.3, the principle of AD conversion

  • In the A/D converter, because the input analog signal is continuous in time, and the output digital signal code is discrete, the A/D converter must perform the conversion at a series of selected moments ( Some specified points on the time axis) sample the input analog signal , and then convert these sampled values ​​into digital quantities.
  • Therefore, A/D conversion has to go through the three steps of "sample and hold-quantize-encode"!

(1) Sampling theorem The
sampling pulse frequency is fs, and the frequency of the input analog signal x(t) is fmax, which must be satisfied

to ensure that the output digital signal y(t) is not distorted!

Usually fs=(2.5-3)fmax, because every time the sampling voltage is converted into the corresponding digital quantity, it takes a certain time, so after each sampling, the sampling voltage must be maintained for a period of time.

(2) Quantization
The size of any digital quantity is expressed as an integral multiple of a certain minimum quantity unit. Therefore, when using a digital quantity to represent an analog quantity, it must also be converted into an integral multiple of this minimum quantity unit. This conversion process is called quantification.

  • The smallest quantity unit is called the quantification unit , and is represented by △
  •  Since the analog voltage is continuous, it may not necessarily be divisible by △, which will inevitably introduce errors. We call this error quantization error . When dividing the analog signal into different quantization levels, different quantization errors can be obtained with different division methods.


One quantization unit is 1/8, and the quantization error is △, and the other
quantization unit is 2/15, and the quantization error is △/2.

(3) Encode
n binary numbers, which can represent 2^n quantization levels. In the above two quantization methods, the quantization level is 8, so both are encoded with three-bit binary numbers!

1.16.1.4, the way AD conversion exists in the system
(1) CPU external expansion dedicated AD chip
(2) CPU internal integrated AD module (internal peripherals)

  • Some single-chip microcomputers directly have analog pins, which carry out AD conversion inside!

STC51 MCU is an externally extended dedicated AD chip XPT2046 

1.16.2. Schematic and data sheet

1.16.2.1 Schematic diagram and schematic diagram of wiring
ADC module:

See the data sheet for the detailed introduction of the chip! xpt2046 Chinese data manual

(1) ADC module and MCU wiring
In order to facilitate wiring, the wiring is as follows:

  • CLK connected to P1.3 : clock signal input
  • CS connected to P1.2   : low level active
  • DI connected to P1.1   : used to input control commands
  • DO connect to P1.0  : output the converted digital signal

This is equivalent to changing the three-wire IO into DI and DO, and CE into CS chip selection

(2) Three analog voltage change principles

  • AINx is an analog input interface. You can select the three signal input modes of VBAT, Temp and AUX by choosing which input, and enter the ADC after the on-chip control register selection.
    According to the above schematic diagram, it is not difficult to see:
  • AIN0(X+) controls voltage change by sliding rheostat
  • AIN1(Y+) depends on thermistor NTC
  • AIN2 (VBAT) depends on photoresistor

1.16.2.2 Data sheet

MCU sends control byte to ADC module through DI to start conversion, addressing, setting ADC resolution, configuration and power down control of XPT2046, after conversion, output to MCU through DO port

Control byte data format:

  • bit7: start bit S-must be 1
  • bit6-4: Address-(A2, A1 and A0) select the current channel of the multiplexer (AIN0, AIN1, AIN2, AIN3)
Pin

A2 A1 A0

Correspondence mode
AIN0 001/011 X+ Sliding rheostat control voltage
AIN1 101 Y+ Thermistor NTC
AIN2 010 VBAT depends on photoresistor
AIN3 110 AUXIN 

                  

  • bit3: Mode selection bit-set the number of sampling bits. 0 means 12bit, 1 means 8bit, generally 12bit
  • bit2: Control reference source mode-1 means single-ended mode, 0 means differential mode. Single-ended mode
  • bit1-0: power down mode enable, 00 means enable. When PD0=0, A2A1A0 = 011, the sliding rheostat control voltage can no longer be selected
Initial value of control bit command
7 6 5 4 3 2 1 0
1 x x x 0 1 0 0


So it is not difficult for us to conclude that we can send the following control commands through DI:

  • Read AIN0: 0b10010100 = 0x94    
  • Read AIN1: 0b11010100 = 0xD4    
  • Read AIN2: 0b10100100 = 0xA4
  • Read AIN3: 0b11100100 = 0xE4

1.16.3. Analyze timing with routines

1.16.3.1 Timing diagram



(1) This timing is the same as SPI communication-DS1302: Link
(2) Read and write (same as DS1302)

  • MCU writes XPT2406 on the rising edge, puts the data first, and then gives the rising edge
  • MCU reads out from XPT2406 on the falling edge, and the data can be read after the falling edge

(3) Both reading and writing are high order first
(4) Pay attention to the BUSY time period of the junction point of writing and reading

  • Used for sample and hold-quantization-coding
  • Just give a delay!

1.16.3.2, official routine analysis
\51 routine\22, AD and DA\digital tube display AD value\potentiometer AD value

(1) write command SPI_Write
(2) read 12-bit digital value SPI_Read
(3) write + read Form a complete AD conversion cycle: Read_AD_Data, read is digital

1.16.4. Code Practice

Use three signal input modes respectively, print through the serial port, and observe the obtained AD value

1.16.4.1, write code
according to the routine 1.16.4.2, add serial port output debugging
1.16.4.3, practice
(1) potentiometer as analog input source
(2) thermistor as analog input source
(3) photoresistor as analog input source Input source

1.16.5. The serial port directly displays the voltage value

1.16.5.1、Key points
(1) Directly display the voltage value instead of sampling the AD value

  • Voltage value = (AD_Val/4096)*5000mV = 1.22*val mV
  • The above voltage value is essentially AD value, it needs to be converted to the corresponding ASCII code before sending
    void uart_send_vol(u16 val)
    {
    	float index = 1.22;
    	float voltage = index * (float)val;
    	u16 vol_display = (u16)voltage;
    	
    	u8 str[4];
    	u8 i = 0;
    	
    	//取出个十百千位
    	for(i = 0;i<4;i++)
    	{
    		str[i] = vol_display%10;
    		vol_display = vol_display/10;
    	}
    	
    	for(i = 0;i<4;i++)
    	{
    		uart_send_byte(str[3-i]+48);
    		if(i==0)
    		uart_send_byte('.');
    		_nop_();
    	}
    	uart_send_byte('V');
    }
    

     

(2) Display in text mode, not hexadecimal mode

Complete program: AD serial printing 

1.16.6.DA conversion

1.16.6.1 Related concepts of DA conversion
(1) What is DA conversion
Digital quantity -> analog quantity
(2) The meaning and use of DA conversion (function signal generator)
(3) Principle of DA conversion

The digital quantity is a combination of binary code digits, and each has a certain weight. In D/A conversion, how to express these weights in a suitable way is the key to conversion. In order to convert digital quantities into analog quantities, Each bit of code must be converted into the corresponding analog quantity according to its weight, and then these analog quantities are added. The schematic diagram of D/A conversion is as follows:
Insert picture description here

 

That is, input the corresponding binary number to get an analog output, and as a three-bit conversion, the corresponding relationship between analog and digital can be represented by the following figure. In the right panel D respectively corresponding V0
Insert picture description here

1.16.6.2, schematics, and Case

(1) of the operational amplifier (amplification + isolation)
(2) the PWM digital signal
(3) is not in fact take LM358, directly connected with the IO port The LED experiment phenomenon is also the same
(4) The real DA is generally a dedicated chip or a built-in CPU module, which outputs a smooth analog value to the digital value.

Program download link for this lesson: AD conversion

This lesson is over!

Guess you like

Origin blog.csdn.net/qq_27148893/article/details/110450835