0% found this document useful (0 votes)
9 views46 pages

Interrupts LCD and ADC

Uploaded by

Vasu Narula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views46 pages

Interrupts LCD and ADC

Uploaded by

Vasu Narula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Interrupts, LCD and ADC

Nested Vectored Interrupt Controller(NVIC)

• Controls system exceptions and peripheral interrupts


• In the LPC176x, the NVIC supports 35 vectored interrupts
• Each peripheral device may have one or more IVT
interrupt lines to the Vectored Interrupt Controller. Base addr Type 0 Vector
• Interrupt numbers relate to where entries are stored Type 1 Vector
Base addr+4
in the Interrupt vector table. Type 2 Vector
Base addr+8

• Interrupt Vector – Is the address of Interrupt Service


Subroutine (ISR)
• Interrupt vector of Interrupt Type N is stored at an
Base addr+4*N Type N Vector
offset N*4 from the base address of Interrupt Vector
Table(IVT)
• If the peripheral device is enabled to generate
Interrupt when some event When Interrupt occurs, NVIC loads
vector to PC and executes the ISR to
provide the service to peripheral
Nested Vectored Interrupt Controller(NVIC)

• If the peripheral device is enabled to generate Interrupt


when some event occurs, the INTR request is sent to
NVIC
• If NVIC is enabled to service the INTR request from the
peripheral, it services the INTR request by executing
ISR pertaining to the peripheral.( i.e Save the return
address, Get the Interrupt Vector from IVT and load that
address to PC. Upon completion of ISR execution
resumes the calling function )
Nested Vectored Interrupt Controller(NVIC)

• In case of Timer, there are 6 INTR enable flags (4 in


MCR and 2 in CCR. i.e 4 Match events and 2 Capture
events can generate the Interrupt when the event
occurs)
• When the event occurs the corresponding bit is set
automatically in the IR register. This indicates the NVIC
about the event
• If the NVIC is enabled to service the Timer Interrupt, it
executes the corresponding ISR and gives the desired
service to the Timer.
• In the ISR, clear the corresponding bit, by writing back 1.
Timer/Counter Interrupt Programming
Interrupt Register (IR)
The Interrupt Register consists of 4 bits for the match interrupts and 2 bits for the capture
interrupts. If an interrupt is generated then the corresponding bit in the IR will be high. Otherwise,
the bit will be low.

Writing a logic one to the corresponding IR bit will reset the interrupt. Writing a zero has no
effect.
Timer/Counter Interrupt Programming
#include<stdio.h>
#include<LPC17xx.h>
unsigned int ticks=0,x;
void TIMER0_IRQHandler(void)
{
LPC_TIM0->IR = 1;
ticks++;
if(ticks==1000)
{
ticks=0;
LPC_GPIO0->FIOPIN=~(LPC_GPIO0->FIOPIN & 0x00000004);
}
}
void init_timer0(void)
{
LPC_TIM0->TCR = 0x00000002; // Timer0 Reset
LPC_TIM0->CTCR =0x00;//Timer
LPC_TIM0->MR0 = 2999; // For 1ms
LPC_TIM0->EMR = 0X00;//Do nothing for EM0
LPC_TIM0->PR = 0;
LPC_TIM0->MCR = 0x00000003; //Reset TC upon Match-0 and generate INTR
LPC_TIM0->TCR = 0x00000001; // Timer0 Enable

return;

Toggle LED connected to p0.2 every second while displaying the status of switch connected to
P1.0 on the LED connected to P2.0
Timer/Counter Interrupt Programming

int main(void)
{
LPC_GPIO0->FIODIR=0x00000004;
LPC_GPIO2->FIODIR=0x00000001;
init_timer0();
NVIC_EnableIRQ(TIMER0_IRQn);//timer 0 intr enabled in NVIC
while(1)
{
LPC_GPIO2->FIOPIN=(LPC_GPIO1->FIOPIN & 0x01) ;
}

}
Timer/Counter Interrupt Programming
Toggle P0.2 whenever counter value reaches 3. I. e for every 4 edges using counter interrupt.
#include<stdio.h>
#include<LPC17xx.h>
void TIMER0_IRQHandler(void)
{
LPC_TIM0->IR = 1; //Clear the interrupt
LPC_GPIO0->FIOPIN=~(LPC_GPIO0->FIOPIN & 0x00000004);

}
void init_timer0(void)
{ LPC_TIM0->TCR = 0x00000002; // Timer0 Reset
LPC_TIM0->CTCR =0x05; // Counter at +ve edge of CAP0.1
LPC_TIM0->MR0 = 3;
LPC_TIM0->EMR = 0X00;
LPC_TIM0->PR = 0;
LPC_TIM0->MCR = 0x00000003;
LPC_TIM0->TCR = 0x00000001; // Timer0 Enable
return;
}

int main(void)
{
LPC_GPIO0->FIODIR=0x00000004;
LPC_PINCON->PINSEL3 |=((3<<22)|(3<<24));
init_timer0();
NVIC_EnableIRQ(TIMER0_IRQn);
while(1);
}
Timer/Counter Interrupt Programming
Timer interrupt for rectangular waveform generation (1.5 second HIGH and 0.5 second LOW)
include<stdio.h>
#include<LPC17xx.h>
unsigned char flag=1;
void TIMER0_IRQHandler(void)
{
LPC_TIM0->IR = 1;

if(flag)
{
flag=0;
LPC_TIM0->TCR = 0x00000002; // Timer0 Reset
LPC_GPIO0->FIOCLR=0x00000004;
LPC_TIM0->MR0 = 500;
LPC_TIM0->TCR = 0x00000001; // Timer0 Enable
}
else
{
flag=1;
LPC_TIM0->TCR = 0x00000002; // Timer0 Reset
LPC_GPIO0->FIOSET=0x00000004;
LPC_TIM0->MR0 = 1500;
LPC_TIM0->TCR = 0x00000001; // Timer0 Enable
}

}
Timer/Counter Interrupt Programming

void init_timer0(void)
{
LPC_TIM0->TCR = 0x00000002; // Timer0 Reset
LPC_TIM0->CTCR =0x00;
LPC_TIM0->MR0 = 1500;
LPC_TIM0->EMR = 0X00;
LPC_TIM0->PR = 3000;
LPC_TIM0->MCR = 0x00000005;
LPC_TIM0->TCR = 0x00000001; // Timer0 Enable
LPC_GPIO0->FIOSET=0x00000004;
return;
}
int main(void)
{
LPC_GPIO0->FIODIR=0x00000004;
init_timer0();
NVIC_EnableIRQ(TIMER0_IRQn);
while(1);
}
External Hardware Interrupts
System Control Block of ARM has SFRs to handle External
Hardware Interrupts.
• Level Triggered- Level 0 or Level 1 triggered
• Edge triggered – Rising Edge or Falling Edge
LPC1768 has four external interrupts EINT0-EINT3

Port Pin PINSEL_FUNC_0 PINSEL_FUN


C_1
P2.10 GPIO EINT0

P2.11 GPIO EINT1


P2_12 GPIO EINT2
P2.13 GPIO EINT3
External Hardware Interrupts

EINT Registers
Register Description
PINSELx To configure the pins as External Interrupts

EXTINT External Interrupt Flag Register contains interrupt flags for


EINT0,EINT1, EINT2 & EINT3.

EXTMODE External Interrupt Mode register(Level/Edge Triggered)

EXTPOLAR External Interrupt Polarity(Falling/Rising Edge, Active


Low/High)
External Hardware Interrupts
EXTINT
31:4 3 2 1 0
RESERVED EINT3 EINT2 EINT1 EINT0

EINTx: Bits will be set whenever the interrupt is detected on the particular interrupt pin.
If the interrupts are enabled then the control goes to ISR.

Writing one to specific bit will clear the corresponding interrupt.

EXTMODE
31:4 3 2 1 0
RESERVED EXTMODE3 EXTMODE2 EXTMODE1 EXTMODE0

EXTMODEx: These bits are used to select whether the EINTx pin is level or edge Triggered

0: EINTx is Level Triggered.


1: EINTx is Edge Triggered.
External Hardware Interrupts

EXTPOLAR
31:4 3 2 1 0
RESERVED EXTPOLAR3 EXTPOLAR2 EXTPOLAR1 EXTPOLAR0

EXTPOLARx: These bits are used to select polarity(LOW/HIGH, FALLING/RISING) of the EINTx
interrupt depending on the EXTMODE register.
0: EINTx is Active Low or Falling Edge (depending on EXTMODEx).
1: EINTx is Active High or Rising Edge (depending on EXTMODEx).

EXTMODEx EXTPOLARx EINTx


0 0 Level 0
0 1 Level 1
1 0 Falling Edge
1 1 Rising Edge
External Hardware Interrupts

Steps to Configure External Hardware Interrupts

• Configure the pins as external interrupts in PINSELx register.


• Configure the EINTx as Edge/Level triggered in EXTMODE register.
• Select the polarity(Falling/Rising Edge, Active Low/High) of the interrupt in
EXTPOLAR register.
• Finally enable the interrputs by calling NVIC_EnableIRQ(EINTx_IRQn)
• Clear the interrupt in EXTINT after entering ISR.
External Hardware Interrupts
Toggle LED connected to P1.23 at each negative edge of the input applied at P2.12 (EINT2, Function-01)

include<LPC17xx.h>
void EINT2_IRQHandler(void);
int main(void)
{

SystemInit();
SystemCoreClockUpdate();

LPC_PINCON->PINSEL4 |= (1<<24); //P2.12 as EINT2 i.e FUNCTION-01


LPC_GPIO1->FIODIR = 0x00800000; //P1.23 is assigned output
LPC_SC->EXTMODE = 0x00000004; //EINT2 is initiated as edge sensitive, 0 for level
LPC_SC->EXTPOLAR = 0x00000000; //EINT2 is falling edge sensitive, 1 for rising edge
NVIC_EnableIRQ(EINT2_IRQn);
while(1) ;
}

void EINT2_IRQHandler(void)
{
LPC_SC->EXTINT = 0x00000004; //clears the interrupt
LPC_GPIO1->FIOPIN = ~ LPC_GPIO1->FIOPIN ;

}
External Hardware Interrupts
Turn ON the LED connected to P1.23 whenever the switch connected to P2.12 (EINT2, Function-01) is
pressed LED remains ON as long as the switch is pressed (Assume, when the switch is pressed Logic-0 is
INPUT.
#include<LPC17xx.h>
void EINT2_IRQHandler(void);
int main(void)
{
LPC_PINCON->PINSEL4 |= (1<<24); //P2.12 as EINT2 i.e FUNCTION-01
LPC_GPIO1->FIODIR = 0x00800000; //P1.23 is assigned output
LPC_SC->EXTMODE = 0x00000000; //EINT2 as level-0 sensitive
LPC_SC->EXTPOLAR = 0x00000000;
NVIC_EnableIRQ(EINT2_IRQn);
while(1) ;

}
void EINT2_IRQHandler(void)
{
LPC_SC->EXTINT = 0x00000004; //clear the interrupt
LPC_GPIO1->FIOSET = 1<<23; //LED ON
for (i=0;i<255;i++);
LPC_GPIO1->FIOCLR = 1<<23; LED OFF

}
External Hardware Interrupts

L
P LED intensity
C P1.23
1
Level-0 input 7
6
+V 8

P2.12
GPIO Interrupts

The pins of Port-0 and Port-2 can generate GPIO interrupts.


GPIO interrupts are mapped to EINT3 ISR
GPIO overall Interrupt Status register (IOIntStatus)
GPIO Interrupts

GPIO Interrupt Enable for port 0 Rising Edge (IO0IntEnR)

Similarly ----- GPIO Interrupt Enable for port 2 (P2.0-P2.13) Rising Edge (IO2IntEnR)
GPIO Interrupts
GPIO Interrupt Enable for port 0 Falling Edge (IO0IntEnF)

Similarly ----- GPIO Interrupt Enable for port 2 (P2.0-P2.13) Falling Edge (IO2IntEnF)
GPIO Interrupts
GPIO Interrupt Status for port 0 Rising Edge Interrupt (IO0IntStatR)

Similarly ----- GPIO Interrupt Status for port 2 (P2.0-P2.13) Rising Edge Interrupt (IO2IntStatR)
GPIO Interrupts
GPIO Interrupt Status for port 0 Falling Edge Interrupt (IO0IntStatF)

Similarly ----- GPIO Interrupt Status for port 2 (P2.0-P2.13) Falling Edge Interrupt (IO2IntStatF)
GPIO Interrupts
GPIO Interrupt Clear register for port 0 (IO0IntClr)

Similarly ----- GPIO Interrupt Clear Register for port 2 (P2.0-P2.13) (IO2IntClr)
GPIO Interrupts
Turn ON the LED connected to P1.23 whenever the +ve edge applied to P0.0 and Turn OFF whenever
the +ve edge applied at P0.1
#include<LPC17xx.h>
void EINT3_IRQHandler(void);
int main(void)
{

SystemInit();
SystemCoreClockUpdate();
LPC_GPIO1->FIODIR = 1<<23; //P1.23 is assigned output
LPC_GPIO1->FIOCLR 1<<23; //Initially LED is kept OFF
LPC_GPIOINT->IO0IntEnR=0x00000003; //P0.0 and P0.1 - Enable Rising Edge
NVIC_EnableIRQ(EINT3_IRQn); //Enable EINT3
while(1) ;
}
void EINT3_IRQHandler(void)
{

unsigned int x=LPC_GPIOINT->IO0IntStatR; // Get the status of Rising Edge Interrupts


LPC_GPIOINT->IO0IntClr |=x; //Clear the Interrupt
if (x==0x00000001) //If Rising Edge at P0.0
LPC_GPIO1->FIOSET = 0x00800000;
else //If Rising edge at P0.1
LPC_GPIO1->FIOCLR = 0x00800000;
GPIO Interrupts
Assume that columns of a 2x2 matrix keyboard are connected to P2.10-P2.11 and rows are connected to P1.0-P1.1.
Write an embedded C program using GPIO interrupt to display the keycode of the key pressed on LEDs connected to
P0.0 to P0.1.

#include <lpc17xx.h>
void EINT3_IRQHandler (void);
unsigned int row, col ;
int main(void)
P2.10
COLUMNS
{ P2.11
LPC_GPIO0->FIODIR =0x03;//p0.0 and p0.1 output
P0.1
LPC_GPIO1->FIODIR =0x03;// p1.0 to p1.2 output
LPC_GPIO1->FIOSET =0x03;// Facilitate any key press detection
LPC_GPIOINT->IO2IntEnR= 1<<10 | 1<<11; //Rising edge- P2.10,P2.11 P0.0
NVIC_EnableIRQ(EINT3_IRQn);//Enable GPIO INTR
while(1);
} P1.0

P1.1
GPIO Interrupts
void EINT3_IRQHandler (void)
{
unsigned int temp3;
temp3 = LPC_GPIOINT->IO2IntStatR; /
if (temp3 == 1<<10)
col = 0;
else if (temp3 == 1<<11)
col = 1;
LPC_GPIOINT->IO2IntClr=1<<10 | 1<<11;//Clear Interrupt
for (row=0;row <2;row++)
{
if (row==0)
temp=0x01;
else if (row==1)
temp=0x02;
LPC_GPIO1->FIOPIN=temp;
if ( (LPC_GPIO2->FIOPIN & (1<<10) | (1<<11)) != 0) //Read the columns
{
LPC_GPIO0->FIOPIN=row *2+col; //Display the keycode
LPC_GPIO1->FIOSET=0x03; // Facilitate any keypress detection
break;
}
}
}
Liquid Crystal Display (LCD) Interfacing

16×2 Liquid Crystal Display which will display the 32


characters at a time in two rows (16 characters in one
row).

•Pin1 (Ground): This is a GND pin of display.


•Pin2 (VCC): This is the voltage supply pin of the display.
•Pin3 (VEE): This pin is used to adjust the contrast by connecting to
a potentiometer.
•Pin4 (Register Select): This pin used to select command or data
register. When RS is 0, Command Register is selected and when
RS=1, Data Register is selected
•Pin5 (Read/Write): This pin is used to select read or write
operation (0 = Write Operation, and 1 = Read Operation).
•Pin 6 (Enable): LOW to HIGH transition at this pin to perform
Read/Write operation
•Pins 7-14 (Data Pins): These pins are used to send
data/command to the display. In 8-bit LCD mode all the pins D7 to
D0 are used. In 4-bit LCD mode only D7-D4 pins are used.
•Pin15 (+ve pin of the LED): This pin is connected to +5V
•Pin 16 (-ve pin of the LED): This pin is connected to GND.
Liquid Crystal Display (LCD) Interfacing

OFF

ON
Liquid Crystal Display (LCD) Interfacing
+V

LPC 1768

P0.27
P0.28

P0.23
P0.24
P0.25
P0.26
Liquid Crystal Display (LCD) Interfacing

#define RS_CTRL 0x08000000 //P0.27


#define EN_CTRL 0x10000000 //P0.28
#define DT_CTRL 0x07800000 //P0.23 to P0.26 data lines

unsigned long int temp1=0, temp2=0,i,j ;


unsigned char flag1 =0, flag2 =0;
unsigned char msg[] = {"WELCOME "}; 3 times send 8-bit mode command, followed by 4-bit mode
void lcd_write(void);
void port_write(void);
void delay_lcd(unsigned int);
unsigned long int init_command[] = {0x30,0x30,0x30,0x20,0x28,0x0c,0x06,0x01,0x80};
int main(void)
{
SystemInit();
SystemCoreClockUpdate();
LPC_GPIO0->FIODIR = DT_CTRL | RS_CTRL | EN_CTRL; //Config output
flag1 =0;//Command
for (i=0; i<9;i++)
{
temp1 = init_command[i];
lcd_write(); //send Init commands to LCD
}
flag1 =1;//Data
i =0;
while (msg[i++] != '\0')
{
temp1 = msg[i];
lcd_write();//Send data bytes
}
while(1);
}
Liquid Crystal Display (LCD) Interfacing
void lcd_write(void)
{
flag2 = (flag1 == 1) ? 0 :((temp1 == 0x30) || (temp1 == 0x20)) ? 1 : 0;//If command is 0x30 (Working in 8-bit mode initially), send ‘3’ on D7-D4 (D3-
D0 already grounded)
temp2 = temp1 & 0xf0;//
temp2 = temp2 << 19;//data lines from 23 to 26. Shift left (26-8+1) times so that higher digit is sent on P0.26 to P0.23
port_write(); // Output the higher digit on P0.26-P0.23
if (!flag2) // Other than command 0x30, send the lower 4-bt also
{
temp2 = temp1 & 0x0f; //26-4+1
temp2 = temp2 << 23;
port_write(); // Output the lower digit on P0.26-P0.23
}
}
void port_write(void)
{
LPC_GPIO0->FIOPIN = temp2;
if (flag1 == 0)
LPC_GPIO0->FIOCLR = RS_CTRL; // Select command register
else
LPC_GPIO0->FIOSET = RS_CTRL; //Select data register

LPC_GPIO0->FIOSET = EN_CTRL; //Apply -ve edge on Enable


delay_lcd(25);
LPC_GPIO0->FIOCLR = EN_CTRL;
delay_lcd(5000);

}
void delay_lcd(unsigned int r1)
{
unsigned int r;
for(r=0;r<r1;r++);
return;
}
Liquid Crystal Display (LCD) Interfacing

1. Interface a matrix keyboard and display the keycode on the LCD.


2. 4- Digit BCD upcounter on LCD
3. Digital Clock on LCD
4. Simulate a Die Tossing on LCD. Use key connected to P2.12 for Die tossing
5. Input an expression of type a operator b from keyboard and display the result
on LCD.
Analog To Digital Converter (ADC)

• Analog to Digital Conversion is used when we want to interface an external analog


signal or when interfacing analog sensors, like for example a temperature sensor.

• The ADC block in LPC1768 Microcontroller is based on Successive Approximation


Register(SAR) conversion method.

• LPC1768 ADC Module uses 12-bit SAR.

• The Measurement range is from VREFN to VREFP, or commonly from 0V to 3.3 V.

• Maximum of 8 multiplexed inputs can be used for ADC.


Analog To Digital Converter (ADC)
Analog voltage and Digital value of the ADC are related as follows:

VA = (VREFP / 2N) (Decimal Equivalent of Digital value)

VA is the Input analog voltage


VREFP Is the Reference voltage of ADC
N is the number of bits

(VREFP / 2N) is Resolution; It is a constant for given value of N and VREFP

Digital output is directly proportional to Analog input voltage

For 3.3 V, N=12, Resolution is 0.805 mV. i.e 0.805 mV change at the
input creates ±1 change at the digital output.
When analog voltage is 0 mV output decimal value is 0
When analog voltage is 0.805 mV output decimal value is 1
When analog voltage is (0.805x2 =1.6) mV output decimal value is 2
When analog voltage is (0.805x3 =2.4) mV output decimal value is 3
When analog voltage is (0.805x4095 = 3.3-0.805) mV output decimal
value is 4095
Analog To Digital Converter (ADC)

ADCR – A/D Control Register

AD0.0 –P0.23 FN 01
AD0.1-P0.24 FN 01
AD0.2-P0.25 FN 01
AD0.3-P0.26 FN 01
AD0.4-P0.30 FN 03
AD0.5-P0.31 FN 03
AD0.6-P0.3 FN 02
AD0.7-P0.2 FN 02
Analog To Digital Converter (ADC)

ADCR – A/D Control Register


Analog To Digital Converter (ADC)
A/D Global Data Register (ADGDR) :

The A/D Global Data Register holds the result of the most recent A/D conversion that has completed, and also includes copies of the status flags
that go with that conversion. Results of ADC conversion can be read in one of two ways. One is to use the A/D Global Data Register to read all data
from the ADC. Another is to use the A/D Channel Data Registers.
Analog To Digital Converter (ADC)

A/D Data Registers (ADDR0 to ADDR7)


The A/D Data Registers hold the result of the last conversion for each A/D channel, when an A/D conversion is
complete. They also include the flags that indicate when a conversion has been completed and when a
conversion overrun has occurred.
Analog To Digital Converter (ADC)
A/D Interrupt Enable register (ADINTEN) : This register allows control over which A/D channels
generate an interrupt when a conversion is complete.
Analog To Digital Converter (ADC)
A/D Status register (ADSTAT) :
The A/D Status register allows checking the status of all A/D channels simultaneously. The DONE and OVERRUN
flags appearing in the ADDRn register for each A/D channel are mirrored in ADSTAT. The interrupt flag (the logical
OR of all DONE flags) is also found in ADSTAT.
Analog To Digital Converter (ADC)
ADC software mode for 2-channel concurrent conversion
#include<LPC17xx.h>
#include<stdio.h>
int main(void)
{
unsigned long temp4, temp5;
unsigned int i;

SystemInit();
SystemCoreClockUpdate();
LPC_PINCON->PINSEL3 = (3<<28) | (3<<30); //P1.30 as AD0.4 and P1.31 as AD0.5
LPC_ADC->ADINTEN = 0;
while(1)
{
LPC_ADC->ADCR = (1<<4)|(1<<21)|(1<<24);//; //ADC0.4, start conversion and operational
while(((temp4=LPC_ADC->ADDR4) & (1<<31)) == 0); //wait till 'done' bit is 1, indicates conversion complete
temp4 = LPC_ADC->ADDR4;
temp4 >>= 4;
temp4 &= 0x00000FFF; //12 bit ADC

LPC_ADC->ADCR = (1<<5)|(1<<21)|(1<<24);// //ADC0.5, start conversion and operational


for(i=0;i<2000;i++); //delay for conversion
while(((temp5=LPC_ADC->ADDR5) & (1<<31)) == 0); //wait till 'done' bit is 1, indicates conversion complete
temp5 = LPC_ADC->ADDR5;
temp5 >>= 4;
temp5 &= 0x00000FFF; //12 bit ADC
//Now you can use temp4 and temp5 for further processing based on your requirement
}
}
Analog To Digital Converter (ADC)
ADC burst mode for 2-channel concurrent conversion
#include<LPC17xx.h>
#include<stdio.h>
int main(void)

{ SystemInit();
SystemCoreClockUpdate();
LPC_PINCON->PINSEL3 =(3<<28)|(3<<30); //P1.30 as AD0.4 and P1.31 as AD0.5
LPC_ADC->ADCR = (1<<4) | (1<<5)|(1<<16) | (1<<21); //Enable CH 4 and 5 for BURST mode with ADC power ON
LPC_ADC->ADINTEN =(1<<4)|(1<<5); // Enable DONE for INTR
NVIC_EnableIRQ(ADC_IRQn);
while(1);
}
void ADC_IRQHandler(void)
{
int channel,temp,result;
channel=(LPC_ADC->ADGDR >>24) & 0x07;
result= ( LPC_ADC->ADGDR >>4) & 0xFFF;
if(channel == 4)
{
temp4 = ( LPC_ADC->ADDR4 >>4) & 0xFFF ; //Read to Clear Done flag
}
else if(channel == 5)
{
temp5 = ( LPC_ADC->ADDR5 >>4) & 0xFFF ; //Read to Clear Done flag
//Now you can use temp4 and temp5 for further processing based on your requirement
}
Analog To Digital Converter (ADC)
Input Analog voltage and display its digital equivalent on LCD
include<LPC17xx.h>
#include<stdio.h>
#define Ref_Vtg 3.300
#define Full_Scale 0xFFF//12 bit ADC
int main(void)
{
unsigned long adc_temp;
unsigned int i; ANALOG INPUT 1.1V
float in_vtg; ADC OUTPUT 555
unsigned char vtg[7], dval[7];
unsigned char Msg3[] = {"ANALOG IP:"};
unsigned char Msg4[] = {"ADC OUTPUT:"};
SystemInit();
SystemCoreClockUpdate();
lcd_init();//Initialize LCD
LPC_PINCON->PINSEL3 |= 3<<30; //P1.31 as AD0.5
LPC_SC->PCONP |= (1<<12);//enable the peripheral ADC
flag1=0;//Command
temp1 = 0x80;//Cursor at beginning of first line TO AD0.5
lcd_write();
flag1=1;//Data
i =0;
while (Msg3[i++] != '\0')
{
temp1 = Msg3[i];
lcd_write();//Send data bytes
}
onverter (ADC)
flag1=0; //Command
temp1 = 0xC0;//Cursor at beginning of second line
lcd_write();
flag1=1;
i =0;
while (Msg4[i++] != '\0')
{
temp1 = Msg4[i];
lcd_write();//Send data bytes
}
while(1)
{
LPC_ADC->ADCR = (1<<5)|(1<<21)|(1<<24);//ADC0.5, start conversion and operational
while(((adc_temp=LPC_ADC->ADGDR) & (1<<31)) == 0);
adc_temp = LPC_ADC->ADGDR;
adc_temp >>= 4;
adc_temp &= 0x00000FFF; //12 bit ADC
in_vtg = (((float)adc_temp * (float)Ref_Vtg))/((float)Full_Scale); //calculating input analog voltage
sprintf(vtg,"%3.2fV",in_vtg); //convert the readings into string to display on LCD
sprintf(dval,"%x",adc_temp);
flag1=0;;
temp1 = 0x8A;
lcd_write();
flag1=1;
i =0;
while (vtg[i++] != '\0')
{
temp1 = vtg[i];
lcd_write();//Send data bytes
}
onverter (ADC)

flag1=0;
temp1 = 0xCB;
lcd_write();
flag1=1;
i =0;
while (dval[i++] != '\0’)
{
temp1 = dval[i];
lcd_write();//Send data bytes
}
for(i=0;i<7;i++)
vtg[i] = dval[i] = 0;
}
}

You might also like