/*
According to Oasis Titan Kit Connections from LPC2148 to LCD and ADC and Push-To-On Switch:
Program Developed By Prof. Arvind Hatkar
Port P1.16 to P1.23 used for LCD Data bits.
Port P0.28 used for LCD RS - Command / Data (Pin No.13)
Port P0.29 used for LCD EN - Enable (Pin No.14)
RW of LCD is connected to ground
AD0.4 (Analog Input), (Pin No.9)
Pin 25 from Connector P1, is connected to Pin 1 of Jumper 5 by flexible Wire, after removing jumper from 1-2 and 2-3.
EINT3 (External Interrupt 3 Input), (Pin No.15) is connected through Push to On Switch S3
S3 can be pressed momentary to issue Start Of Conversion for ADC manually,
On End Of Conversion ADC generates interrupt which is handelled by VIC.
*/
#include <LPC21xx.h>
#define ADC_START 0X01000000
#define ADC_DONE 0X80000000
#define RS 0x10000000;
#define EN 0x20000000;
static unsigned int adcdata, Temp;
void delay(void);
void enable(void);
void initLCD(void);
void LCD_Cmd(unsigned int cmd);
void LCD_WriteString(char * string);
void LCD_WriteData(unsigned int value);
void ADC_Isr(void) __irq
{
adcdata = ADDR; //Latch/Read Converted Data
adcdata = adcdata>>6; //Shift ADC Data to LSB by 6 Bits
adcdata = adcdata & 0x3FF; //Clearing all other Bits
Temp = adcdata; //Make a copy of adcdata
VICVectAddr = 0; // reset VIC
}
int main(void)
{
unsigned char ones,tens,hundreds,thousands;
PINSEL1 = PINSEL1 & 0xFFF3FFFF; //1111 1111 1111 0011 1111 1111 1111 1111, Clear PINSEL1 19:18 Bits, P0.25 used as AD0.4
PINSEL1 = PINSEL1 | 0x00040000; //0000 0000 0000 0100 0000 0000 0000 0000, Set PINSEL1 19:18 Bits to AD0.4
ADCR = 0x00200310; //Code for ADCR is as follows
//XXXX 0 000 XX 1 X 000 0 00000011 00010000
//Reserved EDGE START Reserved PDN Reserved CLKS BURST CLKDIV SEL
//Reserved Falling_Edge No_Start Reserved Operational Reserved 11_clocks/10 bit Software_Controlled Clock_4Mhz AD0.4
// PINSEL1 = PINSEL1 | 0x20000000; //Pin Configured as EXT INT P0.30
PINSEL1 = PINSEL1 & 0xCFFFFFFF; //Pin Configured as GPIO P0.30
IODIR0 = IODIR0 | 0xBFFFFFFF; //Makes P0.30 as Iutput.
VICIntEnable |= (1<<18) ; //Enable 18th Bit, ADC0 IRQ
VICVectCntl0 = (1<<5) | 18 ; //Slot �0� for ADC0 i.e. 5th bit must 1 to enable the slot
VICVectAddr0 = (unsigned int) &ADC_Isr; //Vectored-IRQ for ADC0 has been configured
ADINTEN |= (1<<5); //Enable Completion of a Conversion on ADC channel 1 will to generate interrupt
initLCD(); //Ininialize LCD
LCD_WriteString("ADC By VIC"); //Displat Message on first line of LCD
ADCR |= ADC_START; //Start ADC Conversion
while(1)
{
adcdata = Temp; //Restore copy of adcdata
ones = adcdata % 10;
adcdata = adcdata / 10;
tens = adcdata % 10;
adcdata = adcdata / 10;
hundreds = adcdata % 10;
adcdata = adcdata / 10;
thousands = adcdata % 10;
LCD_Cmd(0xC0 << 16); //Second Line to Display ADC Output
LCD_WriteData(thousands | 0x30);
LCD_WriteData(hundreds | 0x30);
LCD_WriteData(tens | 0x30);
LCD_WriteData(ones | 0x30);
if( !(IOPIN0 & (1<<30)) ) // Evaluates to True for a 'LOW' on P0.30 (1<<30)
{
ADCR |= ADC_START; //Restart ADC Conversion
LCD_Cmd(0x80 << 16);
LCD_WriteString("ADC Restarted"); //Displat Message on first line of LCD
}
}
return 0;
}
void initLCD(void)
{
IODIR1 = 0x00FF0000; //P1.16 to P1.23 (DATA) configured as Output - Using 8 Bit mode
IODIR0 = 0x30000000; //Makes RS and EN Output.
IOPIN0 = 0x0; //Respective Outpot Port0 Pin (Set by IODIR0) will Become LOW
IOPIN1 = 0x0; //Respective Outpot Port1 Pin (Set by IODIR0) will Become LOW
//LCD Initialization Sequence Now starts (<< 16 Shift 16 Bits left)
LCD_Cmd(0x38 << 16); //Function Set Command : 8 Bit Mode , 2 Rows , 5x10 Font Style
LCD_Cmd(0x0E << 16); //Display On/Off Command : Display on , Cursor on , Blink off
LCD_Cmd(0x06 << 16); //Input Set : Increment Mode
LCD_Cmd(0x01 << 16); //Clear Display Command , Cursor at Home
LCD_Cmd(0x80 << 16); //Not required the 1st time but needed to reposition the cursor at home after Clearing Screen
}
void LCD_Cmd(unsigned int cmd)
{
IOCLR0 |= RS; //Enter Instruction Mode RS LOW
IOPIN1 = cmd; //Supply Instruction/Command Code
enable(); //Pulse Enable to process it
}
void enable(void)
{
delay();
IOSET0 |= EN; //Enable=High
delay();
IOCLR0 |= EN; //Enable=Low
delay();
}
void delay(void)
{
int i=0,x=0;
for(i=0; i<19999; i++)
{
x++;
}
}
void LCD_WriteString(char * string)
{
int c=0;
while (string[c]!='\0')
{
LCD_WriteData(string[c]);
c++;
}
}
void LCD_WriteData(unsigned int value)
{
IOSET0 |= RS; //Switch to Data Mode RS HIGH
IOPIN1 = (int) value << 16; //Supply Character Code
enable(); //Pulse Enable to process it
}