Adc code
// 2307123048 , 23070123030
#include <LPC21xx.h>
void ADCInit(void) {
PINSEL1 = 0x05000000; // Commented out in original
}
unsigned int ADC_Read(void) {
static unsigned int adc_data;
// Start conversion on channel 1 (AD0.1)
ADCR = 0x00200300 | (1 << 1) | (1 << 24);
// Wait until DONE bit is set
while (!((adc_data = ADDR) & 0x80000000));
// Extract 10-bit result (bits 15:6)
adc_data = (adc_data & 0x0000FFC0) >> 6;
return adc_data;
}
Main.c
// 2307123048 , 23070123030
#include "lpc21xx.h"
#include <stdio.h>
extern void uart_init(void);
extern char uart_rcv(void);
extern void uart_snd(char ch);
extern void uart_snd_str(char *str);
extern void ADCInit(void);
extern unsigned int ADC_Read(void);
void adc_delay(unsigned int time) {
unsigned int i, j;
for (i = 0; i < time; i++) {
for (j = 0; j < 10000; j++);
}
}
int main(void) {
unsigned int temp;
char buf[16];
uart_init();
ADCInit();
adc_delay(1000);
while (1) {
temp = ADC_Read(); // read AN0.1
// convert to string and store in a buffer
sprintf(buf, "ADC val:0x%03X\r\n", temp);
uart_snd_str(buf); // display buffer
sprintf(buf, "ADC result:%03d", temp);
// uart_snd_str(buf); // display buffer (commented in original)
adc_delay(1000);
}
}
Uart
// 23070123048 , 23070123030
#include <lpc21xx.h>
#define PCLK 15000000 // Peripheral clock = 15 MHz
void uart_init(void) {
/* Configure P0.0 as TXD0 and P0.1 as RXD0 */
PINSEL0 |= 0x00000005;
/* 8 bits, no parity, 1 stop bit */
U0LCR = 0x83; // Enable DLAB
/* Baud rate = 9600
DLL = 97, DLM = 0 for PCLK = 15 MHz */
U0DLL = 0x61;
U0DLM = 0x00;
U0LCR = 0x03; // Disable DLAB
}
void uart_snd(unsigned char ch) {
while (!(U0LSR & 0x20)); // Wait until THR empty
U0THR = ch;
}
unsigned char uart_rcv(void) {
while (!(U0LSR & 0x01)); // Wait until data received
return U0RBR;
}
void uart_snd_str(char *str) {
while (*str) {
uart_snd(*str++);
}
}