arm7 lpc2148 1
Post on 04-Jan-2016
770 Views
Preview:
TRANSCRIPT
How to Interface LCD with LPC2148 ARM
ARM7 LPC2148 Primer Board
The ARM7 LPC2148 Primer board is specifically designed to help students to master the
required skills in the area of embedded systems. The kit is designed in such way that all the
possible features of the microcontroller will be easily used by the students. The kit supports in
system programming (ISP) which is done through serial port.
NXP’s ARM7 (LPC2148), ARM Primer Kit is proposed to smooth the progress of developing and
debugging of various designs encompassing of High speed 32-bit Microcontrollers. LCD (Liquid Crystal Display)
Liquid Crystal Display also called as LCD is very helpful in providing user interface as well as for
debugging purpose. A liquid crystal display (LCD) is a flat panel display that uses the light
modulating properties of liquid crystals (LCs). LCD Modules can present textual information to
user. Interfacing LCD
Fig. 1 shows how to interface the LCD to microcontroller. The 2x16 character LCD interface card
with supports both modes 4-bit and 8-bit interface, and also facility to adjust contrast through
trim pot. In 4-bit interface 7 lines needed to create 4-bit interface; 4 data bits (D0 – D3), three
control lines, address bit (RS), read/write bit (R/W) and control signal (E).
Fig. 1 Interfacing 4 bit LCD to Microcontroller
Interfacing 4 bit LCD with LPC2148
We now want to display a text in LPC2148 Primer Board by using 4 bit LCD module.
The ARM7 LPC2148 Primer board has seven numbers of LCD connections are needed to create
4-bit interface; connected with 4 data bits (P0.19 – P0.22, D4-D7), address bit (RS-P0.16),
read/write bit (R/W-P0.17) and control signal (E-P0.18) to make LCD display. Pin Assignment with LPC2148
LCD MODULE LPC2148 LINES 2x16 LCD Selection
CONTROL
RS P0.16
RW P0.17
E P0.18
DATA
LINES
D0-D3 NC
D4 P0.19
D5 P0.20
D6 P0.21
D7 P0.22
Make switch SW28 to ‘LCD’ label marking
position
Circuit Diagram to Interface 4 bit LCD with LPC2148
Source Code
The Interfacing 4 bit LCD with LPC2148 program is very simple and straight forward, which
display a text in 2 X 16 LCD module using 4 data lines only. Some delay is occurring when a
single command / data is executed.
C Program to display a text in 4 bit LCD using LPC2148
******************************************************************************
*********
Title : Program to 4 bit LCD display
******************************************************************************
*********
#include <lpc214x.h>
#include <stdio.h>
#define RS 0x10000
#define RW 0x20000
#define EN 0x40000
void lcd_cmd (unsigned char);
void lcd_data (unsigned char);
void lcd_initialize (void);
void lcd_display (void);
void LCD4_Convert(unsigned char);
const unsigned char cmd[4] = {0x28,0x0c,0x06,0x01};
unsigned char msg[] = {">PS-Primer 2148<"};
unsigned char msg1[]= {":: LCD Demo! ::"};
void main()
{
PINSEL1 = 0;
IODIR0 = 0xFF << 16;
lcd_initialize();
lcd_display();
while(1);
}
void delay(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<12000;j++);
}
void lcd_cmd(unsigned char data)
{
IOCLR0 |= RS; //0x1000; //RS
IOCLR0 |= RW; //0x2000; //RW
LCD4_Convert(data);
}
void lcd_initialize(void)
{
int i;
for(i=0;i<4;i++)
{
IOCLR0 = 0xF << 19; //IOCLR 0/1
lcd_cmd(cmd[i]);
delay(15);
}
}
void lcd_data (unsigned char data)
{
IOSET0 |= RS; //0x1000; //RS
IOCLR0 |= RW; //0x2000; //RW
LCD4_Convert(data);
}
void lcd_display (void)
{
char i;
/* first line message */
lcd_cmd(0x80);
delay(15);
i=0;
while(msg[i]!='\0')
{
delay(5);
lcd_data(msg[i]);
i++;
}
delay(15);
/* second line message */
lcd_cmd(0xc0);
delay(15);
i=0;
while(msg1[i]!='\0')
{
delay(5);
lcd_data(msg1[i]);
i++;
}
delay(15);
}
void LCD4_Convert(unsigned char c)
{
if(c & 0x80) IOSET0 = 1 << 22; else IOCLR0 = 1 << 22;
if(c & 0x40) IOSET0 = 1 << 21; else IOCLR0 = 1 << 21;
if(c & 0x20) IOSET0 = 1 << 20; else IOCLR0 = 1 << 20;
if(c & 0x10) IOSET0 = 1 << 19; else IOCLR0 = 1 << 19;
IOSET0 = EN;
delay(8);
IOCLR0 = EN;
if(c & 0x08) IOSET0 = 1 << 22; else IOCLR0 = 1 << 22;
if(c & 0x04) IOSET0 = 1 << 21; else IOCLR0 = 1 << 21;
if(c & 0x02) IOSET0 = 1 << 20; else IOCLR0 = 1 << 20;
if(c & 0x01) IOSET0 = 1 << 19; else IOCLR0 = 1 << 19;
IOSET0 = EN;
delay(8);
IOCLR0 = EN;
}
To compile the above C code you need the KEIL software. They must be properly set up and a
project with correct settings must be created in order to compile the code. To compile the
above code, the C file must be added to the project.
In Keil, you want to develop or debug the project without any hardware setup. You must
compile the code for generating HEX file. In debugging Mode, you want to check the port
output without microcontroller Board.
The Flash Magic software is used to download the hex file into your microcontroller through
UART0. Testing the LCD Module with LPC2148
Give +3.3V power supply to LPC2148 Primer Board; the LCD is connected with microcontroller
LPC2148 Primer Board. When the program is downloading into LPC2148 in Primer Board, the
screen should show the text messages.
If you not reading any text from LCD, then you just check the jumper connections & adjust the
trim pot level. Otherwise you just check it with debugging mode in Keil.
How to Interface Sensor with LPC2148 ARM Evaluation Board
ARM7 LPC2148 Evaluation Board
The ARM7 LPC2148 Evaluation board is specifically designed to help students to master the
required skills in the area of embedded systems. The kit is designed in such way that all the
possible features of the microcontroller will be easily used by the students. The kit supports in
system programming (ISP) which is done through serial port.
NXP’s ARM7 (LPC2148), ARM Evaluation Kit is proposed to smooth the progress of developing
and debugging of various designs encompassing of High speed 32-bit Microcontrollers. Temperature Sensor
DS1820 is a temperature sensor which is small sensor. The output of sensor converted to digital
that easy connecting with microcontroller. Interfacing ds1820
Fig. 1 shows how to interface the ds1820 to microcontroller. As you can see the first pin is
connected to GND, the third pin is connected to VCC & the second pin is connected to the
Microcontroller. So when the temperature is sensing, it give the sensor reading to controller.
Fig. 1 interfacing ds1820 to Microcontroller
Interfacing ds1820 with LPC2148
We now want to read the temperature in LPC2148 Evaluation Board from temperature sensor
ds1820. The ARM7 LPC2148 Evaluation board uses the ADC pin for reading temperature from
temperature sensor ds1820. The reading output is displayed into PC through UART1.
The 10 bit ADC used to read temperature. 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. Pin Assignment with LPC2148
On-Chip ADC ADC Inputs LPC2148 ADC Select
POT (R28) ADC0.1 P0.28
Circuit Diagram to Interface ds1820 with LPC2148
Source Code
The Interfacing ds1820 with LPC2148 program is very simple and straight forward, that read
temperature from temperature sensor ds1820 and it display into PC through serial port. C Program to read temperature using LPC2148
******************************************************************************
*********
Title : Program to read temperature using ARM7 Evaluation Board
******************************************************************************
*********
#include<LPC214x.h> // Define LPC2148 Header File
#include <stdio.h>
#define DONE 0x80000000
#define START 0x01000000
#define PRESET 0x00230600
void main ()
{
unsigned long Val;
VPBDIV = 0x02;
Serial_Init ();
PINSEL1 = 0x01 << 24; //Configure P0.28 as ADC0.1
Welcome ();
AD0CR = PRESET | 0x02;
AD0CR |= START; //Start Conversion NOW
while (1)
{
do
{
Val = AD0GDR;
}while ((Val & DONE) == 0);
Val = ((AD0GDR >> 6) & 0x3FF);
printf (">> Current Temperature : %4d ", Val);
printf ("\xF8 \F \r");
}
}
void Delay ()
{
unsigned int i,j;
for (i=0;i<50;i++)
for (j=0;j<500;j++);
}
void Welcome ()
{
printf ("-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
.-.-.-.-.-.-.-.\n\r");
printf (" Developed By : R&D Wing \n\r");
printf (" © 2009 Pantech Solutions Pvt Ltd \n\r");
printf ("-----------------------------------------
---------------\n\r");
printf ("*** Temperature Sensor Interfacing with
Tyro Kit ***\n\r");
printf ("-----------------------------------------
---------------\n\r");
printf (">> Put Jumper J in 'E' Mode to Enable
Temp Sensor Block \n\r");
printf (">> Connect UART1 to COM Port @ 9600 Baud Rate\n\n\r");
printf ("***************************************
*****************\n\r");
printf ("************************* Result ***********************\n\r");
printf ("***************************************
*****************\n\n\r");
}
void Serial_Init ()
{
PINSEL0|= 0x00050000; //Configure TxD1 and RxD1
U1LCR = 0x83;
U1DLL = 195;
U1LCR = 0x03;
}
To compile the above C code you need the KEIL software. They must be properly set up and a
project with correct settings must be created in order to compile the code. To compile the
above code, the C file must be added to the project.
In Keil, you want to develop or debug the project without any hardware setup. You must
compile the code for generating HEX file. In debugging Mode, you want to check the port
output without Evaluation Board.
The Flash Magic software is used to download the hex file into your LPC2148 Evaluation Board
through UART0.
Testing the ds1820 with LPC2148
Give +3.3V power supply to LPC2148 Evaluation Board; the serial cable is connected between
the controller and PC. Open the Hyper Terminal screen, select which port you are using and set
the default settings. Now the screen should show the current temperature readings.
Bring a Hot soldering iron tip near the ds1820's pins, don't touch it keep it 1 or 2mm away. The
screen should update with the rising temperature. Now finally touch the pins of ds1820 with
the tip of iron, the temperature should rise quickly. Keep it there until temperature rise to 80
degrees, and then remove the iron.
How to Interface Switch with LPC2148 ARM
ARM7 LPC2148 Primer Board
The ARM7 LPC2148 Primer board is specifically designed to help students to master the
required skills in the area of embedded systems. The kit is designed in such way that all the
possible features of the microcontroller will be easily used by the students. The kit supports in
system programming (ISP) which is done through serial port.
NXP’s ARM7 (LPC2148), ARM Primer Kit is proposed to smooth the progress of developing and
debugging of various designs encompassing of High speed 32-bit Microcontrollers. Switch
A switch is an electrical component that can break an electrical circuit, interrupting the current
or diverting it from one conductor to another. A switch may be directly manipulated by a
human as a control signal to a system, or to control power flow in a circuit. Interfacing Switch
Fig. 1 shows how to interface the switch to microcontroller. A simple switch has an open state
and closed state. However, a microcontroller needs to see a definite high or low voltage level at
a digital input. A switch requires a pull-up or pull-down resistor to produce a definite high or
low voltage when it is open or closed. A resistor placed between a digital input and the supply
voltage is called a "pull-up" resistor because it normally pulls the pin's voltage up to the supply.
Fig. 1 Interfacing switch to Microcontroller Interfacing Switch with LPC2148
Controlling LED by using switches in LPC2148 Primer Board. It works by turning ON a LED & then
turning it OFF when switch is going to LOW or HIGH.
The ARM7 LPC2148 Primer board has eight numbers of point LEDs, connected with I/O Port
lines (P1.16 – P1.23) to make port pins high. Eight switches, connected with I/O port lines
(P1.24 – P1.31) are used to control eight LEDs. Pin Assignment with LPC2148
Slide Switch LPC2148 Lines Input Logic Selection
DIGITAL
INPUTS
SW20 P1.24
SW21 P1.25
SW22 P1.26
Circuit Diagram to Interface Switch with LPC2148
SW23 P1.27
SW24 P1.28
SW25 P1.29
SW26 P1.30
SW27 P1.31
Source Code
The Interfacing switch with LPC2148 program is very simple and straight forward, that controls
led by using switches when it going LOW or HIGH. C Program to interface switch functions using LPC2148
******************************************************************************
*********
Title : Program to Blink LEDs controlling by switches
******************************************************************************
*********
#include<LPC214x.h> // Define LPC2148 Header File
#include <stdio.h>
#define LED 16
#define Switch 24
void Delay(int);
int main(void)
{
unsigned char Status=1;
PINSEL2 &= 0xFFFFFFF3; //Configure P1.16 - P1.31 as GPIO
IO1DIR = 0x00 << Switch;//Configure P1.24 - P1.31 as Input
IO1DIR|= 0xFF << LED; //Configure P1.16 - P1.23 as Output
while(1)
{
Status = 1;
IOSET1 = 0xFF << LED;
Delay (10);
IOCLR1 = 0xFF << LED;
Delay (10);Delay (10);Delay (10);
while (~Status)
{
Status = ((IO1PIN & (0xFF << Switch)) >> Switch);
IO1PIN = ((~Status) << LED);
}
}
}
void Delay(int n)
{
int p,q;
for(p=0;p<n;p++)
{
for(q=0;q<0x9990;q++);
}
}
To compile the above C code you need the KEIL software. They must be properly set up and a
project with correct settings must be created in order to compile the code. To compile the
above code, the C file must be added to the project.
In Keil, you want to develop or debug the project without any hardware setup. You must
compile the code for generating HEX file. In debugging Mode, you want to check the port
output without LPC2148 Primer Board.
The Flash Magic software is used to download the hex file into your microcontroller IC LPC2148
through UART0.
Testing the switch with LPC2148
Give +3.3V power supply to LPC2148 Primer Board; the switches are connected with the
LPC2148 Primer Board. Check the LED’s & switches are working or not. If you not reading any
output signal in LED, then you just check the jumper connections. Otherwise you just check the
code with debugging mode in Keil.
How to Interface Relay with LPC2148 ARM
ARM7 LPC2148 Primer Board
The ARM7 LPC2148 Primer board is specifically designed to help students to master the
required skills in the area of embedded systems. The kit is designed in such way that all the
possible features of the microcontroller will be easily used by the students. The kit supports in
system programming (ISP) which is done through serial port.
NXP’s ARM7 (LPC2148), ARM Primer Kit is proposed to smooth the progress of developing and
debugging of various designs encompassing of High speed 32-bit Microcontrollers. Relay
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. Interfacing Relays
Fig. 1 shows how to interface the Relay to microcontroller. There are 2 input channels. Each
input is connected to the triggering coil of the respective relay. There are 2 output channels
that each correspond to an input. 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.
Fig. 1 Interfacing Relay to Microcontroller Interfacing Relay with LPC2148
Control the relay operations by using LPC2148 Primer Board. Here we are using two Relays. The
relay consists of a coil and a switch. When the coil is energized, the switch closes, connecting
the two contacts together. ULN2803 is used as a driver for port I/O lines, drivers output
connected to relay modules. Connector provided for external power supply if needed.
Relay Module : Port P1 pins (Realy1 – P1.20) and Relay2-P1.21) for relay module, make port
pins to high, relay will activated.
Pin Assignment with LPC2148
RELAY SPDT LPC2148 Lines RELAY Power Select
RELAY
Modules
Relay-1 P1.20
Relay-2 P1.21
Note : Relay selection make switch SW28 to SM/RL label marking position
Circuit Diagram to Interface Relay with LPC2148
Source Code
The Interfacing Relay with LPC2148 program is very simple and straight forward, which control
the relays in LPC2148 Primer Board. The relay is working that uses a delay procedure loop
based software delay. The C programs are developed in Keil software. C Program to control Relay in LPC2148
******************************************************************************
*********
Title : Program to control Relay
******************************************************************************
*********
#include <LPC214x.h>
#include <stdio.h>
#define RLY1 20 //RLY1 (P1.20)
#define RLY2 21 //RLY2 (P1.21)
void main(void)
{
PINSEL2 = 0x00;
IODIR1 = 1 << RLY1; //Configure P1.20 Output
IODIR1 |= 1 << RLY2; //Configure P1.21 Output
while(1) //loop forever
{
IOSET1 = 1 << RLY1;
Delay();
IOCLR1 = 1 << RLY1;
Delay();
IOSET1 = 1 << RLY2;
Delay();
IOCLR1 = 1 << RLY2;
Delay();
}
}
void Delay()
{
unsigned int i,j;
for(i=0;i<2000;i++) for(j=0;j<900;j++);
}
To compile the above C code you need the KEIL software. They must be properly set up and a
project with correct settings must be created in order to compile the code. To compile the
above code, the C file must be added to the project.
In Keil, you want to develop or debug the project without any hardware setup. You must
compile the code for generating HEX file. In debugging Mode, you want to check the port
output without LPC2148 Primer Board.
The Flash Magic software is used to download the hex file into your microcontroller IC LPC2148
through UART0. Testing the Relay with LPC2148
Give +3.3V power supply to LPC2148 Primer Board; the Relay module is connected with
LPC2148 Primer Board. When the program is downloading into LPC2148 in Primer Board, the
Relay output is working that the Relay is ON some time period and the Relay is OFF some other
time of period.
If you are not getting any output from Relay, then you just check the jumper connections &
check the Relay is connected properly. Otherwise you just check it with debugging mode in Keil.
How to Interface LEDs with LPC2148 ARM
ARM7 LPC2148 Primer Board
The ARM7 LPC2148 Primer board is specifically designed to help students to master the
required skills in the area of embedded systems. The kit is designed in such way that all the
possible features of the microcontroller will be easily used by the students. The kit supports in
system programming (ISP) which is done through serial port.
NXP’s ARM7 (LPC2148), ARM Primer Kit is proposed to smooth the progress of developing and
debugging of various designs encompassing of High speed 32-bit Microcontrollers. LED (Light Emitting Diodes)
Light Emitting Diodes (LED) is the most commonly used components, usually for displaying pins
digital states. Typical uses of LEDs include alarm devices, timers and confirmation of user input
such as a mouse click or keystroke. Interfacing LED
Fig. 1 shows how to interface the LED to microcontroller. As you can see the Anode is
connected through a resistor to GND & the Cathode is connected to the Microcontroller pin. So
when the Port Pin is HIGH the LED is OFF & when the Port Pin is LOW the LED is turned ON.
Fig. 1 Interfacing LED to Microcontroller Interfacing LED with LPC2148
Flash a LED using LPC2148 Primer Board. It works by turning ON a LED & then turning it OFF &
then looping back to START. However the operating speed of microcontroller is very high so the
flashing frequency will also be very fast to be detected by human eye.
The ARM7 LPC2148 Primer board has eight numbers of point LEDs, connected with I/O Port
lines (P1.16 – P1.23) to make port pins high. Pin Assignment with LPC2148
Point LEDs LPC2148 Lines LED Selection
DIGITAL
OUTPUTS
LD1 P1.16
LD2 P1.17
Circuit Diagram to Interface LED with LPC2148
LD3 P1.18
LD4 P1.19
LD5 P1.20
LD6 P1.21
LD7 P1.22
LD8 P1.23
Source Code
The Interfacing LED with LPC2148 program is very simple and straight forward, that uses a delay
procedure loop based software delay. In C programs you cannot be sure of delay, because it
depends on compiler how it optimizes the loops as soon as you make changes in the options
the delay changes. C Program to switch ON and OFF LED using LPC2148
******************************************************************************
*********
Title : Program to Blink LEDs
******************************************************************************
*********
#include<LPC214x.h> // Define LPC2148 Header File
#define led IOPIN1 // Define LED to Port1
#define tled IO1DIR // Define Port1 as output
void delay(int x);
void main()
{
PINSEL2 = 0x00000000; // Define port lines as GPIO
tled = 0x00FF0000; // Define P1.16 – P1.23 as O/P
led = 0x00000000; // Define P1.16 – P1.23 as zero
while(1) // Loop forever
{
led = 0x00FF0000; // Turn ON P1.16 – P1.23
delay(2000);
led = 0x00000000; // Turn OFF P1.16 – P1.23
delay(2000);
}
}
void delay(int x)
{
unsigned int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}
To compile the above C code you need the KEIL software. They must be properly set up and a
project with correct settings must be created in order to compile the code. To compile the
above code, the C file must be added to the project.
In KEIL, you want to develop or debug the project without any hardware setup. You must
compile the code for generating HEX file. In debugging Mode, you want to check the port
output without LPC2148 Primer Board.
The Flash Magic software is used to download the hex file into your microcontroller IC LPC2148
through UART0. Testing the LED with LPC2148
Give +3.3V power supply to LPC2148 Primer Board; the LED is connected with LPC2148 Primer
Board. When the program is downloading into LPC2148 in Primer Board, the LED output is
working that the LED is ON some time period and the LED is OFF some other time period.
If you not reading any output from LED, then you just check the jumper connections & check
the LED is working. Otherwise you just check it with debugging mode in KEIL.
How to Interface GSM with LPC2148 ARM
ARM7 LPC2148 Primer Board
The ARM7 LPC2148 Primer board is specifically designed to help students to master the
required skills in the area of embedded systems. The kit is designed in such way that all the
possible features of the microcontroller will be easily used by the students. The kit supports in
system programming (ISP) which is done through serial port.
NXP’s ARM7 (LPC2148), ARM Primer Kit is proposed to smooth the progress of developing and
debugging of various designs encompassing of High speed 32-bit Microcontrollers. GSM (Global System for Mobile Communication)
GSM is a digital mobile telephony system. GSM digitizes and compresses data, then sends it
down a channel with two other streams of user data, each in its own time slot. It operates at
either the 900 MHz or 1800 MHz frequency band. Interfacing GSM
Fig. 1 shows how to interface the GSM with microcontroller. The GSM module is communicate
the microcontroller with mobile phones through UART. To communicate over UART or USART,
we just need three basic signals which are namely, RXD (receive), TXD (transmit), GND
(common ground).
GSM modem interfacing with microcontroller for SMS control of industrial equipments. The
sending SMS through GSM modem when interfaced with microcontroller or PC is much simpler
as compared with sending SMS through UART.
Text message may be sent through the modem by interfacing only three signals of the serial
interface of modem with microcontroller i.e., TxD, RxD and GND. In this scheme RTS and CTS
signals of serial port interface of GSM Modem are connected with each other.
The transmit signal of serial port of microcontroller is connected with transmit signal (TxD) of
the serial interface of GSM Modem while receive signal of microcontroller serial port is
connected with receive signal (RxD) of serial interface of GSM Modem.
The SMS message in text mode can contain only 140 characters at the most. It depends upon
the amount of information collected from GPS Engine that you need at the base station for
tracking vehicle or person.
Fig. 1 Interfacing GSM modem to Microcontroller
Interfacing GSM with LPC2148
Display a text in mobile from LPC2148 Primer Board by using GSM module through UART. In
LPC2148 Primer Board contains two serial interfaces that are UART0 & UART1. Here we are
using UART0. The GSM modem is being interfaced with the microcontroller LPC2148 Primer
Board for SMS communication. The SMS can be sending and receiving for the data sharing and
situation information and control.
Pin Assignment with LPC2148
Circuit Diagram to Interface GSM with LPC2148
UART DB-9
Connector
LPC2148
Processor Lines Serial Port Section
UART0(P1)
ISP PGM
TXD-0 P0.0
RXD-0 P0.1
UART1
(P2)
TXD-1 P0.8
RXD-1 P0.9
Source Code
The Interfacing GSM module with LPC2148 program is very simple and straight forward, which
send a message to mobiles from LPC2148 Primer Board through GSM module by using UART0.
Some delay is occurring when a single data is sent to mobile through UART. C programs are
written in Keil software. The baud rate of microcontroller is 9600. C Program to send a message from LPC2148
******************************************************************************
*********
Title : Program to send a message from LPC2148 to mobile through GSM
******************************************************************************
*********
#define CR 0x0D
#include <LPC21xx.H>
#include <stdio.h>
void getstring(unsigned char *);
int getchar (void) /* Read character from Serial Port */
void status_ok(void);
void Serial_Init(void);
void delay(unsigned int n);
void main(void)
{
unsigned int cnt=0x80,m;
char xx;
Serial_Init();
delay(50);
while(1)
{
printf("AT\r"); // AT COMMAND FOR INITIALING
status_ok();
printf("AT+IPR=9600\r"); // AT COMMAND FOR BAUD RATE
status_ok();
printf("AT+CMGR=2\r"); // Reading the message detail
// at Index 1 with phone number, data and time
status_ok();
delay(250);
printf("ATD9790550124;\r");//AT COMMAND FOR CALL DIALING
delay(250);
status_ok();
delay(500);
delay(500);
delay(500);
delay(500);
delay(500);
delay(500);
printf("ATH\r"); // AT COMMAND FOR CALL DISCONNECTING
delay(250);
status_ok();
delay(500);
delay(500);
printf("ATDL\r"); // AT COMMAND FOR REDIALING
delay(250);
status_ok();
delay(500);
delay(500);
printf("ATH\r"); // AT COMMAND FOR ANSWERING THE CALL
delay(250);
status_ok();
delay(500);
delay(500);
}
}
void getstring(unsigned char *array)
{
unsigned char temp=0, i=0;
do
{
temp = getchar();
*array++ = temp;
}
while((temp != '\r') && (temp != '\n'));
*array = '\0';
}
int getchar (void) /* Read character from Serial Port */
{
while (!(U0LSR & 0x01));
return (U0RBR);
}
void status_ok(void)
{
getstring(y);
while(!(strstr(y,"OK"))) getstring(y);
pointr = strstr(y,"OK");
lcd_cmd(0xc0);
lcd_data(*pointr++);
lcd_data(*pointr);
delay(500);
lcd_cmd(0x01);
}
void Serial_Init(void)
{
PINSEL0 |= 0X00000005; //Enable Txd0 and Rxd0
U0LCR = 0x00000083; //8-bit data, no parity, 1-stop bit
U0DLL = 0x00000061; //for Baud rate=9600,DLL=82
U0LCR = 0x00000003; //DLAB = 0;
}
void delay(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<12000;j++)
{;}
}
}
To compile the above C code you need the KEIL software. They must be properly set up and a
project with correct settings must be created in order to compile the code. To compile the
above code, the C file must be added to the project.
In Keil, you want to develop or debug the project without any hardware setup. You must
compile the code for generating HEX file. In debugging Mode, you want to check the port
output without LPC2148 Primer Board.
The Flash Magic software is used to download the hex file into your microcontroller IC LPC2148
through UART0. Testing the GSM with LPC2148
Give +3.3V power supply to LPC2148 Primer Board; connect the +5V adapter with GSM module
which is connected with LPC2148 Primer Board through UART0. Open the Hyper Terminal
screen, select which port you are using and set the default settings. Now the screen should
show some text messages.
The following Commands and sequence of events performed for sending text message to a
mobile phone through GSM Modem interfaced with microcontroller:
First select the text mode for SMS by sending the following AT Command to GSM Modem :
AT+CMGF = 1 . This command configures the GSM modem in text mode.
Send the following AT Command for sending SMS message in text mode along with mobile
number to the GSM Modem : AT+CMGS =+923005281046 . This command sends the mobile
number of the recipient mobile to the GSM modem.
Send the text message string ("hello!") to the GSM Modem This is a test message from UART".
Send ASCII code for CTRL+Z i.e., 0x1A to GSM Modem to transmit the message to mobile
phone. After message string has been sent to the modem, send CTRL+Z to the micro-controller,
which is equivalent to 0x1A (ASCII value).
If you not reading any text from UART0, then you just check the jumper connections & just
check the serial cable is working. Otherwise you just check the code with debugging mode in
Keil.
LPC2148 GPIO Programming Tutorial
When getting started in embedded programming, GPIO (viz. General Purpose Input Output)
pins are one of the first things played with. Its also quite evident that the most popular “hello
world” program in embedded systems programming is Blinky i.e a LED connected to pin on the
Microcontroller that keeps blinking. The use of GPIO is not limited to driving LEDS but can be
also use for reading digital signal , generating triggers for external components , controlling
external devices and what not. In this tutorial we see how to use and program GPIO Pins for
lpc214x ARM 7 microcontrollers from NXP/Philips.
Before getting into this you need to have basic understanding of Binary and Hexadecimal
system and Bitwise operations in C.
*=>Guide to Binary and Hexadecimal system is @ Hexadecimal and Binary Number System
basics for Embedded Programming.
*=>Tutorial for Bitwise Operations in C is @ Tutorial : Embedded programming basics in C –
bitwise operations.
I’ll use lpc2148 MCU (having 32-bit ARM 7 CPU) for explanation and programming examples.
The Programs and Register names that I have shown are used in KEIL. You can download KEIL
UV4 from here. If you are using a different IDE/Compiler then you’ll need to change the
Register Names as required. Also do note that Integer Data-Type i.e. an ‘int’ is always 32 bits in
KEIL when programming for 32bit ARM7 MCUs like lpc2148.
Most of the function oriented pins on lpc214x Microcontrollers are grouped into ports. lpc2148
has 2 ports viz. Port 0 and Port 1.
Port 0 is a 32 bit wide I/O port (i.e it can be used for max 32 pins where each pin refers to a
corresponding bit) and has dedicated direction bits for each of the pins present in the port. 28
out of the 32 pins can be used as bi-directional I/O (digital) pins. Pins P0.24 , P0.26 & P0.27 are
unavailable for use and Pin P0.30 can be used as output pin only.
Port 1 is also a 32 bit wide I/0 port but Pins 0 to 15 i.e P1.0 – P1.15 are unavailable for use and
this port too has a dedicated direction bit for each of the usable pins.
Note #1: The naming convention for Pins on MCU is ‘Px.yz’ where ‘x’ is the port number , 0 or 1
in our case since we have only 2 ports to play with in lpc214x , and ‘yz’ is simply the pin number
in port ‘x’. For example : P0.2 refers to Pin number 2 of Port 0 , P1.13 refers to Pin number 13 in
Port 1.
In lpc214x MCUs most of the PINS are Multiplexed i.e. these pins can be configured to provide
different functions. I’ll explain this in upcoming tutorial. For now Just keep in mind that by
default : all functional pins i.e pins in port 0 & 1 are set as GPIO so we can direclty use them
when learning GPIO usage.
Note #2: The functions of the Pins in Port 0 & 1 can be selected by manipulating appropriate
bits in PINSEL0/1/2 registers. Explaining this is outside the scope of this article and will be dealt
in detail in another article. Just remember that assigning ’0′ to these registers forces the
corresponding pins to be used as GPIO. Since by default all pins are configured as GPIOs we
dont need to explicitly assign zero value to PINSELx registers in our programming examples.
Now , lets go through the registers used for GPIO programming.
1. IOxPIN (x=port number) : This register can be used to Read or Write values directly to the
pins. Regardless of the direction set for the particular pins it gives the current start of the GPIO
pin when read.
2. IOxDIR : This is the GPIO direction control register. Setting a bit to 0 in this register will
configure the corresponding pin to be used as an Input while setting it to 1 will configure it as
Output.
3. IOxSET : This register can be used to drive an ‘output’ configured pin to Logic 1 i.e HIGH.
Writing Zero does NOT have any effect and hence it cannot be used to drive a pin to Logic 0 i.e
LOW. For driving pins LOW IOxCLR is used which is explained below.
4. IOxCLR : This register can be used to drive an ‘output’ configured pin to Logic 0 i.e LOW.
Writing Zero does NOT have any effect and hence it cannot be used to drive a pin to Logic 1.
Note #3: Naming convention for GPIO related registers – Replace ‘x’ with the port number to
get the register name. For e.g IOxPIN becomes IO0PIN when used for Port 0 and IO1PINwhen
used to port 1. These are defined in ‘lpc214x.h’ header file for KIEL IDE.
Registers Names defined in ‘lpc214x.h’ header file are basically pointers which point to actual
register in Hardware. Since lpc214x MCUs are 32 bit , the size of the pointer is also 32 bits. Each
bit in these registers mentioned above is directly linked to a corresponding Pin. Manipulating
these bits changes the behavior or state of the pins. For e.g consider IOxDIR register. Bit 0 of
IO0DIR corresponds to pin 0 or port 0 hence bit ‘y’ in IOxDIR corresponds to pin ‘y’ in port ‘x’.
Now setting PIN 2 of Port 0 i.e P0.2 as output can be done in various ways as show :
CASE 1. IO0DIR = (1<<2); //(binary – direct assign: other pins set to 0)
CASE 2. IO0DIR |= 0×0000004; // or 0×4; (hexadecimal – OR and assign: other pins not affected)
CASE 3. IO0DIR |= (1<<2); //(binary – OR and assign: other pins not affected)
First thing is to note that preceding Zeros in Hexadecimal Notation can be ignored since bcoz
have no meaning since we are working with unsigned values here (positive only) which are
assigned to Registers. For eg. ’0×32′ and ’0×032′ and ’0×0032′ all mean the same.
Note #4: Note that bit 31 is MSB on extreme left and bit 0 is LSB on extreme right i.e Big Endian
Format. Hence bit 0 is the 1st bit from right , bit 1 is the 2nd bit from right and so on.
Also note that the BIT and PIN Numbers are Zero(0) indexed which is quite evident since Bit
‘x’ refers to (x-1)th location in the corresponding register.
Case 1 must be avoided since we are directly assigning a value to the register. So while we are
making P0.2 ’1′ others are forced to be assigned a ’0′ which can be avoided by ORing and then
assigning Value.
Case 2 can be used when bits need to be changed in bulk and
Case 3 when some or single bit needs to be changed.
Also Note: All GPIO pins are configured as Input after Reset by default!
Example #1)
Consider that we want to configure Pin 19 of Port 0 i.e P0.19 as Ouput and want to drive it
High(Logic 1). This can be done as :
IO0DIR |= (1<<19); // Config P0.19 as Ouput
IO0SET |= (1<<19); // Make ouput High for P0.19
Example #2)
Making output configured Pin 15 High of Port 0 i.e P0.15 and then Low can be does as follows:
IO0DIR |= (1<<15); // P0.15 is Output pin
IO0SET |= (1<<15); // Output for P0.15 becomes High
IO0CLR |= (1<<15); // Output for P0.15 becomes Low
Example #3)
Configuring P0.13 and P0.19 as Ouput and Setting them High:
IO0DIR |= (1<<13) | (1<<19); // Config P0.13 and P0.19 as Ouput
IO0SET |= (1<<13) | (1<<19); // Make ouput High for P0.13 and P0.19
Example #4)
Configuring 1st 16 Pins of Port 0 (P0.0 to P0.15) as Ouput and Setting them High:
IO0DIR |= 0x0000FFFF; // Config P0.0 to P0.15 as Ouput
IO0SET |= 0x0000FFFF; // Make ouput High for P0.0 to P0.15
Now lets play with some real world examples.
Example #5)
Blinky Example – Now we repeatedly make all pins in port 0 (P0.0 to P0.30) High then Low then
High and so on. You can connect Led to some or all Pins on Port 0 to see it in action. Here we
will introduce some delay between making all pins High and Low so it can be noticed.
#include <lpc214x.h>
void delay(void);
int main(void)
{
IO0DIR = 0xFFFFFFFF; // Configure all pins on Port 0 as Output
while(1)
{
IO0SET = 0xFFFFFFFF; // Turn on LEDs
delay();
IO0CLR = 0xFFFFFFFF; // Turn them off
delay();
}
return 0; // normally this wont execute
}
void delay(void)
{
int z,c;
c=0;
for(z=0; z<4000000; z++) // You can edit this as per your needs
{
c++; // something needs to be here else KEIL compiler will remove the for loop!
}
}
Example #6)
Configuring P0.7 as Input and monitoring it for a external event like connecting it to LOW or
GND. P0.30 is configured as output and connected to LED. If Input for P0.7 is a ‘Low’ (GND) then
output for P0.30 is made High which will activate the LED and make it glow (Since the other
END of LED is connected to LOW i.e GND). Since internal Pull-ups are enabled the ‘default’ state
of the pins configured as Input will be always ‘High’ unless it is explicitly made ‘Low’ by
connecting it to Ground. Consider one end of a tactile switch connected to P0.7 and other to
ground. When the switch is pressed a ‘LOW’ will be applied to P0.7. The setup is shown in the
figure below:
#include <lpc214x.h>
int main(void)
{
IO0DIR &= ~((1<<7)) ; // explicitly making P0.7 as Input - even though by default its already
Input
IO0DIR |= (1<<30); // Configuring P0.30 as Output
while(1)
{
if( !(IO0PIN & (1<<7)) ) // Evaluates to True for a 'LOW' on P0.7
{
IO0SET |= (1<<30); // drive P0.30 High
}
}
return 0; // this wont execute ever :P
}
Explanation:
‘(1<<7)' is simply 7th bit '1'(i.e 0x00000080) & rest all bit are zeros. When ‘(1<<7)'
is ANDed with IO0SET it will make all other bits except 7th bit to '0'. The Value of
7th bit in result will now depend on IO0PIN's 7th bit. If its 1 (which means input is
High) then result after ANDing will be 0×00000080 which is greater than zero and
hence will evaluate to ‘TRUE‘. Also when we use ‘Logical NOT‘ i.e ‘!‘ then ‘!(TRUE)‘
evaluates to FALSE hence code is not executed for High Input. When P0.7 is given
‘Low’ the corresponding bit in IO0PIN i.e 7th bit will be set to 0. In this case the
result of ‘IO0PIN & (1<<7)‘ will be ’0×0 ′ which evaluates to FALSE. Hence ‘!(FALSE)‘
evalutes to TRUE and code inside ‘if’ block gets executed.
BTW:The above while loop can be re-written as :
while( IO0PIN & (1<<7) )
{
IO0SET |= (1<<30); // drive P0.30 High
}
For now lets stick to the original while loop since it keeps things simple.
Note #5: Since LPC214x runs on 3.3 Volts a High on any pin is (and must be) always 3.3 Volts
while Low or GND is 0 Volts. Applying an input of more than 3.3 Volts like 5 Volts will
permanently damage the pin! I have a couple of pins damaged on a few lpc214x boards due to
this – that was when I was learning embedded stuff.
Example #7)
Now we will extended example 6 so that when the button is pressed the LED will glow and
when released or not pressed the LED wont glow. Capturing inputs in this manner, using
switches, leads to a phenomenon called ‘bouncing‘ which needs to be resolved using
‘debouncing‘ as explained below. We will a ‘flag’ variable to swtich between High and Low
states.
Bouncing:
Usually in switches there are Metal contacts which Open and Close. Consider an Open switch.
When it is closed the signal passes over initially and then briefly the contacts might loosen and
possibly result in a Open-Circuit momentarily hence signal doesnt pass over for that short
period of time. After this the contacts settle and signal passes over again ‘steadily’. This
tendency of the metal contacts is referred to as ‘Bouncing’.
Debouncing:
Bouncing needs to be given special attention while processing/capturing inputs. Bouncing can
be eliminated using ‘Debouncing’. Debouncing can be tackled at hardware or software level. In
hardware it can be done using RC circuits , Latches , etc.. For the sake of this article I’ll show
how to deal with it using software. In this we will sample(read) P0.7 status/state two times with
a very brief delay in between. In our case this simple strategy is sufficient to prevent bouncing.
Note that delay must be correctly chosen – it must not be too high nor too low. If the state of
pin is same after taking the second sample then we can say that the new state of the pin is
indeed stable.
I would recommend visiting the following links for understanding debouncing indepth.
1) www.ganssle.com/debouncing.htm
2) www.labbookpages.co.uk/electronics/debounce.html
3) www.eng.uwaterloo.ca/~tnaqvi/downloads/DOC/sd192/SwitchDebouncing.htm
#include <lpc214x.h>
void tiny_delay(void);
int main(void)
{
int flag=0, pinSamplePrevious, pinSampleCurrent;
IO0DIR &= ~((1<<7));
IO0DIR |= (1<<30);
pinSamplePrevious = IO0PIN & (1<<7); //Initial Sample
while(1)
{
pinSampleCurrent = IO0PIN & (1<<7); //New Sample
if( pinSampleCurrent != pinSamplePrevious )
{
//P0.7 might get low or high momentarily due to noise depending the external conditions
or some other reason
//hence we again take a sample to insure its not due to noise
tiny_delay(); // momentary delay
// now we again read current status of P0.7 from IO0PIN
pinSampleCurrent = IO0PIN & (1<<7);
if( pinSampleCurrent != pinSamplePrevious )
{
//State of P0.7 has indeed changed
if(flag) //First time Flag will be = 0 hence else part will execute
{
IO0SET |= (1<<30); // drive P0.30 High
flag=0; //next time 'else' part will excute
}
else
{
IO0CLR |= (1<<30); // drive P0.30 Low
flag=1; //next time 'if' part will excute
}
//set current value as previous since it has been processed
pinSamplePrevious = pinSampleCurrent;
}
}
}
return 0; // this wont execute ever :P
}
void tiny_delay(void)
{
int z,c;
c=0;
for(z=0; z<1500; z++) //Higher value for higher clock speed
{
c++;
}
}
Explanation:
Initially P0.7 will be high since pull -ups are enabled. Hence ‘pinSamplePrevious‘
and ‘pinSampleCurrent‘ will have same value and ‘if‘ will evalute to false so code
inside wont execute.
Event #1) Pressing switch: When switch is pressed P0.7 will now be ‘Low’ but this
might be due to noise or glicthy contact in switch so we again read the value of
P0.7 to confirm that the new state of P0.7 is stable. If it is stable then va lue of
‘pinSampleCurrent’ will differ from ‘pinSamplePrevious’ and the inner ‘if‘ block
evalute to true and code inside will execute. When its executed first time ‘flag‘ will
be 0. Hence, ‘if(flag)‘ will be false and ‘else‘ block will execute which will dr ive
P0.30 Low and set flag to ’1 ′ .
Event #2) Releasing switch: Now After the switch has been released the value of
P0.7 will change to ’1 ′ i.e High. Previous state was ‘Low’ and current is ‘High’
hence pinSamplePrevious’ and ‘pinSampleCurrent ’ will again differ and in similar
manner as above the code inside inner ‘if’ block will execute. This time though ,
since flag is 1, ‘if(flag)‘ will evaluate to true and P0.30 will be set to High i.e ’1 ′
and flag will be set to 0.
Improvising play with Bits:
Using the left shift operation is not confusing but when too many are used together I find it a
little bit out of order and affects code readability to some extent. For this I define a Macro
‘BIT(x)’ which replaces ‘(1<<x)’ as:
#define BIT(x) (1<<x)
After that is defined we can directly use BIT(x). Using this Example 3 can be re-written as:
IO0DIR |= BIT(13) | BIT(19); // Config P0.13 and P0.19 as Ouput
IO0SET |= BIT(13) | BIT(19); // Make ouput High for P0.13 and P0.19
Example 6 can be re-written as:
#include <lpc214x.h>
#define BIT(x) (1<<x)
int main(void)
{
IO0DIR &= ~(BIT(7));
IO0DIR |= BIT(31);
while(1)
{
if( !(IO0PIN & BIT(7)) )
{
IO0SET |= BIT(31);
}
}
return 0;
}
top related