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

Embedded Controller Lab Manual 15ECL67

ACHARYA INSTITUTE OF TECHNOLOGY


Department of Electronics & Communication Engineering

Embedded Controller Lab Manual

[15ECL67]
VI- SEMESTER ECE/TCE CBCS Scheme

By

RAGHUNATH .B.H

Name of Student: _______________________

USN: _____________________________________

Page 1
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Course Title Course Code Core/Elective Credits Semester Year

Embedded Controller Lab 15ECL67 CORE 2 6 2018-19


Even

Contact Hours/ week Lecture Tutorials Practical


3 1 0 2

 Name of the faculty RAGHUNATH.B.H


Designation and mail ID Assistant Professor
raghunath@acharya.ac.in
Department ECE

B) Course Related Specifics

Prerequisites for the Course Knowledge of Digital electronics,


(exemption from the prerequisites is Microprocessor
Microprocessor Architecture and
 provided only in exceptional circumstance Basics of „ C ‟ Programming

1. Understand the architecture of ARM Cortex M3- LPC1768 microcontroller


Course Objectives and assembly programming of ARM.
2. Learn to design, construct, program, verify, analyze and troubleshoot
ARM assembly and C language programs and a nd supporting hardware.

Syllabus :

PART-A : Conduct the following Study experiments to learn ALP using ARM Cortex M3 Registers using an
Evaluation board and the required software tool.
1. ALP to multiply
multiply two 16 bit binary
binary numbers.
numbers.
2. ALP to find the sum of first 10 integer
integer numbers.

PART-B: Conduct the following experiments


experiments on an ARM CORTEX M3 evaluation board using evaluation
version of Embedded 'C' & Keil Uvision-4 tool/compiler.

1. Display “Hello World” message using Internal UAR T.


T.
2. Interface and Control a DC Motor.
3. Interface a Stepper
Stepper motor and rotate it
it in clockwise and anti-clockwise
anti-clockwise direction.
4. Determine Digital output for
for a given Analog input using Internal
Internal ADC of ARM
ARM controller.
5. Interface a DAC and generate Triangular and Square waveforms.
6. Interface a 4x4 keyboard and display the key code on an LCD.
7. Using the Internal PWM
PWM module
module of ARM controller
controll er generate PWM and vary its duty cycle.

8. Demonstrate the use of an external in terrupt to toggle an LED On/Off.


9. Display the Hex digits 0 to F on a 7-segment
7-segment LED interface, with
with an appropriate delay
delay in between.
10. Interface a simple Switch and display its status through Relay, Buzzer and LED.

11. Measure Ambient


Ambient temperature using
using a sensor and SPI ADC
ADC IC.

Page 2
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Cycle 1:
I. Differentiate between Microprocessor and Microcontroller.
II. Introduction to Cortex M3 Controller – 
Controller – 
i. Features of Cortex M3 Controller.
ii. GPIO Operations – 
Operations –  Selection
 Selection and Configuration
III. Usage of Keil Microvision Tool.
i. Creating Project  –  selection of target controller and setting environment
ii. Creating Embedded
Embedded C code.
code.
iii. Compilation and Execution of Embedded
Embedded C Code
IV. Interface of Cortex
Cortex M3 Evaluation board with PC.
i. Connection of Cortex M3 board
board to host PC
ii. Usage of Flash magic
magic to download Embedded
Embedded C code to Cortex M3 board.

Embedded C Codes
1. Interfacing of “LED”
LED” –  8 8 no‟s
no‟s to
 to a GPIO of Cortex
Cort ex M3 Controller
i. To blink a single LED bit by bit.
ii. To scroll
scroll a LED from bottom to top and top to bottom.
2. Interface of toggle switch to a GPIO
GPIO of Cortex
Cortex M3 Controller.
i. Switch Status to be displayed on LED.
LED.
ii. Switch Status to be displayed
displayed on Buzzer. iii. Switch status to be displayed
displayed on Relay.
Cycle 2:
3. Usage of an external
external interrupt to toggle an LED on/off.
4. Usage of Internal UART.
i. To transmit a single character data from Host to UART.
ii. To receive a single character data from UART to Host.
iii. To transmit
transmit a string of character
character data from Host to UART
UART

5. Usage of internal DAC of Cortex


Cortex M3 controller
i. To generate Square waveform.
waveform.
ii. To generate
generate Triangular waveform.
waveform.

6. Interface a Stepper
Stepper motor
motor to a GPIO of Cortex M3 Controller
i. Rotate stepper motor in clockwise direction
ii. Rotate stepper motor in an anti-clockwise direction

Cycle 3:

7. Usage of
of Internal PWM to generate PWM
PWM and vary its duty cycle
cycle
8. Interface a DC Motor to GPIO of cortex M3 controller and and perform it‟s
it‟s control
 control
operation using switches.
9. Interface of Seven segment module to Cortex M3 Controller – 
Controller – 
i. To display hexa digits 0 to F with suitable delay.
ii. To display hexa digits 00 to FF with suitable delay

Cycle 4:

10. Interface of LCD module to Cortex M3 controller – 


controller – 
i. To display single line message.
message.
ii. To display two line messages.
messages.
11. Usage of Internal ADC – 
ADC – 
i. To determine Digital output for a given Analog input and display
display digital data on to LCD.
12. Interface a 4x4 keyboard to Cortex M3 controller and display the key code on a LCD.
13. Measurement
Measurement of ambient temperature using a sensor and SPI ADC IC.

Page 3
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Keil Micro Vision® 5 IDE tool : Step by step Tutorial


Open Keil 5 IDE tool

Step 1. Open the Keil IDE, under main menu goto “Project ->  New uVision Project…” and
Project…” and a window prompt
will open asking a name to save the new project. Type your desired project name and save.

Step 2. After that, a new window will appear as shown below. Make sure “Software Packs” is
Packs” is selected for the 1st
drop down. In the search box below it, type “lpc1768” and
“lpc1768” and then select the device from list below. Finally click
“OK”.
“OK”.

Page 4
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Step 3. Now, inside the “Manage Run-


Run-Time Environment Window” select
Window” select the check boxes for “CORE” 
“CORE”  under
“CMSIS” and
“CMSIS” and “Startup” under
“Startup” under “Device”.
“Device” . If you want to select any other libraries you can do so by selecting the
respective checkboxes. Selecting “Startup” will
“Startup” will automatically added all the necessary startup/boot files required
for LPC17xx device, so we don‟t have to import them from external sources. The selection of libraries can be
changed any time later.

Step 4. Now click on “Options for Target” button


Target” button as shown below:

Page 5
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Step 5. Now, click on the “Output” tab.


“Output” tab. If you want to generate hex file then you can check “Create HEX File”.
File”.

Step 6. Then click on the “Linker” tab


“Linker”  tab and Under that tab check the checkbox option which says “Use Memory
Layout from Target Dialog”.
Dialog”.

Step 7. Now,
 Now, under the “Debug” tab,
“Debug” tab, change the values for “Dialog DLL” and
DLL” and “Parameter” under
“Parameter”  under both simulation
and hardware debugger settings group. You can select “Use Simulator” to
Simulator” to debug your code using simulator itself. If
you are using JTAG programmer or any other hardware debugger supported by KEIL then you can select it from

Page 6
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

top-right drop down box. We will see flashing and debugger options in detail in an upcoming post. Finally to click
“OK” to
“OK” to apply settings and close window.

Step 8. Now, in the source navigation pane on the left area, right click on “Source Group 1” and
1” and select “Add New
Item to Group „Source Group 1′”.
1′”.

Page 7
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Step 10. Now you can write your code in the editor. To compile your program Press “F7” key
“F7” key or in the main menu
goto “Project-
“Project->Build Target”. To
Target”. To check for any compilation errors you can have a look at the build output at the
 bottom of main window. A screenshot of the Keil MDK uVision 5 is given below. The top red box shows the
Source Tree Navigation and the bottom red box shows the build output.

Step 11.  To initiate a debug session using simulator or debug hardware/probe click “Debug->Start/Stop
“Debug->Start/Stop Debug
Session” or
Session” or you can hit “Ctrl+F5” as
“Ctrl+F5” as shown below:

 Now open the flash


flash magic software and follow
follow the below steps.

1. Select the Device as LPC1768 from Select Menu.


2. Select the COM Port. Check the device manager for detected COM port .
3. Select the Baud
Baud rate from 9600-115200. Ex: Set as 115200
115200
4. Select None ISP Option.
5. Oscillator Freq in Mhz . Type 12 . i.e 12Mhz
6. Check the Erase all Flash + Code Rd Prot
7. Browse and Select
Select the desired hex file. [project.hex file]
8. Press the Start Button to flash the hex file.

Check COM port number from Device manager. Right click my computer, then click manage. Double click
device manger

Page 8
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Register Configuration

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.
r egisters.

LPC1768 GPIO

PINSEL FIODIR FIOSET, FIOCLR, FIOPIN

Selects Pin Function Configure Pin Direction Access the Port Pin

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 firstf irst 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, typically GPIO port PINSEL_FUNC_0
PINSEL_FUNC_0
01 First alternate function PINSEL_FUNC_1
PINSEL_FUNC_1
10 Second alternate function PINSEL_FUNC_2
PINSEL_FUNC_2
11 Third alternate function PINSEL_FUNC_3
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

Page 9
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

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.

Values FIOSET
0 No Effect
1 Sets High on Pin

FIOCLR:Fast Port Output Clear Register.

This register controls the state of output pins. Writing 1s produces lows at the corresponding port pins. Writing 0s
has no effect.

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).
Note:It is recommended to configure the PORT direction and pin function before using it.

GPIO Registers in LPC1768

Registers on LPC1768 are present on Peripheral AHB bus( bus(Advanced High performance Bus)
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. Lets 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.

Page 10
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

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.
The same is applicable for PINMODE register which will go through in last section of this article. Have a look at
the diagram given below.

PINSEL0 Register

31 30 29 28 27 26 25 24 23 22
22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

P0.15 P0.14 P0.13 P0.12 P0.11 P0.10 P0.9 P0.8 P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0

PINSEL1

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

P0.31 P0.30 P0.29 P0.28 P0.27 P0.26 P0.25 P0.24 P0.23 P0.22 P0.21 P0.20 P0.19 P0.18 P0.17 P0.16

Example : Port 0.0 pin


pin‟s
‟s func
 function
tion is decided
decided by contents of PINSEL0
PINSEL0 register bit 0 and bit 1

P0.0 function decided by P0.0 Function as


PINSEL0.1 PINSEL0.0
0 0 when PINSEL0.1=
PINSEL0.1= 0 , PINSEL0.0= 0 Then P0.0 Function as General Purpose I/O
0 1 when PINSEL0.1=
PINSEL0.1= 0, PINSEL0.0= 1 Then P0.0 Function as RD1
1 0 when PINSEL0.1=
PINSEL0.1= 1, PINSEL0.0= 0 Then P0.0 function as TXD3
1 1 when PINSEL0.1=
PINSEL0.1= 1, PINSEL0.0= 1 Then P0.0 function as SDA1

Since by default all pins are configured as GPIOs we don‟t need to explicitly assign a „0‟  value to PINSEL
register in Programming
Programming examples.

Page 11
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Example #1)

To configure Pin 4 of Port 0 i.e P0.4 as Output and want to make it High (Logic 1)

LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR = 0x0000 0010; 0001 0000; 4 th bit is 1
// in 32 bit form 0000 0000 0000 0000 0000 0000 0001

Alternate way is
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<< 4); // 1 in 32 bit form left shift by 4 times.
0000 0000 0000 0000 0000 0000 0000 0001 left shit by 4 times results as
0000 0000 0000 0000 0000 0000 0001 0000. which Configures P0.4 as Ouput
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOSET = (1<< 4); // SET High for P0.4 pin

LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR = 0x0000 0010; // omitting left side zeros ; we can write as 0 x10 ; x stands for Hexadecimal number

LPC_GPIO->FIODIR
LPC_GPIO ->FIODIR = 0x10; // Configure pin 4 of port 0 as Output
LPC_GPIO->FIOSET=0x10
LPC_GPIO ->FIOSET=0x10;; //pin 4 of port 0 is made HIGH

Example #2

Configure Pin 17 of Port 1 [ P1.17] as output and then SET that Pin and
and CLEAR it:

LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR = (1<<17); // configuring 17th pin of port 1 [P1.17] as Output pin
LPC_GPIO1->FIOSET
LPC_GPIO1 ->FIOSET = (1<<17); // Output for P1.17 becomes High
LPC_GPIO1->FIOCLR
LPC_GPIO1 ->FIOCLR = (1<<17); // Output for P1.17 becomes Low

Example #3) Configure Pin 3 and 5 of port 0 as output and SET them Logic Hig h.

i.e Configuring P0.3 and P0.5 as Output and Setting them High:

LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<<3) | (1<<5)
(1<<5) // 1 left shift 3 logically ORed
ORed with 1 left shift by 5
0000 0000 0000 0000 0000 0000 0000 1000 [ 1 << 3 ]
LOGIC OR with 0000 0000 0000 0000 0000 0000 0010 0000 [ 1 << 5 ]
0000 0000 0000 0000 0000 0000 0010 1000 3 rd and 5th bits are 1. So P0.3 and P0.5 configures as
output
Alternate way is
LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR =0x0000 0028; // grouping above terms or we can also write as 0x28
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOSET = (1<<3) | (1<<5) // Make ouput High for P0.3 and P0.5

Example #4)

Configuring 1st 8 Pins of Port 0 (P0.0 to P0.7) as Ouput and Setting them High:

LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR = 0x000000FF; // Config P0.0 to P0.7 as Ouput. 0000 0000 0000 0000 0000 1111 1111
LPC_GPIO0->FIOSET
LPC_GPIO0 ->FIOSET = 0x000000FF; // Make output High for P0.0 to P0.7

Example #5)

Configuring Pins of Port 0 (P0.4


(P0.4 to P0.11) as Output and Setting them High: and Low

LPC_GPIO0->FIODIR
LPC_GPIO0 ->FIODIR = 0xFF0; // Config P0.4 to P0.11 as Ouput. 0000 0000 0000 0000 0000 1111 1111 0000
LPC_GPIO0->FIOSET
LPC_GPIO0 ->FIOSET = 0xFF0; // 1111 1111 0000 Make output High for P0.4 to P0.11
LPC_GPIO0->FIOCLR
LPC_GPIO0 ->FIOCLR = 0xFF0; // Make output LOW for P0.4 to P0.11

Example #6) Configuring Pins of Port 1 (P1.14 to P0.21) as Input :

LPC_GPIO1->FIODIR
LPC_GPIO1 ->FIODIR &= ~(0xFF0); // Config P1.14 to P1.21 as Input. ~ means complement
LPC_GPIO1->FIODIR
LPC_GPIO1 ->FIODIR &=~(1<<14); // only Pin p1.14 is configured as input

Note: On reset by default all registers are zero. So all port‟s all the pins acts as input. We can skip setting i/p lines

Page 12
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Blink a LED

#include <lpc17xx.h>

int main(void)
{
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<< 4); // Configuring P0.4 as Output
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR &= ~ ((1<< 21)) ; // explicitly making P0.21 as Input . we can skip this line

//by default all port pins are i/p


while(1)
{
if ( ! (LPC_GPIO1->FIOPIN
(LPC_GPIO1->FIOPIN & (1<< 21)) ) //if key P1.21 pressed
{
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOS ET = (1<< 4); // SET P0.4 High LED ->ON
}
else
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOC LR = (1<< 4); // P0.4 low LED-> OFF
}
}

Lab Program # 1. Interfacing of “LED”


LED” –  8
 8 no‟s
no‟s to
 to a GPIO of Cortex M3 Controller

a. To blink LEDs

#include <LPC17xx.h>
int main()
{
LPC_GPIO0-
LPC_GPIO0- >FIODIR = 0x0FF0; // P0.4-P0.11 pins as o/p.
while(1) //infinite loop
{
int i;
LPC_GPIO0 - >FIOSET = 0x0FF0; // SET po.4 to p0.11 LEDs Turns ON
for (i=10000000; i >0; i- -); // delay
LPC_GPIO0
LPC_GPIO0 - >FIOCLR = 0x0FF0; //clear P0.4 to P.11 LEDs Turns OFF
for (i=10000000;i>0; i--); //delay
}
}

Page 13
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

1.b to Scroll LEDs (Ring Counter) P0.11 P0.10 P0.9 P0.8 P0.7 P0.6 P0.5 P0.4

LED8 LED7 LED6 LED5 LED4 LED3 LED2 LED1


#include <lpc17xx.h>
unsigned int x, y, z; In ARM kit LEDs are connected as shown in above table
int main(void)
{
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = 0x0FF0; // Configure P0.4 to p0.11 as Output
while(1) //infinite loop
{
z = 0x0800; // initialize with 1000 0000 0000; to make P0.11 bit is high

for (x=8;
(x=8; x > 0; x - - )
{
LPC_GPIO0
LPC_GPIO0 - > FIOPIN = z; // Initially P0.11 to turn LED ON
for (y=1000000; y > 0; y- -); // delay
z = z >> 1; // right shift Z content by 1 time
}
}
}

Lab Program # 2.a Interface a simple Switch and display its status through LED.

#include <LPC17xx.h>

int main()
{
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = 1<< 4; // port P0.4 pin is configured as o/p
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR & = ~ (1<< 21); // port p1.21 pin is configured as i/p

while (1) // infinite loop


{
if ( ! ( LPC_GPIO1->
LPC_GPIO1-> FIOPIN & (1<< 21) ) ) // if switch P2.1 is pressed?
{
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOSET = (1<< 4); // if Yes, SET the Port P0.4 pin

}
else

{
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOCLR = (1<< 4); // Else Clear Port P0.4

}
}
}

Page 14
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

2.b. Interface a simple Switch and display its status through Buzzer .

#include <LPC17xx.h>

int main()
{
LPC_GPIO3->FIODIR
LPC_GPIO3->FIODIR = 1<< 25; // port 3.25 pin is configured as o/p
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR & = ~ (1<< 21); // port p1.21 pin is configured as i/p

while(1)
{
if (! (LPC_GPIO1->FIOPIN
(LPC_GPIO1->FIOPIN & (1<< 21))) // if switch P.21 pressed
{
LPC_GPIO3->FIOSET
LPC_GPIO3->FIOSET = (1<< 25); // Port 3.25 pin is set [ High]

}
else

{
LPC_GPIO3->FIOCLR
LPC_GPIO3->FIOCLR = (1<< 25); // Port 3.25 pin is Clear [Low]

}
}
}

2.c. Interface a simple Switch and display its status through Buzzer,Relay and LED.

#include <LPC17xx.h>
int main()
{
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<<4) | (1<<25); // port P0.4-P0.11[LEDS]
P0.4-P0.11[LEDS] & P0.25[Relay] config as o/p
LPC_GPIO3->FIODIR
LPC_GPIO3->FIODIR = (1<<25); // port 3.25[Buzzer]
3.25[Buzzer] configured as o/p
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR &= ~(1<<21); // port P1.21[Switch KEY] configured as i/p

while(1)
{
if (! (LPC_GPIO1->FIOPIN
(LPC_GPIO1->FIOPIN & (1<<21))) // if switch P1.21 pressed

{
LPC_GPIO0->FIOSET = (1<<4) | (1<<25); // turn on LED, RELAY

LPC_GPIO3->FIOSET
LPC_GPIO3->FIOSET = (1<<25); //turn on Buzzer
}
else

{
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOCLR = (1 << 4) | (1 << 25); // turn OFF LED, Relay
LPC_GPIO3->FIOCLR = (1 << 25); //turn
//tur n OFF Buzzer
}
}
}

P1.21 P1.20 P1.19 P1.18 P1.17 P1.16 P1.15 P1.14


Switch Key 8 Switch Key 7 Switch Key 6 Switch Key 5 Switch Key 4 Switch Key 3 Switch Key 2 Switch Key1

ARM kit connection . Switches are connected


connected to Port 1 pin numbers from 14 to 21 as above table.

Page 15
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Applications of Relay

 Commonly used in used in switching circuits.


 For Home Automation projects to switch AC l oads
 To Control (On/Off) Heavy loads at a pre-determined
pre-determined time/condition
 Used in safety circuits to disconnect the load from supply in event of failure
 Used in Automobiles electronics for controlling indicators glass motors etc.

Buzzer Symbol Light Emitting Diode

LPC1768: External Interrupts


EINTx Pins

LPC1768 has four external interrupts EINT0, EINT1, EINT2, EINT3.


As LPC1768 pins are multi functional, these four interrupts are available on multiple pins.
Below table shows mapping of EINTx pins.

Page 16
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

EINT Registers

The registers associated with LPC1768 external interrupts.

Register Description

PINSELx To configure the pins as External Interrupts

External Interrupt Flag Register contains interrupt flags for EINT0,EINT1, EINT2 &
EXTINT
EINT3.

EXTMODE External Interrupt


Interrupt Mode register(Level/Edge
register(Level/Edge Triggered)
Triggered)

EXTPOLAR
EXTPOLAR External Interrupt Polarity(Falling/Rising Edge,
Edge, Active Low/High)

EXTINT

31:4 3 2 1 0

RESERVED EINT3 EINT2 EINT1 EINT0

EINTx:  Bits will be set whenever the interrupt is detected


detected on the particular interrupt pin.
If the interrupts are enabled then the control goes to ISR.
Writing one to specific bit will clear the corresponding interrupt.

EXTMODE

31:4 3 2 1 0

RESERVED EXTMODE3 EXTMODE2 EXTMODE1 EXTMODE0

Page 17
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

EXTMODEx: This bits is used to select whether the EINTx


EINTx pin is level or edge Triggered
0: EINTx is Level Triggered.
1: EINTx is Edge Triggered.

EXTPOLAR 

31:4 3 2 1 0

RESERVED EXTPOLAR3 EXTPOLAR2 EXTPOLAR1 EXTPOLAR0

EXTPOLARx: This bits is used to select polarity(LOW/HIGH,


polarity(LOW/HIGH, FALLING/RISING)
FALLING/RISING) of the EINTx interrupt
depending on the EXTMODE register.

0: EINTx is Active Low or Falling Edge (depending on EXTMODEx).


1: EINTx is Active High or Rising Edge (depending on EXTMODEx).

Steps to Configure Interrupts

1. Configure the pins as external interrupts in PINSELx


PINSELx register.
2. Clear any pending interrupts in EXTINT.
EXTINT.
3. Configure the EINTx as Edge/Level
Edge/Level triggered in EXTMODE
EXTMODE register.
4. Select the polarity(Falling/Rising Edge, Active Low/High) of the interrupt in EXTPOLAR
EXTPOLAR register.
5. Finally enable the interrputs by calling NVIC_EnableIRQ() with IRQ number.

3. a.Demonstrate the use of an external interrupt EINT0 to toggle an LED -ON/OFF.

#include <lpc17xx.h>
void EINT0_IRQHandler(void)
EINT0_IRQHandler(void)
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 1; //0000 0000 0000 0001, EXTINT
EXTINT pin0 is One. Clearing EXTINT0
EXTINT0
LPC_GPIO0->FIOPIN ^ = (1<<4); /* Toggle the LED1 everytime INTR0 is generated */
}
int main( )
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 1; / 0000 0000 0000 0000 0000 0000 0000 0001 Clear EXTINT0
EXTINT0
LPC_PINCON->PINSE
LPC_PINCON->PINSEL4
L4 = (1<< 20); // 1 left shift by 20 times makes P2.10 function as EINT0
EINT0
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<<4); /*configure LED pins as OUTPUT */
 NVIC_EnableIRQ(EINT0_
 NVIC_EnableIRQ(EINT0_IRQn);
IRQn); /* Enable the EINT0
EINT0 interrupts */
while(1)
{ // any main program running
}
}

Page 18
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

3. b.Demonstrate the use of an external interrupt EINT1 to toggle an LED On/Off 

#include <lpc17xx.h>
void EINT1_IRQHandler(void)
EINT1_IRQHandler(void)
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 2; // 0000 0000 0000 0010 Clear Interrupt Flag EXTINT1.
EXTINT1. Reset interrupt
LPC_GPIO0->FIOPIN
LPC_GPIO0->FIOP IN ^ = (1<<11) ; /* Toggle the LED1 everytime INTR1 is generated */
}
int main( )
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 2; // 0000 0000 0000 0010 Clear interrupt flag EXTINT1
LPC_PINCON->PINSEL4 = (1<< 22); /make PINSEL4.22 bit high to make P2.10 pin function as EINT01

LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<<11); /*configure LED P0.11 as OUTPUT */
 NVIC_EnableIRQ(EINT1_
 NVIC_EnableIRQ(EINT1_IRQn);
IRQn); /* Enable the EINT0 interrupts */
*/
while(1)
{ // any main program running
}
}
3. c .Demonstrate the use of an external interrupts EINT0 & EINT1to toggle an LED On/Off 

#include <lpc17xx.h>
void EINT0_IRQHandler(void)
EINT0_IRQHandler(void)
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 1; /* Clear Interrupt Flag ext int 1 */
LPC_GPIO0->FIOPIN
LPC_GPIO0->FIOPIN ^ = (1<< 4) ; /* Toggle the LED1 everytime INTR1 is generated */
}
void EINT1_IRQHandler(void)
EINT1_IRQHandler(void)
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 2; /* Clear Interrupt Flag ext int 1 */
LPC_GPIO0->FIOPIN
LPC_GPIO0->FIOPIN ^ = (1 << 11) ; /* Toggle the LED1 everytime INTR1 is generated */
}
int main( )
{
LPC_SC->EXTINT
LPC_SC->EXTINT = 3; /* Clear Pending interrupts */
LPC_PINCON->PINSEL4 = (1<< 20) | (1<< 22); /* Configure P2_10 as EINT01*/
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR = (1<< 4) | (1<< 11); /*configure LED pins as OUTPUT */
 NVIC_EnableIRQ
 NVIC_EnableIRQ (EINT0_IRQn);
(EINT0_IRQn); /* Enable the EINT0
EINT0 interrupt */
 NVIC_EnableIRQ
 NVIC_EnableIRQ (EINT1_IRQn);
(EINT1_IRQn); /* Enable the EINT1
EINT1 interrupt */
*/
while(1)
{ // any main program running
}
}

Page 19
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

UART module : Universal Asynchronous


Asynchronous Receiver and Transmitter

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.

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


formatting(Number of Data Bits, Stop bits)

DLL Least Significant


Signifi cant 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


registers for UART
UART communication.
communication.

FCR ( FIFO Control Register )

LPC1768 has inbuilt 16byte FIFO for Receiver/Transmitter.


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 TRIGGER RESERVED DMA MODE TX FIFO RESET RX FIFO RESET FIFO ENABLE

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

Bit 1 – 
1 –  RX_FIFO:
  RX_FIFO:
This is used to clear the 16-byte Rx FIFO.

Page 20
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

0--No impact.
1--CLears the 16-byte Rx FIFO and the resets t he FIFO pointer.

Bit 2 – 
2 –  Tx_FIFO:
 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 – 
3 –  DMA_MODE:
  DMA_MODE:
This is used for Enabling/Disabling
Enabling/Disabling DMA mode.
0--Disables the DMA.
1--Enables DMA only when the FIFO(bit-0) bit is SET.

Bit 7:6 – 
7:6 –  Rx_TRIGGER:
  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.
interrupt/DMA.
00-- Trigger level 0 (1 character
character or 0x01)
01-- Trigger level 1 (4 characters
characters or 0x04)
10-- Trigger level 2 (8 characters or 0x08)
11-- Trigger level 3 (14 characters
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.

LCR
31:8 7 6 5:4 3 2 1:0
Break Parity Stop Bit Word Length
Reserved DLAB Parity Select
Control Enable Select Select
Set as-> 1 0 00 0 0 11

Bit 1:0 – 
1:0 –  WLS
 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 – 
2 –  Stop
 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 – 
3 –  Parity
 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.
checking.

Bit 5:4 – 
5:4 –  Parity
 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

Page 21
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Bit 6 – 
6 –  Break
 Break Control
0-- Disable break transmission .
1-- Enable break transmission. Output pin UARTn TXD is forced to logic 0

Bit 8 – 
8 –  DLAB:
 DLAB: Divisor Latch Access Bit
This bit is used to enable the access to divisor latch.
0-- Disable access to divisor latch
1-- 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.

LSR

31:8 7 6 5 4 3 2 1 0
Reserved RXFE TEMT THRE BI FE PE OE RDR

Bit 0 – 
0 –  RDR:
 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 – 
1 –  OE:
 OE: Overrun Error
The overrun error condition is set when the UART Rx FIFO is full
f ull and a new character
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 – 
2 –  PE:
 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 – 
3 –  FE:
 FE: Framing Error
This bit is set when there is error in the STOP bit(LOGIC
bit(LOGIC 0)
0-- No Framing Error
1-- Framing Error

Bit 4 – 
4 –  BI:
 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 – 
5 –  THRE:
 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
t o be transmitted

Bit 6 – 
6 –  TEMT:
 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.

Page 22
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Bit 7 – 
7 –  RXFE:
 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


Enable/Disable the transmission

TER

31:8 7 6-0

Reserved TXEN Reserved

Bit 7 – 
7 –  TXEN:
 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
character is completed, but no
further characters are sent until this bit is set again.
In other words, a 0 in this
thi s bit blocks the transfer of characters.
characters.

  Note: By default this bit will be set after


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
registers contains the PCLK info for all the clock dependent peripherals in which Bit6,Bit7 contains
the Uart Clock(ie.UART_PCLK)
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

2 SystemFreq/2
3 SystemFreq/8

DivAddVal/MulVal
DivAddVal/MulVal == 0

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


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

Page 23
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

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 r equired baudrate from PCLK.
6. Step6: Updtae the DLM,DLL
DLM,DLL with the calculated values.
7. Step6: Finally clear DLAB to disable the access to DLM,DLL.
DLM,DLL.

After this the UART will be ready to Transmit/Receive Data at the specified baudrate.

Page 24
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

4 .Usage of Internal
I nternal UART.

i. To transmit a single character data from Host to UART.


ii. To receive a single character data from UART to Host.
iii. To transmit a string of character data from Host to UART

#include "LPC17xx.h"

void UART0_Init (void)


{
//Make PINSEL 0.4 and PINSEL0.6 pins High
LPC_PINCON->PINSE L0 |= (1 << 4)| (1 << 6);
LPC_PINCON->PINSEL0 // so Pin P0.2 used as TXD0 & Pin P0.3 used as RXD0 /
LPC_UART0->LCR
LPC_UART0->LCR = 0x83; /* 8 bits, no polarity, 1stop bit */
LPC_UART0->DLM = 0;
LPC_UART0->DLL = 9; // 115200 baus rate @25MHz clk
LPC_UART0->LCR
LPC_UART0->LCR = 0x03; /* Lock the baud rate */
LPC_UART0->FDR = 0x21; //MULVAL=2,DIVADDval=1
//MULVAL=2,DIVADDva l=1
}

int UART0_SendChar (int Data)


{
while (!(LPC_UART0->LSR
(!(LPC_UART0->LSR & 0x20)); //if LSR.5th bit =0 wait for valid data.
data.
th
return (LPC_UART0->THR
(LPC_UART0->THR = Data); // LSR.5  bit =1 ,THR has valid data to be transmitted

int UART0_GetChar
UART0_GetChar (void) //LSR line status register
{
while (!(LPC_UART0->LSR
(!(LPC_UART0->LSR & 0x01)); // If LSR.0th bit = 0 The UARTn receiver FIFO is empty. Wait
for data
return (LPC_UART0->RBR);
(LPC_UART0->RBR); //If LSR.0 bit=1 UART receiver not empty. Send it to RBR
}

void UART0_SendString (unsigned char *s)


{
while (*s != 0) // if content of string is not 0
UART0_SendChar(*s++);
UART0_SendChar(*s++); //send char and auto increment
increment i.e s=s+1
}

int main(void)
{
char ch;
SystemInit( );
UART0_Init( ); //initialize UART0
UART0_SendString("hello
UART0_SendString("hello world! "); //string=hello world!
while(1){
ch=UART0_GetChar(
ch=UART0_GetChar( ); // read character
character into ch.
UART0_SendChar(ch);
UART0_SendChar(ch); //send to uart terminal
}
}

Page 25
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

LPC1768 PWM Module

LPC1768 has 6 PWM output pins which can be used as 6-Single edged or 3-Double edged. There as seven match
registers to support these 6 PWM output signals. Below block diagram shows the PWM pins and the associated
Match(Duty Cycle) registers.

Associated Corresponding
PWM Channel Port Pin Pin Functions
PINSEL Register Match Register

0,1 bits of
PWM_1 P2.0 0-GPIO, 1-PWM1[1], 2-TXD1, 3- MR1
PINSEL4

2,3 bits of
PWM_2 P2.1 0-GPIO, 1-PWM1[2], 2-RXD1, 3- MR2
PINSEL4

0-GPIO, 1-PWM1[3], 2-CTS1, 3- 4,5 bits of


PWM_3 P2.2 MR3
TRACEDATA[3] PINSEL4

0-GPIO, 1-PWM1[4], 2-DCD1, 3- 6,7 bits of


PWM_4 P2.3 MR4
TRACEDATA[2] PINSEL4

0-GPIO, 1-PWM1[5], 2-DSR1, 3- 8,9 bits of


PWM_5 P2.4 MR5
TRACEDATA[1] PINSEL4

0-GPIO, 1-PWM1[6], 2-DTR1, 3- 10,11 bits of


PWM_6 P2.5 MR6
TRACEDATA[0] PINSEL4

LPC7168 PWM Registers

The below table shows the registers associated with LPC1768 PWM.

Register Description

Interrupt Register: The IR can be read to identify which of eight possible interrupt sources are pending.
IR
Writing Logic-1 will clear the corresponding interrupt.

TCR Timer Control Register: The TCR is used to control the Timer Counter functions(enable/disable/res
functions(enable/disable/reset).
et).

Page 26
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Timer Counter: The 32-bit TC is incremented


incremented every PR+1 cycles of PCLK. The TC is controlled
TC
through the TCR.

PR Prescalar
Prescalar Register: This is used to specify the Prescalar
Prescalar value for incrementing
incrementing the TC.

Prescale Counter: The 32-bit PC is a counter which is incremented to the value stored in PR. When the
PC
value in PR is reached, the TC is incremented.

Match Control Register: The MCR is used t o control the reseting of TC and generating of interrupt
MCR
whenever a Match occurs.

MR0 Match Register: This register hold the max cycle Time(Ton+Toff).
Time(Ton+Toff) .

MR1- Match Registers: These registers holds the Match value(PWM Duty) for corresponding PWM
MR6 channels(PWM1-PWM6).

PWM Control Register: Enables PWM outputs and selects PWM channel types as either single edge or
PCR
double edge controlled.

LER Load Enable Register: Enables use of new PWM values once the match occurs.

Register Configuration

The below table shows the registers associated


associated with LPC1768
LP C1768 PWM.
TCR Timer Control Register

31:4 3 2 1 0

Reserved PWM Enable Reserved Counter Reset Counter Enable

Bit 0 – 
0 –  Counter
 Counter Enable
This bit is used to Enable or Disable the
t he PWM Timer and PWM Prescalar Counters
0- Disable the Counters
1- Enable the Counter incrementing.
incrementing.

Bit 1 – 
1 –  Counter
 Counter reset
This bit is used to clear the PWM Timer and PWM Prescalar
Prescalar Counter values.
0- Do not Clear.
1- The PWM Timer Counter and the PWM Prescale Counter are synchronously
synchronously reset on the next positive edge of
PCLK.

Bit 3 – 
3 –  PWM
 PWM Enable
Used to Enable or Disable the PWM Block.
0- PWM Disabled
1- PWM Enabled

Page 27
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

MCR Match Control Register

31:21 20 19 18 - 5 4 3 2 1 0

Reserved PWMMR6S PWMMR6R PWMMR6I - PWMMR1S PWMMR1R PWMMR1I PWMMR0 S


PWMMR0S PWMMR0R
PWMM R0R PWMMR0I
P WMMR0I

PWMMRxI
This bit is used to Enable or Disable the PWM interrupts when the PWMTC matches PWMMRx
PWMMRx (x:0-6)
0- Disable the PWM Match interrupt
1- Enable the PWM Match interrupt.

PWMMRxR 
This bit is used to Reset PWMTC whenever it Matches PWMRx(x:0-6)
0- Do not Clear.
1- Reset the PWMTC counter value whenever it matches PWMRx.

PWMMRxS
This bit is used to
t o Stop the PWMTC,PWMPC
PWMTC,PWMPC whenever the PWMTC matches PWMMRx(x:0-6).
0- Disable the PWM stop o match feature
1- Enable the PWM Stop feature. This will stop the PWM whenever the PWMTC reaches the Match register value.

PCR PWM Control  Register Enables PWM outputs and selects PWM channel types as either single
edge or double edge controlled.

31:15 14-9 8-7 6-2 1-0

Unused PWMENA6-PWMENA1 Unused PWMSEL6-PWMSEL2 Unused

PWMSELx
This bit is used to select the single edged and double edge mode form PWMx (x:2-6)
0- Single Edge mode for PWMx
1- Double Edge Mode for PWMx.

PWMENAx
This bit is used to enable/disable the PWM output for PWMx(x:1-6)
0- PWMx Disable.
1- PWMx Enabled.

LER  Load Enable Register: Enables use of new PWM values once the match occurs

31-7 6 5 4 3 2 1 0

Unused LEN6 LEN5 LEN4 LEN3 LEN2 LEN1 LEN0

LENx
This bit is used Enable/Disable the loading of new Match value whenever the PWMTC is reset(x:0-6)
PWMTC will be continously incrementing whenever it reaches the PWMMRO, timer timer will be reset depeding on
PWMTCR configuraion. Once the Timer is reset the New Match values will be loaded from MR0-MR6 depending
on bits set in this register.
0- Disable the loading of new Match Values
1- Load the new Match values from MRx when the timer is reset.

Page 28
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

PWM Working

After looking into the PWM registers, its time to see how the LPC1768 PWM module works.

The TC is continuously incremented and once it matches the MR1(Duty Cycle) the PWM pin is pulled Low. TC
still continues to increment and once it reaches the Cycle time(Ton+Toff)
time(Ton+Toff) the PWM module does the following
things:

 Reset the TC value.


 Pull the PWM pin High.
 Loads the new Match register values.

Summary of PWM operations for the above image:

 Slide1: The TC is being incremented as per the Pre-scalar configuration.


configuration. The PWM output is high as the
TC is still less that duty cycle.
 Slide2: TC is increme
i ncremented
nted to 40 and still the PWM pin as HIGH.
 Slide3: TC is incremented to 60 and it matches the Duty Cycle(MR1=60).
 Slide4: Now the Comparator1(Green)
Comparator1(Green) will trigger
t rigger the R of SR latch and Pulls the PWM output to
Zero(Q=0). TC still continues to increment.
 Slide5: TC is incremented to 80 and PWM pin is low as TC > Duty Cycle.
 Slide6: Now TC is 100 and it matches the Cycle time(MR0==100).
time(MR0==100).
 Slide7: Now the Comparator2(Red)
Comparator2(Red) will trigger the S of SR latch and pulls the PWM output to
ONE(Q==1). It also resets the TC to zero. It updates Shadow buffers with new Match values from
MRO,MR1.

Steps to Configure PWM

1. Configure the GPIO pins for PWM operation in respective PINSELPINSEL register.
2. Configure TCR to enable the Counter for incrementing the TC, and Enable the PWM block.
3. Set the required pre-scalar value
value in PR. In our case it will be zero.
4. Configure MCR to reset the TC whenever it matches MR0.
5. Update the Cycle time in MR0. In our case it will be 100.
6. Load the Duty cycles for required PWMx channels in respective match registers MRx(x: 1-6).
7. Enable the bits in LER register to load and latch the
t he new match values.
8. Enable the required pwm channels in PCR register.

//Program
//Progr am PWM Demo on Scope // Put Scope in DC Mode

Page 29
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

5. Usage of Internal PWM to generate PWM and vary its duty cycle

#include <lpc17xx.h>
int main(void)
{
int j,DutyCycle;
LPC_PINCON->PINSEL
LPC_PINCON->PINSEL77 = 3 << 20; //3 in binary left shift by 20
20 times. Configure
Configure pin P3.26 for PWM mode.
LPC_PWM1->TCR = (1<< 0) | (1<< 2); //enable counter and PWM
LPC_PWM1->MCR
LPC_PWM1->MCR = (1<< 1); /*Reset on PWMMR0, reset TC if it matches MR0 */
LPC_PWM1->MR0
LPC_PWM1->MR0 = 100; /* set PWM cycle(Ton+Toff)=100)
cycle(Ton+Toff)=100) */
LPC_PWM1->PCR
LPC_PWM1->PCR = 1 << 11; /* Enable the PWM output pin for PWM_3 */

while(1)
{
for(DutyCycle=0;
for(DutyCycle=0; DutyCycle < 100; DutyCycle++)
{
LPC_PWM1->MR3
LPC_PWM1->MR3 = DutyCycle; /* Increase the DutyCycle from 0-100 */
for(j=0;j<1000000;j++);
}
}
}

LPC1768 DAC Programming Tutorial

Page 30
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67
LPC1768/LPC1769 DAC Block

ARM Cortex-M3 LPC176x MCUs incorporate a 10 bit DAC and provide buffered analog output. As per the datasheet, it is implemented as a
string DAC which is the most simplest form of DAC consisting of 2  N resistors in series where N = no. of bits which simply forms a Kelvin-
Varley Divider. LPC176x DAC has only 1 output pin, referred to as AOUT. The Analog voltage at the output of this pin is given as:

VAOUT = VALUE * (VREFP-VREFN)/ 1024

When we have V REFN = 0, the equation boils


boils down to:

VAOUT = ( VALUE * V REFP)/1024

Where VALUE is the 10-bit digital value which is to be converted into its Analog counterpart and V REF is the input reference voltage.

Pi ns rela
relating
ting to L PC
PC17
1768
68 D A C blo
lock:
ck:
Pin Description

AOUT Analog Output pin. Provides the converted Analog signal which is referenced to VSSA i.e. the
(P0.26) Analog GND. Set Bits[21:20] in PINSEL1 register to [10] to enable this function.

These are reference voltage input pins used for both ADC and DAC. VREFP is positive
VREFP, VREFN reference voltage and V REFN is negative reference voltage pin. In example shown below we
will use VREFN=0V(GND).

VDDA is Analog Power pin and VSSA is Ground pin used to power the ADC module. These
These are
VDDA, VSSA
generally same as V CC and GND but with additional filtering to reduce noise.

DAC Registers in ARM Cortex-M3 LPC176x

The DAC module in ARM LPC1768/LPC1769 has 3 registers viz. DACR, DACCTRL, DACCNTVAL. In this
tutorial we will only go through to DACR register since the other two are related with DMA operation, explaining
which is not in the scope of this tutorial. Just note that DMA is used to update new values to DACR from memory
without the intervention of CPU. This is particularly useful when generation different types of waveforms using
DAC. We will cover this in another tutorial.

Also note that the DAC doesn‟t have a power control it in PCONP register. Simply select the AOUT alternate
function for pin P0.26 using PINSEL1 register to enable DAC ouput.

The D AC R regist
registe
er in LPC
L PC17
1768
68

The field containing bits [15:6] is used to feed a digital value which needs to be converted and bit 16 is used to
select settling time. The bit significance is as shown below:

1. Bit[5:0]: Reserved.
2. Bit[15:6] –  VALUE starts from bit 6 [ In programming we use Value <<6; ]
Bit[15:6] –  VALUE
3. Bit[16] –  BIAS: Setting this bit to 0 selects settling time of 1us max with max current consumption
Bit[16] –  BIAS: consumption of
700uA at max 1Mhz update rate. Setting it to 1 will select settling time of 2.5us but with reduce max
max
current consumption of 300uA at max 400Khz update rate.
4. Bits[31:17]: Reserved

Page 31
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

LPC1768

Analog out to CRO


DAC out

The total voltage provided by the DAC OUT pin is function of binary number written on to bits 6 (D0) to bit 15(D9) of DAC
Register(DACR) and the reference voltage Vref(3.3V),it is as follows

Vout =Vref  [
9

2
+
  8

4
+
7

8
+
6

16
+
  5

32
+
  4

64
+
3

128
+
2

256
+
1

512
+
0

1024
]
Where D0 is the LSB and D9 is MSB

6a.Interface a DAC and generate Triangular and Square waveforms.

#include <LPC17xx.H
int main (void)
{
int i, m;

LPC_PINCON->PINSE
LPC_PINCON->PINSEL1
L1 = (1<< 21); // make PINSEL1.21
PINSEL1.21 pin high to Select AOUT function
function for P0.26

while(1)
{
for (i = 0; i <= 1023; i++) //send value start from 0 to 1023 insteps of 1
{
LPC_DAC->DACR
LPC_DAC->DACR = (i << 6); // DACR register value should placed in 15:6
for(m = 10; m > 1; m--); //delay to vary freq
}
For (i = 1023 ; i > 0; i--) //once reaches max of 3.3v starts decreasing
{
LPC_DAC->DACR = (i << 6);
for(m = 10; m > 1; m--);
}
}

Page 32
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

6.b. Interface a DAC and generate Square waveforms.

#include <LPC17xx.H>

int main (void)


{
int i;

LPC_PINCON->PINSE
LPC_PINCON->PINSEL1
L1 = (1<<21) ; //Select AOUT function for P0.26

while (1)
{
LPC_DAC -> DACR = (1024 << 6); // 10bit = 1024 Vmax = 3.3 V
for (i = 120000; i > 1; i--); // maintain this value for some delay

LPC_DAC -> DACR = (512 << 6); // for Vmax/2 = 1.74V


for (i = 120000; i > 1; i--); // delay
}
}

STEPPPER MOTOR INTERFACE

 Step
 Stepper Moto
Motor Confi
Configur
gura
ation:
ion:

A  ste
 stepper motor   is a special type of electric motor that moves in increments, or steps, rather than turning
smoothly as a conventional motor does. Typical increments are 0.9 or 1.8 degrees, with 400 or 200 increments
thus representing a full circle. The speed of the motor is determined by the time delay between each incremental
movement.

U8 is a Driver Buffer (ULN2003) device connected to LPC-1768 Device and can be used for driving Stepper
 Motor. On
 Motor. On the LPC-1768, P1.22 to P1.25 is used to generate the pulse sequence required to run the stepper Motor.
Also, the Stepper Motor is powered by its own power supply pin (COM), which is connected to a 12V supply.
Table shows connections for stepper Motor.

Stepper Motor Pin connection table.

Page 33
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Stepper LPC-1768 LPC-1768


Motor Coil Pin No Port No

A 39 P1.22
B 37 P1.23
C 38 P1.24
D 39 P1.25

P1.25 P1.24 P1.23 P1.22 Stepper motor


1 0 0 0 rotates Clockwise
0 1 0 0
0 0 1 0
0 0 0 1

P1.25 P1.24 P1.23 P1.22 Stepper motor rotates


0 0 0 1 Anti Clockwise

0 0 1 0
0 1 0 0
1 0 0 0

7.a. Interface a Stepper


Stepper motor and rotate it in clockwise direction.

#include <LPC17xx.H>

int main (void)


{
unsigned int i, k, z;
z;
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR = (1<< 22)|(1<< 23)|(1<< 24)|(1<< 25); // P1.22 to P1.25 CONFIG AS O/P

while (1)
{
k = 0x02000000; //initially P.25 =1 [ 1000 ]
for(z=0; z<4; z++) // four coils to be made high one by one
{
LPC_GPIO1->FIOPIN
LPC_GPIO1->FIOPIN =k; //Port1 pins assigned with Ka value
for ( i=120000; i >0 ; i-- ) ; // Delay , value of i decides speed of rotation.
k = k>>1; // k content rotate right 1 time. [0100] [0010] [0001]
}
}
}

Page 34
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

7.b. Interface a Stepper motor


motor and rotate it in anti-clockwise
anti-clockwise direction.

#include <LPC17xx.H>

int main (void)


{
unsigned int i, k, z;
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR = 0x03C00000; // alternate way. To make P1.22 to P1.25 config. as o/p,

while (1)
{
k = 0x00400000; // initialize p1.22 as high [0001]
for(z=0; z<4; z++) // executes 4 times
{
LPC_GPIO1->FIOPIN
LPC_GPIO1->FIOPIN =k; //port 1 pins assigned with k value
for (i=120000 ; i >0 ; i--) ; //delay
k = k<<1; // rotate left shift by 1 [0010] , [0100], [1000]
}
}
}

8.Interface and Control a DC Motor.


a. Anti Clock wise Direction

#include <lpc17xx.h>
int main()
{
LPC_GPIO2->FIODIR
LPC_GPIO2->FIOD IR = (1<<8); //Configure
//Confi gure the PORT2 pins as OUTPUT which enable DC motor
LPC_GPIO4->FIODIR
LPC_GPIO4->FIODIR = (1<<28) | (1<<29); //configure port4 pins p4.28,p4.29 as output by making 1

while(1)
{
LPC_GPIO2->FIOSET =(1<<8);
LPC_GPIO2->FIOSET=(1<<8); //enable DC motor by making P2.8=1
LPC_GPIO4->FIOSET
LPC_GPIO4->FIOSET = (1<<28); // P4.28 =1
LPC_GPIO4->FIOCLR
LPC_GPIO4->FIOCLR = (1<<29); // P4.29 = 0 DC motor Anti Clockwise rotation
}
}

b. Clockwise Direction
#include <lpc17xx.h>
int main()
{
LPC_GPIO2->FIODIR
LPC_GPIO2->FIOD IR = (1<<8); //Configure
//Confi gure the PORT2 pins as OUTPUT
LPC_GPIO4->FIODIR
LPC_GPIO4->FIODIR = (1<<28) | (1<<29); //configure port4 pins p4.28,p4.29 as Input

while(1)
{
LPC_GPIO2->FIOSET = (1<<8); //enable DC motor by making P2.8=1
LPC_GPIO4->FIOCLR
LPC_GPIO4->FIOCLR = (1<<28); // P4.28 = 0
LPC_GPIO4->FIOSET
LPC_GPIO4->FIOSET = (1<<29); // P4.29 =1 clockwise rotation
}
}

Page 35
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

c. Control DC
DC motor using switches.
switches. Sw 1.21- clockwise, 1.20 – 
1.20  –  Anticlockwise
 Anticlockwise , 1.19- Stop rotation
#include <lpc17xx.h>
int main()
{
LPC_GPIO2->FIODIR
LPC_GPIO2->FIOD IR = (1<<8); //Configure
//Confi gure the PORT2 pins as OUTPUT
LPC_GPIO4->FIODIR
LPC_GPIO4->FIODIR = (1<<28) | (1<<29); //configure port4 pins p4.28,p4.29 as Input
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR= = 00; //config all the pins of Port1 as i/p
while(1)
{
LPC_GPIO2->FIOSET = (1<<8); //enable DC motor by making P2.8=1
if (! ( LPC_GPIO1->
LPC_GPIO1-> FIOPIN & (1<<21) ) )
{
LPC_GPIO4->FIOSET = (1<<28); // P4.28 =1
LPC_GPIO4->FIOCLR
LPC_GPIO4->FIOCLR = (1<<29); // P4.29 = 0 DC motor Clockwise rotation
}
if (! ( LPC_GPIO1->
LPC_GPIO1-> FIOPIN & (1<<20) ) )
{
LPC_GPIO4->FIOCLR
LPC_GPIO4->FIOCLR = (1<<28); // P4.28 = 0
LPC_GPIO4->FIOSET
LPC_GPIO4->FIOSET = (1<<29); // P4.29 =1 Anti-clockwise
Anti-clockwise rotation
}
if (! ( LPC_GPIO1->
LPC_GPIO1-> FIOPIN & (1<<19) ) )
{
LPC_GPIO4->FIOSET = (1<<28); // P4.28 =1
LPC_GPIO4->FIOSET
LPC_GPIO4->FIOSET = (1<<29); // P4.29 = 1 DC motor Stops rotating.
}
}
}

D C Mo
M otor Pi
P i n Confi
Configur
gura
ati on

DC (direct current) motor rotates continuously. It has two terminals positive and negative. Connecting DC
 power supply to these terminals rotates motor in one direction and reversing the polarity of the power
supply reverses the direction of rotation.

U9 is L293 driver IC used to drive the dc motor. It has enable lines which is used to switch on the DC
motor. It is connected to P4.28. Logic „1‟ enables the driver and logic „0‟ disables the driver.P4.28 and
P4.29 are used for Motor 1 direction and speed control.

DC Motor Pin connection table.

Motor Motor LPC-1768 LPC-1768


Selection Direction Pin No Port No

Clockwise 82 P4.28=1 & P4.29=0


DCM0
Anti clock 85 P4.28=0 & P4.29=1
DCM1
65 P2.8=1
DCM_EN

We can‟t drive a dc motor directly


di rectly through a microcontroller pin. It usually operates in 3.3v-5v and lower currents
 but DC motor normally works
works in 6-12v, 300mA and it has other drawbacks
drawbacks like the back EMF produced
produced by the DC
motor may harm the controller ports and damage them. The solution to the above problems is using motor driving
circuit usually known as H-Bridges. They are basically built using FETs and many dedicated ICs are also available
like L293D etc.

Page 36
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

These are dual H-bridge motor drivers, i.e., by using one IC we can control two DC motors in both clock wise and
counter clockwise directions.
directions. The L293D can provide bidirectional drive currents of up to 60 0mA at voltages from
4.5 V to 36 V.

P4.28 P4.29 EN DC motor Action


0/1 0/1 0 Stop. Not enabled
1 0 1 Rotates Anticlockwise
0 1 1 Rotates Clockwise
1 1 1 Stop
0 0 1 Stop

9. Display the Hex digits 0 to F on a 7-segment


7- segment LED interface, with an appropriate delay in between.

#include <LPC17xx.H>
unsigned char dat7seg[] = {0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0x
{0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x8,0x9,0xa
cb,0x8,0x9,0xa,0x38,0x9C,0x68,
,0x38,0x9C,0x68,
0x1c,0x1e};
int main (void)
{
unsigned int i, j;
unsigned int count=0; // initially count=0;
LPC_GPIO2->FIODIR
LPC_GPIO2->FIODIR = 0x000000FF; // Seven seg on PORT2 p2.0 to p2.7 are output
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR = 0x3C000000; //Dig control on PORT1 p1.26 to p1.29 are output

while(1)
{
if (count > 0xF) count
count = 0; // if count Greater than F, re initialize to 0
for (i=0; i < 20000; i++) //change to inc/dec speed of count
{
LPC_GPIO2->FIOPIN
LPC_GPIO2->FIOPIN = dat7seg[count]; // & 0x000F];
LPC_GPIO1->FIOSET
LPC_GPIO1->FIOSET = (1<<26); // 26 for first Sev segment , 27 for second
for (j=0; j<1000; j++); //change to inc/dec brightness of display
LPC_GPIO1->FIOCLR = (1<<26);
}
Count++; //count = count+1
}
}

LE D Pin
Pi n Configura
Configurattion

Page 37
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

VTCM3_3board provides eight individual SMD led‟s connected to LPC-1768 LPC-1768 device through
74HC151driver IC. D1 to D8 are connected
connected to general purpose I/O pins on LPC-1768 device as shown
in table(14) When LPC-1768 device drives Logic „1‟ the corresponding LED turns on.

LED Pin connection table.

LED  D1 D2 D3 D4 D5 D6 D7 D8
LPC-1768
Pin No
LPC-1768
Port No

 SE V E N SE G M E NT DI
D I PL AY Pin
P in Config
Configura
urattion:
ion:

D11, D12, D13 and D14 are Common Cathode segments connected to LPC-1768 device so that each
segment is individually controlled by a general purpose I/O pin. When the LPC-1768 device drives logic
„0‟ the corresponding segment turns on. See fig and table for more
more details.

D9 D10 D11 D12

Seven Segment Pin connection table.

Seven Segment
 g f a b p c d e
Data Lines
LPC-1768 Pin
75 74 73 70 69 68 67 66
No
LPC-1768 Port
P2.0 P2.1 P2.2 P2.3 P2.4 P2.5 P2.6 P2.7
 No

 SE V E N SE G M E NT Sele
Select
ction
ion Pi n Confi
Configurat
guratii ons:
ns:

As VTCM3_3 board comes with 4 digit seven segment unit. Displays connected to the microcontroller
usually occupy a large number of valuable I/O pins, which can be a big problem especially if it i s needed to
display multi digit numbers. The problem is more than obvious if, for example, it is needed to display four
digit numbers (a simple calculation shows that 32 output pins are needed in this case). The solution to this
 problem is called MULTIPLEXING.
MULTIPLEXING. This is how an optical illusion based on the same operating principle
as a film camera is made. Only one digit is active at a time, but they change their state so quickly making
impression that all digits of a number are simultaneously active. Each digit can made active using
switching transistors Q1, Q2, Q3 and Q4 and these on switched on and off by selection lines which are in
turn connected to LPC-1768
to LPC-1768 ports.
 ports. Table shows
shows the details
details of seven segment
segment selection
selection lines.

Page 38
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

Seven Segment Disp1 Disp2 Disp3 Disp4


Selection Lines (D9) (D10) (D11) (D12)
LPC-1768 Pin No 40 43 44
44 45

LPC-1768 Port No P1.26 P1.27 P1.28 P1.29

Digits to Hex
Display Segments
display code

g f a B p c d e CODE
0 1 0 0 0 1 0 0 0 88
1 1 1 1 0 1 0 1 1 EB
2 0 1 0 0 1 1 0 0 4C
3 0 1 0 0 1 0 0 1 49
4 0 0 1 0 1 0 1 1 2B
5 0 0 0 1 1 0 0 1 19
6 0 0 0 1 1 0 0 0 18
7 1 1 0 0 1 0 1 1 CB
8 0 0 0 0 1 0 0 0 08
9 0 0 0 0 1 0 0 1 09
A 0 0 0 0 1 0 1 0 0A
B 0 0 1 1 1 0 0 0 38
C 1 0 0 1 1 0 0 0 98
Display Encoding:
D 0 1 1 0 1 0 0 0 68
E 0 0 0 1 1 1 0 0 1C
F 0 0 0 1 1 1 1 0 1E

Page 39
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

9.b.Interface of Seven segment module to Cortex M3 Controller – 


Controller  – 

ii. To display hexa digits 00 to FF with suitable


suitable delay

#include <LPC17xx.H> /* NXP LPC17xx definitions */

unsigned char dat7seg[] = {0x88,0xeb,0x4c,0x49,0x2b,0x19,0x


{0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x8,0x9,0
18,0xcb,0x8,0x9,0xa,0x38,0x9C,0x68,0x
xa,0x38,0x9C,0x68,0x1c,0x1e};
1c,0x1e};

int main (void)


{
unsigned int i,j;
unsigned int count=0;
LPC_GPIO2->FIODIR
LPC_GPIO2->FIODIR = 0x000000FF; // Seven seg on PORT2 p2.0 to p2.7 are output
LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR |= 0x3C000000; //Dig control on PORT1 p1.26 to p1.29 are output

while(1)
{
if (count > 0xF) count = 0;
for (i=0; i < 20000; i++) //change to inc/dec speed of count
{
LPC_GPIO2->FIOPIN = dat7seg[count];
dat7seg[cou nt]; // get data from array offset by count
LPC_GPIO1->FIOSET = (1<<26)|(1<<27);
(1<<26)|( 1<<27); //first and second 7 segment devices ON
for (j=0;j<1000;j++); //change to inc/dec brightness of display
LPC_GPIO1->FIOCLR = (1<<26) | (1<<27);
}
count++;
}
}

Page 40
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

16 X 2 LC
L C D D i spla
splayy
An LCD display is specifically manufactured to be used with microcontrollers, which means that it cannot
 be activated by standard IC circuits. It is used for displaying different messages
messages on a miniature liquid
crystal display. It displays all the letters of alphabet, Greek letters, punctuation marks, mathematical
symbols etc. In addition, it is possible to display symbols made up by the user. Other useful features
include automatic message shift (left and right), cursor appearance, LED backlight etc.

There are pins along one side of a small printed board. These are used for connecting to the
microcontroller. There are in total of 14 pins marked with numbers (16 if it has backlight). Their function is
described in the table below:

L CD Pi n Confi
Configur
gura
ati on
LPC- LPC-
LCD
LCD Pin 1768 1768
Pin
Functions Pin Port
No
No No
1 GND - -
2 VCC - -
3 - -
4 RS 24 P2.28
5 R/W GND GND
6 EN 27 P2.27
7 DAT1 75 P2.0
8 DAT2 74 P2.1
9 DAT3 73 P2.2
10 DAT4 70 P2.3
11 DAT5 69 P2.4
12 DAT6 68 P2.5
13 DAT7 67 P2.6
14 DAT8 66 P2.7
15 VCC - -
16 GND - -

Page 41
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

10.) Interface LCD with 1768 and write a Program to display Message on LCD screen.

#include <LPC17xx.h>
#define LCD_RS (1<<28)
#define LCD_EN (1<<27)

void LCD_Command(unsigned
LCD_Command(unsigned char cmd)
{
LPC_GPIO2->FIOPIN = cmd ;
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOCL R |= LCD_RS; // RS=0
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOS ET |= LCD_EN; // EN=1
for(int k=0;k<100000;k++);
k=0;k<100000;k++); // delay
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOCL R |= LCD_EN; //EN=0
}

void LCD_Data(unsigned char data)


{
LPC_GPIO2->FIOPIN = data;
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOS ET |= LCD_RS; // RS=1
LPC_GPIO0->FIOSET
LPC_GPIO0->FIOS ET |= LCD_EN; //EN=1
for(int j=0;j<100000;j++); // delay
LPC_GPIO0->FIOCLR
LPC_GPIO0->FIOCL R |= LCD_EN; // EN=0
}

void LCD_Init(void)
{
LPC_GPIO0->FIODIR
LPC_GPIO0->FIOD IR |= (LCD_RS | LCD_EN); // EN and Read Select Pins are configured as o/p
LPC_GPIO2->FIODIR0
LPC_GPIO2->FIODIR0 = 0x00FF; // Config P2.0 to P2.7 as o/p pins
LCD_Command(0x38);
LCD_Command(0x38); // 8 bits, 2 lines, 5x7 Dots
LCD_Command(0x01);
LCD_Command(0x01); // Clear Display screen
LCD_Command(0x0e);
LCD_Command(0x0e); // Display on & cursor On
}

int main()
{
unsigned char str[]={"Acharya
str[]={"Acharya "};
unsigned int x=0; // initialize x=0
LCD_Init();
LCD_Command(0x80);
LCD_Command(0x80); // Force cursor to beginging of I row, [ 0xc0 for II row]
while(str[x] != '\0') // if content of str in not a null char
{
LCD_Data(str[x]);
LCD_Data(str[x]); // send to LCD as Data

x++; //increment x
}
}

Page 42
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

10 .a) Interface a 4x4 keyboard


keyboard and display
display the key code on an LCD.
LCD.

#include <LPC17xx.H>
#include "lcd.h"

void col_write( unsigned char data )


{
LPC_GPIO1->FIOSET |= (data << 14) & (1<<14 | 1<<15 | 1<<16 | 1<<17);
}

int main (void)


{
unsigned char key, i;
unsigned char rval[] = {0x7,0xB,0xD,0xE};
{0x7,0xB,0xD,0xE}; // to make one coloumn low 0111, 1011, 1101, 1110
unsigned char keyPadMatrix[
keyPadMatrix[ ] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
E','F'};

init_lcd();

LPC_GPIO1->FIODIR
LPC_GPIO1->FIODIR = (1<<14 | 1<<15 | 1<<16 | 1<<17); //Set COLs as Outputs

lcd_putstring16(0,"Press
lcd_putstring16(0,"Press HEX_KeyBoard..");
HEX_KeyBoard..");
lcd_putstring16(1,"Key
lcd_putstring16(1,"Key Pressed = ");

while (1)
{
key = 0;
for( i = 0; i < 4; i++ )
{
LPC_GPIO1->FIOCLR |= (1<<14 | 1<<15 | 1<<16 | 1<<17);
col_write(rval[i]);
col_write(rval[i]); // turn on COL output one by one
if (!(LPC_GPIO1->FIOPIN
(!(LPC_GPIO1->FIOPIN & 1<<21)) // read rows - break when key press detected
 break;
key++;
if (!(LPC_GPIO1->FIOPIN
(!(LPC_GPIO1->FIOPIN & 1<<20))
 break;
key++;
if (!(LPC_GPIO1->FIOPIN
(!(LPC_GPIO1->FIOPIN & 1<<19))
 break;
key++;
if (!(LPC_GPI
( !(LPC_GPIO1->FIOPIN
O1->FIOPIN & 1<<18))
 break;
key++;
}
if (key == 0x10) // if no key pressed key++ ncrements
ncrements upto 16 i.e 0x10
lcd_putstring16(1,"Key
lcd_putstring16(1,"Key Pressed = ");
else
{
lcd_gotoxy(1,14);
lcd_putchar(keyPadMatrix[key]);
}
}
}

Page 43
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

HEX KEY PAD Pin configurations:

The hex keypad is a peripheral that is organized in rows and Columns. Hex key Pad 16 Keys arranged in a 4 by 4
grid, labeled with the hexadecimal digits 0 to F. An example of this can been seen in Figure 1, below. Internally,
the structure of the hex keypad is very simple. Wires run in vertical columns (we call them C0 to C3) and in
horizontal rows (called R0 to R3). These 8 wires are available externally, and will be connected to the lower 8 bits
of the port. Each key on the keypad is essentially a switch that connects a row wire to a column wire. When a key is
 pressed, it makes an electrical connection between the row and column. Table shows connections for HEX KEY
Pad matrix.

HEX KEY Pad matrix table.

ROW/COLOUMNS R1 R2 R3 R4 C1 C2 C3 C4
LPC-1768 Pin No 32 33 34 35 89 88 87 86
LPC-1768 Port No P1.18 P1.19 P1.20 P1.21 P1.14 P1.15 P1.16 P1.17

Page 44
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

11. Determine Digital


Digital output for a given Analog input using
using Internal ADC of ARM controller

//Program display Internal ADC value on 16x2 lcd

#include <LPC17xx.H> /* NXP LPC17xx definitions */


#include <stdio.h>
#include "lcd.h"

#define ADC_DONE
ADC_DONE 0x80000000
#define ADC_OVERRUN
ADC_OVERRUN 0x40000000

#define ADC_CLK
ADC_CLK 1000000 /* set to 1Mhz */

volatile int ADCValue;


ADCValue;

void ADCInit( int Clk )


{
LPC_SC->PCONP
LPC_SC->PCONP |= (1 << 12); /* Enable CLOCK into ADC controller */

LPC_PINCON->PINSEL1
LPC_PINCON->PINSEL 1 |= 0x00004000;
0x00004 000; // config P0.23 work as ADO[0] or make 1<<14;
1<< 14;

LPC_ADC->ADCR
LPC_ADC->ADCR = ( 1 << 0 ) | /* select channel ; Here 0th bit is 1 ie channel 0 is selected*/
(14 << 8 ) | /* CLKDIV = Fpclk / ADC_Clk - 1 * CLKDIV starts from 8 bit
( 1 << 21 ) ; /* PDN = 1, normal operation */
return ;
}

int ADCRead( )
{
int regVal, ADC_Data;
LPC_ADC->ADCR &= 0xFFFFFF00;
0xFFFFFF00 ; // clear data
LPC_ADC->ADCR
LPC_ADC->ADCR |= (1 << 24) | 1; /* switch channel,start
channel,start A/D convert */

while ( 1 ) /* wait until end of A/D convert */


{
regVal = LPC_ADC->ADDR0;
LPC_ADC->ADDR0; //Contains the recent ADC value for respective channel
if ( regVal & ADC_DONE
ADC_DONE ) break; /* read result of A/D conversion */
}

LPC_ADC->ADCR &= 0xF8FFFFFF; /* stop ADC now */

if ( regVal & ADC_OVERRUN )


return ( 0 ); /* save data when it's not overrun, otherwise, return zero */
ADC_Data = ( regVal >> 4 ) & 0xFFF; //read data in 4:15 so shift by 4 times to bring to 0:7
return ( ADC_Data );
/* return A/D conversion value */
}

int main (void)


{
char lstr[10];
double volt;

ADCInit( ADC_CLK );
init_lcd();
lcd_putstring16(0,"ADC
lcd_putstring16(0,"ADC Val
Val = 000 ");
lcd_putstring16(1,"VOLTAGE
lcd_putstring16(1,"VOLTAGE = 0.00V ");

while(1)

Page 45
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

{
ADCValue = ADCRead(); // read ADC value
sprintf (lstr, "%4u", ADCValue);
ADCValue);
lcd_gotoxy(0,10); //goto 1st line, 10 char place
lcd_putstring(lstr); //print

volt = ADCValue * 3.3/4096; //(ADCValue * 3.3) / 4096) ;


sprintf (lstr, "%0.2f", volt);
lcd_gotoxy(1,10); //goto 2nd line 10th char place
lcd_putstring(lstr); //print
delay(500);
}
}

Page 46
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

LPC1768 has an inbuilt 12 bit Successive Approximation ADC which is multiplexed among 8 input pins.
The ADC reference voltage is measured across VREFN to VREFP, meaning it can do the conversion within this
range. Usually the VREFP is connected to VDD and VREFN is connected to GND.
As LPC1768 works on 3.3 volts, this will be the ADC reference voltage.
12
 Now the resolution of ADC = 3.3/ 2 = 3.3/4096 =0.000805 = 0.8mV

The ADC pin can be enabled by configuring the corresponding PINSEL register to select ADC function.
When the ADC function is selected for that pin in the Pin Select register, other Digital signals are disconnected
from the ADC input pins.

Adc Port Associated PINSEL


Pin Functions
Channel Pin Register

0-GPIO, 1-AD0[0] , 2-I2SRX_CLK,


AD0 P0.23 14,15 bits of PINSEL1
PINSEL1
3-CAP3[0]

0-GPIO, 1-AD0[1], 2-I2SRX_WS,


2-I2SRX_WS,
AD1 P0.24 16,17 bits of PINSEL1
PINSEL1
3-CAP3[1]

0-GPIO, 1-AD0[2] , 2-I2SRX_SDA,


AD2 P0.25 18,19 bits of PINSEL1
PINSEL1
3-TXD3

0-GPIO, 1-AD0[3] , 2-AOUT, 3-


AD3 P0.26 20,21 bits of PINSEL1
PINSEL1
RXD3

AD4 P1.30 0-GPIO, 1-VBUS, 2- , 3-AD0[4] 28,29 bits of PINSEL3


PINSEL3

AD5 P1.31 0-GPIO, 1-SCK1, 2- , 3-AD0[5] 30,31 bits of PINSEL3


PINSEL3

AD6 P0.3 0-GPIO, 1-RXD0, 2-AD0[6], 3- 6,7 bits of PINSEL0


PINSEL0

AD7 P0.2 0-GPIO, 1-TXD0, 2-AD0[7], 3- 4,5 bits of PINSEL0


PINSEL0

ADC Registers

The below table shows the registers associated with LPC1768 ADC.
We are going to focus
f ocus only on ADCR and ADGDR as these are sufficient for simple A/D conversion.

Register Description

ADCR A/D COntrol


COntrol Register:
Register: Used for Configuring
Configuring the ADC

ADGDR A/D Global Data Register: This register contains the ADC‟s DONE bit and the result of the

Page 47
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

most recent A/D conversion

ADINTEN A/D Interrupt Enable Register

ADDR0 - ADDR7 A/D Channel Data Register: Contains the recent ADC value for
f or respective channel

ADSTAT A/D Status Register: Contains DONE & OVERRUN flag for all the ADC channels

ADC Register Configuration

 Now lets see how to configure the individual registers


registers for ADC conversion.
conversion.

ADCR A/D COntrol


COntrol Register:
Register: Used for Configuring
Configuring the ADC

31:28 27 26:24 23:22 21 20:17 16 15:8 7:0

Reserved EDGE START Reserved PDN


PDN Reserved BURST CLCKDIV SEL

Bit 7:0 – 
7:0 –  SEL
 SEL : Channel Select
These bits are used to select a particular channel for ADC conversion. One bit is allotted for each channel. Setting
the Bit-0 will make the ADC to sample AD0[0] for conversion. Similary setting bit-7 will do the conversion for
AD0[7].

Bit 15:8 – 
15:8 –  CLCKDIV
 CLCKDIV : Clock Divisor
The APB clock (PCLK_ADC0) is divided by (this value plus one) to produce the clock for the A/D converter,
which should be less than or equal to 13 MHz.

Bit 16 – 
16 –  BURST
 BURST
This bit is used for BURST conversion. If this bit is set the ADC module will do the conversion for all the channels
that are selected(SET) in SEL bits.
CLearing this bit will disable the BURST conversion.

Bit 21 – 
21 –  PDN
 PDN : Power Down Mode
Setting this bit brings ADC out of power down mode and makes
makes it operational.
Clearing this bit will power down the ADC.

Bit 24:26 – 
24:26 –  START
 START
When the BURST bit is 0, these bits control whether and when an A/D conversion is started:
000 - Conversion Stopped
001- Start Conversion Now

The remaining cases (010 to 111) are about starting conversion on occurrence of edge on a particular CAP or MAT
 pin.

Bit 27 - EDGE
This bit is significant only when the START field contains 010-111. It starts conversion on selected CAP or MAT
input.
0 - On Falling Edge

Page 48
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

1 - On Rising Edge

ADGDR ( ADC Global Data Register )


ADGDR A/D Global Data Register: This register contains the ADC‟s DONE bit and the result
of the most recent A/D conversion

31 27 26:24 23:16 15:4 3:0

DONE OVERRUN CHN Reserved RESULT Reserved

Bit 15:4 - RESULT


This field contains the 12bit A/D conversion value for the selected channel  in ADCR.SEL
The vale for this register should be read oncve the conversion is completed ie DONE bit is set.

Bit 26:24 - CHN : Channel


These bits contain the channel number for which the A/D conversion is done and the converted value is available in
RESULT bits(e.g. 000 identifies channel 0, 011 channel 3...).

Bit 27 - OVERRUN
This bit is set during the BURST mode where the previous conversion data is overwritten by the new A/D
conversion value.

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
ADCR is written. If the ADCR is written while a conversion is still in progress, this bit is set and a new conversion
is started.

Temperature Sensor

The LM35 series are precision integrated-circuit temperature


temperature sensors, whose output voltage is linearly
li nearly proportional
to the Celsius (Centigrade) temperature. The output of sensor converted to digital that easy connecting with
microcontroller.

12. Measurement of ambient temperature using a sensor and SPI ADC IC.

#include <LPC17xx.H>
#include <stdio.h>
#include "lcd.h"
#include "spi.h"

int main (void)


{
int msb,lsb;
int ADCValue;
ADCValue;
float temp;
char lstr[10];

SPI_Init();
init_lcd();

lcd_putstring16(0,"TEMP
lcd_putstring16(0,"TEMP = 000 Deg_C");

Page 49
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

while(1)
{
SPI_EnableChipSelect();
delay(1);
SPI_Write(0x01);
msb = SPI_Write(0xA0);
SPI_Write(0xA0); // SGL=1, ODD=0, MSBF=1
lsb = SPI_Write(0x00);
SPI_Write(0x00);

SPI_DisableChipSelect();
msb &= 0x0F;
ADCValue = (msb << 8) | lsb;

temp = ((ADCValue * 3.3) / 4096) * 100;


sprintf (lstr, "%0.1f", temp);
lcd_gotoxy(0,7);
lcd_putstring(lstr);

delay(500);
}
}

Spi.c

#include <lpc17xx.h>
#include "spi.h"

uint8_t dummy_u8;
static uint32_t spi_GetPclk(void);
void SPI_Init(void)
{
LPC_PINCON->PINSEL0 |= 0xC0000000;
0xC0000 000; //SCK
LPC_PINCON->PINSEL1 |= 0x0000003C;
0x00000 03C; //SSEL, MISO, MOSI

LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR |= 0x00058000; //SSEL, MOSI, SCK are Outputs 15,16,18
LPC_GPIO0->FIODIR
LPC_GPIO0->FIODIR &= ~0x00020000; //MISO is Input 17

SPI_DisableChipSelect();
SPI_DisableChipSelect(); // Disable the Slave Select

LPC_SC->PCONP
LPC_SC->PCONP |= (1 << 8); // enable power to spi clock

LPC_SPI->SPCCR
LPC_SPI->SPCCR = spi_GetPclk()/SCK_Freq;
spi_GetPclk()/SCK_Freq; // Set Spi Clock

LPC_SPI->SPCR = ((0<<SBIT_CPHA) | (0<<SBIT_CPOL) | (1<<SBIT_MSTR));

dummy_u8 = LPC_SPI->SPSR;
LPC_SPI->SPSR ; /* Dummy read to clear the flags */
dummy_u8 = LPC_SPI->SPDR; /* Dummy read to clear the flags */
}

uint8_t SPI_Write (uint8_t spiData_u8)


{
LPC_SPI->SPDR = spiData_u8;
dummy_u8 = 0;
while(dummy_u8
while(dummy_u8 == 0)
{
dummy_u8
dummy_u8 = LPC_SPI->SPSR
LPC_SPI->SPSR & 0x80; } // wait until data is sent
dummy_u8 = LPC_SPI->SPSR;
spiData_u8 = (uint8_t)LPC_SPI->SPDR;
(uint8_t)LPC_SPI->SPDR;
return spiData_u8;
}

Page 50
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

static uint32_t spi_GetPclk(void)


spi_GetPclk(void)
{
uint32_t v_spiPclk_u32,v_Pclk_u32;
v_spiPclk_u32,v_Pclk_u32;
v_spiPclk_u32 = (LPC_SC->PCLKSEL0 >> 16) & 0x03;
switch( v_spiPclk_u32 )
{
case 0x00:
v_Pclk_u32 = SystemCoreClock/4;
SystemCoreClock/4; //SystemFrequency
//SystemFrequency or SystemCoreClock
SystemCoreClock
 break;
case 0x01:
v_Pclk_u32 = SystemCoreClock;
SystemCoreClock;
 break;
case 0x02:
v_Pclk_u32 = SystemCoreClock/2;
SystemCoreClock/2;
 break;
case 0x03:
v_Pclk_u32 = SystemCoreClock/8;
SystemCoreClock/8;
 break;
}
return (v_Pclk_u32);
}

Vref =3.3V
LM35

1 2 3

5V

ADC1
75E

LPC1768
1µf

Page 51
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

 ARM Cortex M3 Series: An Introduction


Brief History
 ARM was founded in 1990 as Advanced RISC Machines Ltd., a joint venture of Apple Computer, Acorn
Computer Group, and VLSI Technology. In 1991, ARM introduced the ARM6 processor family, and VLSI
became the initial licensee. Subsequently, additional companies, including Texas Instruments, NEC, Sharp
and ST Microelectronics, licensed the ARM processor designs. Nowadays ARM partners ship in excess of 2
billion ARM processors each year. Unlike many semiconductor companies, ARM does not manufacture
processors or sell the chips directly. Instead it licenses the processor designs to business partners. This
business model is commonly called intellectual property (IP) licensing.
 Architecture Versions
Over the years, ARM has continued to develop new processors and system blocks. These include the popular
 ARM7TDMI processors, more recently the ARM11 processor family and latest being the ARM Cortex
Processor family. Table 2.1 summarizes the various architecture versions and proc essor families.
 Architecture Processor Family
 ARMv1 ARM1
 ARMv2 ARM2, ARM3
 ARMv3 ARM6, ARM7
 ARMv4 StrongARM, ARM7TDMI,
ARM7TDMI, ARM9TDMI
 ARMv5 ARM7EJ, ARM9E, ARM10E, XScale

Page 52
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

 ARMv6 ARM11, ARM Cortex – 


Cortex – M
 ARMv7 ARM Cortex-A, ARM Cortex-R, ARM
ARM Cortex-M

 ARM Cortex Processor Profiles


In the ARMv7 architecture the design is divided into three profiles  – 
1.  A Profile (ARMv7-A) - is designed for high performance open application platforms. They can handle
complex applications such as high end embedded operating systems.
Example products include high-end smartphones, tablet PCs and PDAs.

2. R Profile (ARMv7-R) - is designed for high end end embedded systems in which
which real time performance is
needed. Example applications include high-end car braking systems.
3. M-Profile 
M-Profile  (ARMv7-M)  –   is designed for deeply embedded microcontroller type systems. Processors
belonging to this profile are the subject for this manual and are studied in greater detail.

Evolution of the ARM Instruction Set


Enhancement and extension of instruction sets used by the ARM processors has been one of the key driving
forces of the architecture’s evolution. Historically, two different instruction sets were supported on the ARM
processor: the ARM instructions, which are 32 bits and Thumb instructions, which are 16 bits. During
program execution, the processor can be dynamically switched between the ARM state and the Thumb state.
The Thumb instruction set provides only a subset of the ARM instructions, but it can provide higher code
density. It is useful for products with tight memory requirements.
The Thumb-2 technology extended the Thumb Instruction Set Architecture (ISA) into a highly efficient and
powerful instruction set that delivers significant benefits in terms of ease of use, code size and performance.
The extended instruction set in Thumb-2 is a superset of the previous 16-bit Thumb instruction set, with
additional 16-bit instructions alongside 32-bit instructions. It allows more complex operations to be carried
out in the Thumb state, thus allowing higher efficiency by reducing the number of states switching between
 ARM state and Thumb state.
The Cortex-M3 processor supports only the Thumb-2 (and traditional Thumb) instruction set. Instead
of using ARM instructions, as in traditional ARM processors, it uses Thumb-2 instruction set for all
operations. As a result, the Cortex-M3 processor is not backward compatible with traditional ARM processors,
which use the ARM as well as Thumb instruction set.
The Thumb-2 instruction set is a very important feature of the ARMv7 architecture. For the first time,
hardware divide instruction is available on an ARM processor, and a number of multiply instructions are also
available.
Salient Features of the Cortex-M3
• 32-bit
32-bit microprocessor.
• 32-bit
32-bit data path, 32-bit register bank and 32 -bit memory interfaces.
• Harvard Architecture –  separate
 separate instruction bus and data bus.
• 3-stage
3-stage pipeline with branch speculation.
• Thumb-2
Thumb-2 instruction set.
• No switching between ARM state and thumb state. st ate.
• Instruction fetches are 32 bits. Up to two instructions can be fetched in one cycle. As a result, there’s more
available bandwidth for data transfer.
• ALU with hardware divide and single cycle multiply.
• Configurable Nested Vector Interrupt Contro ller (NVIC).
• Maximum of 240 external interrupts can be configured.
• Low gate count, suitable for low power designs.
• Memory Protection Unit (MPU).
• Operation Mode Selection –  user
 user and privilege modes.
• Advanced debug components.

Registers

• The Cortex-M3
Cor tex-M3 has registers R0 through R15. R13 (the stack pointer) is banked, with only one copy of the
R13 visible at a time.
• R0  – R12:
 R12: General Purpose Registers.
Registers. These are 32 bit registers for data operations. Some 16-bit Thumb
instructions can only access a subset of these registers (low registers R0-R7).
• R13:
R13: Stack Pointers.
Pointers. Contains two stack pointers. They are banked so that only one is visible
visible at a time.
 Main Stack Pointer (MSP)  –   The main stack pointer used by the Operating system and
exception handlers.
 Process Stack Pointer (PSP) – 
(PSP)  –  used
 used by the application code.
• R14:
R14: Link Register.
Register. When a subroutine is called, the return address is stored in the link register.
• R15:
R15: Program Counter.
Counter. The program counter is the current program address.
• The Cortex-M3
Cortex -M3 also has a number of special registers. They are

Page 53
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in
Embedded Controller Lab Manual 15ECL67

• Program Status registers (PSR)


• Interrupt Mask registers (PRIMASK, FAULTMASK and BASEPRI).
• Control register (CONTROL)
• The Cortex-M3
Cortex -M3 has 18 registers in total compared to 37 registers for traditional ARM.
Memory Map
The Cortex-M3 has predefined memory maps, which allows built in peripherals, such as the interrupt
controller and debug components, to be accessed by simple memory access instructions. The predefined
memory map also allows the Cortex-M3 processor to be highly optimized for speed and ease of integration in
system-on-a-chip (SoC) designs.
The Cortex-M3 design has an internal bus infrastructure optimized for this memory usage. In addition, the
design allows these regions to be used differently. For example, data memory can still be put into the CODE
region, and program code can be executed from an external Random Access Memory (RAM) region. The
Cortex-M3 memory map is outlined in figure as shown.
Operation Modes
The Cortex-M3 processor has two modes of operation and two privilege levels. The operation modes (thread
mode and handler mode) determine whether the processor is running a normal program or running an
exception handler like an interrupt handler or system exception handler. The privilege levels provide a
mechanism for safeguarding memory accesses to critical regions as well as providing a basic security model.
When the processor is running a main program (thread mode), it can be either in a privileged state or a user
state, but exception handlers can only be in a privileged state. When the processor exits reset, it is in thread
mode with privileged access right. In this state, a program has access to all memory ranges and can use all
supported instructions. Software in the privileged access level can switch the program into the user access
level using the control register. When an exception takes place, the processor will always
switch back to the privileged state and return to the previous state when exiting the exception handler. A user
program cannot change back to the privileged state by writing to the control register . It has to go through an
exception handler that programs the control register to switch the processor back into the privileged access
level when returning to thread mode.

Nested Vector Interrupt Controller (NVIC)


The Cortex-M3 processor includes an interrupt controller called the Nested Vectored Interrupt Controller
(NVIC). It is closely coupled to the processor core and provides a number of features as follows:
 Nested interrupt support
  Vectored interrupt support
support
 Dynamic priority changes support
 Reduction of interrupt latency
 Interrupt masking
Nested Interrupt support - All the external interrupts and most of the system exceptions can be
programmed to different priority levels. When an interrupt occurs, the NVIC compares the priority of this
interrupt to the current running priority level. If the priority of the new interrupt handler is higher than the
current level, the interrupt handler of the new interrupt will override the current running task.
 Vectored Interrupt Support -
Support - When an interrupt is accepted, the starting address of the interrupt service
routine (ISR) is located from a vector table in memory.
Dynamic Priority Changes Support - Support - Priority levels of interrupts can be changed by software during run
time. Interrupts that are being serviced are blocked from further activation until the ISR is completed, so
their priority can be changed without risk of accidental reentry.
Reduction of Interrupt Latency -
Latency  - The Cortex-M3 processor also includes a number of advanced features
to lower the interrupt latency. These include automatic saving and restoring some register contents and
reducing delay in switching from one ISR to another.
Interrupt Masking 
Masking  - Interrupts and system exceptions can be masked based on their priority level or
masked completely using  the interrupt masking registers BASEPRI, PRIMASK, and FAULTMASK.

Page 54
Dept of ECE, Acharya Institute of Technology raghunath@acharya.ac.in

You might also like