Arm Interfacing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

UNIT – V: INTERFACING ARM WITH EXTERNAL PERIPHERALS [8 HRS]

 Interfacing - A/D and D/A converter,


 Interfacing -LEDs, Switches,
 Interfacing -Relays,
 Interfacing -LCD,
 Interfacing -Stepper Motors,
 Interfacing -Real Time Clock,
 Interfacing -Serial Communication,
 Interfacing -GSM and GPS.

LPC2148 (32-bit ARM7TDMI-S processor) GPIO Ports and Registers


(ARM7 + 16 bit Thumb + JTAG Debug + fast Multiplier + enhanced ICE)
Introduction to GPIO
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.
ARM LPC2148 Pin Diagram

ARM 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.

Note : The Port 0 pins do not have built-in pull-up or pull-down resistors. Hence, while using GPIOs
on Port 0, in some cases, we need to connect pull-up or pull-down resistors externally.
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.
GPIO registers that control the GPIO operations:
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.
Examples:
a) Configure pin P0.0 to P0.3 as input pins and P0.4 to P0.7 as output pins.
IO0DIR = 0x000000F0;
b) Configure pin P0.4 as an output. Then set that pin HIGH.
IO0DIR = 0x00000010; OR IO0DIR = (1<<4);
IO0SET = (1<<4);
c) Configure pin P0.4 as an output. Then set that pin LOW.
IO0DIR = 0x00000010; OR IO0DIR = (1<<4);
IO0CLR = (1<<4);

Fast GPIO Registers


There are 5 fast GPIO registers :
1. FIOxDIR (Fast 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.
2. FIOxMASK (Fast Mask register for port) : This is a 32-bit wide register. This register controls
the effect of fast registers (FIOxPIN, FIOxSET, FIOxCLR) on port pins. Setting a bit to ‘0’
configures the corresponding pin access to the fast registers i.e. we can write/read the corresponding
pin in fast mode using fast registers. Setting a bit to ‘1’ configures the corresponding pin unaffected
by fast registers.
3. FIOxPIN (Fast Port Pin value register using FIOMASK) : This is a 32-bit wide register. This
register is used to read/write the value on port pins, only if that corresponding port pins have access to
fast registers (Access to port pins using fast registers is enabled using Zeroes in FIOxMASK register).
4. FIOxSET (Fast Port Output Set register using FIOMASK) : This is a 32-bit wide register.
This register is used to make pins of Port HIGH. Writing one to specific bit makes that pin HIGH.
Writing zero has no effect. Reading this register returns the current contents of the port output
register. Only bits enabled by ZEROES in FIOMASK can be altered.
5. FIOxCLR (Fast Port Output Clear register using FIOMASK) : 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. Only bits enabled by ZEROES in FIOMASK can be altered.
Aside from the 32-bit long and word only accessible registers mentioned above, every fast GPIO port
can also be controlled via several byte and half-word accessible registers. Refer chapter 8 (Page 83)
on GPIO in the datasheet of LPC2148 provided in the attachments section for their use.
Examples:
a) Configure pin P0.0 to P0.3 as input pins and P0.4 to P0.7 as output pins.
FIO0DIR = 0x000000F0;
b) Configure pin P0.4 as an output. Then set that pin HIGH.
FIO0DIR = 0x00000010; OR FIO0DIR = (1<<4);
FIO0SET = (1<<4);
c) Configure pin P0.4 as an output. Then set that pin LOW.
FIO0DIR = 0x00000010; OR FIO0DIR = (1<<4);
FIO0CLR = (1<<4);

Why this is called Fast GPIO :


Fast GPIO registers are relocated to the ARM local bus. This makes software access to GPIO pins 3.5
times faster than access through Slow GPIO registers. This effect will not always be visible when a
program is written in c code. It may be more evident in an assembly code than in a c code.
1. INTERFACING LEDs TO ARM7 CONTROLLER-(LPC2148 ):
Light Emitting Diodes (LEDs)are popularly used display components used to indicate the ON and
OFF state of a system. These are also used to realize various counters like binary
countersexperimentally. These LEDs can be easily interfaced with the Port pinsof any
Microcontrollerbyusingcurrent limiting resistors ofthe order of220 Ohms. The diagram below
shows the interfacing of LED array to the Port1 pins of LPC2148 ARM 7microcontroller.

1.1 PROGRAM-1
This program blinks the LEDs continuously with a small delay. The LEDs are connected to the
Port1 pins P1.24 toP1.31 and the these pins are configured as General Purpose output pins.

#include <lpc2148.H> //LPC2148Header


void delay ()
{
for(int i=0x00; i<=0xFF; i++)
for(int j=0x00; j<=0xFF; j++); //Delay program
}
void main()
{
PINSEL2=0X00000000; // Set P1.24 TO P1.31as GPIO
IO1DIR=0XFF000000; //PortpinsP1.24 toP 1.31ConfiguredasOutputport.
while(1) //Infiniteloop
{
IO1SET=0XFF000000; // Pins P1.24 to P1.31 goes to high state delay();
IO1CLR=0XFF000000; // Pins P1.24 toP1.31 goes to low
statedelay();
}
}

1.2 PROGRAM–2
This program glows LEDs alternately by sending 55H and AAH through the port1Pins
#include<LPC214X.H> //LPC2148 HEADER
voiddelay(void) // DelayProgram
{
unsigned int i;
i =0xfff fff;
while(i-);
}
int main(void)
{
PINSEL2=0x00000000; //Port1isI/O
IODIR1=0XFF<<24; // Port PinsP1.24 to P1.31as Output Pins
while(1) //Infinite loop
{
IOSET1=0X55<<25; //P1.25,P1.27,P1.29&P1.31LEDswillGlow
delay(); //Call delay function
IOCLR1=0X55<<25; //P1.25,P1.27,P1.29&P1.31 LEDswillbeoff
IOSET1=0XAA<<24; //P1.24,P1.26,P1.28&P1.30 LEDsareGlow
delay(); // Call delayfunction
IOCLR1=0XAA<<24; //P1.24,P1.26,P1.28&P1.30 LEDsareoff
}
}
1.3 INTERFACING LEDs & PUSH BUTTON SWITCHES USING LPC2148

Program for turning LED ON or OFF depending on the status of the pin. Here, LED is interfaced to
P0.0. A switch is interfaced to P0.1 to change the status of the pin.
Interfacing Diagram

LPC2148 Switch and LED interface Hardware Connections

#include <lpc214x.h>
#include <stdint.h>

int main(void)
{
PINSEL0 = 0x00000000; /* Configuring P0.0 to P0.15 as GPIO */

IO0DIR = 0x00000001; /* Make P0.0 bit as output bit, P0.1 bit as an input pin */
while(1)
{
if ( IO0PIN & (1<<1) ) /* If switch is open, pin is HIGH */

{
IO0CLR = 0x00000001; /* Turn on LED */
}
else /* If switch is closed, pin is LOW */
{
IO0SET = 0x00000001; /* Turn off LED */
}
}
}
2. INTERFACING A RELAY TO ARM7 CONTROLLER-(LPC2148 )
Relays are devices which allow low power circuits to switch a relatively high Current/ Voltage
ON/OFF. A relay circuit is typically a smaller switch or device which drives (opens/closes) an
electric switch that is capable of carrying much larger current amounts.
Figure below shows the interfacing of the Relay to ARM controller. When the input is energized,
the relay turns on and the '+' output is connected to +12v. When the relay is off, the '+'output is
connected to Ground. The '-'output is permanently wired to Ground.
The relay is interfaced to P0.30 Pin through an Opto-isolator. This opto-isolator protects the port pin
from damage due to any high currents .The opto-isolator consists of a pair of an LED and a Photo
transistor as shown in the diagram. The power transistor is used at the input. So, when the input is
high , the output of the transistor is LOW and the relay is in OFF state .Similarly when we apply
allow to the transistor ,the output is high and the relay is ON.
PROGRAM

The following program configures the P0.30 pin as an out port. When a low sign arisen through
this pin to the relay the relay is switched ON and when a high sign arisen the relay is switched
OFF. A constant delay is created between the two events and hence the relay switches ON and
OFF in regular intervals of time.

#include <LPC214X.H> //LPC2148HEADER


# define relay 1<<30 //ASSIGNP0.30 Pin to RELAY input PIN
void DELAY(void) // Delayfunction
{
unsigned int i;
i=0xffffff;
while(i--);
}
int main(void) // Main program
{
IODIR0=1<<30; //P0.30Port Pin as Out port
while(1)
{
IOSET0=1<<30; //SWITCHOFFRELAY
DELAY(); //CALLDELAY
IOCLR0=1<<30; // SWITCH ON RELAY
DELAY(); // CALLDELAY
} //REPEATLOOP
}
3. INTERFACING OF DAC-ARMLPC2148
A digital-to-analog converter is a device for converting a digital signal into to an analog signal
(current or voltage ). Digital-to-Analog Converters are the interface between the abstract digital
world and the analog real world. Simple switches, a network of resistors, current sources or
capacitors may be used to implement this conversion. A DAC inputs a binary number and outputs
an analog voltage or current signal. The Microchip Technology Inc. MCP4921 is 2.7 – 5.5V, low-
power, 12-Bit Digital-to-Analog Converter (DAC) with SPI interface. The MCP4921 DAC
provides high accuracy and low noise performance for industrial applications where calibration or
compensation of signals is required. With an SPI connection there is always one master device
(usually a microcontroller) which controls the peripheral devices. Typically there are three lines
common to all the devices,

Master In Slave Out (MISO) -The Slave line for sending data to the master,
Master Out Slave In (MOSI) -The Master line for sending data to the peripherals,
Serial Clock (SCK) - The clock pulses which synchronize data transmission generated by the
master,
Slave Select pin - the pin on each device that the master can use to enable and disable specific
devices.
When a device's Slave Select pin is low, it communicates with the master. When it's high, it
ignores the master. In SPI, the clock signal is controlled by the master device LPC2148 . All data
is clocked in and out using this pin. These lines need to be connected to the relevant pins on the
LPC21xx processor. Any unused GIO pin can be used for CS, instead pull this pin high.
Conversion speed is the time it takes for the DAC to provide an analog output when the digital
input word is changed. The MCP4291 DAC - SPI connections with LPC21xx have four I/O
lines(P0.4 – P0.7) required. The analog output is generated by using these four lines.
#include <lpc214x.h>

#include <stdint.h>
#define CS_PIN (1 << 10) // Assuming CS pin is connected to P0.10
void SPI_Init(void) {
// Configure SPI pins (SCK, MISO, MOSI) and CS pin
PINSEL0 |= (1 << 8) | (1 << 10); // P0.4 = SCK0, P0.6 = MISO0
PINSEL0 |= (1 << 12) | (1 << 14); // P0.5 = MISO0, P0.7 = MOSI0
IO0DIR |= CS_PIN; // Set CS pin as output
IO0SET = CS_PIN; // Set CS pin high initially
// Enable SPI0, Master mode, CPOL=0, CPHA=0, Clock divider = 8
SSPCR0 = 0x00C7;
// Enable SPI0
SSPCR1 = 0x02;
}
void SPI_Send(uint16_t data) {
IO0CLR = CS_PIN; // Set CS pin low to enable communication
SSPDR = (0x7000 | data); // Write data to SSPDR register
while (!(SSPSR & (1 << 4))); // Wait until transmission is complete
IO0SET = CS_PIN; // Set CS pin high to disable communication
}
int main(void) {
SPI_Init(); // Initialize SPI peripheral
while (1) {
// Generate a ramp signal (0 to 4095) and send it to the DAC
for (uint16_t i = 0; i <= 4095; i++) {
SPI_Send(i);
}
}
}
DIGITAL TO ANALOG CONVERTER (DAC) IN LPC2148

LPC2148 has one 10-bit DAC


Settling time software selectable
DAC output can drive max of 700 micro-Ampere or 350 micro-Ampere
DAC peripheral has only one register, DACR
DAC Register Pin Description:
Pin Type Description
AOUT Output Analog Output. After the selected settling time after the DACR is
written with a new value, the voltage on this pin (withrespect to
VSSA) is VALUE/1024 * VREF.
VREF Reference Voltage Reference. This pin provides a voltage reference levelfor the
D/A converter.
VDDA, Power Analog Power and Ground. These should be nominally the same
VSSA voltages as V3 and VSSD, but should be isolated to minimize noise
and error.
Digital to Analog Control Register (DACR) Description

31-17 16 15-6 5-0


Reserved BIAS 10-bit Digital Value Reserved

Bit Symbol Value Description Reset


value
5:0 - Reserved, user software should not write ones to
reserved NA bits. The value read from a reserved bit NA
is not defined.
15:6 VALUE After the selected settling time after this field is 0
written with a 0 new VALUE, the voltage on the
AOUT pin (with respect to VssA) is VALUE/1024 *
VREF.
16 BIAS 0 The settling time of the DAC is 1 µs max, andthe 0
maximum current is 700 µA.
1 The settling time of the DAC is 2.5 µs and the
maximum current is 350 µA.
31:17 - Reserved, user software should not write ones to NA
reserved NA bits. The value read from a reserved bit
is not defined.
Figure: Configuring internal DAC of LPC2148

3.1 PROGRAM FOR SINE WAVEFORM GENERATION USING DACINLPC2148

#include <lpc214x.h>
#include <stdint.h>
void delay_ms(uint16_t j)
{
uint16_t x, i;
for (i = 0; i < j; i++) {
for (x = 0; x < 6000; x++); /* loop to generate 1 millisecond delay with Cclk = 60MHz */
}
}
int main(void) {
uint16_t value;
uint8_t i;
i = 0;
PINSEL1 = 0x00080000; /* P0.25 as DAC output */
uint16_t sin_wave[42] = {
512, 591, 665, 742, 808, 873, 926, 968, 998, 1017, 1023, 1017, 998, 968, 926, 873, 808, 742,
665, 591, 512, 436, 359, 282, 216, 211, 151, 97, 55, 25, 6, 0, 6, 25, 55, 97, 151, 211, 216, 282, 359,
436
};

while (1) {
while (i != 42) {
value = sin_wave[i];
DACR = ((1 << 16) | (value << 6)); // Bias bit = 1, Digital value left-shifted by 6 bits
delay_ms(1);
i++;
}
i = 0;
}
}
3.2 PROGRAM FOR GENERATINGTRIANGULAR WAVEFORM GENERATION
#include <lpc214x.h>
#include <stdint.h>
void delay_ms(uint16_t j)
{
uint16_t x, i;
for (i = 0; i < j; i++) {
for (x = 0; x < 6000; x++); /* loop to generate 1 millisecond delay with Cclk = 60MHz */
}
}
int main(void) {
uint16_t value;
uint8_t i;
i = 0;
PINSEL1 = 0x00080000; /* P0.25 as DAC output */
DACR = (1 << 16); // Set DACR with Bias bit = 1 initially
while (1) {
value = 0;
// Generate rising edge of triangular waveform
while (value != 1023) {
DACR = ((1 << 16) | (value << 6));
value++;
delay_ms(1); // Introduce a delay between increments
}
// Generate falling edge of triangular waveform
while (value != 0) {
DACR = ((1 << 16) | (value << 6));
value--;
delay_ms(1); // Introduce a delay between decrements
}
}
}
3.3 PROGRAM FOR GENERATING SQUARE WAVEFORM GENERATION

#include <lpc214x.h>
#include <stdint.h>

void delay_ms(uint16_t j) {
uint16_t x, i;
for (i = 0; i < j; i++)
{
for (x = 0; x < 6000; x++); // Loop to generate 1 millisecond delay with Cclk = 60MHz
}
}
int main(void) {
uint16_t value;
uint8_t i;
i = 0;
PINSEL1 = 0x00080000; // P0.25 as DAC output
while (1) {
// Generate high level of square waveform
value = 1023;
DACR = ((1 << 16) | (value << 6));
delay_ms(50); // Adjusted delay for desired frequency
// Generate low level of square waveform
value = 0;
DACR = ((1 << 16) | (value << 6));
delay_ms(50); // Adjusted delay for desired frequency
}
}
4. LPC2148 INTERFACING WITH ON-CHIP (INTERNAL) ADC:
Analog to Digital Converter (ADC) is used to convert analog signal/voltage into its equivalent digital
number so that microcontroller can process that numbers and make it human readable. The ADC
characterized by resolution. The resolution of ADC indicates the number of digital values. Let’s take
example: In LPC2148 microcontroller we have in-built 10-bit ADC. So for 10-bit ADC resolution is

10-bit and maximum value will be 210=1024. This means our digital value or discrete level lies
between 0 to 1023. There is one more term important to understand while dealing with ADC and it is
step size. Step size is the minimum change in input voltage which can be resolved by ADC. The
concept of step size is closely associated with the resolution of ADC.

So in this case we can measure minimum 2.23 mV (Approx.) with our microcontroller. This is how
step size defines an accuracy of ADC circuit.
Features of ADC:
 2 internal ADC's - ADC0 (6 Channel), ADC1 (8 Channel)

 Type: 10-bit, Successive Approximation type,

 Supports burst mode (repeated conversion at 3-bit to 10-bit resolution)

 Supports simultaneous conversion on both ADC's

 Conversion time: 2.44 micro-seconds

 Start of Conversion by software control / on timer match /transition on a pin

 Range: 0 V – VREF (+3.3 V)

 Max. clock frequency is 4.5 MHz, (by programming ADC Control (ADxCONRegister)

On-Chip ADC in LPC2148-Internal Diagram


Pin Assignment for ADC in LPC2148
Block Symbol Description I/O
AD0.1 Channel 1 P0.28
AD0.2 Channel 2 P0.29
AD0.3 Channel 3 P0.30
ADC0
AD0.4 Channel 4 P0.25
AD0.6 Channel 6 P0.4
AD0.7 Channel 7 P0.5
AD1.0 Channel 0 P0.6
AD1.1 Channel 1 P0.8
AD1.2 Channel 2 P0.10
AD1.3 Channel 3 P0.12
ADC1
AD1.4 Channel 4 P0.13
AD1.5 Channel 5 P0.15
AD1.6 Channel 6 P0.21
AD1.7 Channel 7 P0.22
ADC REGISTERS:
1. AD0CR (ADC0 Control Register)
AD0CR is a 32-bit register.
This register must be written to select the operating mode before A/D conversion can occur.
It is used for selecting channel of ADC, clock frequency for ADC, number of clocks or number of
bits in result, start of conversion and few other parameters.

AD0
CR (ADC0 Control Register)
Bits 7:0 – SEL
These bits select ADC0 channel as analog input. In software-controlled mode, only one of these bits
should be 1.e.g. bit 7 (10000000) selects AD0.7 channel as analog input.
Bits 15:8 – CLKDIV
The APB(ARM Peripheral Bus)clock is divided by this value plus one, to produce the clock for
ADC.
This clock should be less than or equal to 4.5MHz.
Bit 16 – BURST
0 = Conversions are software controlled and require 11 clocks
1 = In Burst mode ADC does repeated conversions at the rate selected by the CLKS field for the
analog inputs selected by SEL field. It can be terminated by clearing this bit, but the conversion that
is in progress will be completed.
When Burst = 1, the START bits must be 000, otherwise the conversions will not start.
Bits 19:17 – CLKS
Selects the number of clocks used for each conversion in burst mode and the number of bits of
accuracy of Result bits of AD0DR.
e.g. 000 uses 11 clocks for each conversion and provide 10 bits of result in
corresponding ADDR register.
000 = 11 clocks / 10 bits
001 = 10 clocks / 9 bits
010 = 9 clocks / 8 bits
011 = 8 clocks / 7 bits
100 = 7 clocks / 6 bits
101 = 6 clocks / 5 bits
110 = 5 clocks / 4 bits
111 = 4 clocks / 3 bits
Bit 20 – RESERVED
Bit 21 – PDN
0 = ADC is in Power Down mode
1 = ADC is operational
Bit 23:22 – RESERVED
Bit 26:24 – START
When BURST bit is 0, these bits control whether and when A/D conversion is started
000 = No start (Should be used when clearing PDN to 0)
001 = Start conversion now
010 = Start conversion when edge selected by bit 27 of this register occurs on CAP0.2/MAT0.2 pin
011= Start conversion when edge selected by bit 27 of this register occurs on CAP0.0/MAT0.0 pin
100 = Start conversion when edge selected by bit 27 of this register occurs on MAT0.1 pin
101 = Start conversion when edge selected by bit 27 of this register occurs on MAT0.3 pin
110 = Start conversion when edge selected by bit 27 of this register occurs on MAT1.0 pin
111 = Start conversion when edge selected by bit 27 of this register occurs on MAT1.1 pin
Bit 27 – EDGE
This bit is significant only when the Start field contains 010-111. In these cases,
0 = Start conversion on a rising edge on the selected CAP/MAT signal
1 = Start conversion on a falling edge on the selected CAP/MAT signal
Bit 31:28 – RESERVED
2. AD0GDR (ADC0 Global Data Register)
AD0GDR is a 32-bit register.
This register contains the ADC’s DONE bit and the result of the most recent A/D conversion.

AD0G
DR (ADC0 Global Data Register)
Bit 5:0 – RESERVED
Bits 15:6 – RESULT
When DONE bit is set to 1, this field contains 10-bit ADC result that has a value in the range of 0
(less than or equal to VSSA) to 1023 (greater than or equal to VREF).
Bit 23:16 – RESERVED
Bits 26:24 – CHN
These bits contain the channel from which ADC value is read.
e.g. 000 identifies that the RESULT field contains ADC value of channel 0.
Bit 29:27 – RESERVED
Bit 30 – Overrun
This bit is set to 1 in burst mode if the result of one or more conversions is lost and overwritten
before the conversion that produced the result in the RESULT bits.
This bit is cleared by reading this register.
Bit 31 – DONE
This bit is set to 1 when an A/D conversion completes. It is cleared when this register is read and
when the AD0CR is written.
If AD0CR is written while a conversion is still in progress, this bit is set and new conversion is
started.
3. ADGSR (A/D Global Start Register)
ADGSR is a 32-bit register.
Software can write to this register to simultaneously start conversions on both ADC.

ADGSR (A/D Global Start Register)


BURST (Bit 16), START (Bit <26:24>) & EDGE (Bit 27)
These bits have same function as in the individual ADC control registers i.e. AD0CR & AD1CR.
Only difference is that we can use these function for both ADC commonly from this register.
4. AD0STAT (ADC0 Status Register)
AD0STAT is a 32-bit register.
It allows checking of status of all the A/D channels simultaneously.

AD0STAT (ADC0 Status Register)


Bit 7:0 – DONE7:DONE0
These bits reflect the DONE status flag from the result registers for A/D channel 7 - channel 0.
Bit 15:8 – OVERRUN7:OVERRUN0
These bits reflect the OVERRUN status flag from the result registers for A/D channel 7 - channel 0.
Bit 16 – ADINT
This bit is 1 when any of the individual A/D channel DONE flags is asserted and enables ADC
interrupt if any of interrupt is enabled in AD0INTEN register.
Bit 31:17 – RESERVED
5. AD0INTEN (ADC0 Interrupt Enable)
AD0INTEN is a 32-bit register.
It allows control over which channels generate an interrupt when conversion is completed.
AD0INTEN (ADC0 Interrupt Enable)
Bit 0 – ADINTEN0
0 = Completion of a A/D conversion on ADC channel 0 will not generate an interrupt
1 = Completion of a conversion on ADC channel 0 will generate an interrupt
Remaining ADINTEN bits have similar description as given for ADINTEN0.
Bit 8 – ADGINTEN
0 = Only the individual ADC channels enabled by ADINTEN7:0 will generate interrupts
1 = Only the global DONE flag in A/D Data Register is enabled to generate an interrupt
6. AD0DR0-AD0DR7 (ADC0 Data Registers)
These are 32-bit registers.
They hold the result when A/D conversion is completed.
They also include flags that indicate when a conversion has been completed and when a conversion
overrun has occurred.

AD0 Data Registers Structure


Bit 5:0 – RESERVED
Bits 15:6 – RESULT
When DONE bit is set to 1, this field contains 10-bit ADC result that has a value in the range of 0
(less than or equal to VSSA) to 1023 (greater than or equal to VREF).
Bit 29:16 – RESERVED
Bit 30 – Overrun
This bit is set to 1 in burst mode if the result of one or more conversions is lost and overwritten
before the conversion that produced the result in the RESULT bits.
This bit is cleared by reading this register.
Bit 31 – DONE
This bit is set to 1 when an A/D conversion completes. It is cleared when this register is read.
Steps for Analog to Digital Conversion
 Configure the ADxCR (ADC Control Register) according to the need of application.
 Start ADC conversion by writing appropriate value to START bits in ADxCR. (Example,
writing 001 to START bits of the register 26:24, conversion is started immediately).
 Monitor the DONE bit (bit number 31) of the corresponding ADxDRy (ADC Data Register)
till it changes from 0 to 1. This signals completion of conversion. We can also monitor DONE
bit of ADGSR or the DONE bit corresponding to the ADC channel in the ADCxSTAT
register.
 Read the ADC result from the corresponding ADC Data Register.
ADxDRy. E.g. AD0DR1 contains ADC result of channel 1 of ADC0.
5. INTERFACING OF 2X16 LCD MODULE-LPC21XX
Interfacing 16X2 LCD to ARM Microcontroller (LPC2148)

Description:
LCD (liquid crystal display) screen is an electronic display module.
LCD 16x2 means it can display16 characters per line and there are two such lines.

LCD (liquid crystal display) screen is an electronic display module.LCD 16x2 means it can display16
characters per line and there are two such lines.
DB0-DB7:-Data pins:The data and command are given through these pins.
EN:-enable The lcd controller take the data from data pins only get a high to low transition in enable pin.
R/W : Mode of operation is select using this pin. Giving low to this pin for write something on lcd .read
from lcd by making this pin in high state.
RS: Register select pin.LCD has two registers namely data register and command register.
 Writing commands codes for initializing the LCD:
 For display something on LCD first we have to initialize the LCD by giving some commands.
 Make RS pin low for giving commands.
 The LCD controller will take the values in data pins as commands when RS pin is in low state.
 If RS is in high state the controller will take the values in data pins for displaying.
INSTRUCTION HEX CODE
8bit ,1Line ,5x7Dots : 0x30
8bit ,2Line ,5x7Dots : 0x38
4bit ,1Line ,5x7Dots : 0x20
bit ,2Line ,5x7Dots : 0x28
Entry Mode : 0x06
Display off cursor off (clearing display without clearing content in DDRAM) : 0x08
Display on cursor on : 0x0E
Display on cursor off : 0X0C
Display on cursor blinking : 0X0F
Shift entire display left : 0X18
Shift entire display right : 0X1C
Move cursor left by one character : 0X10
Move cursor right by one character : 0x14
Clear display and DDRAM content : 0x01
LCD can interface with a controller in two modes.
8bit mode: - in this mode using 8 port pins of the controller to give the data to LCD. This is the default
mode.
4bit mode:-if we want to reduce the port pins of the controller for LCD we can use this mode, in this
mode we supply data in the form of nibbles. Data pins D4-D7 are used in this mode.
There are two 8-bit registers in LCD controller Instruction and Data register.

Instruction Register (IR)


Instruction register corresponds to the register where you send commands to LCD e.g LCD shift
command, LCD clear, LCD address etc.

Data Register (DR): Data register is used for storing data which is to be displayed on LCD. when send
the enable signal of the LCD is asserted, the data on the pins is latched in to the data register and data is
then moved automatically to the DDRAM and hence is displayed on the LCD. Data Register is not only
used for sending data to DDRAM but also for CGRAM, the address where you want to send the data, is
decided by the instruction you send to LCD.

Commands and Instruction set


Only the instruction register (IR) and the data register (DR) of the LCD can be controlled by the MCU.
Before starting the internal operation of the LCD, control information is temporarily stored into these
registers to allow interfacing with various MCUs, which operate at different speeds, or various peripheral
control devices. The internal operation
of the LCD is determined by signals sent from the MCU. These signals, which include register selection
signal (RS), read/write signal (R/W), and the data bus (DB0 to DB7), make up the LCD instructions
(Table 3). There are four categories of instructions that:

 Designate LCD functions, such as display format, data length, etc.


 Set internal RAM addresses
 Perform data transfer with internal RAM
 Perform miscellaneous functions
LCD Initialization
Before using the LCD for display purpose, LCD has to be initialized either by the internal reset circuit or
sending set of commands to initialize the LCD. It is the user who has to decide whether an LCD has to be
initialized by instructions or by internal reset circuit. we will dicuss both ways of initialization one by
one.

Sending Commands to LCD


To send commands we simply need to select the command register. Everything is same as we have done
in the initialization routine. But we will summarize the common steps and put them in a single subroutine.
Following are the steps:

 Move data to LCD port


 select command register
 select write operation
 send enable signal
 wait for LCD to process the command
Sending Data to LCD
To send data we simply need to select the data register. Everything is same asthe command routine.
Following are the steps:

 Move data to LCD port


 select data register
 select write operation
 send enable signal
 wait for LCD to process the data

LCD Display commands:

Clear Display (0x01):Clears the display and returns the cursor to the home position.

Return Home (0x02):Sets the cursor to the home position (address 0) without clearing the display.

Entry Mode Set (0x04):Sets the cursor direction (left-to-right or right-to-left) and specifies whether the
display should shift when writing new characters.

Display On/Off Control (0x08):Controls the display, cursor, and cursor blink settings.

Cursor/Display Shift (0x10):Shifts the display or moves the cursor without changing the display data.

Function Set (0x20):Sets interface data length (4 or 8 bits), number of display lines (1 or 2), and
character font.

Set CGRAM Address (0x40 to 0x7F):Sets the CGRAM (Character Generator RAM) address for storing
custom character patterns.

Set DDRAM Address (0x80 to 0xFF):Sets the DDRAM (Display Data RAM) address for writing
characters to be displayed.

Scroll Display (0x18):Scrolls the entire display either left or right.

Set Cursor Position (0x80 + address):Sets the cursor position by specifying the DDRAM address.

Set Display Start Line (0x40 + line):Sets the start line of the display when using multiple lines.

Cursor Home (0x02):Sets the cursor to the home position without clearing the display. This is similar to
Return Home but is a duplicate command.

Display Control (0x08):Controls the display on/off, cursor on/off, and cursor blink settings. This is a
duplicate command of Display On/Off Control.

Cursor Control (0x0C):Controls the cursor on/off and cursor blink settings. This is a duplicate command
of Display On/Off Control.

Display Shift (0x10 + direction):Shifts the entire display left or right without changing the display data
Set CGRAM Data (0x40 + data):Writes data to CGRAM for defining custom characters.

Set DDRAM Data (0x80 + data):Writes data to DDRAM for displaying characters on the LCD.
PROGRAM TO INTERFACE LCD DISPLAY:

#include <LPC214x.H> /* LPC214x definitions */


#define RS (1<<16) //Set/Reset Pin (Control_Pin)
#define EN (1<<17)
void Delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<250;j++);
}

void cmd_lcd(unsigned int cmd)


{
IOCLR1=RS;
IOSET1=EN;
IOSET1=((cmd)&(0xff))<<18;
Delay(50);
IOCLR1=EN;
IOCLR1=RS;
IOCLR1=((cmd)&(0xff))<<18;
}
void data_lcd(unsigned int data)
{
IOSET1=RS;
IOSET1=EN;
IOSET1=((data)&(0xff))<<18;
Delay(50);
IOCLR1=EN;
IOCLR1=RS;
IOCLR1=((data)&(0xff))<<18;
}
void init_lcd()
{
cmd_lcd(0X38); //8 BIT LCD
cmd_lcd(0X06); //DISPLAY OFF CURSOR OFF
cmd_lcd(0X0C); //DISPLAY ON CURSOR OFF
cmd_lcd(0X01); //CLEAR THE LCD
}
int main (void)
{
unsigned char s[]="WELCOME TO LBRCE ",i,j;
IODIR1=0XfffF0000;
init_lcd();
Delay(50);
while(1)
{
cmd_lcd(0x80); // Initial starting possion
i=0;
for(;s[i]!='\0';)
data_lcd(s[i++]); // Print the data up to null character
i=0;
j=s[0]; // Take the 1st character in j
for(;s[i]!='\0';i++)
s[i] = s[i+1]; // Shift the characters in array
s[i-1]=j; // Take the 1st character in last
s[i]='\0'; // Finally take null character
Delay(500);
}

6. INTERFACING OF STEPPER MOTORS

# include <LPC214X.H> // LPC2148 HEADER


void delay_ms() ; // Delay function
void main() ; // Main program starts
{
PINSEL2 = 0X00000000; // Set P1.19 TO P1.22 as GPIO
IO1DIR=0x000000F0 ; // Set Port 1 as out port
while(1) // Infinite Loop
{
IO1PIN = 0X00000090; // Send the code1 for phase 1
delay_ms() ; // Call Delay
IO0PIN = 0X00000050 ; // Send the code 2 for phase 2
delay_ms() ; // Call Delay
IO1PIN = 0X00000060 ; // Send the code 3 for phase 3
delay_ms() ; // Call Delay
IO1PIN = 0X000000A0 ; // Send the code 3 for phase 3
delay_ms() ; // Call Delay
}
}
void delay_ms() // Delay function program
{
int i,j ;
for(i=0;i<0x0a;i++)
for (j=0;j<750;j++) ;
}
7. INTERFACING -GSM AND GPS:

GSM MODULE INTERFACING WITH LPC2148

GSM/GPRS module is used to establish communication between a computer and a GSM-GPRS system.
Global System for Mobile communication (GSM) is an architecture used for mobile communication in
most of the countries. Global Packet Radio Service (GPRS) is an extension of GSM that enables higher
data transmission rate. GSM (Global System for Mobile Communications) is the technology of the
world's mobile phone networks. GSM is an open, digital cellular technology used for transmitting mobile
voice and dataservices. GSM operates in the 900MHz and 1.8GHz bands GSM supports data transfer
speedsof up to 9.6 kbps, allowing the transmission of basic data services such as SMS. The SIM300
module is a Triband GSM/GPRS solution in a compact plug in module featuring an industry-standard
interface

Features of GSM MODEM

 Single supply voltage 3.2v-4.5v


 Typical power consumption in SLEEP Mode: 2.5mA.
 SIM300 tri-band
 MT,MO,CB, text and PDU mode, SMS storage: SIM card
 Supported SIM Card :1.8V,3V

GSM Module functional blocks


GSM Mobile Vs GSM Module

A GSM mobile is a complete system in itself with embedded processors that are dedicated to provide an
interface between the user and the mobile network. The AT commands are served between the
processors of the mobile termination and the terminal equipment. The mobile handset can also be
equipped with a USB interface to connect with a computer, but it may or may not support AT commands
from the computer or an external processor/controller. The GSM/GPRS module, on the other hand,
always needs a computer or external processor/controller to receive AT commands from. GSM/GPRS
module itself does not provide any interface between the user and the network, but the computer to
which module is connected is the interface between user and network. An advantage that GSM/GPRS
modules offer is that they support concatenated SMS which may not be supported in some GSM mobile
handsets. Applications of GSM/GPRS module The GSM/GPRS module demonstrates the use of AT
commands. They can feature all the functionalities of a mobile phone through computer like making and
receiving calls, SMS, MMS etc. These are mainly employedfor computer based SMS and MMS services.

AT Commands: (Attention Commands):AT commands are used to control MODEMs..

GSM AT Commands and their functions


AT Command Function of AT Command
ATD Dial
AT+CGMS Send SMS Message
AT+CMSS Send SMS Message from storage
AT+CMGL List SMS Messages
AT+CMGR Read SMS Messages
AT+CSCA? Service Centre Address
AT+CPMS To choose storage from ME or SM
AT+IPR=0 To choose auto from baud rate
AT+CMGF= To choose PDU Mode or Text Mode

UART data format


8. INTERFACING OF GSM MODULE:
interfacing of LPC2148 with GSM modem using UART protocol, MAX232 IC is used for voltage level
shifting from 0V/5V to -12V/+12V.

GSM modem interfacing with LPC2148


Pin assignment for GSM interfacing
UART DB-9 Connector LPC2148 Processor Lines
TXD-0 P0.0
UART0 (P1) ISP PGM RXD-0 P0.1
TXD-1 P0.8
UART1 (P2) RXD-1 P0.9
Algorithm for GSM module interfacing with LPC2148
1) Start
2) Initialise UART0 or UART1 serial interface using following instructionPINSEL0=0X0000
0005;//Enable P0.0-TxD0,P0.1-RxD0 U0LCR=0X83; //8-BIT Character length, NO parity,1 stop bit
U0DLL=97; //Baud rate=9600@PCLK=15Mhz – Set the data rateU0LCR=0X03; Divisor Latch
Access Bit (DLAB) to Zero
3) Transmit different AT commands through UART module using instruction
while(!(U0LSR&0X20));//Monitor TI flag
4) If transmission buffer is Empty, Transmit AT commands
5) U0THR=ch; // U0THR (UART0 Transmit Holding Register)
6) Provide delay while transmitting each command
7) To transmit a single character use PUTCH function & to transmit a string use PUTSfunction
8) END
PROGRAM FOR GSM INTERFACING
GPS MODULE INTERFACING

The SKG13BL is a complete GPS engine module that features super sensitivity, ultra low power and
small form factor. The GPS signal is applied to the antenna input of module, and a complete serial data
message with position, velocity and time information is presented at the serial interface with NMEA
protocol or custom protocol.

It is based on the high performance features of the MediaTek MT3337 single- chip architecture,
Its –165dBm tracking sensitivity extends positioning coverage into place like urban canyons and dense
foliage environment where the GPS was not possible before. The small form factor and low power
consumption make the module easy to integrate into portable device like PNDs, mobile phones,
cameras and vehicle navigation systems.

Features of GPS module

 Ultra high sensitivity: -165dBm

 Built-in 12 multi-tone active interference canceller

 Low power consumption: Typical 22mA@3.3V

 ±10ns high accuracy time pulse (1PPS)

 NMEA Output:GGA,GSA,GSV,RMC

 Advanced Features: AlwaysLocate; AIC

 QZSS,SBAS(WAAS,EGNOS,MSAS,GAGAN)

 UART interface: 4800/9600/38400/115200 bps

 Small form factor: 15x13x2.2mm and RoHS compliant (Lead-free)

Applications

 LBS (Location Based Service)


 PND (Portable Navigation Device)
 Vehicle navigation system
 Mobile phone
 Extremely fast TTFF at low signal level
Interfacing of GPS Module

UART DB-9 Connector LPC2148 Processor Lines


UART0 (P1) ISP PGM
TXD-0 P0.0
RXD-0 P0.1
UART1 (P2)
TXD-1 P0.8
RXD-1 P0.9

Algorithm for GPS module interfacing with LPC2148

1) Start
2) Initialise UART0 or UART1 serial interface using following instruction PINSEL0=0X0000
0005;//Enable P0.0-TxD0,P0.1-RxD0
3) U0LCR=0X83; //8-BIT Character lenth,NO parity,1 stop bit
4) U0DLL=97; //Baud rate=9600@PCLK=15MhzU0LCR=0X03;//Dlab=0
5) Receive GPS Message of location and longitude through UART module using function
6) UARTGetch()
7) Store single character in Variable GPSData GPSDATA=Uart0Getch();
8) Copy each single received character in array lattitude and longitude
9) Send this array characters to LCD for displaying message
10) END
PROGRAM FOR GPS INTERFACING
INTERFACING OF REAL TIME CLOCK
Program to display DD/MM/YYYY
#include <lpc214x.h>
#include "UART0.h"
typedef struct {
uint8_t seconds, minutes, hours, day_of_month, month;
uint16_t year;
} RTC_Time;

RTC_Time currentTime;

void RTC_ISR(void) __irq {


ILR |= 0x01; // Clear RTC interrupt flag
VICVectAddr = 0; // Acknowledge interrupt
}

void RTC_Set_Time(RTC_Time setTime) {


SEC = setTime.seconds;
MIN = setTime.minutes;
HOUR = setTime.hours;
DOM = setTime.day_of_month;
MONTH = setTime.month;
YEAR = setTime.year;
}

RTC_Time RTC_Get_Time(void) {
RTC_Time time;
time.seconds = SEC;
time.minutes = MIN;
time.hours = HOUR;
time.day_of_month = DOM;
time.month = MONTH;
time.year = YEAR;
return time;
}

int main(void) {
IO0DIR = 0x00000010; // Set P0.4 as output pin for LED
UART0_init();

VICVectAddr0 = (unsigned)RTC_ISR;
VICVectCntl0 = 0x0000002D;
VICIntEnable = 0x00002000;
VICIntSelect = 0x00000000;

RTC_Set_Time((RTC_Time){.seconds = 0, .minutes = 25, .hours = 11, .day_of_month = 6, .month =


10, .year = 2017});
CIIR = 0x01; // Enable seconds value increment interrupt
CCR = 0x01; // Enable RTC clock

while(1) {
currentTime = RTC_Get_Time();
UART0_SendString("Time: ");
UART0_SendInt(currentTime.hours);
UART0_SendChar(':');
UART0_SendInt(currentTime.minutes);
UART0_SendChar(':');
UART0_SendInt(currentTime.seconds);
UART0_SendString(" Date: ");
UART0_SendInt(currentTime.day_of_month);
UART0_SendChar('/');
UART0_SendInt(currentTime.month);
UART0_SendChar('/');
UART0_SendInt(currentTime.year);
UART0_SendChar('\n');
}

return 0;
}
INTERFACING OF SERIAL COMMUNICATION:

Circuit for serial communication with LPC2148 and PC

Algorithm for UART serial communication


1) Start
2) Initialise UART0 serial interface using following instructionPINSEL0=0X0000 0005;//Enable
P0.0-TxD0,P0.1-RxD0 U0LCR=0X83; //8-BIT Character lenth,NO parity,1 stop bit, DLAB=1
U0DLL=97; //Baud rate=9600@PCLK=15Mhz U0LCR=0X03;//DLAB=0
3) LPC2148 will receive characters transmitted by PC
4) LPC2148 will transmit the characters received back to PC
5) Transmit different AT commands through UART module using instruction
while(!(U0LSR&0X20));//Monitor TI flag
6) If transmission buffer is Empty,Transmit single character at a timeU0THR=ch;
7) Provide delay while transmitting each command
8) To transmit a single character use PUTCH function & to transmit a string use PUTSfunction
9) END
Program for Serial Transmission and Reception:

You might also like