#define uchar unsigned char
unsigned char aa,bb;
void delay(uchar dd)
{
int x,y;
for(x=dd;x>0;x--)
for(y=100;y>0;y--);
}
unsigned char Read_ADC(unsigned char adcvcc)
{
//Set P1.0--P1.1 to the mode suitable for AD conversion
ADC_CONTR = ADC_CONTR|0x80; //1000,0000 turn on the A/D conversion power
delay(20); //20ms
ADC_CONTR = ADC_CONTR&0xE0; //1110,0000 clear ADC_FLAG, ADC_START bit and the lower 3 bits
ADC_CONTR = ADC_CONTR&0xf8|adcvcc; //Set the current channel number
delay(10); //Delay to make the input voltage stableADC_DATA
= 0; //Clear A/D conversion result registerADC_CONTR
= ADC_CONTR|0x08; //0000, 1000ADCS = 1, start conversiondo
{ ; } while((ADC_CONTR & 0x10)==0); //0001, 0000 wait for A/D conversion to
endADC_CONTR = ADC_CONTR&0xE7; //1110, 0111 clear ADC_FLAG and ADC_START bits, stop A/D conversionreturn
ADC_DATA;
}
void main()
{
//Add the ADC port setting, such as) 1.0, if P1.1 is set as ADC port:
P1M0 = 0x03; //0000, 0011 is used for A/D conversion P1.x port, set to open drain
firstP1M1 = 0xC3; //0000, 0011P1.0--P1.1 is set to open drain first. Disconnect the internal pull-up resistor
bb=0x01;
while(1)
{
aa=Read_ADC(1); //The first A/D conversion; if you want to change to another path, just change the input channel.
P3=aa; //This is to display the A/D data on the LED of P3 port.
}
}
Previous article:STM8S-Discovery third program - DS18B20
Next article:Video Data Acquisition System Based on High-Speed A/D Converter
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- 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!
- 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
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Recommended development directory for CH579M
- Free gifts! 11 popular Maxim development boards are waiting for you!
- Have you been affected by the price increase? Share the best pin-compatible alternative solution under the wave of price increase#, a certain brand?
- Request: DC-DC small package chip similar to LM2596
- What is the reason! Another wind turbine fire accident! And caused a ground wildfire
- 【Iprober 520 current probe】Practical use in motor control
- Can you please help me find out where is the problem with the program?
- How to add c files in esp32 idf components to compile
- Have you noticed the LPDSP32 audio codec inside RSL10? Does your project use this resource?
- Will the push-pull circuit be straight through?