Arm Interfacing
Arm Interfacing
Arm Interfacing
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);
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.
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
#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.
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
#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)
Max. clock frequency is 4.5 MHz, (by programming ADC Control (ADxCONRegister)
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.
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.
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.
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.
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:
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
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.
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.
NMEA Output:GGA,GSA,GSV,RMC
QZSS,SBAS(WAAS,EGNOS,MSAS,GAGAN)
Applications
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;
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;
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: