View
8
Download
0
Category
Preview:
Citation preview
Electronic Engineering Laboratory IV
BEE31101
Instruction Sheet
Fa
cult
y o
f E
lect
ric
an
d E
lect
ron
ic E
ng
ine
eri
ng
Lab No. 4
Lab Title Introduction to ARM M3 Microcontroller (LPC1768)
Semester 02
Session 2019/20
Lab Durations 2 Hours
Independent Studies 1 Hour
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
ii
FKEE, Sem02 Session 2019/20
Table of Content
Table of Content ii
1.0 Outcomes 1
2.0 Guidelines 1
3.0 Pre-Lab (5%) 2
4.0 Procedures Error! Bookmark not defined.
Overview 3
Example 1 Error! Bookmark not defined.
Example 2 3
5.0 Lab Activities (40%)
Lab Activity 1 Error! Bookmark not defined.
Lab Activity 2 Error! Bookmark not defined.
5.0 Observations (15%) 12
6.0 Questions (15%) 12
7.0 References Error! Bookmark not defined.
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
1
FKEE, Sem02 Session 2019/20
1.0 Outcomes
After completing this module, student should be able to:
(1) Students will be able to write a program to configure the I/O pins based on the
microcontroller’s memory map.
(2) Students will be able to write a program for simple input/output operations using mbed
API
2.0 Instructions
1. Grouping: Lab group is not predetermine and consists with at most two team members.
2. Pre-Lab: Must be handwritten and submitted to the instructor at the beginning of lab session.
Verified by the instructor and returned to the students at the end of lab session. The verified
pre-lab will be attached with the final report for submission.
3. Lab Activities: All lab activities such as sample code, examples and lab assignments must
be held in the respective lab location and completed within the given times.
4. Demonstration: Student must demonstrate the successful sample code, examples and lab
assignments to the respective instructor. Verification only will be given upon completion of all
lab activities and initialized by the instructor on the cover page.
5. Report Organization: Report must be organized according to given report template.
6. Appendix: Handwritten source code with detail description of each command for lab
activities. Marks for lab activities are given based on attachment of the appendix. Printed
source code is required only as an attachment of lab activities.
7. Report Submission: Report must be received by respective technical staff (at respective
lab) before 4.00pm; not later than three (3) days upon completion of lab session.
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
2
FKEE, Sem02 Session 2019/20
3.0 Pre-Lab (5%)
1. Explain memory mapped I/O in microprocessor/microcontroller.
(2 marks)
2. The initialization section of a certain program reads:
FIO0DIR0=0xF0;
FIO2DIR2=0x02;
Explain the settings that have been made.
(3 marks)
3. Explain the Application Programming Interface (API) in embedded system development.
(2 marks)
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
3
FKEE, Sem02 Session 2019/20
4.0 Guidelines
Overview
Memory mapped I/O is the method of performing input and output between the CPU and
peripheral devices in a microprocessor system. It uses the same address space to address
both memory and I/O devices. The memory and registers of the I/O devices are mapped
to (associated with) address values. When an address is accessed by the CPU, it may
refer to a portion of physical RAM, or it can instead refer to memory of the I/O device.
Thus, the CPU instructions used to access the memory can also be used for accessing
devices. Figure 1.1 shows the memory map of the NXP LPC1768 microcontroller. For
example, the general purpose I/O (GPIO) is mapped to the memory addresses between
0x2009C000 to 0x2009FFFF. Meanwhile, the analog to digital converter (ADC) is mapped
to the memory addresses between to 0x40034000 to 0x40037FFF. In this lab, we will
configure the GPIO of the LPC1768 for a simple I/O operation.
Figure 1.1: Memory map of the LPC1768 microcontroller
General Purpose Input/Output (GPIO)
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
4
FKEE, Sem02 Session 2019/20
The LPC1768 has five 32-bit digital I/O ports (Port 0, 1, 2, 3 and 4). However, not all bits
are physically implemented (has physical connection to the I/O pin). For example, Port 0
has 32-bit I/O port, but Pins 12, 13, 14, and 31 of this port are not available. Figure 1.2
shows the I/O posts of the LPC1768 microcontroller. Note that, the grey bits indicate the
pins are not available.
Figure 1.2: The LPC1768 ports
It is possible to set each port pin as an input or as an output. Each port has a 32-bit
register that controls the direction of each of its pins. These are called the FIODIR
registers. To specify which port the register relates to, the port number is embedded within
the register name, for example, FIO0DIR is the direction register for Port 0. Each bit in this
register then controls the corresponding bit in the I/O port, for example bit 0 in the direction
register controls bit 0 in the port. If the bit in the direction register is set to 1, then that port
pin is configured as an output; if the bit is set to 0, the pin is configured as an input.
It is sometimes more convenient not to work with the full 32-bit direction register,
especially when we might just be thinking of one or two bits within the register. For this
reason, it is also possible to access any of the bytes within the larger register, as single-
byte registers (8-bit). These registers have a number code at their end. For example,
FIO2DIR0 is byte 0 of the Port 2 direction register. A second set of registers, called FIOPIN,
holds the data value of the microcontroller’s pins, whether they have been set as input or
output. If a port bit has been set as an output, then writing to its corresponding bit in its
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
5
FKEE, Sem02 Session 2019/20
FIOPIN register will control the logic value placed on that pin. If the pin has been set as
input, then reading from that bit will tell you the logic value asserted at the pin. Example in
Figure 1.3 shows how to configure the lower 4-bit of the Port 0, Byte 0 as the output while
the upper 4-bit as the input. Figure 1.4 shows the port and its associated pin on the
LPC1768 microcontroller.
Figure 1.3: Example of port configurations
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
6
FKEE, Sem02 Session 2019/20
Figure 1.4: Port and its associated pin on the LPC1768
Figure 1.5 and 1.6 show the addresses of the FIODIR and FIOPIN respectively.
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
7
FKEE, Sem02 Session 2019/20
Figure 1.5: Fast GPIO port Direction control byte and half-word accessible
register description
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
8
FKEE, Sem02 Session 2019/20
Figure 1.6: Fast GPIO port Pin value register byte and half-word accessible
register description
Program Example 1: Sets up a digital output pin using control registers, and
flashes an led.
Figure 1: Analog inputs of the mbed LPC1678 microcontroller
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
9
FKEE, Sem02 Session 2019/20
Listing 1.1: Example program for blinking the LED1 on the LPC1768 microcontroller
5.0 Lab Activities (40%)
Lab Assignment 1:
Modify the program in Listing 1.1 to blink the LED2 and LED3 on the LPC1768
microcontroller.
Introduction to the mbed microcontroller
#include "mbed.h"
// function prototypes
void delay(void);
//Define addresses of digital i/o control registers, as pointers to
volatile data
#define FIO1DIR2 (*(volatile unsigned char *)(0x2009C022))
#define FIO1PIN2 (*(volatile unsigned char *)(0x2009C036))
int main() {
FIO1DIR2=0xFF; // set port 2, lowest byte to output
FIO1PIN2=0x00;
while(1) {
FIO1PIN2 = 0x04; // OR bit 0 with 1 to set pin high
Delay();
FIO1PIN2 = 0x00; // AND bit 0 with 0 to set pin low
Delay();
}
}
//delay function
void delay(void){
int j; //loop variable j
for (j=0;j<1000000;j++) {
j++;//waste time
}
}
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
10
FKEE, Sem02 Session 2019/20
The mbed takes a microcontroller (LPC1678) surrounds it with some very useful support
circuitry. It places this on a conveniently sized little printed circuit board (PCB) and
supports it with an online compiler, program library and handbook. This gives a complete
embedded system development environment, allowing users to develop and prototype
embedded systems simply, efficiently and rapidly. Fast prototyping is one of the key
features of the mbed approach. The mbed takes the form of a 2 inch by 1 inch (53 mm by
26 mm) PCB, with 40 pins arranged in two rows of 20, with 0.1 inch spacing between the
pins. This spacing is a standard in many electronic components. Figure 1.7 shows the
mbed architecture.
The API (Application Programming Interface) is a set of function of subroutine definitions
that is used to help the application developers (or a programmers) to develop a program
faster and easier by providing the necessary building blocks and libraries. Figure 1.8
illustrates the concept of API in mbed microcontroller.
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
11
FKEE, Sem02 Session 2019/20
Figure 1.8: The mbed API
The mbed Software Development Kit (SDK) is a C/C++ microcontroller software platform
relied upon by tens of thousands of developers to build projects fast. The mbed library is
made up of a set of utilities, which are all itemized in the mbed website Handbook
(www.mbed.org). In the second lab session, we will learn about how to interface the
LPC1678 microcontroller to the simple I/O devices (digital and analog) its associated APIs.
Digital Input and Output
Our first API is DigitalOut where it is used to configure and control a digital output pin. The
DigitalOut API component creates a C++ class, called DigitalOut. The class then has a set
of member functions as follows:
The first of these is a C++ constructor, which must have the same name as the class itself.
This can be used to create C++ objects. By using the DigitalOut constructor, we can create
C++ objects where we can then write to it and read from it, using the functions write( ) and
read( ). Listing 1.2 demonstrates the use of mbed API to produce an output as in the
program in Listing 1.1. Please visit to www.mbed.com for other mbed APIs.
Electronic Engineering Laboratory IV (BEE31101) Lab 1: Introduction to ARM M3 Microcontroller (LPC1768)
12
FKEE, Sem02 Session 2019/20
Listing 1.2: Example program for blinking the LED1 on the LPC1768 microcontroller
using the mbed API
Lab Assignment 2:
Repeat Lab Assignment 1 by using mbed API. (LED2 and LED3)
5.0 Observations (15%)
1. From Listing 1.1, discuss how 0.1s delay can be implemented using C programming.
(5 marks)
2. From Lab Assignment 2, write down simple command line to blinking the LED1, LED2 and
LED3 on the LPC1768 microcontroller using the mbed API.
(10 marks)
6.0 Questions (15%)
1. By referring to Figure below, without using the mbed API, write a C++ program to perform
the operation as follows: when sw1 is pressed, the LEDs will act as a binary counter. While
when sw2 is pressed, all the LEDs will turn off.
(15 mark)
#include "mbed.h"
DigitalOut myled(LED1);
int main() { //the function starts here
while (1) { //a continuous loop is created
myled = 1; //switch the led on, by setting the output to logic 1
wait(1); //wait 1 seconds
myled = 0; //switch the led off
wait(1); //wait 1 seconds
} //end of while loop
} //end of main function
Recommended