Vous êtes sur la page 1sur 133

Advanced Processors

Unit III : Real World Interfacing with ARM7


Based Microcontroller-1
Course Objectives:
● To understand need and application of ARM Microprocessors in
embedded system.
● To learn interfacing of real world input and output devices.

Course Outcomes:
On completion of the course, student will be able to
● Describe the ARM microprocessor architectures and its feature.
● Interface the advanced peripherals to ARM based microcontroller.
Outline:
1. Interrupt structure of LPC2148
2. Interfacing with LED, LCD
3. Interfacing with KEYPAD, GLCD
4. Simple LPC2148 GPIO Programming examples Using timers of
LPC2148 to generate delay
5. Serial communication programming for transmission and reception
from computer
6. Programming for UART
Outline:
1. Interrupt structure of LPC2148
2. Interfacing with LED, LCD
3. Interfacing with KEYPAD, GLCD
4. Simple LPC2148 GPIO Programming examples Using timers of
LPC2148 to generate delay
5. Serial communication programming for transmission and reception
from computer
6. Programming for UART
 Interrupt : “An interrupt is a signal sent to the CPU which
indicates that a system event has a occurred which needs
immediate attention”.
 Interrupt ReQuest (IRQ) can be thought of as a special
request to the CPU to execute a function(small piece of code)
when an interrupt occurs.
 ISR : This function or ‘small piece of code’ is technically
called an ‘Interrupt Service Routine‘ or ‘ISR’.
 So when an IRQ arrives to the CPU, it stops executing the
current code and start executing the ISR. After the ISR
execution has finished the CPU gets back to where it had
stopped.
Interrupts are handled by Vectored Interrupt Controller(VIC)
LPC214x VIC
Features
● ARM PrimeCell Vectored Interrupt Controller
● 32 interrupt request inputs
● 16 vectored IRQ interrupts
● 16 priority levels dynamically assigned to interrupt requests
● Software interrupt generation
Interrupt structure of LPC2148
 The VIC is a component from the ARM prime cell.
 VIC module is a highly optimised interrupt controller.
 The VIC is used to handle all the on-chip interrupt sources from
peripherals.
 Each of the on-chip interrupt sources is connected to the VIC on a fixed
channel: your application software can connect each of these channels to
the CPU interrupt lines (FIQ, IRQ) in one of three ways.
Interrupt structure of LPC2148
 The VIC allows each interrupt to be handled as an FIQ interrupt, a
vectored IRQ interrupt, or a non vectored IRQ interrupt.
 FIQ is the fastest followed by vectored IRQ with non-vectored IRQ
being the slowest.
❖ LPC2148 external interrupt inputs: 4
❖ Processor and on-chip user peripherals generate interrupts
❖ LPC2148 uses ARM PrimeCell (PL190) Vectored Interrupt
Controller for managing interrupts.
❖ When interrupt occurs,
➔ VIC identifies the source of interrupts
➔ Passes requests on interrupt request pins as per the configuration
➔ If more than one interrupt occurs at a time, VIC resolves priority
❖ 32 interrupt request inputs, LPC2148 uses 22 of 32 interrupts
Block diagram of the Vectored Interrupt Controller (VIC)
ARM PrimeCell Vectored Interrupt Controller (PL190)
SFRs
• VICIntSelect (R/W) 0 = IRQ, 1 = FIQ
• VICIntEnable (R/W) Enable Selective Interrupt Source
• VICIntEnClr (R/W) Disable Selective Interrupt Source
• VICIRQStatus (R)to know the status of enabled interrupt
• VICFIQStatus (R)to know the status of enabled FIQ
• VICSoftInt to trigger a software interrupt
• VICSoftIntClear to clear software interrupt
• VICVectCntl0 to VICVectCntl15 Assign interrupt source
• VICVectAddr0 to VICVectAddr15 Assign interrupt address
• VICVectAddr Holds the address of currently active interrupt
• VICDefVectAddr Holds the address of Non-Vectored ISR
Fast Interrupt reQuest (FIQ)
❖ FIQ requests have the highest priority.
❖ Any interrupt source may be assigned as the FIQ interrupt.
❖ The VIC interrupt select register has a unique bit for each
interrupt.
❖ However setting multiple bits in the Interrupt Select Register will
enable multiple FIQ interrupt sources.
❖ On entry the interrupt source can be determined by examining the
VIC FIQ Status register and the appropriate code executed.
❖ Once you have selected an FIQ source the interrupt can be
enabled in the VIC interrupt enable register.
❖ Once an FIQ interrupt is generated, the processor will change to
FIQ mode and vector to 0x0000001C, the FIQ vector.
Leaving An FIQ Interrupt
❖ Before you exit the ISR code you must make sure that any
interrupt status flags in the peripheral have been cleared.
❖ If this is not done you will get continuous interrupts until the flag
is cleared.
Vectored IRQ
❖ Vectored IRQs have the middle priority, but only 16 of the 32
requests can be assigned to this category.
❖ The VIC provides a programmable hardware lookup table which
delivers the address of the C function to run for a given interrupt
source.
❖ Any of the 32 requests can be assigned to any of the 16 vectored
IRQ slots, among which slot 0 has the highest priority and slot 15
has the lowest.
❖ Each slot contains a vector address register and a vector control
register.
❖ For a Vectored IRQ the VIC provides a hardware lookup table for
the address of each ISR. The interrupt priority of each peripheral
may also be controlled.
❖ The Vector Control Register contains two fields: a channel field
and an enable bit.
❖ The other register in the VIC slot is the Vector Address Register.
❖ As its name suggests, this register must be initialised with the
address of the appropriate C function to run when the interrupt
associated with the slot occurs.
Leaving An IRQ Interrupt
❖ The interrupt status flags are cleared in the peripheral which
generated the request.
❖ At the end of the interrupt you must do a dummy write to the
Vector Address Register.
❖ This signals the end of the interrupt to the VIC and any pending
IRQ interrupt will be asserted.
Non-Vectored Interrupts
❖ The VIC is capable of handling 16 peripherals as vectored
interrupts and at least one as an FIQ interrupt.
❖ If there are more than 17 interrupt sources on the chip, any extra
interrupts can be serviced as non-vectored interrupts.
❖ The non-vectored interrupt sources are served by a single ISR.
❖ The address of this ISR is stored in an additional vector address
register called the default vector address register.
Leaving A Non-Vectored IRQ Interrupt
❖ As with the vectored IRQ interrupt, you must clear the peripheral
flag and write to the vector address register.
Important Note
 Each Peripheral in lpc2148 has only 1 IRQ associated with it.
 But inside each device there may be different sources which can raise an
interrupt.
 Like the TIMER0 peripheral has 4 match + 4 capture registers and any one
or more of them can be configured to trigger an interrupt.
 Hence such devices have a dedicated interrupt register which contains a
flag bit for each of these source(For Timer block its ‘T0IR’).
 So , when the ISR is called first we need to identify the actual source of the
interrupt using the Interrupt Register and then proceed accordingly.
 Also just before , when the main ISR code is finished we also need to
acknowledge or signal that the ISR has finished executing for the current
IRQ which triggered it.
 This is done by clearing the flag(i.e the particular bit) in the device’s
interrupt register and then by writing a zero to VICVectAddr register
which signifies that interrupt has ISR has finished execution successfully.
Outline:
1. Interrupt structure of LPC2148
2. Interfacing with LED, LCD
3. Interfacing with KEYPAD, GLCD
4. Simple LPC2148 GPIO Programming examples Using timers of
LPC2148 to generate delay
5. Serial communication programming for transmission and
reception from computer
6. Programming for UART
LPC214x GPIO
LPC214x Pin connect block
● The purpose of the Pin connect block is to configure the
microcontroller pins to the desired functions.
● The pin connect block allows selected pins of the
microcontroller to have more than one function.
● Configuration registers control the multiplexers to allow
connection between the pin and the on chip peripherals.
● Peripherals should be connected to the appropriate pins prior to
being activated, and prior to any related interrupt(s) being
enabled.
● Selection of a single function on a port pin completely excludes
all other functions otherwise available on the same pin.
● The only partial exception from the above rule of exclusion is the
case of inputs to the A/D converter.
● The Pin Control Module contains registers as shown in Table
below.
Pin function Select register 0 (PINSEL0 - 0xE002 C000)
● The PINSEL0 register controls the functions of the pins as per
the settings listed in Table 40.
● The direction control bit in the IO0DIR register is effective only
when the GPIO function is selected for a pin.
● For other functions, direction is controlled automatically.
● PINSEL0 : PINSEL0 is used to configure PORT0 pins P0.0 to
P0.15.
Pin function Select register 1 (PINSEL1 - 0xE002 C004)
● The PINSEL1 register controls the functions of the pins as per the
settings listed in table.
● The direction control bit in the IO0DIR register is effective only
when the GPIO function is selected for a pin.
● For other functions direction is controlled automatically.
● PINSEL1 : PINSEL1 is used to configure PORT0 pins P0.16 to
P0.31.
Pin function Select register 2 (PINSEL2 - 0xE002 C014)
● The PINSEL2 register controls the functions of the pins as per the
settings listed in Table.
● The direction control bit in the IO1DIR register is effective only
when the GPIO function is selected for a pin.
● For other functions direction is controlled automatically.
● PINSEL2 : PINSEL2 is used to configure PORT1 pins P1.16 to
P1.31.
Pin function select register values
● The PINSEL registers control the functions of device pins as
shown below.
● Pairs of bits in these registers correspond to specific device pins.
LPC214x GPIO
Features
● Every physical GPIO port is accessible via either the group of
registers providing an enhanced features and accelerated port
access or the legacy group of registers
● Accelerated GPIO functions:
➔ GPIO registers are relocated to the ARM local bus so that the
fastest possible I/O timing can be achieved
➔ Mask registers allow treating sets of port bits as a group, leaving
other bits unchanged
➔ All registers are byte and half-word addressable
➔ Entire port value can be written in one instruction
● Bit-level set and clear registers allow a single instruction set or
clear of any number of bits in one port
● Direction control of individual bits
● All I/O default to inputs after reset
● Backward compatibility with other earlier devices is maintained
with legacy registers appearing at the original addresses on the
APB bus
Applications
● General purpose I/O
● Driving LEDs, or other indicators
● Controlling off-chip devices
● Sensing digital inputs
Pin description
PORT0 is a 32-bit port
● Out of these 32 pins, 28 pins can be configured as either general
purpose input or output.
● 1 of these 32 pins (P0.31) can be configured as general-purpose output
only.
● 3 of these 32 pins (P0.24, P0.26 and P0.27) are reserved. Hence, they
are not available for use. Also, these pins are not mentioned in pin
diagram.

PORT1 is also a 32-bit port. Only 16 of these 32 pins (P1.16 – P1.31) are
available for use as general-purpose input or output.
Slow and Fast GPIO Registers
LED
 The intensity of the light emitted by an LED depends on the amount of
forward current passed through the device. The maximum allowable forward
current is denoted by IFmax.
 When designing an LED circuit, we have to know the typical voltage drop,
VTyp across the device, and the maximum allowable voltage drop, VFmax.
 The brightness of the emitted light is measured in millicandela (mcd) and this
is usually referenced to the forward current.
For example, standard red LEDs are quoted to have brightness of 5 mcd when
operated at 10 mA.
Interfacing LED
● 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 10mA.
We use a red LED here.

R = (5 – 2)/10mA = 300 ohms(Use 270 ohms)


LED Interfacing with LPC2148
Schematic Diagram
While writing an application to blink LEDs, we have to follow
general steps:
■ Connect necessary pins using pin connect block. The purpose of
pin connect block is to configure microcontroller pins to desired
function. By default GPIO is enabled.
■ We have to set Pins into an output mode.
■ And then set or clear the bit of respective pin to turn ON & OFF
LED
Embedded C code for the Interfacing
#include<lpc21xx.h>
int main (void)
{
PINSEL0=0x00000000; This include statement adds standard header file for all
PINSEL1=0x00000000; LPC21xx series of microcontrollers. All register names
IODIR0 = 0x007F8000; //configure P0.15 to P0.22 as output pins
while (1)
has been defined in lpc21xx.h file.
//Loop forever
{
IOSET0=0x007F8000; //turn LEDs ON by setting P0.15 to P0.22 as High
delay();
IOCLR0=0x007F8000; //turn LEDs Off by setting P0.15 to P0.22 as Low
delay();
}
}
void delay(void)
{
unsigned int j;
for(j=0;j<1000000;j++);
Return;
}
Embedded C code for the Interfacing
#include<lpc21xx.h>
int main (void)
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IODIR0 = 0x007F8000; //configure P0.15 to P0.22 as output pins
while (1) //Loop forever
The purpose of pin connect block is
{ to configure microcontroller pin to
IOSET0=0x007F8000; //turn LEDs ON by setting P0.15 to P0.22 as High
delay(); desired function.
IOCLR0=0x007F8000; //turn LEDs Off by setting P0.15 to P0.22 as Low
delay();
}
}
void delay(void)
{
unsigned int j;
for(j=0;j<1000000;j++);
Return;
}
Embedded C code for the Interfacing
#include<lpc21xx.h>
int main (void)
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IODIR0 = 0x007F8000; //configure P0.15 to P0.22 as output pins
while (1) //Loop forever
{
IOSET0=0x007F8000; Since we have connected LEDs to
//turn LEDs ON by setting P0.15 to P0.22 as High
delay();
IOCLR0=0x007F8000;
Pin P0.15 to P0.22. We have to set
//turn LEDs Off by setting P0.15 to P0.22 as Low
delay(); Pins into an output mode. As Pins
}
} belongs to PORT0. The direction
void delay(void)
{ control bit in IO0DIR register been
unsigned int j;
for(j=0;j<1000000;j++);
used to configure this pin.
Return;
}
Embedded C code for the Interfacing
#include<lpc21xx.h>
int main (void)
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IODIR0 = 0x007F8000; //configure P0.15 to P0.22 as output pins

while (1) //Loop forever


{ This is never ending while loop.
IOSET0=0x007F8000; //turn LEDs ON by setting P0.15 to P0.22 as High
delay();
IOCLR0=0x007F8000; //turn LEDs Off by setting P0.15 to P0.22 as Low
delay();
}
}
void delay(void)
{
unsigned int j;
for(j=0;j<1000000;j++);
Return;
}
Embedded C code for the Interfacing
#include<lpc21xx.h>
int main (void)
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IODIR0 = 0x007F8000; //configure P0.15 to P0.22 as output pins
while (1) //Loop forever
{
IOSET0=0x007F8000; //turn LEDs ON by setting P0.15 to P0.22 as High
delay(); IOSET0=0x007F8000 would
IOCLR0=0x007F8000; //turn LEDs Off by setting P0.15 to P0.22 as Low
delay();
make output HIGH for Pin P0.15
} To P0.22.
}
void delay(void)
{
unsigned int j;
for(j=0;j<1000000;j++);
Return;
}
Embedded C code for the Interfacing
#include<lpc21xx.h>
int main (void)
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IODIR0 = 0x007F8000; //configure P0.15 to P0.22 as output pins
while (1) //Loop forever
{
IOSET0=0x007F8000; //turn LEDs ON by setting P0.15 to P0.22 as High
delay(); We have called delay() loop in
IOCLR0=0x007F8000; //turn LEDs Off by setting P0.15 to P0.22 as Low
delay(); between which generate
} significant delay between the
}
void delay(void) process of turning LED ON
{
unsigned int j;
and OFF.
for(j=0;j<1000000;j++);
Return;
Embedded C code for the Interfacing
#include<lpc21xx.h>
int main (void)
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IODIR0 = 0x007F8000; //configure P0.15 to P0.22 as output pins
while (1) //Loop forever
{
IOSET0=0x007F8000; //turn LEDs ON by setting P0.15 to P0.22 as High
delay();
IOCLR0=0x007F8000; //turn LEDs Off by setting P0.15 to P0.22 as Low
delay();
}
IOCLR0=0x007F8000 would
} make output LOW for Pin P0.15
void delay(void)
{ TO P0.22.
unsigned int j;
for(j=0;j<1000000;j++);
Return;
}
Embedded C code for the Interfacing
#include<lpc21xx.h>
int main (void)
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IODIR0 = 0x007F8000; //configure P0.15 to P0.22 as output pins
while (1) //Loop forever
{
IOSET0=0x007F8000; //turn LEDs ON by setting P0.15 to P0.22 as High
delay();
IOCLR0=0x007F8000; //turn LEDs Off by setting P0.15 to P0.22 as Low
delay();
}
}
void delay(void)
{
unsigned int j;
for(j=0;j<1000000;j++);
Return;
}
Liquid Crystal Display(LCD):
● Liquid Crystal Display(LCDs) provide a cost effective way to
put a text output unit for a microcontroller.
● Most of the 16x2 LCDs use a Hitachi HD44780 or a compatible
controller.
LCD pin configuration
HD44780 instruction set
LCD command codes
Timing Diagram
LCD Interfacing with LPC2148
Schematic Diagram
Embedded C code for the Interfacing
/* LCD:
RS---P0.28
EN---P0.29
Data--P1.16 to P1.23 */
#include<lpc21xx.h>
unsigned char array[]="SKNCOE PUNE";
unsigned int i=0;
void lcdcmd(unsigned int cmd);
void lcddata(unsigned int data);
void delay(unsigned int itime);
int main()
{
PINSEL0=0X00000000;
PINSEL1=0X00000000;
PINSEL2=0X00000000;
IODIR0=0X30000000;
IODIR1=0X00FF0000;
lcdcmd(0x38); lcdcmd(0xc0);
delay(100); delay(100);
lcdcmd(0x06); for(i=7;i<11;i++)
delay(100); {
lcdcmd(0x01); lcddata(array[i]);
delay(100); delay(1000);
lcdcmd(0x0e); }
delay(100); return 0;
lcdcmd(0x80); }
delay(100); void delay(unsigned int itime)
for(i=0;i<6;i++) {
{ int i,j;
lcddata(array[i]); for(i=0;i<itime;i++)
delay(1000); for(j=0;j<200;j++);
} }
void lcdcmd(unsigned int cmd ) void lcddata(unsigned int data )
{ {
IOCLR1=0X00FF0000; IOCLR1=0X00FF0000;
cmd=cmd<<16; data=data<<16;
IOSET1=cmd; IOSET1=data;
IOCLR0=0X10000000; IOSET0=0X10000000;
IOSET0=0X20000000; IOSET0=0X20000000;
delay(100); delay(100);
IOCLR0=0X20000000; IOCLR0=0X20000000;
} }
Outline:
1. Interrupt structure of LPC2148
2. Interfacing with LED, LCD
3. Interfacing with KEYPAD, GLCD
4. Simple LPC2148 GPIO Programming examples Using timers of
LPC2148 to generate delay
5. Serial communication programming for transmission and
reception from computer
6. Programming for UART
Interfacing with KEYPAD
➢ Switch is the most widely used input device.
➢ When a switch is pressed either low or high signal is given to
controller depending upon the circuit designing.
➢ This approach requires one pin for a single switch.
➢ With increasing requirement of input devices, more numbers of
controller pins are required which may not be available all the
time.
➢ To reduce number of required pins, we can arrange keys in
matrix form.
➢ In matrix keypad, keys are connected in rows and columns.
➢ When any switch is pressed, rows and columns come into contact
which is detected by controller to identify which key has been
pressed.
For identify the pressed key, we have to follow given procedure.
1. Set output port (Rows) and input port (Columns).
2. Ground first row and set rest of the rows.
3. Read input port, if all are 1s, no switch in that row is pressed.
Whenever any key is pressed, one of the input pin will be
grounded by pressed key. By reading input pins, we can identify
the column in which key has been pressed.
4. Now ground second row and set all others rows at logic 1 and see
whether any key has been pressed in that row or not. Continue
same process until you find out in which row, key is pressed.
5. Once we know column and row in which key has been pressed,
we can identify the pressed key easily.
Keypad Interfacing with LPC2148
Schematic Diagram

P0.28
P0.28
P0.29
/* 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 */
Embedded C program for the Interfacing keypad and LCD

#include<lpc21xx.h>
void lcdcmd(unsigned int); IO0PIN=0x00000380; // First Scan
void lcddata(unsigned int); Line(row)
void delay_lcd(void); if(( IO0PIN & 0x0000003c )!= 0x0000003c))
int main(void) {
{ switch(IO0PIN & 0x0000003c)
while(1) {
{ case 0x00000038 : lcddata("C");break;
IODIR1=0x00ff0000; case 0x00000034 : lcddata("D");break;
IODIR0=0x300003c0; case 0x0000002c : lcddata("E");break;
lcdcmd(0x38); case 0x0000001c : lcddata("F");break;
lcdcmd(0x0e); }
lcdcmd(0x01); }
lcdcmd(0x06);
lcdcmd(0x83);
lcdcmd(0xC0);
IO0PIN=0x00000340;// Second Scan Line(row)
IO0PIN=0x000001c0; // Four Scan Line(row)
if(( IO0PIN & 0x0000003c )!= 0x0000003c) )
if(( IO0PIN & 0x0000003c )!= 0x0000003c)
{
{
switch(IO0PIN & 0x0000003c)
switch(IO0PIN & 0x0000003c)
{
{
case 0x00000038 : lcddata("8");break;
case 0x00000038 : lcddata("0");break;
case 0x00000034 : lcddata("9");break;
case 0x00000034 : lcddata("1");break;
case 0x0000002c : lcddata("A");break;
case 0x0000002c : lcddata("2");break;
case 0x0000001c : lcddata("B");break;
case 0x0000001c : lcddata("3");break;
}
}
}
}
IO0PIN=0x000002c0;// Third Scan Line(row)
delay_lcd();
if(( IO0PIN & 0x0000003c )!= 0x0000003c)
}
{
}
switch(IO0PIN & 0x0000003c)
{
case 0x00000038 : lcddata("4");break;
case 0x00000034 : lcddata("5");break;
case 0x0000002c : lcddata("6");break;
case 0x0000001c : lcddata("7");break;
}
}
void lcdcmd(unsigned int cmddata)
{ void delay_lcd(void)
IOCLR1=0x00ff0000; {
IOCLR0=0x10000000; int j;
cmddata=cmddata<<16; for(j=0;j<10000;j++);
IOSET1=cmddata; return;
IOSET0=0x20000000; }
delay_lcd();
IOCLR0=0x20000000;
delay_lcd();
return;
}
void lcddata(unsigned int outdata)
{
IOCLR1=0x00ff0000;
IOSET0=0x10000000;
outdata=outdata<<16;
IOSET1=outdata;
IOSET0=0x20000000;
delay_lcd();
IOCLR0=0x20000000;
delay_lcd();
return;
Graphics LCD
Graphics LCD

Internal block diagram of a KS0108B (NT7108C) based 128x64 pixel GLCD module
➢ The NT1707C drives the 64 display lines, COM1 – COM64.
➢ The first NT7108C drives the left half segments (SEG1 to
SEG64) and the second one drives the right half segments
(SEG65 to SEG128) of the display.
➢ 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.
Structure of the GLCD
GLCD display coordinates
Pin description of Winstar WDG01510 GLCD module
Display control instructions
Data print on GLCD
Interfacing a KS0108 based Graphics LCD
Displaying Image
 We can display binary image up to 128x64 pixel resolution.
 Binary image means each pixel has only two values i.e. 0 or 1.
- GLCD 128x64 JHD12864E can display only Binary image.
- So, we need to convert image into binary format
- There are several ways to do this, we will use Image2GLCD application.
- Image2GLCD application converts JPG, GIF, BMP, PNG format image
files to binary format.
Outline:
1. Interrupt structure of LPC2148
2. Interfacing with LED, LCD
3. Interfacing with KEYPAD, GLCD
4. Simple LPC2148 GPIO Programming examples Using timers of
LPC2148 to generate delay
5. Serial communication programming for transmission and
reception from computer
6. Programming for UART
Timers in LPC2148 ARM7 Microcontroller
● The LPC2148 has two functionally identical general purpose
timers: Timer0 and Timer1.
● These both timers are 32-bit along with 32-bit prescaler.
● Timer allows us to generate precise time delay.

32 bit Timer Counter with 32 bit Prescaler


● The tick rate of the Timer Counter (TC) is controlled by the 32-
bit number written in the Prescaler Register (PR) in the
following way.
● There is a Prescale Counter (PC) which increments on each tick
of the PCLK.
● When it reaches the value in the prescaler register, the timer
count is incremented and the Prescaler Counter (PC) is reset, on
the next PCLK.
● This cause the timer counters to increment on every PCLK
when PR=0, every 2 PCLKs when PR=1, etc.
Timer block diagram
REGISTERS USED IN TIMERS:
T0TCR:This register is used to control the functions of the Timer0. Enable
and reset operations of the Timer0 register can be controlled by this
register.
T0PR:This is a 32 bit Prescale register which holds the maximum value
which the Prescale counter can take.
T0PC:This is a 32 bit Prescale counter which specifies the division value
of the processor clock before feeding it to the timers. The value in the
register increments with every input pulse or Processor clock fed into it.
Prescale register T0PR gives the max value this T0PC register can take.
When reaching the max count T0PC register gets reset on the next
Processor clock.
T0TC:A 32 bit timer counter is incremented whenever Prescale
counter T0PC value reaches its maximum level given in the Prescale
register T0PR.
T0MR0:These register stores the value which should be compared
to the T0TC register. Operations specified based on the T0MCR will
be performed whenever a match encounters.
T0MCR:The Match control register is used to specify the operation
whenever a match occurs between the value stored in T0MR0 and
T0TC register.
T0IR:Interrupt register which consists bits for match and capture
interrupts. Writing will reset the interrupts in this register.
CALCULATION OF TIME DELAY:
Peripheral Frequency = CPU Clock / VPB divider
Since we are using Default 1/4 divider here our peripheral frequency will be
Peripheral Frequency = 20 MHz / 4 = 5MHz
Then we have to calculate T0MR0 (count) value to generate required delay by
matching
Count = (5MHz * Required time delay) – 1
= (5,000,000 * 100ms) – 1
= (5,000,000 * .1) -1
= 4,99,999
Now count 499999 gives the T0MR0 value to generate 100ms delay. So to make
it as one sec we have to multiply it with 10. And that we can use it in the Prescale
register T0PR, thus we can generate a delay of 1 sec using this.
PCLK = 30MHz
Hence, we will load T0PR with a value 29, so that the TC will
increment after every 30 PCLK rising edges.
30MHz/30 = 1MHz, which gives 1µsec time.

To get 100msec time, we will have to load T0MR with 100000

(1 µsec * 1000 = 1msec, 1msec * 100 = 100msec. Hence, 1000 *


100 = 100000)
This will give us a delay of 100msec.
STEPS TO PROGRAM TIMERS:
1. Reset timer0 initially to deactivate counting.
2. Load calculated values in the Prescaler register T0PR and
Match register T0MR0.
3. Initialize T0PC and T0TC registers.
4. Select operations using match registers when match is
encountered.
5. Start the Timer by enabling it through T0TCR register.
6. Wait till the interrupt and then clear the flag by writing T0IR
register.
LED Interfacing with LPC2148
Schematic Diagram
This code LED’s connected to the Port 0 are toggled with 1 sec time delay
#include<lpc21xx.h>
void delay(void);
int main(void)
{
PINSEL0=0x00000000;
PINSEL1=0x00000000;
IODIR0 = 0x007F8000; //configure P0.15 to P0.22 as output pins
while (1) //Loop forever
{
IOSET0=0x007F8000; //turn LEDs ON by setting P0.15 to P0.22 as High
delay();
IOCLR0=0x007F8000; //turn LEDs Off by setting P0.15 to P0.22 as Low
delay();
}
}
void delay(void)
{
T0TCR=(1<<1); //Reset Timer0
T0MR0=499999; //Loading match register value
T0PR=10; //Loading Prescalar register value
T0PC=T0TC=0;
T0MCR=(1<<0)|(1<<2); //Generates interrupt and reset on match
T0TCR=(1<<0); //Starting Timer
while(!(T0IR&(1<<0))); //Waiting for interrupt
T0IR=(1<<0); //Clearing interrupt
}
void DelayUS(unsigned int microseconds)
{
T0IR = 0;
T0TCR = 0;
T0PR = 11; //12 clock cycles = 1 uS
T0PC = 0;
T0TC = 0;

T0TCR = 0x01; //enable timer

//wait until timer counter reaches the desired delay


while(T0TC < microseconds);

T0TCR = 0x00; //disable timer


}
void DelayMS(unsigned int milliseconds)
{
T0IR = 0;
T0TCR = 0;
T0PR = 11999; //12000 clock cycles = 1 mS
T0PC = 0;
T0TC = 0;

T0TCR = 0x01; //enable timer

//wait until timer counter reaches the desired delay


while(T0TC < milliseconds);

T0TCR = 0x00; //disable timer


}
void DelayS(unsigned int seconds)
{
T0IR = 0;
T0TCR = 0;
T0PR = 11999999; //12000000 clock cycles = 1 S
T0PC = 0;
T0TC = 0;

T0TCR = 0x01; //enable timer

//wait until timer counter reaches the desired delay


while(T0TC < seconds);

T0TCR = 0x00; //disable timer


}
Contents:
1. Interrupt structure of LPC2148
2. Interfacing with LED, LCD
3. Interfacing with KEYPAD, GLCD
4. Simple LPC2148 GPIO Programming examples Using
timers of LPC2148 to generate delay
5. Serial communication programming for transmission and
reception from computer
6. Programming for UART
 The asynchronous serial protocol has a number of built-in rules -
mechanisms that help ensure robust and error-free data transfers.
 Data bits
 Synchronization bits
 Parity bits
 Baud rate.
 The baud rate specifies how fast data is sent over a serial line.
It’s usually expressed in units of bits-per-second (bps).
Framing the data
 Each block (usually a byte) of data transmitted is actually sent in
a packet or frame of bits. Frames are created by appending
synchronization and parity bits to our data.
A serial frame.

9600 8N1 (an example)


 9600 8N1 - 9600 baud, 8 data bits, no parity, and 1 stop bit - is
one of the more commonly used serial protocols.
 A device transmitting the ASCII characters ‘O’ and ‘K’ would
have to create two packets of data.
 The ASCII value of O(that’s uppercase) is 79, which breaks
down into an 8-bit binary value of 01001111, while K’s binary
value is 01001011.
 All that’s left is appending sync bits.
 It isn’t specifically stated, but it’s assumed that data is transferred
least-significant bit first.
 Notice how each of the two bytes is sent as it reads from right-to-
left.

Since we’re transferring at 9600 bps, the time spent holding each
of those bits high or low is 1/(9600 bps) or 104 µs per bit.
Wiring and Hardware
 A serial bus consists of just two wires - one for sending data and
another for receiving. As such, serial devices should have two
serial pins: the receiver, RX, and the transmitter, TX.
Hardware Implementation
 RS-232, which can be found on some of the more ancient
computers and peripherals, is like TTL serial flipped on its head.
 RS-232 signals usually range between -13V and 13V, though the
spec allows for anything from +/- 3V to +/- 25V.
 On these signals a low voltage (-5V, -13V, etc.) indicates either
the idle line, a stop bit, or a data bit of value 1.
 A high RS-232 signal means either a start bit, or a 0-value data
bit. That’s kind of the opposite of TTL serial.
 The MAX232 is an IC originally designed by a company called
Maxim IC that converts the +/-13V(+/-25V) signals of RS232
down to the 0/5V signals that our Microcontroller can understand.
 It also boosts the voltage of our Microcontroller to the needed +/-
13V(+/-25V) of the RS232 protocol so that a computer can
understand our Microcontroller and vice versa.
UART section of LPC 2148:

● The LPC 2148 contains two UARTs which are compatible with
UART IC 16C550 (Industry Standard UART IC).
● UART0 provides only standard transmit and receive data lines
● UART1 also provides a full modem control handshake interface
along with standard transmit and receive data lines.
LPC214x UART0
Features:
● 16 byte Receive and Transmit FIFOs
● Register locations conform to ‘550 industry standard.
● Receiver FIFO trigger points at 1, 4, 8, and 14 bytes.
● Built-in fractional baud rate generator with autobauding
capabilities.
● Mechanism that enables software and hardware flow control
implementation.
UART0 block diagram
LPC214x UART1
Features
● UART1 is identical to UART0, with the addition of a modem
interface.
● 16 byte Receive and Transmit FIFOs.
● Register locations conform to ‘550 industry standard.
● Receiver FIFO trigger points at 1, 4, 8, and 14 bytes.
● Built-in fractional baud rate generator with autobauding capabilities.
● Mechanism that enables software and hardware flow control
implementation.
● Standard modem interface signals included with flow control (auto-
CTS/RTS) fully supported in hardware (LPC2144/6/8 only).
UART1 block diagram
The master formula for calculating baud rate is given as :

which can be further simplified to :


Program to receive and transmit data using UART0

#include<LPC21xx.h>
void main()
{
char a;
PINSEL0=0x00000005;//P0.0 as TxD0 & P0.1 as RxD0
U0LCR=0x83;//8 -bit character length,DLAB=1
U0DLM=0x00;//
U0DLL=0x61;// Baud rate=9600
U0LCR=0x03;// DLAB=0; to load baud rate value

while(1)
{
while(!(U0LSR&0x01)); //monitoring TI flag
a=U0RBR;

while(!(U0LSR&0x20)); // monitoring RI flag


U0THR=a;
}
}
#include <lpc21xx.h> //Includes LPC2148 register definitions
#define Fosc 12000000
#define Fcclk (Fosc * 5)
#define Fpclk (Fcclk / 4)

#define UART_BPS 9600 //Set Baud Rate here

const unsigned char SEND_STRING[] = "Welcome to SKNCOE\n";


const unsigned char SEND_STRING1[] = "Test Passed\n";

void Delay_Ticks(unsigned int Delay) //Function to generate finite delay


{
unsigned int i;
for(; Delay>0; Delay--)
for(i=0; i<50000; i++);
}
void Init_UART0(void) //This function setups UART0
{
unsigned int Baud16;
U0LCR = 0x83; // DLAB = 1
Baud16 = (Fpclk / 16) / UART_BPS;
U0DLM = Baud16>>8; //

U0DLL = Baud16 & 0xff; //


U0LCR = 0x03;
}

void UART0_SendByte(unsigned char data)


//A function to send a byte on UART0
{
U0THR = data;
while( (U0LSR&0x20)==0 );
}
void UART0_SendStr(const unsigned char *str)
//A function to send a string on UART0
{
while(1)
{
if( *str == '\0' ) break;
UART0_SendByte(*str++);
}
}
int main(void)
{
PINSEL0 = 0x00000005; // Enable UART0 Rx and Tx pins
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
Init_UART0();
UART0_SendStr(SEND_STRING);
while((U0LSR&0x01)==0);
UART0_SendStr(SEND_STRING1);
while(1)
{
}
return(0);
}
References
[1] UM10139 LPC214x User manual Rev. 4 — 23 April 2012
[2] The Insider's Guide To The Philips ARM7-Based Microcontrollers-Trevor Martin
Thank You

Vous aimerez peut-être aussi