You are on page 1of 4

ANALOG TO DIGITAL CONVERSION IN LPC2129

AIM : To design a program to convert the analog value to digital value by means of a LPC2129 kit and display the converted value in the serial window. ALGORITHM: 1. Initialize the header files. 2. Set the desired baudrate and frequency values. The frequency values are set for both crystal and clock. 3. configure UART by setting the values for UOLCR(line control register), VPBDIV(bus clock divider) a. UOLCR-->0x83, [it defines that the divisor latch acess is enabled and 8-bit word length is set. ] b. VPBDIV-->0x01, [it defines the bus clock divider where 0x01 represents the processor clock.] 4. The conversion starts by setting the ADCR bit. ADCR bit defines the operating mode before the conversion occurs. Where, ADCR_bit.START =1. 5. The converted bits are serially transmitted by UART. 6. The serial transmission is performed by initalizing adc_serial_tx function to transmit the converted value. 7. The converted digital values are displayed in the serial window. SAMPLE CODE:

#include "nxp/iolpc2129.h" unsigned char arr[10]={"adc value"}; typedef unsigned int U8; /* 8 bit unsigned UPGRADE*/ typedef unsigned int U16; /* 16 bit unsigned */ int AD_Result; #define DESIRED_BAUDRATE 19200 #define CRYSTAL_FREQUENCY_IN_HZ 11059200 #define PCLK CRYSTAL_FREQUENCY_IN_HZ #define DIVISOR (PCLK/(16*DESIRED_BAUDRATE)) void UART0_send_data(unsigned char chr)

// since VPBDIV=0x01

{ while (U0LSR_bit.THRE==0); U0THR = chr; } void Arm_Uart0_Init() { unsigned int VPBDIV; //UART Config.

PINSEL0 = PINSEL0 & ~0xF | 0x5; U0LCR=0x83; // U0LCR: UART0 Line Control Register. // 0x83: enable Divisor Latch access, set 8-bit word length. // 1 stop bit, no parity, disable break transmission. // VPBDIV: VPB bus clock divider 0x01: PCLK=processor clock

VPBDIV=0x01; .

U0DLL=DIVISOR&0xFF; U0DLM=DIVISOR>>8; U0LCR=0x03 ; U0FCR=0x05 ;

// U0DLL: UART0 Divisor Latch (LSB). // U0DLM: UART0 Divisor Latch (MSB). // U0LCR: UART0 Line Control Register // 0x03: same as above, but disable Divisor Latch access. // U0FCR: UART0 FIFO Control Register // 0x05: Clear Tx FIFO and enable Rx and Tx FIFOs

} void serial_tx() { int i; for(i=0;arr[i]!='\0';i++) { UART0_send_data(arr[i]); // Serial transmission. } UART0_send_data('-'); UART0_send_data('-'); } void adc_serial_tx(unsigned long int val) { unsigned int adc1000,i; serial_tx(); i=1000; do

{ adc1000=val/i; UART0_send_data(adc1000+0x30); val=val%i; i=i/10; }while(i!=0x00); UART0_send_data(0x0d); UART0_send_data(0x0A); } void main() { Arm_Uart0_Init(); ADCR = 0x00201001; ADCR_bit.START = 1; while(1) { /* if(ADDR_bit.DONE) { AD_Result = ((ADDR>>6)&0x3FF); ADCR_bit.START = 1; // Start A/D convertion adc_serial_tx(AD_Result); }*/ serial_tx(); } } OUTPUT: Snap shot of the serial communication window:

RESULT Thus the analog value is converted to digital value and the output is seen in the serial communication window (win x talk).

You might also like