AD conversion
Let's first look at R1 and R2. R2 is an adjustable resistor. If we increase R2, the voltage on the RA0 pin will increase. If we decrease R2, the voltage on the RA0 pin will decrease. So how does the microcontroller know the voltage change? This requires AD conversion, which converts analog quantity into digital quantity.
How does a PIC microcontroller represent voltage?
PIC uses ten binary digits to represent voltage, that is, the value 0 to 1023 to represent voltage. For example, if the value is 400, what voltage does it represent? This depends on the reference voltage.
For example, if we set the positive reference voltage to 3.3V, when the input voltage is 0, the value is 0. When the input voltage is 3.3V, the value is 1023. If the input voltage is 1.2V, how much voltage does it represent?
First, let's figure out what voltage a value represents: 3.3V divided by 1023 is approximately equal to 0.003V.
Then, 1.2V divided by 0.003V equals 400. This tells us that 400 represents 1.2V.
See the figure below, we can see AN0~AN7. These are ports that can be configured as analog inputs. Only these pins can be used as AD conversion ports.
Example explanation:
For example: Let's look at the first schematic diagram. If an analog quantity is input from the RA0/AN0 pin and the voltage is greater than 1.2v, the LED will turn on, otherwise the LED will turn off.
AD configuration steps:
1. Set the port
Set RA0 port as input TRISA = 0x01;
Set RA0 port to analog ANSELA = 0x01;
2. Configure ADC module
Select the conversion clock for the ADC.
How to choose the conversion clock? It depends on the current clock frequency. You can choose according to the table in the data sheet.
We set the microcontroller clock frequency to 32MHZ. When selecting the ADC period, do not select the shaded part. In the 32MHz column, we randomly selected the ADC clock period of 1us, and the corresponding clock source is Fosc/32. AD control register 1 ADCON1 ADCS<2:0>=010 Note: ADCS<2:0> means bits 0 to 2 of ADCS
Configuring the reference voltage
Here we configure the positive reference voltage as the power supply voltage. AD control register 1 ADCON1 ADPREF<1:0>=00;
Configure left/right alignment
The value after AD conversion is 10-bit binary, but the microcontroller we use only 8-bit, so the PIC microcontroller uses two 8-bit registers to store the AD value, ADRESH is used to store the high-bit result, and ADRESL is used to store the low-bit result. But the sum of ADRESH and ADRESL is 16. So how are these 10-bit values placed in it? This is set by left and right alignment.
If it is right-aligned, the lower 8 bits are placed in ADRESL and the remaining 2 bits are placed in ADRESH.
If it is left-aligned, the upper 8 bits are placed in ADRESH, and the remaining 2 bits are placed in ADRESL. See the figure below.
We choose right alignment here, so AD control register 1 ADCON1 ADFM = 1
The above explains the configuration of the ADCON1 register. Now let's explain the ADCON0
Select ADC input channel
There is only one AD conversion module, but there are 8 AD input channels AN0~AN7. So it is impossible to perform AD conversion at the same time. We assign the one that needs to be used to that one. According to the hardware, we assign the AD conversion module to AN0.
So CHS<4:0> of ADCON0=0000;
Enable ADC module
ADC module is turned on, ADCON0 ADON = 1, which simply enables the ADC module. AD conversion does not start. If the ADC module is not used, it is recommended to turn it off. It can save some power!!!
3 Start AD conversion
ADCON0's GO/DONE = 1 starts AD conversion.
4 Wait for the AD conversion to finish
5 Read the results
Generally, we do not take the AD conversion value once. Instead, we take it multiple times and calculate the average value. This ensures the accuracy of the conversion. When configuring the ADC module, there are many places that do not explain why the configuration is done this way, because many configurations are actually quite random. It is not so absolute. You must choose one. Of course, the actual configuration still depends on your project requirements.
//Development environment MPLAB X IDE, microcontroller PIC16LF1823.
#include
__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_ON&CPD_OFF&BOREN_ON
&CLKOUTEN_OFF&IESO_ON&FCMEN_ON);//This should be put on the previous line
__CONFIG(PLLEN_OFF&LVP_OFF);
#define ADC_NUM 8 //Number of conversions
#define LED LATA1
void init_GPIO(void)
{
TRISA = 0x01;//Port is set as input
ANSELA = 0x01;//Set as analog input
PORTA = 0x00;
LATA = 0x00;
}
void init_fosc(void)
{
OSCCON = 0xF0;//32MHZ
}
void init_AD(void)
{
ADCON1= 0xA0;//Right-aligned, AD clock is Fosc/32, reference voltage is power supply voltage,
ADCON0= 0x00;//Select channel AN0
ADCON0bits.ADON = 1;//Turn on module
}
unsigned int ADC_BAT_ONE(void)//Convert once
{
unsigned int value;
value=0;
ADCON0bits.CHS =0;//Select channel AN0
ADCON0bits.ADGO=1;//Start conversion
while(ADCON0bits.GO==1);//Wait for conversion to end
value=(unsigned int)ADRESH;//Force type conversion, because ADRESH is a character type and can only represent 8-bit binary. So it must be converted to an integer that can accommodate 10-bit binary.
value= value<<8;// Shift the upper two bits to the left by 8 bits
value += ADRESL;//Add the lower eight bits to the value of ADRESL.
return value;
}
unsigned int ADC_BAT_contiue(void)
{
unsigned int ADV_MCU[ADC_NUM],ADV_CNT,ADV_ALL;
ADV_ALL=0;
for(ADV_CNT=0;ADV_CNT
ADV_MCU[ADV_CNT]=ADC_BAT_ONE();
}
for(ADV_CNT=0;ADV_CNT
ADV_ALL += ADV_MCU[ADV_CNT];
}
ADV_ALL= ADV_ALL/ADC_NUM;
return ADV_ALL;//Get the result and return
}
/*
*
*/
int main(int argc, char** argv) {
init_fosc();//Set the clock
init_GPIO();//Set the I/O port
init_AD();//Set AD
while(1)
{
if( ADC_BAT_contiue()>400)//Judge whether the input voltage is greater than 1.2V
{
LED=1;//Light is on
}
else
{
LED=0;//Light is off
}
}
}
Previous article:PIC microcontroller communication protocol
Next article:PIC microcontroller disassembly
- Popular Resources
- Popular amplifiers
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
- Disassembling a 32G USB flash drive
- STM32MP157A-DK1 evaluation + GO language web service (5)
- MSP432 serial port 0 transceiver
- The algorithm of digital filtering in single chip microcomputer
- TI Wireless Connectivity Technology Selection Guide
- Adding SDCard functionality to ESP32-S3 with MicroPython
- Show off the development boards I bought in recent months
- The flavor of the New Year is the “poor flavor” of backward productivity
- ZigBee Power Consumption Calculation
- When burning the program, you mistakenly select "cold start" and the program can only be downloaded when P3.2/P3.3 is 0/0. Solution