Freescale MCU KEA128 ADC Learning

Publisher:数据梦行者Latest update time:2021-04-07 Source: eefocusKeywords:Freescale Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Keywords:Freescale Reference address:Freescale MCU KEA128 ADC Learning

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

Digital human heart rate detector based on single chip microcomputer
0 Preface Although there are many instruments for detecting heart rate at present, there are few portable full-digital heart rate measuring devices that can achieve accurate measurement, upload data to PC, and have multiple functions such as sound and light alarm. The digital human heart rate detector introduced in
[Microcontroller]
Digital human heart rate detector based on single chip microcomputer
Summary of MCU serial communication programming
The main purpose of this summary is to make it clearer how the serial port interrupt of the microcontroller occurs. That is, how to trigger the sending function of the serial port interrupt. In the description, I use special font colors to emphasize the key points and precautions. Procedure 1: This program uses soft
[Microcontroller]
Design and implementation of radio frequency communication base station based on single chip microcomputer control
With the development of modern communication technology, data transmission is an important research topic. At present, wired communication technology has always been the mainstream of the market, which is also the biggest bottleneck in the space area where wiring cannot be freely carried out. Wireless communication te
[Power Management]
Design and implementation of radio frequency communication base station based on single chip microcomputer control
The process of learning single chip microcomputer
The process of learning microcontrollers should be a step-by-step, continuous learning and accumulation process, which can be divided into three stages. The first stage: master the necessary basic knowledge for developing single-chip microcomputers. The first is to master the basic principles of single-chip m
[Microcontroller]
Method of controlling DC motor speed by 51 single chip microcomputer controlling PWM signal
The design uses a special chip to form a PWM signal generation system and explains in detail the principle and generation method of PWM signals and how to adjust the PWM signal duty cycle through software programming to control its input signal waveform. In addition, this system uses an infrared tube to measure the sp
[Microcontroller]
Method of controlling DC motor speed by 51 single chip microcomputer controlling PWM signal
Calculation of accurate timing of the timer of PIC microcontroller
Here, TMR0 of 16C711 microcontroller is used as timer interrupt, hoping to achieve accurate timing. In the program, TMR0 uses the 32-frequency division of the crystal oscillator, and the initial value is #0FCH. Therefore, POPBEAR brothers calculated the timing time of each timer interrupt as (256-X)*32*4/32768=0.015625
[Microcontroller]
Design of signal processing system based on dual single chip microcomputer
When the detection and control system processes a large amount of data and multiple information, only one single-chip microcomputer often cannot meet the system's real-time and scalability requirements, and the processing time is long. For example, in the process of collecting liquid level information, the single-ch
[Microcontroller]
Design of signal processing system based on dual single chip microcomputer
Six important parts of microcontroller learning and application
1. Bus A circuit is always made up of components connected by wires. In analog circuits, wiring is not a problem because the devices are generally in serial relationship and there are not many wires between the devices. However, computer circuits are different. They are based on microprocessors. All devices must be
[Power Management]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号