51 microcontroller - ADC analog-to-digital conversion experiment

Table of contents

1. Function description

2. Introduction to ADC

2.1 Successive approximation ADC conversion principle

2.2 Main technical indicators of ADC

3. Introduction to XPT2046 chip

3.1 XPT2046 external pins

3.2 Command words of XPT2046

3.3 XPT2046 timing diagram

4. Test file test.c

5. Experimental phenomena


1. Function description

        The STC89C51 microcontroller we use does not have an ADC interface internally, so an external ADC conversion chip is needed to convert the analog signal into a digital signal for processing by the microcontroller. Our development board integrates an ADC analog-to-digital conversion circuit, and the ADC chip selected is a 12-bit AD chip-XPT2046. This experiment uses the ADC conversion circuit to collect the voltage value of the potentiometer AD, the voltage value of the thermistor NTC circuit, and the voltage value of the photoresistor GR circuit, and displays the converted digital quantity on LCD1602.

2. Introduction to ADC

        We know that all the internal operations of the 51 microcontroller system use digital quantities, that is, 0 and 1. Therefore, for the microcontroller system, analog quantities cannot be directly manipulated and analog quantities must be converted into digital quantities. The so-called digital quantity is a quantity that represents the size of a certain signal using a binary code composed of a series of 0s and 1s. When using a digital quantity to represent the same analog quantity, the number of digits can be more or less. The more digits, the higher the precision, and the fewer digits, the lower the precision.

2.1 Successive approximation ADC conversion principle

        The AD converter using the successive approximation method consists of a comparator, DA converter, buffer register and control logic circuit, as shown in the following figure:

        The conversion process of the successive approximation method is: clear each bit of the successive approximation register to zero during initialization. When the conversion starts, first set the highest position 1 of the successive approximation register and send it to the DA converter. The analog value generated after DA conversion is sent to the comparator. It is called U0 and is compared with the analog quantity Ux sent to the comparator to be converted. If U0<Ux, the bit is retained as 1, otherwise it is cleared. Then set the second highest bit of the successive approximation register to 1, send the new digital quantity in the register to the DA converter, and compare the output U0 with Ux. If U0<Ux, the bit 1 is retained, otherwise it is cleared. Repeat this process until the lowest bit of the register is approached. After the conversion is completed, the digital quantity in the successive approximation register is sent to the buffer register to obtain the output of the digital quantity. The operation process of successive approximation is carried out under the control of a control circuit.

2.2 Main technical indicators of ADC

  1. Resolution: refers to the number of discrete digital signal values ​​it can output for analog signals within the allowable range. The resolution of a 12-bit ADC is 12 bits. The minimum input voltage change that a 10V full-scale 12-bit ADC can resolve is: 10Vx (1÷12) = 2.4mv.
  2. Conversion rate: is the speed at which data conversion can be repeated, that is, the number of conversions per second. The time required to complete an A/D conversion (including settling time) is the reciprocal of the conversion rate.

3. Introduction to XPT2046 chip

        XPT2046 is a typical successive approximation analog-to-digital converter (SAR ADC), which includes sampling/holding, analog-to-digital conversion, serial port data output and other functions. The communication method of XPT2046 is SPI.

3.1 XPT2046 external pins

  • CS: Chip selector.
  • DIN: serial data input terminal
  • DCLK: external clock input terminal
  • DOUT: serial data output terminal
  • X+, Y+, X-, Y-: position selection terminal
  • VBAT: battery monitoring input
  • AUX: ADC auxiliary input channel

CS, DIN, DCLK and DOUT are respectively connected to the corresponding pins of the microcontroller, and X+, Y+ and VBAT respectively input the analog signals to be converted.

3.2 Command words of XPT2046

  Select channel bits A2-A0, as shown in the figure below:

3.3 XPT2046 timing diagram

  1. The host sends the command word to XPT2046 through DIN, and XPT2046 reads the data when DCLK is high.
  2. XPT2046 sends the converted digital quantity to the host through DOUT, and the host reads the data when DCLK is high.

The procedure is as follows:

#include <REGX52.H>
sbit XPT2064_DIN=P3^4;
sbit XPT2064_CS=P3^5;
sbit XPT2064_DCLK=P3^6;
sbit XPT2064_DOUT=P3^7;
/*
     *函数名:	 XPT2064_ReadAD(unsigned char command)
     *函数功能: 主机发送命令字给XPT2046,XPT2046将转换的数字量发送给主机
     *输入:	 command:命令字
     *输出:	 D_num:转换好的数字量
*/
unsigned int XPT2064_ReadAD(unsigned char command)
{
	unsigned int D_num=0;
	unsigned char i=0;
	XPT2064_CS=0;
	for(i=0;i<8;i++)
	{	
		XPT2064_DIN=command&(0x80>>i);
		XPT2064_DCLK=1;
		XPT2064_DCLK=0;
	}
	for(i=0;i<16;i++)
	{
		XPT2064_DCLK=1;
		if(XPT2064_DOUT==1)D_num|=0x8000>>i;
		XPT2064_DCLK=0;
	}
	return D_num/128;
}

4. Test file test.c

#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "XPT2046.h"
#define XPT2046_XP   0x9c
#define XPT2046_YP   0xdc
#define XPT2046_VBAT 0xac
#define XPT2046_AUX  0xec
int main()
{
	unsigned int AD,NTC,GR=0;
	LCD_Init();
	while(1)
	{
		AD=XPT2064_ReadAD(XPT2046_XP);
		NTC=XPT2064_ReadAD(XPT2046_YP);
		GR=XPT2064_ReadAD(XPT2046_VBAT);
		LCD_ShowString(1,1,"AD");
		LCD_ShowString(1,5,"NTC");
		LCD_ShowString(1,9,"GR");
		LCD_ShowNum(2,1,AD,3);
		LCD_ShowNum(2,5,NTC,3);
		LCD_ShowNum(2,9,GR,3);
		Delay(10);
	}
}

5. Experimental phenomena

        When the potentiometer is turned, heat is transferred to the thermistor NTC or the ambient lighting is changed, that is, the voltage analog quantity is changed, the corresponding digital quantity is obtained after conversion by ADC, and they are displayed on the LCD1602.

Guess you like

Origin blog.csdn.net/ssssshhbh/article/details/129315742