Interrupts LCD and ADC
Interrupts LCD and ADC
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
EINT Registers
Register Description
PINSELx To configure the pins as External Interrupts
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.
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
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).
include<LPC17xx.h>
void EINT2_IRQHandler(void);
int main(void)
{
SystemInit();
SystemCoreClockUpdate();
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
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)
{
#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
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
}
void delay_lcd(unsigned int r1)
{
unsigned int r;
for(r=0;r<r1;r++);
return;
}
Liquid Crystal Display (LCD) Interfacing
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)
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)
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)
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
{ 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;
}
}