The operation mode of this ADC module is actually quite simple.
The first thing is configuration. Let’s list what to configure.
ADICLK ADC module input clock selection. Bus clock, bus clock divided by 2 or external
After ADIV divides the above clock again, it can be used by the ADC core module.
MODE Select 8/10/12 bit conversion
ADLSMP short or long sampling. If not set, the default is short
ADLPC High speed or low power consumption, low power consumption will be slower, if not set, the default is high speed
AIEN enables or disables interrupts. It is disabled by default.
ADCO continuous conversion function, the default is single. If it is single, after one sampling is completed, you need to select the channel again to start the next conversion.
ADTRG Software trigger to start conversion or hardware trigger. The default is software
ACFE comparison function is turned on or off by default. The comparison function means that you set a value, and only when the sampled value is greater or less than this value will an interrupt be triggered, otherwise the sampling will continue.
ACFGT This is used in conjunction with the comparison function above to further set whether the interrupt is triggered when the value is greater or less than the set value. The default value is
REFSEL Reference voltage selection. Select VREFH/VREFL or VDDA/VSSA. Default VREFH/VREFL
ADC_APCTL1 is very important. The ADC has a total of 16 channels, each channel corresponds to a pin. This register is equivalent to configuring which channel to use. The lower 16 bits of this register are available, one bit corresponds to one channel.
HTRGMASKE
HTRGMASKSEL These two registers are prepared for hardware triggering. If you don't use hardware triggering, just keep the default values.
The ADC_CV register is used to store the comparison value. Remember there is a register to enable the comparison function?
AFDEP FIFO depth, can be 0 to disable FIFO or 1 for 2 levels, 2 for 3 levels and so on up to 8 levels. This is also very important.
ASCANE FIFO scanning mode is turned on and off. It is off by default. I don’t know what scanning mode is yet.
Now that we have configured a lot, it’s time to talk about how to use this ADC.
After you have set the above registers, the ADC is initialized. If you use the library function, just fill in the above function configurations in the configuration structure and you can call the Init function.
Next, let's talk about what to do after Init to start ADC
The software trigger I configured is the default one.
After you have configured, the ADC is waiting for you to select the channel. There are 16 external channels AD0~AD15 and 5 internal channels, including ground, temperature, air gap, VREFH, and VREFL. These internal channels are dedicated and you cannot change their purpose. You cannot change the temperature channel to external or other functions.
After you set ADCH and select the channel, the ADC will start converting. If you have enabled the comparison function, it will automatically compare after sampling. If not, it will just sample. After sampling, the COCO flag will be set to 1. If the interrupt is enabled, an interrupt will be entered. Then you can read the sampling result through the ADC_R register. You don't need to worry about the COCO flag. When you read ADC_R, the system will automatically clear COCO for you. If you have enabled the continuous conversion function, then you can leave at this time, do other things, and wait for the next sampling to be completed. If you don't turn it on continuously, then after you read the value of ADC_R, you have to set the value of ADCH again to tell the ADC module which channel to sample next, otherwise the ADC will ignore you and go to rest.
If ADC is so simple, it seems that it has too few functions. If I have several channels to sample, I have to set the next channel every time I read ADC_R, and I have to determine which channel should be the next. It is too troublesome, so the system also provides you with an enhanced function, FIFO. There are two FIFOs in the system, one FIFO stores the channel number, and the other FIFO stores the sample value. The depth of these two FIFOs must be consistent. There is only one place in the register to set the FIFO depth. There is no setting for the channel number FIFO depth or the sample value FIFO depth. There is only one FIFO depth.
How to use the FIFO depth? Under normal circumstances, you need to select the channel after Init before the ADC starts converting. With FIFO, the operation sequence is still the same, except that you can set the channel several times in succession at this time, and the system will automatically save the value you set into the FIFO. For example, if you write 0x01 to ADCH, then 0x02, and finally 0X03, the system will write these three values into the FIFO in sequence. What you need to pay attention to here is that the FIFO depth you set must be the same as the number of times you write the channel number. This means that you cannot set the FIFO depth to 5, and then when writing the channel number, you only write three in succession. In this way, the ADC will not start sampling and will wait until you write 5 before starting. The advantage of FIFO is that, for example, if you set the FIFO depth to 5, then COCO will be set to 1 and interrupt will be triggered only after all 5 channels have been sampled. When reading, you still read the value of ADC_R, but just like setting the channel number, you can read it several times in a row. When using FIFO, you must pay attention to the fact that FIFO means first in first out. When you write the channel number continuously, if the order is like this, 5, 6, 3, then the order of the values you read from ADC_R is 5, 6, 3. You must remember the order, otherwise don't blame me if it gets messed up.
The function of the FIFO just mentioned above is that if you have several channels to sample, you can use FIFO to help you sample all of them before you interrupt to get them all out at once. Then another idea of using FIFO is that I have a channel to sample, and I don’t want to interrupt to read the sampled value every time I sample it. Can I use FIFO to help me sample several values at a time, and then read them out at once after sampling. I think this function should be the FIFO scanning mode that I didn’t touch above.
void ADC_Module_Init()
{
ADC_ConfigType sADC_Config = {0};
sADC_Config.u8ClockDiv = ADC_ADIV_DIVIDE_4;
sADC_Config.u8ClockSource = CLOCK_SOURCE_BUS_CLOCK;
sADC_Config.u8Mode = ADC_MODE_12BIT;
sADC_Config.sSetting.bIntEn = 1; //Enable interrupt
sADC_Config.u8FiFoLevel = ADC_FIFO_LEVEL4;
//sADC_Config.sSetting.bFiFoScanModeEn=1;
sADC_Config.u16PinControl |= 0x08; // Enable AD sampling function of AD3 pin
ADC_SetCallBack(ADC_CallBack);
ADC_Init( ADC, &sADC_Config);
ADC_SetChannel(ADC,ADC_CHANNEL_AD22_TEMPSENSOR);
ADC_SetChannel(ADC,ADC_CHANNEL_AD29_VREFH);
ADC_SetChannel(ADC,ADC_CHANNEL_AD23_BANDGAP);
ADC_SetChannel(ADC,ADC_CHANNEL_AD3);
}
void ADC_CallBack(void)
{
uint8 i;
//When reading the result register, COCO can reset automatically
while( !ADC_IsFIFOEmptyFlag(ADC) ) //wait to read all data in FIFO
{
TempsensorBuffer[Index] = ADC_ReadResultReg(ADC); //These arrays are all static
VrefhBuffer[Index] = ADC_ReadResultReg(ADC);
BandgapBuffer[Index] = ADC_ReadResultReg(ADC);
LightsensorBuffer[Index] = ADC_ReadResultReg(ADC);
Index++; //This Index is also static
}
if (Index == (MAXBUFFSIZE-1))
{
ADC_Calculate(); //After storing a set of AD values, an algorithm must be performed
}
u8Adc_Converted_Completed_Flag = 1;
ADC_SetChannel(ADC,ADC_CHANNEL_AD22_TEMPSENSOR);
ADC_SetChannel(ADC,ADC_CHANNEL_AD29_VREFH);
ADC_SetChannel(ADC,ADC_CHANNEL_AD23_BANDGAP);
ADC_SetChannel(ADC,ADC_CHANNEL_AD3);
}
void ADC_Calculate() //The simplest average value algorithm is used here, and the median value filtering method can also be used
{
UINT8 i;
UINT32 tmp=0;
for (i=0;i
Previous article:NXP Freescale Kinetis KEA128 Study Notes 4 - ADC
Next article:Lesson 5 MC9S08DZ60 Serial Peripheral Interface SPI
Recommended ReadingLatest update time:2024-11-23 00:02
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- 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
- MSP430F5529 general I/O port settings
- PWM principle PWM frequency and duty cycle detailed explanation
- TI mmWave sensors for contactless private gesture detection
- Monitoring electric energy meter based on IoT --- Hardware display
- PIC Timer0
- Question about the value of pull-up resistor
- I would like to ask what specific things I need to learn to write drivers for hardware in FPGA
- Millimeter wave guided weapons and their countermeasure technology
- Analog Discovery 2 Review (3) Frequency Response Test Tool
- Four major measurement parameter instrument control system fault analysis steps