0% found this document useful (0 votes)
266 views71 pages

Features of 32-Bit Arm Cortex-M3 Lpc1768 Microcontroller: Embedded Controller Lab Manual (15ECL68)

This document describes the features of the 32-bit ARM Cortex-M3 LPC1768 microcontroller, including: 1. It has an ARM Cortex-M3 processor that can run at up to 100 MHz with an MPU. 2. The Cortex-M3 has a Nested Vectored Interrupt Controller (NVIC). 3. It has up to 512 KB of on-chip flash memory that can operate at 100 MHz with zero wait states. It then provides details on programming and configuring the GPIO pins on the LPC1768, describing the various registers used like PINSEL, FIODIR, FIOSET, FIOCLR, and FI

Uploaded by

varalakshmikr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views71 pages

Features of 32-Bit Arm Cortex-M3 Lpc1768 Microcontroller: Embedded Controller Lab Manual (15ECL68)

This document describes the features of the 32-bit ARM Cortex-M3 LPC1768 microcontroller, including: 1. It has an ARM Cortex-M3 processor that can run at up to 100 MHz with an MPU. 2. The Cortex-M3 has a Nested Vectored Interrupt Controller (NVIC). 3. It has up to 512 KB of on-chip flash memory that can operate at 100 MHz with zero wait states. It then provides details on programming and configuring the GPIO pins on the LPC1768, describing the various registers used like PINSEL, FIODIR, FIOSET, FIOCLR, and FI

Uploaded by

varalakshmikr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Embedded Controller Lab Manual [15ECL68]

FEATURES OF 32-BIT ARM CORTEX-M3 LPC1768


MICROCONTROLLER

1. ARM Cortex-M3 processor, running at frequencies of up to 100 MHz. A Memory Protection


Unit (MPU) supporting eight regions is included.
2. ARM Cortex-M3 has built-in Nested Vectored Interrupt Controller (NVIC).
3. Up to 512 kB on-chip flash programming memory. Enhanced flash memory accelerator
enables high-speed 100 MHz operation with zero wait states.
4. In-System Programming (ISP) and In-Application Programming (IAP) via on-chip
bootloader software.
5. On-chip SRAM includes:
a. 32/16 kB of SRAM on the CPU with local code/data bus for high-performance CPU
access.
b. Two/one 16 kB SRAM blocks with separate access paths for higher throughput.
6. Eight channel General Purpose DMA controller (GPDMA) on the AHB multilayer matrix.
7. USB 2.0 full-speed device/Host/OTG controller with dedicated DMA controller and on-chip
PHY for device, Host, and OTG functions.
8. Four UARTs with fractional baud rate generation, internal FIFO, DMA support, and RS-485
support.
9. One UART has modem control I/O, and one UART has IrDA
support.
10.CAN 2.0B controller with two channels.
11.70 General Purpose I/O (GPIO) pins with configurable pull-up/down
resistors and a new, configurable open-drain operating mode.

12.Four general purpose timers/counters, with a total of eight capture


inputs and ten compare outputs. Each timer block has an external count
input and DMA support.
13.One motor control PWM with support for three-phase motor control.

LPC1768 GPIO Programming

LPC1768 is an ARM Cortex-M3 based MCU by Phillips/NXP and has plenty of General Purpose
Input Output pins to play with. The Name of Registers, Data structures that I have used in this guide
are defined in LPC17xx.h header file. LPC17xx.h header is based on CMSIS(Cortex
Microcontroller System Interface Standard) developed by ARM.
Most of the function oriented pins on LPC176x Microcontrollers are grouped into Ports. LPC1768
has 5 ports viz. Port 0 to 4. The associated registers for each port are grouped into a structure with
the following naming convention : LPC_GPIOx , where x is the port number.

DEPT. OF ECE, RLJIT 1


Embedded Controller Lab Manual [15ECL68]

Register Configuration
As all the LPC1768 SFRs(Special Function Registers) are defined in lpc17xx.h, this has to be
included at the beginning of our project/code.
LPC1768 has its GPIOs divided into five ports PORT0 - PORT4, although many of them are not
physically 32bit wide. Refer the data sheet for more info. The Below registers will be used for
Configuring and using the GPIOs registers for sending and receiving the Digital signals.
A structure LPC_GPIOn(n= 0,1,2,3) contains all the registers for required for GPIO operation. Refer
lpc17xx.h file for more info on the registers.

 PINSEL:GPIO Pins Select Register


Almost all the LPC1768 pins are multiplexed to support more than 1 function. Every GPIO
pin has a minimum of one function and max of four functions.
 The required function can be selected by configuring the PINSEL register.
 As there can be up to 4 functions associated with a GPIO pin, two bits for each pin are
available to select the function. This implies that we need two PINSEL registers to configure
a PORT pins.
 By this the first 16(P0.0-P0.16) pin functions of PORT0 can be selected by 32 bits of
PINSELO register.
 The remaining 16 bits(P0.16-P0.32) are configured using 32bits of PINSEL1 register.
 As mentioned earlier every pin has max of four functions.
 Below table shows how to select the function for a particular pin using two bits of the
PINSEL register.

Value Function Enumeration

00 Primary (default) function, PINSEL_FUNC_0


typically GPIO port

DEPT. OF ECE, RLJIT 2


Embedded Controller Lab Manual [15ECL68]

01 First alternate function PINSEL_FUNC_1


10 Second alternate function PINSEL_FUNC_2
11 Third alternate function PINSEL_FUNC_3

FIODIR: Fast GPIO Direction Control Register.


This register individually controls the direction of each port pin.

Values Direction
0 Input
1 Output

FIOSET: Fast Port Output Set Register.


This register controls the state of output pins. Writing 1s produces highs at the corresponding port
pins. Writing 0s has no effect. Reading this register returns the current contents of the port output
register not the physical port value.

FIOCLR: Fast Port Output Clear Register.


This register
controls
Values FIOSET the state
of 0 No Effect output
pins. Writing
1s 1 Sets High on Pin
produces lows at the corresponding port pins. Writing 0s has no effect.

DEPT. OF ECE, RLJIT 3


Embedded Controller Lab Manual [15ECL68]

Values FIOCLR
0 No Effect
1 Sets Low on Pin

FIOPIN: Fast Port Pin Value Register.

This register is used for both reading and writing data from/to the PORT.
Output: Writing to this register places corresponding values in all bits of the particular PORT pins.
Input: The current state of digital port pins can be read from this register, regardless of pin direction
or alternate function selection (as long as pins are not configured as an input to ADC).

GPIO Registers in LPC1768

Registers on LPC1768 are present on Peripheral AHB bus(Advanced High performance Bus) for
fast read/write timing. So, these are basically Fast I/O or Enhanced I/O and hence the naming
convention in datasheet uses a prefix of “FIO” instead of something like “GIO” for all the registers
related to GPIO. Let’s go through these as given below.
1) FIODIR : 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.
2) FIOMASK : This gives masking mechanism for any pin i.e. it is used for Pin access control.
Setting a bit to 0 means that the corresponding pin will be affected by changes to other registers like
FIOPIN, FIOSET, FIOCLR. Writing a 1 means that the corresponding pin won’t be affected by
other registers.
3) FIOPIN : 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.
4) FIOSET : It is 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 FIOCLR is used which is explained below.
5) FIOCLR : It is 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.

DEPT. OF ECE, RLJIT 4


Embedded Controller Lab Manual [15ECL68]

• Most of the PINS of LPC176x MCU are Multiplexed i.e. these pins can be


configured to provide up to 4 different functions.
• By default, after Power-On or Reset : all pins of all ports are set as GPIO so we
can directly use them when learning GPIO usage. The different functions that
any particular pin provides can be selected by setting appropriate value in
the PINSEL register for the corresponding pin.
• Each pin on any port has 2 corresponding bits in PINSEL register. The first 16
pins (0-15) on a given port will have a corresponding 32 bit PINSEL register
and the rest 16 bits will have another register.
• For example bits 0 & 1 in PINSEL0 are used to select function for Pin 1 of Port
0, bits 2 & 3 in PINSEL0 are used to select function for PIN 2 of port 0 and so
on.

DEPT. OF ECE, RLJIT 5


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 6


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 7


Embedded Controller Lab Manual [15ECL68]

Experiment No.1
DISPLAY “Hello World” MESSAGE USING INTERNAL UART
Date:

Aim: To d i s p l a y “ Hello W o r l d ” m e s s a g e u s i n g internal UART of ARM Cortex-M3


LPC1768 microcontroller.
Embedded C code:
#include <lpc17xx.h>
void inituart0(void);
void msg(unsigned char *data);
void write(unsigned char txdata);
int main(void)
{
inituart0();
msg("Hello World");
write(0x0D);
write(0x0A);
while(1);
}
void inituart0(void)
{
LPC_PINCON->PINSEL0=0x50;
LPC_UART0->LCR=0x83;
LPC_UART0->DLL=12;
LPC_UART0->FCR=0x07;
LPC_UART0->FDR=0xF2;
LPC_UART0->LCR=0x03;
}
void msg(unsigned char *data)
{
while(*data!=0) write(*data++);
}
void write(unsigned char txdata )
{
while(!(LPC_UART0->LSR&0x20));
LPC_UART0->THR=txdata;
}

DEPT. OF ECE, RLJIT 8


Embedded Controller Lab Manual [15ECL68]

Pin Configuration:

UART module
UART module and registers. LPC1768 has 4-UARTs numbering 0-3, similarly the pins are also
named as RXD0-RXD3 and TXD0-TXD3.As the LPC1768 pins are multiplexed for multiple
functionalities, first they have to be configured as UART pins.
Below table shows the multiplexed UARTs pins.

Port Pin PINSEL_FUNC_0 PINSEL_FUNC_ PINSEL_FUNC_ PINSEL_FUNC_3


Pin Number 1 2

P0.02 98 GPIO TXD0 ADC0[7]

P0.03 99 GPIO RXD0 ADC0[6]

P2_0 48 GPIO PWM1[1] TXD1

DEPT. OF ECE, RLJIT 9


Embedded Controller Lab Manual [15ECL68]

P2.1 49 GPIO PWM1[2] RXD1

P0.10 62 GPIO TXD2 SDA2 MAT3[0]

P0.11 63 GPIO RXD2 SCL2 MAT3[1]

P0.0 82 GPIO CAN1_Rx TXD3 SDA1

P0.1 85 GPIO CAN1_Tx RXD3 SCL1

UART Registers
The below table shows the registers associated with LPC1768 UART.

Register Description

RBR Contains the recently received Data

THR Contains the data to be transmitted

FCR FIFO Control Register

LCR Controls the UART frame formatting(Number of Data Bits, Stop bits)

DLL Least Significant Byte of the UART baud rate generator value.

DLM Most Significant Byte of the UART baud rate generator value.

UART Register Configuration


Now lets see how to configure the individual registers for UART communication.

DEPT. OF ECE, RLJIT 10


Embedded Controller Lab Manual [15ECL68]

FCR ( FIFO Control Register )


LPC1768 has inbuilt 16byte FIFO for Receiver/Transmitter. Thus it can store 16-bytes of data
received on UART without overwriting. If the data is not read before the Queue(FIFO) is filled then
the new data will be lost and the OVERRUN error bit will be set.

FCR

31:8 7:6 5:4 3 2 1 0

RESERVED RX RESERVED DMA TX FIFO RX FIFO FIFO


TRIGGER MODE RESET RESET ENABLE

Bit 0 – FIFO:
This bit is used to enable/disable the FIFO for the data received/transmitted.
0--FIFO is Disabled.
1--FIFO is Enabled for both Rx and Tx.

Bit 1 – RX_FIFO:
This is used to clear the 16-byte Rx FIFO.
0--No impact.
1--CLears the 16-byte Rx FIFO and the resets the FIFO pointer.

Bit 2 – Tx_FIFO:
This is used to clear the 16-byte Tx FIFO.
0--No impact.
1--Clears the 16-byte Tx FIFO and the resets the FIFO pointer.

Bit 3 – DMA_MODE:
This is used for Enabling/Disabling DMA mode.
0--Disables the DMA.
1--Enables DMA only when the FIFO(bit-0) bit is SET.

Bit 7:6 – Rx_TRIGGER:


This bit is used to select the number of bytes of the receiver data to be written so as to enable the
interrupt/DMA.
00-- Trigger level 0 (1 character or 0x01)
01-- Trigger level 1 (4 characters or 0x04)
10-- Trigger level 2 (8 characters or 0x08)
11-- Trigger level 3 (14 characters or 0x0E) 

LCR ( Line Control Register )


This register is used for defining the UART frame format ie. Number of Data bits, STOP bits etc.

DEPT. OF ECE, RLJIT 11


Embedded Controller Lab Manual [15ECL68]

LCR

31:8 7 6 5:4 3 2 1:0

Reserved DLAB Break COntrol Parity Select Parity Enable Stop Bit Select Word Length Select

Bit 1:0 – WLS : WordLenghtSelect


These two bits are used to select the character length
00-- 5-bit character length
01-- 6-bit character length
10-- 7-bit character length
11-- 8-bit character length

Bit 2 – Stop Bit Selection:


This bit is used to select the number(1/2) of stop bits 
0-- 1 Stop bit
1-- 2 Stop Bits

Bit 3 – Parity Enable:


This bit is used to Enable or Disable the Parity generation and checking.
0-- Disable parity generation and checking.
1-- Enable parity generation and checking.

Bit 5:4 – Parity Selection:


These two bits will be used to select the type of parity.
00-- Odd parity. Number of 1s in the transmitted character and the attached parity bit will be odd.
01-- Even Parity. Number of 1s in the transmitted character and the attached parity bit will be even.
10-- Forced "1" stick parity.
11-- Forced "0" stick parity 

Bit 6 – Break Control


0-- Disable break transmission.
1-- Enable break transmission. Output pin UARTn TXD is forced to logic 0

Bit 8 – DLAB: Divisor Latch Access Bit


This bit is used to enable the access to divisor latch.
0-- Disable access to divisor latch
0-- Enable access to divisor latch 

LSR (Line Status Register)


The is a read-only register that provides status information of the UART TX and RX blocks.

DEPT. OF ECE, RLJIT 12


Embedded Controller Lab Manual [15ECL68]

LSR

31:8 7 6 5 4 3 2 1 0

Reserved RXFE TEMT THRE BI FE PE OE RDR

Bit 0 – RDR: Receive Data Ready


This bit will be set when there is a received data in RBR register. This bit will be automatically
cleared when RBR is empty.
0-- The UARTn receiver FIFO is empty.
1-- The UARTn receiver FIFO is not empty.

Bit 1 – OE: Overrun Error


The overrun error condition is set when the UART Rx FIFO is full and a new character is received.
In this case, the UARTn RBR FIFO will not be overwritten and the character in the UARTn RSR
will be lost.
0-- No overrun
1-- Buffer over run

Bit 2 – PE: Parity Error


This bit is set when the receiver detects a error in the Parity.
0-- No Parity Error
1-- Parity Error

Bit 3 – FE: Framing Error


This bit is set when there is error in the STOP bit(LOGIC 0)
0-- No Framing Error
1-- Framing Error

Bit 4 – BI: Break Interrupt


This bit is set when the RXDn is held in the spacing state (all zeroes) for one full character
transmission
0-- No Break interrupt
1-- Break Interrupt detected.

Bit 5 – THRE: Transmitter Holding Register Empty


THRE is set immediately upon detection of an empty THR. It is automatically cleared when the
THR is written.
0-- THR register is Empty
1-- THR has valid data to be transmitted

Bit 6 – TEMT: Transmitter Empty


TEMT is set when both UnTHR and UnTSR are empty; TEMT is cleared when any of them contain
valid data.
0-- THR and/or the TSR contains valid data.
1-- THR and the TSR are empty.

DEPT. OF ECE, RLJIT 13


Embedded Controller Lab Manual [15ECL68]

Bit 7 – RXFE: Error in Rx FIFO


This bit is set when the received data is affected by Framing Error/Parity Error/Break Error.
0-- RBR contains no UARTn RX errors.
1-- RBR contains at least one RX error.

TER (Transmitter Enable register)


This register is used to Enable/Disable the transmission

TER

31:8 7 6-0

Reserved TXEN Reserved

Bit 7 – TXEN: Trsnamitter Enable


When this bit is 1, the data written to the THR is output on the TXD pin.
If this bit is cleared to 0 while a character is being sent, the transmission of that character is
completed, but no further characters are sent until this bit is set again.
In other words, a 0 in this bit blocks the transfer of characters.

 Note: By default this bit will be set after Reset.

Baudrate Calculation
LPC1768 generates the baud rate depending on the values of DLM,DLL.
Baudrate = PCLK/ (16 * ( 256 * DLM + DLL) * (1+ DivAddVal/MulVal))

Getting the PCLK value.


PCLKSELx registers contains the PCLK info for all the clock dependent peripherals in which
Bit6,Bit7 contains the Uart Clock(ie.UART_PCLK) information.
The UART_PCLK and the actual Peripheral Clock(PCLK) is calculated as below.
(Refer data sheet for more info)

UART_PCLK PCLK

0 SystemFreq/4

1 SystemFreq

DEPT. OF ECE, RLJIT 14


Embedded Controller Lab Manual [15ECL68]

2 SystemFreq/2

3 SystemFreq/8

DivAddVal/MulVal == 0

Using the above parameters , DLL/DLM is calculated as below.


(256 * DLL + DLM) = PCLK / (16* Baudrate).

Steps for Configuring UART0


Below are the steps for configuring the UART0.

1. Step1: Configure the GPIO pin for UART0 function using PINSEL register.
2. Step2: Configure the FCR for enabling the FIXO and Reste both the Rx/Tx FIFO.
3. Step3: Configure LCR for 8-data bits, 1 Stop bit, Disable Parity and Enable DLAB.
4. Step4: Get the PCLK from PCLKSELx register 7-6 bits.
5. Step5: Calculate the DLM,DLL vaues for required baudrate from PCLK.
6. Step6: Updtae the DLM,DLL with the calculated values.
7. Step6: Finally clear DLAB to disable the access to DLM,DLL.
After this the UART will be ready to Transmit/Receive Data at the specified baudrate.

Output:
Connect the LPC1768 microcontroller kit and dump the program using FLASHMAGIC
software. Now in FLASHMAGIC software goto TOOLS TERMINALOK. Now press
RESET in LPC1768 kit.

DEPT. OF ECE, RLJIT 15


Embedded Controller Lab Manual [15ECL68]

Experiment No.2
Interface and Control of a DC Motor
Date:

Aim: to write a embedded C program to control the speed of DC motor and change the direction of the
motor using ARM Cortex-M3 LPC1768 microcontroller.

Embedded C code:
#include <lpc17xx.h>
void delay(unsigned int d)
{
unsigned int i,j; for(i=0;i<d;i++) for(j=0;j<500;j++);
}
int main(void)
{
int dc;
LPC_PINCON->PINSEL4 = 0x41;
LPC_PWM1->MR0 = 4000;
LPC_PWM1->MCR = 2;
LPC_PWM1->PCR = (1<<9)|(1<<12);
//or =0x1200
LPC_PWM1->TCR = 2;
LPC_PWM1->TCR = 9;
while(1)
{
for(dc=0; dc<4000; dc++)
{
LPC_PWM1->MR1 = dc;
LPC_PWM1->MR4 = 0;
LPC_PWM1->LER = 0x31;
delay(5);
}
for(dc=0; dc<4000; dc++)
{
LPC_PWM1->MR4 =dc;
LPC_PWM1->MR1 = 0;
LPC_PWM1->LER = 0x31;
delay(5);
}
}
}

DEPT. OF ECE, RLJIT 16


Embedded Controller Lab Manual [15ECL68]

Pin Configuration:

DEPT. OF ECE, RLJIT 17


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 18


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 19


Embedded Controller Lab Manual [15ECL68]

OUTPUT: Connect DC motor module to FRC-1.

DEPT. OF ECE, RLJIT 20


Embedded Controller Lab Manual [15ECL68]

Experiment No.3
Interface and Controlling direction of a Stepper Motor
Date:

Aim: to write a embedded C program to Interface stepper motor and program to rotate it in clockwise
and anti-clockwise directions using ARM Cortex-M3 LPC1768 microcontroller..

Embedded C code:
#include <lpc17xx.h>
void delay(unsigned int t)

{ unsigned int i,j;


for(i=0;i<t;i++)
for(j=0;j<10000;j++);
}
int main( )
{
unsigned int a[ ] = {3,6,12,9};
int i,j;
LPC_GPIO2->FIODIR = 15;

while(1)
{
for(i=0;i<50;i++)

for (j=0;j<=3;j++)
{
LPC_GPIO2->FIOPIN = a[j];
delay(1);
}

for(i=0;i<50;i++)

for (j=3;j>=0;j--)
{
LPC_GPIO2->FIOPIN = a[j];
delay(1);
}

}
}

Pin Configuration:

DEPT. OF ECE, RLJIT 21


Embedded Controller Lab Manual [15ECL68]

Output: Connect stepper motor module to FRC-1.

DEPT. OF ECE, RLJIT 22


Embedded Controller Lab Manual [15ECL68]

Experiment No.4A
Interface a DAC and Generate Square Waveform
Date:

Aim: to write a Embedded C program to configure an internal DAC to produce triangular waveform on
CRO using ARM Cortex-M3 LPC1768 microcontroller.

Embedded C code:

#include <lpc17xx.h>
void delay(unsigned int d)
{
unsigned int i,j; for(i=0;i<d;i++) for(j=0;j<100;j++);
}
int main(void)
{
LPC_PINCON->PINSEL1 = (1<<21);
while(1)
{
LPC_DAC->DACR = 1023<<6;
delay(1);
LPC_DAC->DACR = 0<<6;
delay(1);
}
}

DEPT. OF ECE, RLJIT 23


Embedded Controller Lab Manual [15ECL68]

Pin Configuration:

Circuit Diagram
LPC1768

FRC-5

P0.26

CRO
AOUT

DEPT. OF ECE, RLJIT 24


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 25


Embedded Controller Lab Manual [15ECL68]

31:17 16 15:6 5:0

Reserve BIAS VALUE Reserved


d

Output: Connect DAC module to FRC-5


Note: To see waveform on monitor use AOUT in logic analyzer window.

DEPT. OF ECE, RLJIT 26


Embedded Controller Lab Manual [15ECL68]

Experiment No.4B
Interface a DAC and Generate Triangular Waveform
Date:

Aim: to write a Embedded C program to configure an internal DAC to produce square waveform on CRO
using ARM Cortex-M3 LPC1768 microcontroller.

Embedded C code:

#include <lpc17xx.h>
int main(void)
{
unsigned int j=0;
LPC_PINCON->PINSEL1 = (1<<21);
while(1)
{
for (j=0;j<1023;j++)
LPC_DAC->DACR = j<<6;
for (j=1023;j>0;j--)
LPC_DAC->DACR = j<<6;
}
}

DEPT. OF ECE, RLJIT 27


Embedded Controller Lab Manual [15ECL68]

Pin Configuration:

OUTPUT: Connect DAC module to FRC-5.


Note: To see waveform on monitor use AOUT in logic analyzer window.

DEPT. OF ECE, RLJIT 28


Embedded Controller Lab Manual [15ECL68]

Experiment No. 5A
Program to Configure LCD Display
Date:

Aim: to write a Embedded C program to display digital output on LCD for a given analog input using
internal ADC of ARM Cortex-M3 LPC1768 microcontroller.

Embedded C code:
#include<lpc17xx.h>
void delay(unsigned int d);
void lcdcmd(unsigned int cmd);
void lcddata(unsigned int value);
void lcdstr(unsigned char addr,char *msg);
void convert(unsigned int adc);
int main()
{
unsigned int adc=0,t=1;
LPC_GPIO1->FIODIR=0x03FF8000;
lcdcmd(0x38);
lcdcmd(0x0C);
lcdcmd(0x01);
lcdcmd(0x06);
lcdstr(0x80,"ADC TEST");
lcdstr(0xc0,"ADC Value=");
LPC_SC->PCONP|=(1<<12);
LPC_ADC->ADCR=(1<<21)|(10<<8);
LPC_PINCON->PINSEL1=(1<<18);
while(1)
{
LPC_ADC->ADCR|=4;
delay(10);
LPC_ADC->ADCR|=(1<<24);
while(LPC_ADC->ADDR2&(t<<31)==0);
adc=((LPC_ADC->ADDR2>>4)&0XFFF);
adc=(adc*(3.901/4096)*10);
convert(adc);
}
}
void convert(unsigned int adc)
{ lcdcmd(0xCB);
lcddata((adc/10)|0x30);
lcddata('.');

DEPT. OF ECE, RLJIT 29


Embedded Controller Lab Manual [15ECL68]

lcddata((adc%10)|0x30);
lcddata('V');
}
void delay(unsigned int d)
{
unsigned int i,j;
for(i=0;i<d;i++)
for(j=0;j<1200;j++);
}
void lcdcmd(unsigned int cmd)
{
cmd=cmd<<18;
LPC_GPIO1->FIOPIN=(1<<17)|(cmd);
delay(5);
LPC_GPIO1->FIOPIN=(0<<17)|(cmd);
}
void lcdstr(unsigned char addr,char *msg)
{
lcdcmd(addr);
while(*msg!=0)
lcddata(*msg++);
}
void lcddata(unsigned int value)
{
value=value<<18;
LPC_GPIO1->FIOPIN=(1<<15)|(1<<17)|value;
delay(5);
LPC_GPIO1->FIOPIN=(1<<15)|(0<<17)|value;
}

DEPT. OF ECE, RLJIT 30


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 31


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 32


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 33


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 34


Embedded Controller Lab Manual [15ECL68]

Pin Configuration:

Output:
Connect ADC module to FRC-5
Connect LCD module to FRC-2

DEPT. OF ECE, RLJIT 35


Embedded Controller Lab Manual [15ECL68]

Experiment No. 5B
Interface a Keyboard and Display Key on an LCD
Date:

Aim: to write a Embedded C program to Interface a 4x4 keyboard and to display the key code on LCD
using ARM Cortex-M3 LPC1768 microcontroller.

Embedded C code:
#include <lpc17xx.h>
void delay(unsigned int d);
void lcdstr(unsigned char addr,char *msg);
void lcddata(unsigned int value);
void lcdcmd(unsigned int cmd); char scan(int key);
int main()
{
LPC_GPIO1->FIODIR = 0x03FF8000;
lcdcmd(0x38); lcdcmd(0x0C);
lcdcmd(0x01);
lcdcmd(0x06);
lcdstr(0x80,"4x4 Key Pad Test");
lcdstr(0xC0,"Key Pressed= ");
LPC_GPIO0->FIODIR = 0xFC3;
while(1)
{
LPC_GPIO0->FIOCLR = 0x3C0;
LPC_GPIO0->FIOSET = 0x380;
delay(50);
if(scan(0x038))lcdstr(0xCD,"0");
if(scan(0x034))lcdstr(0xCD,"4");
if(scan(0x02C))lcdstr(0xCD,"8");
if(scan(0x01C))lcdstr(0xCD,"C");
LPC_GPIO0->FIOCLR = 0x3C0;
LPC_GPIO0->FIOSET = 0x340;
if(scan(0x038))lcdstr(0xCD,"1");
if(scan(0x034))lcdstr(0xCD,"5");
if(scan(0x02C))lcdstr(0xCD,"9");
if(scan(0x01C))lcdstr(0xCD,"D");
LPC_GPIO0->FIOCLR = 0x3C0;
LPC_GPIO0->FIOSET = 0x2C0;
if(scan(0x038))lcdstr(0xCD,"2");
if(scan(0x034))lcdstr(0xCD,"6");
if(scan(0x02C))lcdstr(0xCD,"A");
if(scan(0x01C))lcdstr(0xCD,"E");

DEPT. OF ECE, RLJIT 36


Embedded Controller Lab Manual [15ECL68]

LPC_GPIO0->FIOCLR = 0x3C0;
LPC_GPIO0->FIOSET = 0x1C0;
if(scan(0x038))lcdstr(0xCD,"3");
if(scan(0x034))lcdstr(0xCD,"7");
if(scan(0x02C))lcdstr(0xCD,"B");
if(scan(0x01C))lcdstr(0xCD,"F");
}
}
void delay(unsigned int d)
{
unsigned int i,j;
for(i=0;i<d;i++)
for(j=1;j<1200;j++);
}
void lcddata(unsigned int VALUE)
{
VALUE = VALUE<<18;
LPC_GPIO1->FIOPIN = (1<<15)|(1<<17)|VALUE;
delay(5);
LPC_GPIO1->FIOPIN = (1<<15)|(0<<17)|VALUE;;
}
void lcdcmd(unsigned int cmd)
{
cmd = cmd<<18;
LPC_GPIO1->FIOPIN = ((1<<17)|cmd);
delay(5);
LPC_GPIO1->FIOPIN = ((0<<17)|cmd);
}
void lcdstr(unsigned char addr,char *msg)
{
lcdcmd(addr); while(*msg!=0) lcddata(*msg++);
}
char scan(int key)
{
while((LPC_GPIO0->FIOPIN & 0x03C)==key)
{
delay(50);
if((LPC_GPIO0->FIOPIN & 0x03C)== 0x03C)return(1);
}
return(0) ;}

Pin Configuration:

DEPT. OF ECE, RLJIT 37


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 38


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 39


Embedded Controller Lab Manual [15ECL68]

Experiment No.6
Generation of PWM

DEPT. OF ECE, RLJIT 40


Embedded Controller Lab Manual [15ECL68]

Date:

Aim: to write a Embedded C program to configure an internal PWM module to generate PWM wave and
to vary its duty cycle on CRO using ARM Cortex-M3 LPC1768 microcontroller.

Embedded C code:
#include <lpc17xx.h>
void delay(unsigned int d)
{
unsigned int i,j; for(i=0;i<d;i++) for(j=0;j<500;j++);
}
int main(void)
{
int dc;
LPC_PINCON->PINSEL4 = 1;
LPC_PWM1->MR0 = 10000;
LPC_PWM1->MCR = 2;
LPC_PWM1->PCR = (1<<9); //or 0x200
LPC_PWM1->TCR = 2;
LPC_PWM1->TCR = 9;
while(1)
{
for(dc=0; dc<10000; dc++)
{
LPC_PWM1->MR1 = dc;
LPC_PWM1->LER =3;
delay(5);
}
for(dc=10000; dc>0; dc--)
{
LPC_PWM1->MR1 =dc;
LPC_PWM1->LER =3;
delay(5);
}
}
}

Pin Configuration:

DEPT. OF ECE, RLJIT 41


Embedded Controller Lab Manual [15ECL68]

Output: Connect PWM module to FRC-1 and waveform will be displayed on CRO.

DEPT. OF ECE, RLJIT 42


Embedded Controller Lab Manual [15ECL68]

Experiment No.7
Demonstration of external interrupt to toggle LED
Date:

Aim: to write a Embedded C program to demonstrate the use of an external interrupt to toggle an LED
On/Off using ARM Cortex-M3 LPC1768 microcontroller.

Embedded C code:
#include <lpc17xx.h>
void EINT0_IRQHandler(void)
{
LPC_SC->EXTINT = 1; LPC_GPIO0->FIOPIN ^= 0x20;
}
int main()
{
LPC_PINCON->PINSEL4 = (1<<20); //or 0x00100000;
LPC_SC->EXTINT = 1;
LPC_SC->EXTMODE = 1;
LPC_SC->EXTPOLAR = 0;
LPC_GPIO0->FIODIR = 0x20;
NVIC_EnableIRQ(EINT0_IRQn);
while(1);
}

EINTx Pins

LPC1768 has four external interrupts EINT0-EINT3. As LPC1768 pins are multi functional, these four
interrupts are available on multiple pins.

Below table shows mapping of EINTx pins.

Port Pin PINSEL_FUNC_0 PINSEL_FUNC_1 PINSEL_FUNC_2 PINSEL_FUNC_3

P2.10 GPIO EINT0 NMI

P2.11 GPIO EINT1 I2STX_CLK

P2_12 GPIO EINT2 I2STX_WS

P2.13 GPIO EINT3 I2STX_SDA

DEPT. OF ECE, RLJIT 43


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 44


Embedded Controller Lab Manual [15ECL68]

DEPT. OF ECE, RLJIT 45


Embedded Controller Lab Manual [15ECL68]

Pin Configuration:

DEPT. OF ECE, RLJIT 46


Embedded Controller Lab Manual [15ECL68]

Experiment No.8
To Display Hex Digits on Seven Segment Interface
Date:

Aim: to write a Embedded C program on an 7-Seven segment LED interface to display the hex digits 0 to
F with an appropriate delay using ARM Cortex-M3 LPC1768 microcontroller.
Embedded C code:
#include<lpc17xx.h>
void display(unsigned char value);
void delay(unsigned int d);
unsigned char
data[16]={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
int main()
{
unsigned char i,j; LPC_GPIO2->FIODIR=7; while(1)
{
for(i=0; i<16; i++)
{
for(j=0;j<6;j++) display(data[i]); delay(50);
}
}
}
void display(unsigned char value)
{
unsigned char m;
for(m=0; m<8;m++)
{
if(value&0x80)
LPC_GPIO2->FIOSET=2;
else
LPC_GPIO2->FIOCLR=2;
LPC_GPIO2->FIOSET=1;
LPC_GPIO2->FIOCLR=1;
value<<=1;
}
LPC_GPIO2->FIOSET=4;
LPC_GPIO2->FIOCLR=4;
}
void delay(unsigned int d)
{
unsigned int i,j;

DEPT. OF ECE, RLJIT 47


Embedded Controller Lab Manual [15ECL68]

for(i=0;i<d;i++)
for(j=0;j<10000;j++);
}

7-segment Display
The 7-segment display, also written as “seven segment display”, consists of seven LEDs
(hence its name) arranged in a rectangular fashion as shown. Each of the seven LEDs is called a
segment because when illuminated the segment forms part of a numerical digit (both Decimal and Hex)
to be displayed. An additional 8th LED is sometimes used within the same package thus allowing the
indication of a decimal point, (DP) when two or more 7-segment displays are connected together to
display numbers greater than ten.
Each one of the seven LEDs in the display is given a positional segment with one of its connection pins
being brought straight out of the rectangular plastic package. These individually LED pins are labelled
from a through to g representing each individual LED. The other LED pins are connected together and
wired to form a common pin
So by forward biasing the appropriate pins of the LED segments in a particular order, some segments
will be light and others will be dark allowing the desired character pattern of the number to be
generated on the display. This then allows us to display each of the ten decimal digits 0 through to 9 on
the same 7-segment display.
The displays common pin is generally used to identify which type of 7-segment display it is. As each
LED has two connecting pins, one called the “Anode” and the other called the
“Cathode”, there are therefore two types of LED 7-segment display called: Common Cathode
(CC) and Common Anode (CA)
The difference between the two displays, as their name suggests, is that the common cathode has all the
cathodes of the 7-segments connected directly together and the common anode has all the anodes of the
7-segments connected together and is illuminated as follows.
 The Common Cathode (CC) – In the common cathode display, all the cathode connections of the
LED segments are joined together to logic “0” or ground. The individual
segments are illuminated by application of a “HIGH”, or logic “1” signal via a
current limiting resistor to forward bias the individual Anode terminals (a-h).
Common Cathode 7-segment Display

DEPT. OF ECE, RLJIT 48


Embedded Controller Lab Manual [15ECL68]

The Common Anode (CA) – In the common anode display, all the anode connections of the LED
segments are joined together to logic “1”. The individual segments are
illuminated by applying a ground, logic “0” or “LOW” signal via a suitable
current limiting resistor to the Cathode of the particular segment (a-g).
Common Anode 7-segment Display

In general, common anode displays are more popular as many logic circuits can sink more current than
they can source. Also note that a common cathode display is not a direct replacement in a circuit for a
common anode display and vice versa, as it is the same as connecting the LEDs in reverse, and hence
light emission will not take place.

Depending upon the decimal digit to be displayed, the particular set of LEDs is forward biased. For
instance, to display the numerical digit 0, we will need to light up six of the LED segments
corresponding to a, b, c, d, e and f. Thus the various digits from 0 through 9 can be displayed using a

7-segment display as shown.

Then for a 7-segment display, we can produce a truth table giving the individual segments that need to

be illuminated in order to produce the required decimal digit from 0 through F as shown below.

DEPT. OF ECE, RLJIT 49


Embedded Controller Lab Manual [17ECL67

7-segment Display Truth Table

Digit h g f e d c b a
0 1 1 0 0 0 0 0 0

1 1 1 1 1 1 0 0 1
2 1 0 1 0 0 1 0 0
3 1 0 1 1 0 0 0 0
4 1 0 0 1 1 0 0 1
5 1 0 0 1 0 0 1 0
6 1 0 0 0 0 0 1 0
7 1 1 1 1 1 0 0 0
8 1 0 0 0 0 0 0 0
9 1 0 0 1 0 0 0 0
A 1 0 0 0 1 0 0 0
B 1 0 0 0 0 0 1 1
C 1 1 0 0 0 1 1 0
D 1 0 1 0 0 0 0 1
E 1 0 0 0 0 1 1 0
F 1 0 0 0 1 1 1 0

Output: Connect seven segment module to FRC-1.

DEPT. OF ECE, RLJIT 50


Embedded Controller Lab Manual [17ECL67

Pin Configuration:

DEPT. OF ECE, RLJIT 51


Embedded Controller Lab Manual [17ECL67

Experiment No.9
Interface of Simple Switch
Date:

Aim: to write a Embedded C programto . Interface a simple Switch to display its status
through Relay, Buzzer and LED using ARM Cortex-M3 LPC1768 microcontroller.
Embedded C code:
#include <lpc17xx.h>
int main()
{
LPC_GPIO2->FIODIR = 0x30;
LPC_GPIO2->FIOPIN = 0x10;
while(1)
{
while((LPC_GPIO2->FIOPIN & 4)!=4)
LPC_GPIO2->FIOPIN= 0x20;
LPC_GPIO2->FIOPIN= 0x10;
}
}
Output: Connect RELAY module to FRC-1.
Pin Configuration:

DEPT. OF ECE, RLJIT 52


Embedded Controller Lab Manual [17ECL67

Experiment No.10
Interface Temperature Sensor
Date:

Aim: to write a Embedded C program to interface temperature sensor and SPI ADC IC to
measure the ambient temperature LED using ARM Cortex-M3 LPC1768 microcontroller.
Embedded C code:
#include <lpc17xx.h>
void delay(unsigned int d);
void lcdstr(unsigned char addr,char *msg);
void lcddata(unsigned int value);
void lcdcmd(unsigned int cmd);
void convert(unsigned int adc);
unsigned int temperature(void);
unsigned int adc=0,temp=0;
int main()
{
LPC_GPIO1->FIODIR = 0x03FF8000;
lcdcmd(0x38);
lcdcmd(0x0C);
lcdcmd(0x01);
lcdcmd(0x06);
lcdstr(0x80," AMBIENT ROOM ");
lcdstr(0xC0," TEMPe: ");
LPC_GPIO0->FIODIR = 0xFC;
LPC_GPIO0->FIOPIN = 0x00;
while(1)
{
adc=temperature();
adc = (1000*(adc*(5.0/4096)))-100;
convert(adc);
}
}
unsigned int temperature()
{
unsigned char cnt=0; LPC_GPIO0->FIOSET |= 0xC0; LPC_GPIO0->FIOCLR |=
0x40; for(cnt=0;cnt<15;cnt++)
{
LPC_GPIO0->FIOCLR |= 0x80;
temp = LPC_GPIO0->FIOPIN;
if(temp & 0x100) adc=adc|1;
else adc=adc & 0xFFE; if(cnt<14) adc<<=1;

DEPT. OF ECE, RLJIT 53


Embedded Controller Lab Manual [17ECL67

LPC_GPIO0->FIOSET |= 0x80;
}
LPC_GPIO0->FIOSET |= 0x40;
LPC_GPIO0->FIOCLR |= 0x80;
adc=adc & 0xFFF;
return(adc);
}
void delay(unsigned int d)
{
unsigned int i,j;
for(i=0;i<d;i++)
for(j=1;j<1200;j++);
}
void lcddata(unsigned int value)
{
value = value<<18;
LPC_GPIO1->FIOPIN = ((1<<15)|(1<<17)|value);
delay(5);
LPC_GPIO1->FIOPIN = ((1<<15)|(0<<17)|value);
}
void lcdcmd(unsigned int cmd)
{
cmd = cmd<<18;
LPC_GPIO1->FIOPIN = ((1<<17)|cmd);
delay(5);
LPC_GPIO1->FIOPIN = ((0<<17)|cmd);
}
void lcdstr(unsigned char addr,char *msg)
{
lcdcmd(addr);
while(*msg!=0)
lcddata(*msg++);
}
void convert(unsigned int adc)
{
lcdcmd(0xC8);
lcddata((adc/100)|48);
lcddata(((adc/10)%10)|48);
lcddata('.');
lcddata((adc % 10)|48);
lcddata(223);
lcddata('C');

DEPT. OF ECE, RLJIT 54


Embedded Controller Lab Manual [17ECL67

}
Output: Connect PWM/Temperature Sensor module to FRC-4, LCD to FRC-2
Pin Configuration:

DEPT. OF ECE, RLJIT 55


Embedded Controller Lab Manual [17ECL67

THE KEIL SOFTWARE FLOW

First open the icon keil µvision4 on Windows desktop and the following steps are give
below.

 The menu bar provides you with menus for editor operations, project
maintenance, development tool option settings, program debugging, external
tool control, window selection and manipulation, and on-line help.
 The toolbar buttons allow you to rapidly execute µVision4 commands. A Status
Bar provides editor and debugger information.
 The various toolbars and the status bar can be enabled or disabled from the
View Menu commands.

DEPT. OF ECE, RLJIT 56


Embedded Controller Lab Manual [17ECL67

Creating a Project

This will explains the steps required to setup a simple application and to generate
HEX output.

STEP 1: Go to “Project” and close the current project “Close Project”.

STEP 2: Go to the “Project” menu and click on “New Micro vision Project”

DEPT. OF ECE, RLJIT 57


Embedded Controller Lab Manual [17ECL67

STEP 3: Here we can see a small window as “Create New Project” and here we can
select our destination to wherever we want.

STEP 4: Open one drive and create a “New Folder” and to give any name related to
the Project.

DEPT. OF ECE, RLJIT 58


Embedded Controller Lab Manual [17ECL67

STEP 5: Again we can see a small window as “Select Device for Target ‘Target 1’”,
here select NXP founded by Philips.

STEP 6: Within the NXP Philips select LPC1768

STEP 7: If we want to add Startup file we can click “Yes” or else click on “No”.

DEPT. OF ECE, RLJIT 59


Embedded Controller Lab Manual [17ECL67

STEP 8: Next go to “File” and click on “New”.

STEP 9: These are the main three windows in the keil IDE. One is Project Workspace,
second is Editor Window and third is Output Window.

DEPT. OF ECE, RLJIT 60


Embedded Controller Lab Manual [17ECL67

STEP 10: In editor window we can start to write our program and after editing we
have to save.

DEPT. OF ECE, RLJIT 61


Embedded Controller Lab Manual [17ECL67

STEP 11: Save the file, if the program is in “C” save as “filename.C” or else if it is in
ASM save as “filename.ASM”.

STEP 12: Next add this source file to Group1, for that go to “Project Workspace” drag
the “Target 1” in that right click on “Source Group1” and click on “Add Files to
Group “Source Group1””.

DEPT. OF ECE, RLJIT 62


Embedded Controller Lab Manual [17ECL67

STEP 13: Here one small window will open as “Add Files to Group “Source Group1”,
default the Files of type will be in C source Files (*.C). If our program is in C we need
to select C source Files (*.C) or select ASM Source file (*.s,*.src,*.a*).

STEP 14: Then go to “Project” click on “Build Target” or F7. There we can see errors
and warning in Output Window.

DEPT. OF ECE, RLJIT 63


Embedded Controller Lab Manual [17ECL67

Keil Simulation

STEP 15: Go to “Project” click on “Rebuild all target Files”

and start Debug.

From View menu we can get Memory Window.

From Peripherals we can get I/O ports, Serial etc.

DEPT. OF ECE, RLJIT 64


Embedded Controller Lab Manual [17ECL67

 For access internal memory type i:0x_memory location example: i:0x30 and
 For external and program memory x:0x_memory location, c:0x_memory
location respectively.
 From Register window we can edit and access the values also.

DEPT. OF ECE, RLJIT 65


Embedded Controller Lab Manual [17ECL67

HEX file creation

STEP 16: Follow the STEP up to 16 and go to “Project” and click on “Option for
Group ‘Source Group1’”. There we can see one window as “Option for Target
‘Target1’”. In that window, within the Device menu we need to select LPC1768 and
click ok.

STEP 17: Next within the Target menu we need to set clock frequency as 25.0 MHz

STEP 18: Then go to Output click on create HEX file and click ok.

DEPT. OF ECE, RLJIT 66


Embedded Controller Lab Manual [17ECL67

STEP 19: Then go to “Project” click on “Rebuild all target files”. There we can see
errors and warning in Output Window.

DEPT. OF ECE, RLJIT 67


Embedded Controller Lab Manual [17ECL67

FlashMagic Programming with communication port (COM1)

STEP 20: For programming with communication port first we need to select the
following:

 Device: LPC1768 within ARM Cortex ,


 COM Port: COM 3,
 Baud rate: 19200,
 Interface: None [ISP],
 Oscillator frequency 25.0 MHz.
 Click on “Erase of all Flash code RD plot.”

STEP 21: Under the options go to Advanced options.

DEPT. OF ECE, RLJIT 68


Embedded Controller Lab Manual [17ECL67

STEP 22: Under the Advanced options go to Hardware configuration click on the
Assert DTR and RTS while COM port open and click ok.

STEP 23: Next browse the file and select the path.

DEPT. OF ECE, RLJIT 69


Embedded Controller Lab Manual [17ECL67

DEPT. OF ECE, RLJIT 70


Embedded Controller Lab Manual [17ECL67

STEP 24: After selecting ISP mode and reset at the kit and click on start.

STEP 25: After the above steps we can see the finished indication.

DEPT. OF ECE, RLJIT 71

You might also like