The STC89LE516AD/X2 in the STC series of microcontrollers provides 8-channel 8-bit high-speed A/D converters, located on the P1 port, thus eliminating the trouble of an off-chip ADC. These 8-channel ADCs are voltage input type and can be used for key scanning, battery voltage detection, spectrum detection, etc. The ADC conversion process requires 17 machine cycles. By properly configuring the relevant function registers, its operation can be controlled. The following is an introduction to the relevant function registers.
A/D conversion register summary table:
(1) P1_ADC_EN: Special function register
P1.x is used as an A/D conversion input channel to enable the special function register. When the corresponding bit is "1", the corresponding P1.x port is used as an A/D conversion and the internal pull-up resistor is automatically disconnected.
ADC_START is the analog/digital conversion (ADC) start control bit. When it is set to 1, the conversion starts.
ADC_FLAG is the analog/digital conversion end flag. When the A/D conversion is completed, ADC_FLAG=1.
(3) ADC_DATA: A/D conversion result special function register
The calculation formula for analog/digital conversion result is Result = 256 × Vin/Vcc
Vin is the input voltage of the analog input channel, Vcc is the actual working voltage of the microcontroller, and the microcontroller working voltage is used as the analog reference voltage.
Built-in ADC usage routine:
STC_ADC.h:
sfr P1_ADC_EN=0x97;
sfr ADC_CONTR=0xc5;
sfr ADC_DATA=0xc6;
int GetAD(unsigned char channel) //channel is the channel number to be retrieved (0~7)
{
unsigned char AD_finished=0;
ADC_DATA=0;
ADC_CONTR=channel;
ADC_CONTR|=0x08;
while(!AD_finished)
AD_finished=(ADC_CONTR&0x10);
ADC_CONTR&=0xF7;
return (ADC_DATA);
}
void AD_Init(unsigned char ADin) // ADC initialization, ADin is an eight-bit binary number, corresponding to AD7~0 respectively, and setting 1 means opening this path
{
P1=P1|ADin; //To set the P1.x port for A/D conversion, first set it to high
P1_ADC_EN=ADin;
}
_main.c:
#include
#include /* ------------------------------------------------- This program is used to illustrate the use of STC microcontroller ADC ------------------------------------------------- */ void delay(unsigned time) { while(time--); } void main() { AD_Init(0x63); //0110,0011, to set the P1.x port for A/D conversion, first set it to high //0110,0011,P1's P1.0,P1.1,P1.5,P1.6 are set as A/D conversion input pins //Disconnect the internal pull-up resistors of P1.0, P1.1, P1.5, and P1.6 while(1) { P2=GetAD(0); delay(10000); P2=GetAD(1); delay(10000); P2=GetAD(5); delay(10000); P2=GetAD(6); delay(10000); } }
Previous article:STC12C5A60S2 MCU realizes ISP automatic download
Next article:STC12C5A60S2 dual serial port communication
Recommended ReadingLatest update time:2024-11-15 08:22
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- When using the ADC of Jinxin Electronics AVP32F335, the sampling value deviation
- mPower1203 programmable power supply also supports third-party applications to obtain the current of the device under test in real time
- The role of reference voltage in AD conversion
- DIY a USB hub shell
- STM8S001J3 uses HalfDuplex mode and uses IO mapping and cannot receive data.
- Analysis of the Causes of Operational Amplifier Oscillation and Self-excitation
- (Bonus 14) GD32L233 Review - Driving Segment Code LCD
- China's chip self-sufficiency rate was 30% last year, and the goal is to reach 70% by 2025 (attached with representative companies in 70 sub-sectors of domestic chips)
- Getting Started with the TI AWR1642BOOST-ODS EVM Development Board
- [STM32WB55 Review] A brief analysis of the execution process of the BLE demo program