- void
Read_init (unsigned char CHA){ unsigned char AD_FIN=0; //Store A/D conversion flag CHA &= 0x07; //Select one of the 8 interfaces of ADC (0000 0111 clears the high 5 bits) ADC_CONTR = 0x40; //ADC conversion speed (0XX0 0000 where XX controls the speed, please set according to the data sheet) _nop_(); ADC_CONTR |= CHA; //Select the current A/D channel _nop_(); ADC_CONTR |= 0x80; //Start A/D power supply DELAY_MS(1); //Let the input voltage reach stability (1ms is enough) - }
- unsigned
char Read (void) { unsigned char AD_FIN=0; //Store A/D conversion flag ADC_CONTR |= 0x08; //Start A/D conversion (0000 1000 makes ADCS = 1) _nop_(); _nop_(); _nop_(); _nop_(); while (AD_FIN ==0){ //Wait for A/D conversion to end AD_FIN = (ADC_CONTR & 0x10); //0001 0000 Test whether A/D conversion is finished } ADC_CONTR &= 0xE7; //1111 0111 clear ADC_FLAG bit, turn off A/D conversion, - return
(ADC_DATA); //Return A/D conversion result (8 bits) - }
Above - 8-bit ADC program module (applicable to STC12C2052AD series)
C Code
- #include
//MCU header file - #include
//51 basic operations (including _nop_ empty function) - void
DELAY_MS (unsigned int a){ unsigned int i; while( a-- != 0){ for(i = 0; i < 600; i++); } - }
- void
UART_init (void) { //EA = 1; //Enable general interrupts (if interrupts are not used, they can be //masked) //ES = 1; //Enable UART serial port interrupt TMOD = 0x20; //Timer T/C1 working mode 2 SCON = 0x50; //Serial port working mode 1, serial port reception is allowed (serial port reception is prohibited when SCON = 0x40 ) TH1 = 0xF3; //Set the high 8 bits of the timer initial value TL1 = 0xF3; //Set the initial value of the timer to the lower 8 bits PCON = 0x80; //Baud rate multiplication (shield the baud rate of this sentence to 2400) TR1 = 1; //Timer starts - }
- void
UART_T (unsigned char UART_data){ //Define the serial port sending data variable SBUF = UART_data; //Send the received data back while(TI == 0); //Check the send interrupt flag TI = 0; //Set the transmit interrupt flag to 0 (cleared by software) - }
- void
Read_init (unsigned char CHA){ unsigned char AD_FIN=0; //Store A/D conversion flag CHA &= 0x07; //Select one of the 8 interfaces of ADC (0000 0111 clears the high 5 bits) ADC_CONTR = 0x40; //ADC conversion speed (0XX0 0000 where XX controls the speed, please set according to the data sheet) _nop_(); ADC_CONTR |= CHA; //Select the current A/D channel _nop_(); ADC_CONTR |= 0x80; //Start A/D power supply DELAY_MS(1); //Let the input voltage reach stability (1ms is enough) - }
- unsigned
char Read (void) { unsigned char AD_FIN=0; //Store A/D conversion flag ADC_CONTR |= 0x08; //Start A/D conversion (0000 1000 makes ADCS = 1) _nop_(); _nop_(); _nop_(); _nop_(); while (AD_FIN ==0){ //Wait for A/D conversion to end AD_FIN = (ADC_CONTR & 0x10); //0001 0000 Test whether A/D conversion is finished } ADC_CONTR &= 0xE7; //1111 0111 clear ADC_FLAG bit, turn off A/D conversion, - return
(ADC_DATA); //Return A/D conversion result (8 bits) - }
- void
main (void) { unsigned char R; UART_init(); //Serial port initialization program Read_init(0); //ADC initialization P1M0 = 0x01; //P1.7~.0: 0000 0001 (high impedance) //Note: When changing the ADC channel, the corresponding IO interface must be changed to high impedance input at the same time. P1M1 = 0x00; //P1.7~.0: 0000 0000 (forced push) while(1){ R = Read (); UART_T (R); //Serial port secretary, send ADC reading value to serial port for display } - }
Above - 8-bit ADC application example (applicable to STC12C2052AD series)
C Code
- void
Read_init (unsigned char CHA){ unsigned char AD_FIN=0; //Store A/D conversion flag CHA &= 0x07; //Select one of the 8 interfaces of ADC (0000 0111 clears the high 5 bits) ADC_CONTR = 0x40; //ADC conversion speed (0XX0 0000 where XX controls the speed, please set according to the data sheet) _nop_(); ADC_CONTR |= CHA; //Select the current A/D channel _nop_(); ADC_CONTR |= 0x80; //Start A/D power supply DELAY_MS(1); //Let the input voltage reach stability (1ms is enough) - }
- unsigned
int ADC_Read (void) { unsigned char AD_FIN=0; //Store A/D conversion flag ADC_CONTR |= 0x08; //Start A/D conversion (0000 1000 makes ADCS = 1) _nop_(); _nop_(); _nop_(); _nop_(); while (AD_FIN ==0){ //Wait for A/D conversion to end AD_FIN = (ADC_CONTR & 0x10); //0001 0000 Test whether A/D conversion is finished } ADC_CONTR &= 0xE7; //1111 0111 clear ADC_FLAG bit, turn off A/D conversion, - return
(ADC_RES*4+ADC_RESL); //Return the A/D conversion result (the high 8 bits of the 10-bit ADC data are in ADC_RES, and the low 2 bits are in ADC_RESL) - }
Above - 10-bit ADC program module (for STC12C5A60S2 series)
C Code
- #include
//MCU header file - #include
//51 basic operations (including _nop_ empty function) - void
DELAY_MS (unsigned int a){ unsigned int i; while( a-- != 0){ for(i = 0; i < 600; i++); } - }
- void
UART_init (void) { //EA = 1; //Enable general interrupts (if interrupts are not used, they can be //masked) //ES = 1; //Enable UART serial port interrupt TMOD = 0x20; //Timer T/C1 working mode 2 SCON = 0x50; //Serial port working mode 1, serial port reception is allowed (serial port reception is prohibited when SCON = 0x40 ) TH1 = 0xF3; //Set the high 8 bits of the timer initial value TL1 = 0xF3; //Set the initial value of the timer to the lower 8 bits PCON = 0x80; //Baud rate multiplication (shield the baud rate of this sentence to 2400) TR1 = 1; //Timer starts - }
- void
UART_T (unsigned char UART_data){ //Define the serial port sending data variable SBUF = UART_data; //Send the received data back while(TI == 0); //Check the send interrupt flag TI = 0; //Set the transmit interrupt flag to 0 (cleared by software) - }
- void
Read_init (unsigned char CHA){ unsigned char AD_FIN=0; //Store A/D conversion flag CHA &= 0x07; //Select one of the 8 interfaces of ADC (0000 0111 clears the high 5 bits) ADC_CONTR = 0x40; //ADC conversion speed (0XX0 0000 where XX controls the speed, please set according to the data sheet) _nop_(); ADC_CONTR |= CHA; //Select the current A/D channel _nop_(); ADC_CONTR |= 0x80; //Start A/D power supply DELAY_MS(1); //Let the input voltage reach stability (1ms is enough) - }
- void
ADC_Read (void) { unsigned char AD_FIN=0; //Store A/D conversion flag ADC_CONTR |= 0x08; //Start A/D conversion (0000 1000 makes ADCS = 1) _nop_(); _nop_(); _nop_(); _nop_(); while (AD_FIN ==0){ //Wait for A/D conversion to end AD_FIN = (ADC_CONTR & 0x10); //0001 0000 Test whether A/D conversion is finished } ADC_CONTR &= 0xE7; //1111 0111 clear ADC_FLAG bit, turn off A/D conversion, - }
- void
main (void) { UART_init(); //Serial port initialization program Read_init(0); //ADC initialization P1M1 = 0x01; //P1.7~P1.0: 0000 0001 (high impedance) //Note: When changing the ADC channel, the corresponding IO interface must be changed to high impedance input at the same time. P1M0 = 0x00; //P1.7~P1.0: 0000 0000 (forced) while(1){ ADC_Read (); //Call ADC conversion program UART_T (ADC_RES); //Serial port secretary, send the high 8 bits of the ADC read value to the serial port 0000 0000 UART_T (ADC_RESL); //Serial port secretary, send the lower 2 bits of the ADC read value to the serial port XXXX XX00 } - }
Previous article:What is a stack? An example of using the 51 MCU stack pointer SP
Next article:51 MCU timer/counter, interrupt
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
Guess you like
- ZYNQ cannot be burned to NAND Flash via JTAG
- Qorvo based small PA introduction series
- .Power amplifier circuit
- Watch for free: How to solve the 32.0 GT/s PCIe5 test? Two experts reveal the secrets for you
- [Electronic Basics] How to connect the ground wire to the earth? Do you understand these 3 key points?
- GD32L233C-START evaluates the implementation of serial port USART0 printf redirection
- First field trial of near field communication mobile payment launched
- DSP has a problem with printf function running away in CCS environment
- USB transformation of ESP32-S2-Saola-1
- Is there any delay when directly connecting FPGA I/O pins?