Skip to content

Instantly share code, notes, and snippets.

@Embedded-linux
Created September 4, 2013 14:02
Show Gist options
  • Select an option

  • Save Embedded-linux/6437359 to your computer and use it in GitHub Desktop.

Select an option

Save Embedded-linux/6437359 to your computer and use it in GitHub Desktop.
ADC for LPC 2148
//————————————————————————————————————————————————————————————————————————//
// Program : Example Analog to Digital converter
// Description : Display A/D convert value on terminal program(used UART0
// : communication)
// Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz
// Filename : adc.c
// C compiler : Keil CARM Compiler
//————————————————————————————————————————————————————————————————————————//
#include “lpc214x.h” // Header file for Phillips LPC2148 controller
#include “stdio.h” // Library for sprintf function
#include “uart.h” // Library for UART
//—————————————— Function for Initial system clock ————————————————//
void init()
{
PLL0CFG=0x24; // MSEL = 4,PSEL = 2
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;
PLL0CON=0x1;
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked
PLL0CON=0x3; // Connect the PLL as the clock source
PLL0FEED=0xAA; // Feed process
PLL0FEED=0x55;
MAMCR=0x2; // Enabling MAM and setting number of clocks used for
// Flash memory fetch (4 cclks in this case)
MAMTIM=0x4;
VPBDIV=0x02; // PCLK at 30 MHz
}
//————————————————— Function delay ———————————————————————//
void delay_ms(long ms) // delay 1 ms per count @ CCLK 60 MHz
{
long i,j;
for (i = 0; i < ms; i++ )
for (j = 0; j < 6659; j++ );
}
//————————————————— Main Program ————————————————————————//
void main()
{
int val=0;
char s[30]; // Buffer for keep string from integer convert
init(); // Initialize the system
SCS = 0x03; // select the “fast” version of the I/O ports
uart0_init(9600); // Initial UART0
PINSEL1 |= 0x10000000; // Enable AD0.3 for used
AD0CR = 0x00210608; // Setup A/D: 10-bit AIN0 @ 3MHz
while (1)
{
AD0CR |= 0x01000000; // Start A/D Conversion
while ((AD0DR3 & 0x80000000) == 0); // Wait for the conversion to complete
val = ((AD0DR3 >> 6) & 0x03FF); // Extract the A/D result
sprintf(s,”Analog(AD0.3): %d \r”,val);
// Convert string to display analog value
uart0_puts(s); // Display to Terminal program
delay_ms(200); // Delay for display
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment