PIC microcontroller AD conversion

Publisher:感恩的7号Latest update time:2018-08-04 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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
         }


     }
}


Reference address:PIC microcontroller AD conversion

Previous article:PIC microcontroller communication protocol
Next article:PIC microcontroller disassembly

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号