0% found this document useful (0 votes)
33 views65 pages

ES Module4

The document provides an introduction to ARM programming focusing on embedded systems, covering topics such as interfacing with LEDs, LCDs, temperature sensors, and UART programming. It outlines the structure of C programs, control mechanisms using timers, and data acquisition techniques. Learning outcomes include the ability to demonstrate interfacing techniques and program serial communication effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views65 pages

ES Module4

The document provides an introduction to ARM programming focusing on embedded systems, covering topics such as interfacing with LEDs, LCDs, temperature sensors, and UART programming. It outlines the structure of C programs, control mechanisms using timers, and data acquisition techniques. Learning outcomes include the ability to demonstrate interfacing techniques and program serial communication effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

Introduction to ARM Programming

Module IV
19ECS431: Embedded Systems

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Syllabus
Module IV: Introduction to ARM Programming:

Simple C programs for application with LED, Control of LED


with software and hardware timer,

LCD interfacing, programming of LCD, ADC,

Interfacing of LM35 temperature sensor,

UART programming

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Learning Outcomes:
After completion of this unit, the student will be able to:

1. Explain the structure of C program.

2. Demonstrate the control of led using software and hardware timers.

3. Apply the interfacing techniques led and lcds.

4. Explain data acquisition using sensors.

5. Can program a serial communication.


02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
Interfacing of LED with LPC2148
Introduction
• General-purpose input/output (GPIO) is a pin on an IC (Integrated Circuit).
• It can be either input pin or output pin, whose behaviour can be controlled at the
run time.
• A group of these pins is called a port (Example, Port 0 of LPC2148 has 32 pins).
• LPC2148 has two 32-bit General Purpose I/O ports.
1. PORT0
2. PORT1
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.
LPC2148 Pin diagram

• Almost every pin of these two ports has some alternate


function available.
• For example, P0.0 can be configured as the TXD pin for
UART0 or as PWM1 pin as well.
• The functionality of each pin can be selected using the Pin
Function Select Registers.
Pin Function Select Registers
• Pin Function Select Registers are 32-bit registers. These registers are used to select
or configure specific pin functionality.

• There are 3 Pin Function Select Registers in LPC2148:

1. PINSEL0 : - PINSEL0 is used to configure PORT0 pins P0.0 to P0.15.

2. PINSEL1 : - PINSEL1 is used to configure PORT0 pins P0.16 to P0.31.

3. PINSEL2 : - PINSEL2 is used to configure PORT1 pins P1.16 to P1.31.


Fast and Slow GPIO Registers:
• There are 5 Fast (also called Enhanced GPIO Features Registers) GPIO Registers and
4 Slow (also called Legacy GPIO Registers) GPIO Registers available to control PORT0
and PORT1.
• The Slow Registers allow backward compatibility with earlier family devices using
the existing codes.

Slow GPIO Registers


There are 4 Slow GPIO registers :
1. IOxPIN (GPIO Port Pin value register): This is a 32-bit wide register. This register is
used to read/write the value on Port (PORT0/PORT1). But care should be taken while
writing. Masking should be used to ensure write to the desired pin.
Examples :
a) Writing 1 to P0.4 using IO0PIN
IO0PIN = IO0PIN | (1<<4)
b) Writing 0 to P0.4 using IO0PIN
IO0PIN = IO0PIN & (~(1<<4) )
c) Writing F to P0.7-P0.4
IO0PIN = IO0PIN | (0x000000F0)
2. IOxSET (GPIO Port Output Set register) : This is a 32-bit wide register. This register is
used to make pins of Port (PORT0/PORT1) HIGH. Writing one to specific bit makes that
pin HIGH. Writing zero has no effect.

3. IOxDIR (GPIO Port Direction control register) : This is a 32-bit wide register. This
register individually controls the direction of each port pin. Setting a bit to ‘1’
configures the corresponding pin as an output pin. Setting a bit to ‘0’ configures the
corresponding pin as an input pin.

4. IOxCLR (GPIO Port Output Clear register) : This is a 32-bit wide register. This register
is used to make pins of Port LOW. Writing one to specific bit makes that pin LOW.
Writing zeroes has no effect.
#include <lpc214x.h>

void delay_ms(unsigned int count)


{
unsigned int j=0,i=0;
for(j=0;j<count;j++)
{
for(i=0;i<1000;i++);
}
}

/* start the main program */


int main()
{
PINSEL2 = 0x000004; //Configure the P1 Pins for GPIO;
IODIR1 = 0xffffffff; //Configure the P1 pins as OUTPUT;

while(1)
{
IOSET1 = 0xffffffff; // Make all the Port pins as high
delay_ms(1000);

IOCLR1 = 0xffffffff; // Make all the Port pins as low


delay_ms(10000);
}
}
Blinking of LED using timer
Crystal Input
Clock in
LPC2148
CPU
Oscillator PLL

GPIO
Timer
VPB DIV UART
ADC
DAC
I2C
SPI
Crystal Input Clock in
LPC2148
Oscillator PLL CPU

Prescalar 1KH Timer Counter


VPB DIV Register
(PR)
z (TC)
TCl
k

PR = (Frequency of Pclk) / (Frequency


of Tck)
CTCR : Count Control
register
• Used to select Timer/Counter Mode.
• For Timing purpose we use this in Timer Mode.
• When the value of the CTCR is set to 0×0 Timer Mode
is selected.
Prescalar
Register (PR) Register (32 bit) – Divides the
•PR : Prescale
Input frequency to a desired value

PR
Pcl Tcl
k k

PR = (Frequency of Pclk) / (Frequency


of Tck)
TCR : Timer Control
Register
• This register is used to enable , disable and reset TC.
• When bit0 is 1 timer is enabled and when 0 it is
disabled.
• When bit1 is set to 1 TC and PC are set to zero
together in sync on the next positive edge of PCLK.
• Rest of the bits of TCR are reserved.
TC : Timer Counter Register
(32 •bit)
This is the main counting register.
• Timer Counter increments when PC reaches its maximum
value as specified by PR.
• If timer is not reset explicitly(directly) or by using an interrupt
then it will act as a free running counter which resets back to
zero when it reaches its maximum value which is 0xFFFFFFFF.

Total Counts = 429,49,67,296


Setting up & configuring
Timers• Set appropriate value in TxCTCR (Count Control
Register  Set as Timer )
• Define the Prescale value in TxPR (Prescale
Register)
• Reset Timer – Which resets PR and TC
• Set TxTCR to 0×01 to Enable the Timer when
required (Timer Control Register)
• Reset TxTCR to 0×00 to Disable the Timer when
required (Timer Control Register)
Initialize
Timer
T0CTCR = 0x00;
T0PR = PRESCALE-1;
T0TCR = 0x02;
//Reset Timer
For 1 ms
Tick
Tclk =
Pclk = 60
PR 1KHz TC
MHz

PR+1 =
60MHz/1KHz
= 60,000
PR = 59999
For 1 us
Tick
Tclk =
Pclk = 60 1MHz
PR
MHz
TC0

PR+1 =
60MHz/1MHz
= 60
PR = 59
Blinking of LED using timer -
Program
#include<lpc214x.h>
int main()
{ T0CTCR=0X00;
T0PR=0XEA5F;
IO1DIR=0X00FF0000;
while(1)
{
IO1SET=0X00FF0000;
T0TC=0X0;
T0TCR=0X00000001; while(T0TC < 10);

T0TC=0X0;
IO1CLR=0X00FF0000;
T0TCR=0X00000001; while(T0TC < 10);

}
}
UAR
T • Two Identical UARTs  UART0 & UART1
• We are using UART0 for our example.

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Crystal Input
Clock in
LPC2148Fosc
CPUclk 60MHz

CPU
Oscillator PLL

Pclk
GPIO
Timer
VPB DIV UART
ADC
DAC
I2C
SPI
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
Crystal Input UART Clock in
LPC2148
Oscillator PLL CPU

Baud Rate
Divisor = Pclk / (Baud rate x 16) x 16
= 60M/(16*9600)
=391 VPB DIV Divisor UART
=0187H
Load DLL with 0x87 and DLM with 0x01
DLL: Divisor latch LSB
DLM: Divisor latch MSB

DLM DLL

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Pins
Involved

P0.0 [Pin 19] 


TXD0

P0.1 [Pin 21] 


02/05/2025 RXD0 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
SFRs
Involved
• P0 Pin Select Register - PINSEL0
• UART0 Line Control Register - U0LCR
• UART0 Line Status Register - U0LSR
• UART0 Divisor Latch MSB - U0DLM
• UART0 Divisor Latch LSB - U0DLL
• UART0 Transmit Holding Register - U0THR
• UART0 Receive Buffer Register - U0RBR

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Pin Select
SFRs
• PINSEL0 for GPIO pins 0.0 to 0.15
• PINSEL1 for GPIO pins 0.16 to 0.31
• PINSEL2 for GPIO pins 1.16 to 1.31

Each Pin is Associated with multiple functions

Pin Select enable you to select which pin


functions you would like to use

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


How to Use
PINSEL
• Each associated 'pin' in PINSEL0 and PINSEL1 is
assigned a 2-bit address.

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Function
Selection Bits

Example for P0.0 & P0.1 as


TxD & RxD
PINSEL0 = 0x00000005
0 1 0 1

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


UART0 Line Control Register
- U0LCR

1 1 1

Used to load value into DLL and DLM

02/05/2025
U0LCR =
19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
UART0 Line Status Register
- U0LSR

Bit – 0 : Receive Data Ready (RDR)


Bit – 5 : Transmit Holding Register Empty
(THRE)

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Divisor
Latch
• UART0 Divisor Latch MSB - U0DLM
• UART0 Divisor Latch LSB - U0DLL
• Holds the value used to divide the clock supplied by
the prescaler in order to produce the baud rate clock.
• The Divisor Latch Access Bit (DLAB) in U0LCR must be
one in order to access the UART0 Divisor Latches

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Baud Rate
Clock

U0DL U0DL
M L

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Baud Rate
Calculations

For Pclk = 60MHz


For Pclk = 30MHz
Divisor = Pclk / (Baud x
Divisor = Pclk / (Baud x
16)
16)
= 60 x 10^6 /
= 30 x 10^6 /
(9600 x 16)
(9600 x 16)
= 391
= 195
= 0x01
= 0x00
87 U0DLM =
C3 U0DLM =
0x01 U0DLL
0x00 U0DLL
=
=
0x87
0xC3

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


UART0 Receive Buffer -
U0RBR
• The U0RBR is the top byte of the UART0 Rx FIFO

• The Divisor Latch Access Bit (DLAB) in U0LCR must be


zero in order to access the U0RBR

• The U0RBR is always Read Only

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


UART0 Transmit Holding Register
- U0THR
• The U0THR is the top byte of the UART0 TX FIFO.

• The Divisor Latch Access Bit (DLAB) in U0LCR must be


zero in order to access theU0THR.

• The U0THR is always Write Only

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Algorithm for Initializing
UART0
1. Generate Pclk for 60MHz
initPLL();
2. Select UART Function for the Pins
PINSEL0 = 0X00000005;
3. Set UART0 Line Control Register for 8 Bit UART, No Parity ,1
Stop Bit with Divisor Latch Access Bit (DLAB) Enabed.
U0LCR = 0X00000083;
4. Set U0DLL & U0DLM for 9600 Baud.
U0DLL = 0X87;
U0DLM = 0X01;
5.Clear Divisor Latch Access Bit (DLAB)
U0LCR = 0X00000003;

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Program
#include<lpc214x.h> PINSEL0=0X00050005;
int main (void) U0LCR=0X83;
{ U0DLL=195;
/* char data; */ U0LCR=0X03;
PLL0CFG=0X00000024; U0THR='B';
PLL0CON=0X00000001; while(!(U0LSR&0x20));
PLL0FEED=0X000000AA;
PLL0FEED=0X00000055; U1LCR=0X83;
while(!(PLL0STAT&0X400)); U1DLL=195;
PLL0CON=0X0000003; U1LCR=0X03;
PLL0FEED=0X000000AA; while(!(U1LSR&0x01));
PLL0FEED=0X00000055; /* data=U1RBR; */
VPBDIV=0X02; while(1);
}

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


LCD -
Introduction
• Interfacing of a 2x16 LCD with LPC2148 in 8-bit mode.
• 2x16 has 2 lines with 16 chars on each line.
• It supports all the ASCII chars and is basically used for displaying the
alphanumeric characters.
• Here each character is displayed in a matrix of 5x7 pixels.
• Apart from alphanumeric chars it also provides the provision to display
the custom characters by creating the pattern.

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


LCD

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


LCD

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


LCD -
• If the RSIntroduction
signal is LOW then the LCD interprets the 8-bit info as Command and writes
it Command register and performs the action as per the command.
• If the RS signal is HIGH then the LCD interprets the 8-bit info as data and copies it to data
register. After that the LCD decodes the data for generating the 5x7 pattern and finally
displays on the LCD.

Read/Write(RW): This signal is used to write the data/cmd to LCD and reads the busy flag
of LCD. For write operation the RW should be LOW and for read operation the R/W should
be HIGH.

Enable(EN): This pin is used to send the enable trigger to LCD. After sending the data/cmd,
Selecting the data/cmd register, Selecting the Write operation. An HIGH-to-LOW pulse has
to be sent on this enable pin which will latch the info into the LCD register and triggers the
LCD to act accordingly.
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
LCD -
Commands

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


LCD – Cursor
Positioning

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Interfacing of LCD with LPC 2148

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Interfacing
of
LCD
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE
Simple C programs for application with LM35
LM 35 Temperature Sensor

Interfacing the LM35 to microcontroller.


As you can see the third pin is connected to GND,
the first pin is connected to VCC &
the second pin is connected to the Microcontroller input.

Just use single PIN female to female wire to connect with


the leads of LM35 temperature sensor.

So when the temperature is sensing, it give the sensor


reading to controller.

Interfacing LM35 to Microcontroller

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE


Simple C programs for application with LM35
LM 35 Temperature Sensor: for LPC2148

Interfacing the LM35 with LPC2148.


Read the temperature in LPC2148 Starter Board from
temperature sensor LM35. The ARM7
LPC2148 Starter Board uses the ADC pin for reading
temperature from temperature sensor LM35. The reading
output is displayed into PC through UART1.

The 10 bit ADC used for reading the temperature from


LM35. Basic clocking for the A/D converters is provided
by the VPB clock. A programmable divider is included in
each converter, to scale this clock to the 4.5 MHz (max)
clock needed by the successive approximation process. A
fully accurate conversion requires 11 of these clocks.
Interfacing LM35 with LPC2148 Microcontroller

02/05/2025 19ECS431: Embeded Systems; Module IV C Programs, Dept of EECE

You might also like