Exp. 7.: On-chip ADC Programming in LPC2148 Development Board.
CODE
#include <lpc214x.h>
void delay(unsigned char time) {
unsigned int i, j;
for (j = 0; j < time; j++) {
for (i = 0; i < 8000; 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(10); // delay
IOCLR1 = (1 << 18); // en=0
}
void LCDdata(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(10); // delay
IOCLR1 = (1 << 18); // en=0
}
void LCDinit() {
LCD_command(0x38); // 8bit mode and 5x8 dots (function set)
delay(10); // delay
LCD_command(0x80); // set cursor to 0th location on 1st line
delay(10); // delay
LCD_command(0x01); // clear lcd (clear command)
delay(10); // delay
LCD_command(0x06); // cursor increment and display shift (entry mode set)
delay(10); // delay
LCD_command(0x0c); // display on, cursor off, cursor character blinking off
}
void LCD_write_string(unsigned char *string) {
while (*string) { // Check for End of String
LCDdata(*string++); // sending data on LCD byte by byte
}
}
int main(void) {
unsigned int ADCResult = 0;
unsigned char i, Thousands, Hundreds, Tens, Ones;
PINSEL1 = 0x04000000;
PINSEL2 = 0X00; // Configure PORT1 as GPIO
IODIR1 = 0x07 << 16; // Configure P1.18, P1.17, P1.16 as output
IODIR0 = 0xFF << 16; // Configure P0.23 - P0.16 as output
LCDinit(); // Initialize LCD 16x2
LCD_command(0xc0); // second line
LCD_write_string("ADC O/P=");
AD0CR = 0x01200404;
while (1) {
AD0CR |= 1 << 24; // Start ADC
while ((AD0DR2 & 0x80000000) == 0); // Wait for the conversion to be completed
ADCResult = AD0DR2; // Store converted data
ADCResult = (ADCResult >> 6) & 0x3FF;
LCD_command(0xCA); // Goto 10th place on second line of LCD
i = ADCResult / 1000; // Get the thousands place
Thousands = i + 0x30; // Convert it to ASCII
LCDdata(Thousands); // Display thousands place
i = (ADCResult % 1000) / 100; // Get the Hundreds place
Hundreds = i + 0x30; // Convert it to ASCII
LCDdata(Hundreds); // Display Hundreds place
i = ((ADCResult % 1000) % 100) / 10; // Get the Tens place
Tens = i + 0x30; // Convert it to ASCII
LCDdata(Tens); // Display Tens place
i = ADCResult % 10; // Get the Ones place
Ones = i + 0x30; // Convert it to ASCII
LCDdata(Ones); // Display Ones place
delay(250); // Delay between two conversions
}
return 0;
}

Comments
Post a Comment