1. Conversion principle of successive approximation ADC
The successive approximation AD converter is similar to the counting A/D converter, except that the digital quantity is generated by the "successive approximation register SAR". SAR uses the "bisection search method" to generate digital quantities. Taking the 8-bit digital quantity as an example, SAR first generates half of the 8-bit digital quantity, that is, 10000000B, to test the size of the analog quantity Vi. If Vn>Vi, clear the highest bit. If Vn 2. Timing diagram and pins 3. Code 3.1, main /* Experimental phenomenon: After downloading the program, the first 4 digits of the digital tube display the AD value detected by the photosensitive sensor 1. MCU-->AD/DAC module pin interpretation P34-->DI DIN serial data input terminal, when CS is low, data is latched in on the rising edge of DCLK P35-->CS CS chip select signal, controls the conversion timing and enables the serial input and output registers. When the high level is high, the ADC is powered off. P36-->CL DCLK clock, external clock signal input P37-->DO DOUT serial data output port. Data is shifted out on the falling edge of DCLK and is in high impedance state when cs is high. BUSY busy signal, when cs is high level, it is in high impedance state LOVDD Digital power input terminal AUX ADC auxiliary input channel 2. Single chip microcomputer --> dynamic digital tube module J22-->J6 P22-->J9(A) P23-->J9(B) P24-->J9(C) */ #include "reg52.h" // This file defines some special function registers of the microcontroller #include "XPT2046.h" typedef unsigned int u16; typedef unsigned char u8; // data type definition sbit LSA = P2^2; //These three ports jointly control the bit selection of the digital tube sbit LSB = P2^3; sbit LSC = P2^4; u8 disp[4]; // The character array is used to store the data of the first four digits of the digital tube u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // 0~9 digital display /*Delay function*/ void delay(u16 i) { while(i--); } /*Data processing module*/ void datapros() { u16 temp; // integer variable /*The static local variable modified by static is only executed once, and the life cycle of the local variable is extended until it is released after the program ends. When a global variable is modified with static, the global variable can only be accessed in this file and cannot be accessed in other files, even if it is declared as extern. Static modifies a function, and this function can only be called in this file, and cannot be called by other files. Local variables modified by Static are stored in the static variable area of the global data area. */ static u8 i; if(i==50) { i=0; /* AIN0 potentiometer: If you want to detect the conversion potentiometer analog signal, the control word command register value is 0X94 or 0XB4. If you want to detect and convert thermistor analog signal, the control word command register value is 0XD4. AIN2 photoresistor: If you want to detect and convert the photoresistor analog signal, the control word command register value is 0XA4. If you want to detect and convert the analog signal on the AIN3 channel, the control word command register value is 0XE4. */ temp = Read_AD_Data(0xA4); // AIN2 photoresistor } i++; disp[0] = smgduan[temp/1000]; // Thousands disp[1] = smgduan[temp%1000/100]; // hundreds place disp[2] = smgduan[temp%1000%100/10]; //ten digit disp[3] = smgduan[temp%1000%100%10]; // units } /*Digital tube display module*/ void DigDisplay() { u8 i; for(i=0;i<4;i++) { switch(i) // bit selection { case(0): LSA=1;LSB=1;LSC=1; break;//display bit 0 case(1): LSA=0;LSB=1;LSC=1; break;//display the first bit case(2): LSA=1;LSB=0;LSC=1; break;//display the second digit case(3): LSA=0;LSB=0;LSC=1; break;//display the third bit } P0=disp[i]; // Send data delay(100); // Delay for a while P0 = 0x00; // erase } } /* Main function */ void main() { while(1) { datapros(); // data processing function DigDisplay(); // Digital tube display function } } 3.3, XPT2046 chip control pin definition, function, variable declaration file #ifndef _XPT2046_H_ #define _XPT2046_H_ // Include header file #include #include // Redefine keywords #ifndef uchar #define uchar unsigned char #endif #ifndef uint #define uint unsigned int #endif #ifndef ulong #define ulong unsigned long #endif //IO definition sbit DOUT = P3^7; sbit CLK = P3^6; sbit CS = P3^5; sbit DIN = P3^4; // Function definition uint Read_AD_Data(uchar cmd); uint SPI_Read(void); void SPI_Write(uchar dat); 3.3, XPT2046.c chip control #include "XPT2046.h" // Initialize touch function void SPI_Start(void) { CLK=0; // Clock, external clock signal input CS=1; // Chip select signal, controls conversion timing and enables serial input and output registers. ADC powers down when high DIN = 1; // Serial data input terminal, when CS is low, data is latched in on the rising edge of DCLK CLK=1; CS=0; } /*Write data When the CLK external clock rises, data is input from the DIN port and latched */ void SPI_Write(uchar dat) { uchar i; // character type variable CLK=0; //External clock low level for(i=0;i<8;i++) { /* Successive approximation AD conversion principle The successive approximation AD converter is similar to the counting A/D converter, except that the digital quantity is generated by the "successive approximation register SAR". SAR uses the "bisection search method" to generate digital quantities. Taking an 8-bit digital quantity as an example, SAR first generates half of the 8-bit digital quantity. That is, 10000000B, test the size of the analog quantity Vi, if Vn>Vi, clear the highest bit, if Vn After bit6 is determined, SAR uses the bisection search method to determine bit5, that is, using half of the lower 6 bits yy100000B (y is the determined bit) to test the size of the analog quantity. Repeat this process until the lowest bit 0 is determined and the conversion is completed. */ DIN = dat >> 7; // Shift right by 7 bits and assign the highest bit of dat to DIN dat<<=1; // dat=dat<<1 Move the highest bit stored in dat out for the next loop CLK = 0; // Place data on the rising edge, such as transferring data to the data line CLK = 1; } } /*Read data function CLK Clock signal input port Data is shifted out at the falling edge (CLK=1 changes to CLK=0) */ uint SPI_Read(void) { uint i, dat = 0; // define integer variables CLK = 0; for(i=0;i<12;i++) { dat <<=1; // dat = dat << 1; Shift left by 1 bit CLK=1; // Falling edge data shift out CLK = 0; /* and&& and a && b a and b or|| or a || b a or b Not! !1=0 Bitwise AND (&) a & b a and b Bitwise OR (|) a | b a or b */ dat |= DOUT; // dat = dat | DOUT } return dat; } /*cmd: read X or Y*/ uint Read_AD_Data(uchar cmd) { uchar i; // unsigned character type variable uint AD_Value; // integer variable CLK =0; // External clock signal input CS = 0; //Chip select signal, controls conversion timing and enables serial input and output registers. ADC powers down when high (does not work when high, works when low) SPI_Write(cmd); for(i=6;i>0;i--); // Send a clock cycle and clear BUSY CLK = 1; // Falling edge shifts out data _nop_(); // Delay 1us _nop_(); // Delay 1us CLK = 0; _nop_(); // Delay 1us _nop_(); // Delay 1us AD_Value = SPI_Read(); CS = 1; return AD_Value; }
Previous article:AD analog-to-digital conversion thermistor
Next article:STC89C51 AD analog-to-digital conversion ---- self-study notes
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- What content do you most want to see about Bluetooth?
- [Erha Image Recognition Artificial Intelligence Vision Sensor] 4. Object Recognition and Line Patrol Function Test
- Analog/digital mixed circuit acceleration simulation technology
- Chip war: salary increase starts at 50%, engineers are more expensive than bosses
- The first GD32VF103 project
- I would like to ask about the circuit in the figure. C1 and C2 are the discharge circuits of the capacitors.
- Privileged Classmate 2020 Video Tutorial "Learning Verilog by Coding (FPGA Tools and Syntax)"
- Analog Circuit Troubleshooting
- Application of solid state relays in servo motors
- Circuit principle help