Unit III AP Part 1 Interfacing
Unit III AP Part 1 Interfacing
Introduction
Current passing through the LED should be
limited to maximum forward current (IF).
To find the value of this resistor, we use the
forward voltage and forward current.
We design a circuit with 5V supply and forward
current of 20mA. We use a red LED here.
R = (5 – 2)/10mA = 300 ohms(Use 270 ohms)
2
1.A Interfacing with LEDs
LED pin connections & interfacing diagram
3
1.A Interfacing with LEDs
ALGORITHM Flowchart
1. Initialize the ports of LPC 2148.
2. Make the port lines high to glow LED.
3. Call delay.
4. Make the port lines low to glow LED off.
5. Call delay.
6. Go to step 2.
4
1.A Embedded C code for LED Interfacing
#include<lpc21xx.h> // header file
LCD16x2 6
1.B 16 X 2 LCD Interfacing
7
1.B 16 X 2 LCD Interfacing
LCD Commands
LCD16x2.
8
1.B 16 X 2 LCD Interfacing
LCD Interfacing Details
16x2 LCD
Description Signal
EN W/R
LCD LPC21xx
D0 ~D7 RS
LCD D0 P0.12
LCD D1 P0.13
P0.12
LCD D2 P0.14
P0.19 LCD D3 P0.15
LCD D4 P0.16
P1.20
LCD D5 P0.17
P1.21 LCD D6 P0.18
LCD D7 P0.19
LPC 2148 LCD RS P1.20
Fig.- Interfacing of LPC-2148 with 16x2 LCD LCD EN P1.21 9
Timing Diagram
• RS=0 • RS=1
• Command • Data
• En=1 • En=1
• DELAY • DELAY
• EN=0 • En=0
16
1.C 4 X 4 Keypad Interfacing: Grounding &
Reading Columns
17
1.C 4 X 4 Keypad Interfacing: Grounding &
Reading Columns .. cont
18
/* Keypad Connection:
P0.2,P0.3,P0.4,P0.5 ---Col---Read Lines
P0.6,P0.7,P0.8,P0.9 ---Row---Scan Lines
LCD: RS---P0.28; EN---P0.29
Data--P1.16 to P1.23 */ 19
1.C 4 X 4 Keypad Interfacing: Code
#include<lpc21xx.h> IO0PIN=0x00000380; // First Scan Line(row),
void lcdcmd(unsigned int);
//R1=0,R2=R3=R4=1
void lcddata(unsigned int);
void delay_lcd(void); if(( IO0PIN & 0x0000003C)!= 0x0000003C))
int main(void) {
{ IOCLR0=0x10000000; IOSET0=0x10000000;
22
Keypad(4x3)and LCD interfacing with ARM7 (1)
23
Keypad(4x3)and LCD interfacing with ARM7 (2)
24
Keypad(4x3)and LCD interfacing with ARM7 (3)
char keypad(void) //Keypad Scan
{ IOCLR1|=(1<<20);
while(1) IOSET1|=(1<<21)|(1<<22)|(1<<19);
{ if(!(IOPIN1&(1<<16)))
IOCLR1|=(1<<19); //Making row1 LOW {
IOSET1|=(1<<20)|(1<<21)|(1<<22); //Making rest of the while(!(IOPIN1&(1<<16)));
rows '1' return '4';
if(!(IOPIN1&(1<<16))) //Scan for key press }
{ if(!(IOPIN1&(1<<17)))
while(!(IOPIN1&(1<<16))); {
return '1'; //Returning value to display while(!(IOPIN1&(1<<17)));
} return '5';
if(!(IOPIN1&(1<<17))) }
{ if(!(IOPIN1&(1<<18)))
while(!(IOPIN1&(1<<17))); {
return '2'; while(!(IOPIN1&(1<<18)));
} return '6';
if(!(IOPIN1&(1<<18))) }
{
while(!(IOPIN1&(1<<18)));
return '3';
} 25
Keypad(4x3)and LCD interfacing with ARM7 (4)
IOCLR1|=(1<<21); IOCLR1|=(1<<22);
IOSET1|=(1<<22)|(1<<20)|(1<<19); IOSET1|=(1<<19)|(1<<20)|(1<<21);
if(!(IOPIN1&(1<<16))) if(!(IOPIN1&(1<<16)))
{ {
while(!(IOPIN1&(1<<16))); while(!(IOPIN1&(1<<16)));
return '7'; return '*';
} }
if(!(IOPIN1&(1<<17))) if(!(IOPIN1&(1<<17)))
{ {
while(!(IOPIN1&(1<<17))); while(!(IOPIN1&(1<<17)));
return '8'; return '0';
} }
if(!(IOPIN1&(1<<18))) if(!(IOPIN1&(1<<18)))
{ {
while(!(IOPIN1&(1<<18))); while(!(IOPIN1&(1<<18)));
return '9'; return '#';
} }
}
} 26
Interfacing KS0108 based JHD12864E Graphics LCD
• There are many displays out there based on KS0108 or compatible
display controller.
• We will look at the working of the display, the hardware setup and
programming with ARM7 (LPC2148).
28
Internal block diagram of a KS0108B
(NT7108C) based 128x64 pixel GLCD module
29
Internal block diagram of a KS0108B
(NT7108C) based 128x64 pixel GLCD module
30
Explanation
The two halves of the display can be individually accessed through the chip select
pins (CS1 and CS2) of the two NT7108C drivers. Each half consists of 8 horizontal
pages (0-7) which are 8 bits (1 byte) high. This is illustrated in the drawing below.
Y6-Y1:Line Selection
34
Display Control Instructions
Set Y Address
Y Address of display data RAM is set in
Y Address counter. Whenever
write/read operation is executed, Y
Address counter gets automatically
incremented by one.
Set X Address (Page)
X Address (page) of display data RAM
needs to be set in X Address Register.
Here, user needs to increment page
through program after every Y Address
overflow occurs on current page. Note
that if X Address (page) is not
incremented after Y Address overflow
occurs, then overwrite will occur on
current page
Set Display Start Line (Z Address)
Z Address of display data RAM is set in
Display Start Line Register. It is used
to set row (0 - 63) line from where data
will be displayed on screen. Usually it
GLCD_Command(0x3E); /* Display OFF */ sets to 0 i.e. data will be displayed from
0th (first line) top start line.
GLCD_Command(0x3F); /* Display ON */
Writing to GLCD 128x64
Above figure shows X (Row) and Y (Column) address to select location for data
write. Two controllers control each half of display.
Each half of display has vertical 64 pixels‟ addresses which are accessed by Y
address from 0x40 to 0x7F. These two controllers alternatively selected by
CS1 and CS2 pins to select either half of display for write,
To select left half of display we need to make CS1 = 1, CS2 = 0.
To select right half of display we need to make CS1 = 0, CS2 = 1.
X address is shown in above figure as page 0 – 7. Each page contains 8 rows. 36
Display Data on GLCD
To display data on LCD we need to write it on LCD data pins.
After providing proper address we can send data to GLCD.
Data value is directly mapped by pixels to glow. Accordingly, we need to send data.
37
GLCD Interfacing Details Description Signal
LCD LPC2148
D0 P0.12
D1 P0.13
D2 P0.14
D3 P0.15
D4 P0.16
D5 P0.17
D6 P0.18
D7 P0.19
RS P1.20
EN P1.21
CS1 P1.22
CS2 P1.23
GRST P1.24
RW P1.25
38
Steps to program GLCD:
Initialization
To initialize the display, we need to do steps given below,
- Send Display OFF command i.e. 0x3E
- Send Y address e.g. here 0x40 (Start address).
- Send X address (Page) e.g. here 0xB8 (Page0).
- Send Z address (Start line) e.g. here 0xC0 (from 0th line).
- Now send Display ON command i.e. 0x3F
• “An interrupt is a signal sent to the CPU which indicates that a system event has
a occurred which needs immediate attention“
• An „Interrupt ReQuest„ i.e an „IRQ„ can be thought of as a special request to the CPU
to execute a function when an interrupt occurs.
44
Introduction to Interrupts
Vectored and Non-Vectored Interrupts
The term „Vectored„ means that the CPU is aware of the address of the ISR
when interrupt occurs and Non-Vectored means that CPU doesn‟t know the
address of the ISR and it needs to be supplied.
1. For the Vectored Stuff , the System maintains a table called IVT or
Interrupt Vector Table which has ISR addresses.
2. On the other hand Non-Vectored ISRs doesn‟t point to a unique ISR and
instead CPU needs to be supplied with the address of the „default‟ or
say.. a „common‟ ISR.
3. In LPC214x this is given by a register called „VICDefVectAddr„. The user
must assign the address of the default ISR.
45
Interrupts in LPC214x
Interrupts in LPC214x are handled by Vectored Interrupt
Controller(VIC)
Features of VIC
46
Vector Interrupt Controller (VIC) Features
VIC accepts 32 interrupt request inputs, LPC 2148 uses 22 of 32 interrupts
• Interrupts are classified into 3 categories:
1. Fast Interrupt Request i.e FIQ : which has highest priority
2. Vectored Interrupt Request (IRQ): which has „middle‟ priority between
FIQ and Non-Vectored IRQ.
3. Non-Vectored IRQ : which has the lowest priority.
Note:
The programmable assignment scheme means –
Priorities of interrupts from the various peripherals can be dynamically assigned
and adjusted.
OR
Any of the 22 interrupts can be assigned to FIQ/ VIRQ/ NVIRQ 47
Vector Interrupt Controller (VIC) Features
Therefore we can classify Interrupts into two types :
1. Fast IRQs or FIQs
2. Normal IRQs or IRQs which can be further classified as : Vectored IRQ and
Non-Vectored IRQ.
48
Vector Interrupt Controller (VIC)
Features:
VIRQ & NVIRQ:
1. VIC has 16 VIRQ slots, Slot0 – Slot15. Only 16 of the 32 requests can
be assigned to this category. Any IRQ configured interrupts can
be assigned IRQ slots.
2. Slot 0 has the highest priority and slot 15 has the lowest.
3. Interrupts configured as IRQ, not assigned as VIRQ slot, is
assigned as NVIRQ
4. VIRQ & NVIRQ interrupts are combined and VICIRQ is
generated.
• The VIC ORs the requests from all vectored and non-
vectored IRQs to produce the IRQ signal .
• If any of the vectored IRQs are requesting, the VIC provides
the address of the highest-priority requesting IRQs service
routine, otherwise it provides the address of a default
routine that is shared by all the non-vectored IRQs.
• All registers in the VIC are word registers.
50
Table 1
VIC has plenty of registers. Most of the registers that are used to configure interrupts.
Each bit corresponding to a particular interrupt source and this mapping is same for all
of these registers.
What I mean is that bit 0 in these registers corresponds to Watch dog timer interrupt ,
bit 4 corresponds to TIMER0 interrupt .. and so on.
51
LPC2148 Interrupt Related Registers or VIC
Registers
Now we will have a look at some of the important Registers that are used to implement
interrupts in lpc214x:
52
1. Interrupt Select register
(VICIntSelect - 0xFFFF F00C)
• 0 = The interrupt request with this bit number is assigned to the IRQ category.
• 1 = The interrupt request with this bit number is assigned to the FIQ category.
53
2. Interrupt Enable register
(VICIntEnable - 0xFFFF F010)
During read, 1s indicate interrupt requests or software interrupts that are enabled to
contribute to FIQ or IRQ.
During write, ones enable interrupt requests or software interrupts to contribute to FIQ or
IRQ, zeroes have no effect. 54
3. Interrupt Enable Clear register
(VICIntEnClear - 0xFFFF F014)
• This register allows software to clear one or more bits in the Interrupt
Enable register.
• 0= No effect;
• 1= Clears the corresponding bit in the Interrupt Enable register, thus disabling interrupts
for this request.
55
4. IRQ Status register
(VICIRQStatus - 0xFFFF F000)
.
• This register reads out the state of those interrupt requests that are
enabled and classified as IRQ.
• It does not differentiate between vectored and
non-vectored IRQs
56
5. FIQ Status register
(VICFIQStatus - 0xFFFF F004)
• This is a read only register. This register reads out the state of those interrupt requests
that are enabled and classified as FIQ.
• If more than one request is classified as FIQ, the
FIQ service routine can read this register to see which request(s) is (are) active.
57
6. Software Interrupt register
(VICSoftInt - 0xFFFF F018)
• The contents of this register are ORed with the 32 interrupt requests from
the various peripherals.
58
7. Software Interrupt Clear register
(VICSoftIntClear - 0xFFFF F01C)
• This register allows software to clear one or more bits in the Software
Interrupt register.
60
10. Vector Address registers 0-15
(VICVectAddr0-15 - 0xFFFF F100-13C)
• These are a read/write accessible registers. These registers
hold the addresses of the Interrupt Service routines (ISRs) for
the 16 vectored IRQ slots.
61
11. Default Vector Address register
(VICDefVectAddr - 0xFFFF F034)
• This is a read/write accessible register. This register
holds the address of the Interrupt Service routine (ISR)
for non-vectored IRQs.
62
12. Protection Enable register
(VICProtection - 0xFFFF F020)
63
UNIT III
Unit III : Real World Interfacing with ARM7 Based
Microcontroller -1
1. Interrupt structure of LPC2148
2. Interfacing with LED, LCD, GLCD, KEYPAD
3. Simple LPC2148 GPIO Programming examples
73
Match Registers
r What is a Match Register anyways ?
r Ans: A Match Register is a Register which contains a
specific value set by the user.
r When the Timer starts – every time after TC is
incremented the value in TC is compared with match
register.
r If it matches then it can Reset the Timer or can
generate an interrupt as defined by the user.
r Match Registers can be used to:
m Stop Timer on Match and trigger an optional interrupt.
m Reset Timer on Match and trigger an optional interrupt.
m To count continuously and trigger an interrupt on match.
74
Timer Registers
MCR : Match Control register – This register is used to
control which all operations can be done when the value in MR
matches the value in TC. Bits 0,1,2 are for MR0 , Bits
3,4,5 for MR1 and so on. Heres a quick table which shows
the usage:
For MR0:
m Bit 0 : Interrupt on MR0 i.e trigger an interrupt when MR0 matches TC.
Interrupts are enabled when set to 1 and disabled when set to 0.
m Bit 1 : Reset on MR0. When set to 1 , TC will be reset when it matched
MR0. Disabled when set to 0.
m Bit 2 : Stop on MR0. When set to 1 , TC & PC will stop when MR0 matches
TC.
m Similarly bits 3-5 , 6-8 , 9-11 are for MR1 , MR2 , MR3
respectively.
75
Timer…
Pin description…
76
Capture Registers
r What are Capture Registers ?
Ans: As the name suggests it is used to Capture
Input signal.
When a transition event occurs on a Capture pin , it
can be used to copy the value of TC into any of the
4 Capture Register or to generate an Interrupt.
• . CAP0.3..0 & CAP1.3..0 (Input)
Capture Signals- A transition on a this (capture)
pin is found, it loads one of the Capture Registers
with the value in the Timer Counter and optionally
generate an interrupt.
77
Timer…
Pin description…
List of all CAPTURE signals, together with pins
on where they can be selected:
Capture 0:
• CAP0.0 (3 pins) : P0.2, P0.22 and P0.30
• CAP0.1 (1 pin) : P0.4
• CAP0.2 (3 pin) : P0.6, P0.16 and P0.28
• CAP0.3 (1 pin) : P0.29
Capture 1:
• CAP1.0 (1 pin) : P0.10
• CAP1.1 (1 pin) : P0.11
• CAP1.2 (2 pins) : P0.17 and P0.19
• CAP1.3 (2 pins) : P0.18 and P0.21 78
Timer Calculations
1. The delay or time required for 1 clock cycle at „X‟ MHz is given
by :
3. Similarly when we set PR = 59999 the delay in this case will be:
Prof: M. N. Kakatkar
Sinhgad College of Engineering, Pune
Email: [email protected]
Introduction to UART Basics
In this topic we will go through the basics of
UART(SERIAL) programming for LPC214x family of
microcontrollers.
Introduction
Computers transfer data in two ways: Parallel and Serial.
Parallel: Eight or more data lines, few feet only, short time
Serial: Single data line, long distance
The PIC18 has serial communication capability built into it.
Basics of Serial Communication
The byte of data must be converted to serial bits using a
parallel-in-serial-out shift register
Transfers a block of
Transfers a single byte at data at a time
a time
Half-and Full-Duplex Transmission
Start and Stop Bits
In the asynchronous method, each character is placed
between start and stop bits (framing)
MSB LSB
DB-25 DB-9
25-Pin Connector 9-Pin Connector
RS232 Pins (cont‟d)
Bits [7 to 6] : Used to determine that how many UART0 Rx FIFO characters must be written
before an interrupt is activated.
Bit [1 to 0] – Word Length Select : Used to select the length of an individual data chunk. [00] for 5 bit
character length. Similarly [01] , [10] , [11] for 6 , 7 , 8 bit character lengths respectively.
Bit 2 – Stop bit select : 0 for using 1 stop bit and 1 for using 2 stop bits.
Bit 3 – Parity Enable : 0 to disabled Partiy generation & checking and 1 to enable it.
Bit [5 to 4] – Parity Select : [00] to Odd-parity , [01] for Even-parity , [10] for forced
“1”(Mark) parity and [11] for forced “0”(Space) parity.
Bit 6 – Break Control : 0 to disable break transmission and 1 to enable it. TxD pin will be
forced to logic 0 when this bit is 1
Bit 7 – Divisior Latch Access bit : 0 to disable access to divisor latches and 1 to enable access.
3) U0LSR – Line Status Register : used to read the status of Rx and Tx
blocks.
Bit 0 – Receiver Data Ready(RDR) : 0 means U0RBR is empty(i.e Rx FIFO is empty) and 1 means
U0RBR contains valid data.
Bit 1 – Overrun Error(OE) : 0 means Overrun hasn‟t occured and 1 means Overrun has occured.
Overrun is the condition when RSR(Receive Shift Register)[See note 1] has new character assembled
but the RBR FIFO is full and the new assembled character is eventually lost since no data is written
into FIFO if its full. (Note: Reading U0LSR clears this bit)
Bit 2 – Parity Error(PE) : 0 mean no parity error and 1 mean a parity error has occured. When the
value of the parity bit in the recieved character is in wrong state then a parity error occurs. (Note:
Reading U0LSR clears this bit)
Bit 3 – Framing Error(FE) : 0 means no framing error has occured and 1 means that a framing error has
taken place. Framing error occurs when the stop bit of a received character is zero. (Note: Reading
U0LSR clears this bit)
Bit 4 – Break Interrupt : 0 means no Break Interrupt occurs and 1 means that it has occurred. A
Break Interrupt occurs when the RxD line is pulled low (i.e all 0s) i.e held in spacing state for 1 full
character after which Rx Block goes into Idle state. Rx Block gets back to active state when RxD pin
is pulled high (i.e all 1s) i.e held in marking state for 1 full character. (Note: Reading U0LSR clears this
bit)
Bit 5 – Transmit Holding Register Empty(THRE) : 0 means U0THR contains valid data and 1 means
its empty
Bit 6 – Transmitter Empty (TEMT) : 0 means U0THR and/or U0RSR contains valid data and 1 means
that both U0THR and U0RSR are empty.
Bit 7 – Error in RX FIFO(RXFE) : 0 means that U0RBR has no Rx Errors or Rx FIFO is disabled(i.e
0th bit in U0FCR is 0) and 1 means that U0RBR has atleast one error. (Note: This bit is cleared only if
U0LSR is read and there are no other subsequent errors in Rx FIFO .. else this bit will stay 1)
4) U0TER – Transmit Enable Register : This register is used
to enable UART transmission.
Reserved 7 6 5 4 3 2 1 0
This register actually holds to different 4-bit values (a divisor and a multiplier) for prescaling which are:
Remark from User manual : “If the fractional divider is active (DIVADDVAL > 0) and DLM = 0, the
value of the DLL register must be 2 or greater!”
UART Baud Rate Generation:
Note :
In real world there are very less chances that you will get
the actual baudrate same as the desired baudrate.
In most cases the actual baudrate will drift a little above
or below the desired baud and also, as the desired
baudrate increases this drift or error will also increase –
this is because of the equation itself and the
limitations on MULVAL , DIVADDVAL! For e.g. if the
desired baud rate is 9600 and you get a baud like 9590 ,
9610 , 9685 , 9615 , etc.. then in almost all cases it will
work as required.
In short , a small amount of error in actual baudrate is
generally tolerable in most systems.
The formula for calculating baud
rate is given as :
As it can been seen this formula has 2 prominent parts which are : A Base value and a Fractional
Multiplier i.e:
BaudRate = [ Base ] x [ Fraction(i.e. Prescale) ]
This Fractional Multiplier can be used to scale down or keep the base value as it is .. hence its very useful
for fine-tuning and getting the baudrate as accurate as possible.
Examples for calculating Baud Rates
1. We start with DLM=0 , DIVADDVAL=0 and MULVAL=1 and get an
initial value for DLM.
2. We will get a very close baudrate to the desired one. (if we are
lucky)
3. If not then we perform some finetuning using DLM , MULVAL and
DIVADDVAL and get a new value for DLM
U0DLL = = 195.3125
We get U0DLL = 195.3125 , since it must be an integer we use 195.
With U0DLL = 195 our actual baud rate will Now all we have to do is to get this error as low
be = 9615.38 with an error of +15.38 from as possible.
9600.
First lets compute the required fraction given This can be done by multiplying it by a suitable
as: fraction defined using MULVAL and
[Desired Baud / Actual Baud Rate]. = 9600/ DIVADDVAL – as given in equation.
9615.38 = 0.9984.
Now What?
The closest downscale value possible(<1) to
In short this is the value i.e 0.9984 that 0.9948 is 0.9375 when MULVAL=15 &
needs to be multiplied by baud value we got DIVADDVAL=1.
above to bring back it to ~9600. But 0.9375 is too less because
9615.38x0.9375 = 9014.42 which is way too
far.
This the value
we use
Luckily when U0DLL = 183 we get a baud rate of Now , one thing can be done is that we set
9605.53 i.e ~9605! Yea! MULVAL = 15 and now start decreasing U0DLL
from 195
For PCLK = 30Mhz and Required Baudrate is
9600 bauds for UART0
U0DLL = 183
U0DLM = 0
MULVAL = 15
DIVADDVAL = 1
Examples for calculating Baud Rates
Ex 2 : PCLK = 60Mhz and Required Baudrate is 9600 bauds for UART0
Initially we made U0DLM „0‟, Now set U0DLM = 1 to get U0DLL in range
With U0DLL = 135 our actual baud rate will Now all we have to do is to get this error as low
be = 9590.79 with an error of -9.21 from as possible.
9600.
First lets compute the required fraction given Let us say this can be done by multiplying it by
as: a suitable fraction defined using MULVAL and
[Desired Baud / Actual Baud Rate]. = 9600/ DIVADDVAL – as given in equation.
9590.79 = 1.0009
Now What?
The fractional value we need to multiply our
current baud to get back to ~9600 is 1.0009 but Hence, in such case we have to use MULVAL =
that‟s out of range(>1) for a factor 15 and find new value for U0DLL..
Here we get U0DLL = 110 which gives a baud of Now , one thing can be done is that we set
9605.53 which is ~9605! MULVAL = 15 and now start decreasing U0DLL
from 135
For PCLK = 60Mhz and Required Baudrate is
9600 bauds for UART0
U0DLL = 110
U0DLM = 1
MULVAL = 15
DIVADDVAL = 1
Configuring and Initializing UART
We will use the following configuration for UART0:
• BaudRate = 9600 buads (with PCLK=15Mhz)
• Data Length = 8 bits
• No Parity Bit
• and 1 Stop Bit
In order to get bauds at 15Mhz PCLK we must use the following settings for
baud generation :
RxD TxD
PINSEL0 =x05
/* 8 bits, no Parity, 1 Stop bit, DLAB set to 1 */
//We now use Line Control Register (U0LCR) to configure the UART block (i.e the
data format used in transmission)
long int n;
PINSEL0 = 0x00000005; //set line ctrl reg. for UART0 with above parameters
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock*/