Thursday, 14 February 2019

ADC interfacing with LPC2148




Features of ADC:





  • ADC0 (6 Channel), ADC1 (8 Channel) 
  • 10-bit, Successive Approximation 
  • Supports burst mode(repeated conversion at 3-bit to 10-bit resolution)
  • Conversion time: 2.44 us
  • SoC by software control, on timer match, transition on a pin
  • Voltage Range: 0 V – VREF (+3.3 V) 
  • Max. clock freq is 4.5 MHz, (by programming ADCCON) 


  • Functional block diagram of ADC in LPC2148:


    Functional Block Diagram:
    ADC0: It has 6 channel ( AD0.0 and AD0.5 not available).


    ADC1: It has 8 channel


    This Successive Approximation process requires a clock less than or equal to 4.5 MHz. We can  adjust this clock using clock divider settings.

    ADC Control register(AD0CR):

    Bits 7:0 – SEL
    Selects which of the AD0.7:0/AD1.7:0 pins is (are) to be sampled and converted. For AD0, bit 0 selects (00000001) Pin AD0.0, and bit 7 selects(10000000) pin AD0.7. In software-controlled mode, only one of these bits should be 1. In hardware scan mode, any value containing 1 to 8 can be one.

    Bits 15:8 – CLKDIV
    The APB clock (PCLK) is divided by (this value plus one) to produce the clock for the A/D converter, which should be less than or equal to 4.5 MHz Typically, software should program the smallest value in this field that yields a clock of 4.5 MHz or slightly less, but in certain cases (such as a high-impedance analog source) a slower clock may be desirable. 



    Bit 16 – BURST
    If bit is 0: ADC will not perform Repeated A to D Conversion.
    If bit is 1: ADC will perform Repeated A to D Conversion The AD converter does repeated conversions at the rate selected by the CLKS field.Repeated conversions can be terminated by clearing this bit, but the conversion that’s in progress when this bit is cleared will be completed.
    Note: START bits must be 000 when BURST = 1, otherwise conversions will not be start.

    Bits 19:17 – CLKS
    This field selects the number of clocks used for each conversion in Burst mode and result size in bits. 

    CLKS field - 19-18-17No. of Clock cycles used per bit conversion
    00011 clocks cycles / 10 bit conversion
    00110 clocks/ 9 bits
    0109 clocks/ 8 bits
    0118 clocks/ 7 bits
    1007 clocks/ 6 bits
    1016 clocks/ 5 bits
    1105 clocks/ 4 bits
    1114 clocks/ 3 bits
    when we select 000 then it uses 11 clocks for each conversion and provide 10 bits of result in corresponding ADDR register.

    Bit 20 :RESERVED

    Bit 21PDN
    Power Down
    If bit is 1: The A/D converter is operational. 

    If bit is 0: The A/D converter is in power-down mode.

    Bit 23:22 – RESERVED 

    Bit 26:24 – START
    START field - 26-25-24
    000No start of Conversion
    001Start of Conversion Now
      
    Bit 27 – EDGE
    (In use only when START field contains Values from 010 TO 111)  

    ADC data register(AD0DR):
    Contains most recent converted data and EoC(Done)
     
    Bits 5:0 – RESERVED

    Bits 15:6 – RESULT
    When DONE(31) bit is set to 1, this field contains 10-bit ADC result that has a value in the range of 0  to 1023. 



    Bit 29:16 – RESERVED

    Bit 30 – OVERRUN
    This bit is set to 1 in burst mode if the result of one or more conversions is lost and overwritten before the conversion that produced the result in the RESULT bits.
    This bit is cleared by reading this register.
     


    Bit 31 – DONE
    This bit is set to 1 when an A/D conversion completes.  

    ADC Global data register(AD0GDR): 
    Contains done bit, most converted data, channel number 


    Bits 5:0 – RESERVED 

    Bits 15:6 – RESULT
    When DONE(31) bit is set to 1, this field contains 10-bit ADC result that has a value in the range of 0  to 1023. 


    Bit 23:16 – RESERVED

    Bits 26:24 – CHN
    These bits contain the channel from which ADC value is read.
    e.g. 000 identifies that the RESULT field contains ADC value of channel 0.
     


    Bit 29:27 – RESERVED

     Bit 30 – OVERRUN
    This bit is set to 1 in burst mode if the result of one or more conversions is lost and overwritten before the conversion that produced the result in the RESULT bits.
    This bit is cleared by reading this register.
     


    Bit 31 – DONE
    This bit is set to 1 when an A/D conversion complete.

    Example

    1. Select ADC-0, Channel-2, Clock frequency 3 MHz (let PCLK is 15 MHz), burst mode repeated conversion) and 10-bit resolution. Power-up ADC and issue start of conversion.
      Solution: AD0CR    = 0x01210404; // configure SEL, CLKDIV, BURST CLKS & PDN bit fields set START, signal No start of conversion
    Interfacing Diagram:

    Interfacing Diagram


    LM35 Temperature sensor interfacing with LPC2148

    Interfacing Diagram
    Note: For Proteus Simulation use Global data register(AD0GDR) instead of Data register(AD0DR2)  in programming.

    Algorithm:
    1) Configure ADC control register according to need of application.
    2) Start ADC conversion by writing appropriate value to START bits in ADxCR.
    3) Monitor the DONE bit (bit number 31) of the corresponding ADC data register till it changes      from 0 to 1.
    4) Read ADC result from ADC data register and Display on (LCD or Serial Window of terminal)

    Code:ADC interfacing with LPC2148



    /**********************************************************
    Expt. 6.: ADC interfacing with to LPC2148
    Platform:  LPC2148 Development Board.
    College: PICT
    
    Hardware Setup:-
    ADC Channel = AD0.2, P0.29
    LCD Data line = P0.16 to P0.23
    LCD Control line = P1.16,P1.17,P1.18
    **********************************************************/
    #include <lpc214x.h>
    //Calibrated to 1ms
    void  delay_ms(unsigned int time)  
    {
     unsigned int  i, j;
     for (j=0; j<time; j++)
      for(i=0; i<8002; i++);
    }

    void LCD_command(unsigned char command)
    {
     IOCLR0 = 0xFF<<16; // Clear LCD Data lines
     IOCLR1=1<<16;    // RS=0 for command
     IOCLR1=1<<17;    // RW=0 for write
     IOSET0=command<<16; // put command on data line
     IOSET1=(1<<18);   // en=1
     delay_ms(10) ;   // delay
     IOCLR1=(1<<18);   // en=0
    }

    LCD_DATA(unsigned char data)
    {
     IOCLR0 = 0xFF<<16; // Clear LCD Data lines
     IOSET1=1<<16;    // RS=1 for data
     IOCLR1=1<<17;    // RW=0 for write
     IOSET0= data<<16;  // put command on data line
     IOSET1=(1<<18);   //en=1
     delay_ms(10) ;    //delay
     IOCLR1=(1<<18);   //en=0
     }

    LCD_init()
    {
     LCD_command(0x38); //8bit mode and 5x8 dotes
     delay_ms(10) ;   // delay
     LCD_command(0x01);  //clear lcd(clear command)
     delay_ms(10) ;   // delay
     LCD_command(0x06);  //cursor increament and
     delay_ms(10) ;   // delay
     LCD_command(0x0E);
     delay_ms(10) ;
     LCD_command(0x80);  //set cursor to 0th location 1st lne
     delay_ms(10) ;
    }

    void LCD_write_string(unsigned char *string)
    {
      int i = 0;
      while(string[i]!='\0')  //Check for End of String
      {
      LCD_DATA(string[i]);
      delay_ms(10) ;
      i=i++;  //sending data on LCD byte by byte
      }
    }

    int main(void)
    {
      unsigned char var1[]= {"ADC:"};
     unsigned char val[4];
     unsigned int ADC_Result=0,i;
     PINSEL1 = 0x04000000;
     PINSEL2 = 0X00;  //Configure PORT1 as GPIO
     IODIR1= 0x07<<16; //Configure P1.18, P1.17, P1.16
     IODIR0= 0xFF<<16; //Configure P0.23 - P0.16 as output
     LCD_init();    //Initialize LCD 16x2
      /************** Configure ADC0 for following
     ADC Channel = AD0.2
     ADC Clock = 3 MHz
     Clock Selection = 11 Clock Cycles/10bit
     Start Condition = No start
     Power Down = 1, EDGE = 0, BURST = 1 *************/
     AD0CR = 0x00210404;

     while (1)
     {
          LCD_write_string(var1);
          AD0CR |= 1<<24; //Start ADC
      while (!(AD0DR2&0x80000000));  //Wait for  conversion
     
      ADC_Result = AD0DR2; //Store converted data
      ADC_Result = (ADC_Result>>6) & 0x3FF;
     
       for(i=0;i<4;i++)
     {
      val[i]=ADC_Result%0X0A ;
      val[i]=val[i]+0X30;
       ADC_Result=ADC_Result/0X0A;
     }
     
        LCD_DATA(val[3]);
        LCD_DATA(val[2]);
        LCD_DATA(val[1]);
        LCD_DATA(val[0]);
        delay_ms(1200);
        LCD_command(0x01);  //clear lcd(clear command)
     
     
     }
    }


    ADC interfacing with LPC2148 (Display output on UART0)



    /**********************************************************
    Expt. 6.: ADC interfacing with to LPC2148
    Platform:  LPC2148 Development Board.
    College: PICT
    
    Hardware Setup:-
    ADC Channel = AD0.2 (P0.29),  uart0= P0.0 and P0.1
    **********************************************************/
    #include <lpc214x.h>
    void uart0Init(void);
    void uart0Putch(unsigned char ch);
    void UART0_Txstring(unsigned char *Str);

    void  delay_ms(unsigned int time)
    {
     unsigned int  i, j;
     for (j=0; j<time; j++)
      for(i=0; i<8002; i++);
    }
    void uart0Init(void)   
    {
        PINSEL0=0x00000005;// port 0 tx P0.1 and rx P0.0 selected
        U0LCR=0x83; //8bit data, no parity, 1 stop bit
        U0DLL=97;// 9600 baud rate @15Mhz Pclk
        U0LCR=0x03;// DLAB=0

    }
    void uart0Putch(unsigned char ch)
    {
        U0THR=ch;    // Transmitter holding register
        while(!(U0LSR & 0x20));// wait still THR=0
    }
    void UART0_Txstring(unsigned char *Str)
    {
    int i=0;
    while(Str[i]!='\0')
     {
       uart0Putch(Str[i]);
       i++;
     }
    }


    int main(void)
    {
    int adcdata;
    int voltage;
    float voltage1;
    unsigned char volt[3];
    unsigned char volt1[3];
    PINSEL1 = 0x04000000;

    uart0Init();
    AD0CR = 0x00210404;
      while(1)
     {         AD0CR |= 1<<24;
     if(AD0DR2 & 0x80000000) ////EOC bit monitoring
     {
       adcdata=(AD0DR2 & 0x0000FFC0);
       adcdata=adcdata>>6;
       voltage1=((adcdata/1023.0)*3.3);
       voltage=adcdata;
       sprintf(volt1, "%.1f", voltage1);
       sprintf(volt, "%.1d", voltage);
       UART0_Txstring("\n Analog voltage is: ");
       UART0_Txstring(volt1);
       delay_ms(3000);
     UART0_Txstring("\n Digital value of Analog voltage is: ");
       UART0_Txstring(volt);
       delay_ms(5000);
      } 
     }

     
    }




    No comments:

    Post a Comment