foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/labsheetsem22019-2020/ece3186_…  · web viewthe...

14
FACULTY OF ENGINEERING LAB SHEET EMBEDDED IOT SYSTEMS & APPLICATIONS ECE3186 TRIMESTER 2 (2019/2020) ESD1: Construction of C program to control various I/O peripherals via the microcontroller *Note: On-the-spot evaluation may be carried out during or at the end of the experiment. Students are advised to read through

Upload: others

Post on 24-Mar-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

FACULTY OF ENGINEERING

LAB SHEET

EMBEDDED IOT SYSTEMS & APPLICATIONS

ECE3186TRIMESTER 2 (2019/2020)

ESD1: Construction of C program to control various I/O peripherals via the microcontroller

*Note: On-the-spot evaluation may be carried out during or at the end of the experiment. Students are advised to read through this lab sheet before doing experiment. Your performance, teamwork effort, and learning attitude will count towards the marks.

Page 2: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

ESD1: Construction of C program to control various I/O peripherals via the microcontroller

Objectives: To demonstrate the GPIO operations of an ARM Cortex M based microcontroller. To develop embedded C programs to access various I/O peripherals of the

microcontroller using mbed SDK

Equipment: Desktop computer with USB interface USB A-to-mini B cable NXP FRDM-KL25Z development board Windows 7 with Internet connectivity ARM mbed online IDE 4x 1k resistors, 4x tact-switches, 1x solderless breadboard, jumper wires

Introduction

The FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz. It includes 128KB FLASH, 16KB RAM and lots of interfaces including USB Host, USB Device, SPI, I2C, ADC, DAC, PWM, Touch Sensor and other I/O interfaces. Other features include easy access to MCU I/O, battery-ready, low-power operation, a standard-based form factor with expansion board options and a built-in debug interface for flash programming and run-control. It is packaged as a development board with connectors to break out to strip board and breadboard, and includes a built-in USB FLASH programmer. The KL25Z board pin descriptions are depicted below:

Programming with mbed Online IDE

Page 2 of 11

Page 3: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

1. Connect the OpenSDA USB port of the KL25Z board to the computer via the USB cable.

2. Browse to https://os.mbed.org/ and signup an account.

3. Once registered, click on the “Compiler” button to launch the mbed online IDE.

4. Select the target board by clicking the button on the top-right as shown below.

5. Click on the “Add platform” button as follows.

Page 3 of 11

Page 4: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

6. Next, click on “FRDM-KL25Z” as shown below.

7. Click “Add to your mbed compiler” to add the board as follows.

8. Once the board is added, go back to the mbed online IDE and repeat Step 4.

9. Now, it can be seen that the FRDM-KL25Z is already added.

10. Click “FRDM-KL25Z” and click “Select Platform” as shown below.

Page 4 of 11

Page 5: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

11. In the Compiler window, click on “New” on the upper left hand corner.

12. Select the “Blinky LED Hello World” as the template and click “OK” as follows.

Page 5 of 11

Page 6: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

13. Next, double-click on “main.cpp” under Program Workspace and click on “Compile” as shown below.

14. Select the drive labelled as “MBED” and save the binary file into that drive as follows.

15. Press the reset button on the KL25Z board and observe the operation.

Page 6 of 11

Page 7: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

Experiment 1.1: Digital Output with Onboard RGB LED

1. There is one RGB LED on the FRDM-KL25Z development board.

2. In this experiment, a program to control the RGB LED will be developed.

3. The RGB LED is active low and each color is assigned a label in the mbed SDK as shown in Table A.1.

Table A.1LED LabelRed LED_REDGreen

LED_GREEN

Blue LED_BLUE

4. Create a new program using the “Blinky LED Hello World” template.

5. Exercise 1.1: Analyze the code and make necessary changes so that the RGB LED will cycle through SEVEN (7) colors (red, green, blue, yellow, purple, cyan and white) repeatedly with each color shown for 1 second.

Page 7 of 11

Page 8: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

Experiment 1.2: Digital Input with External Switches

1. Construct and connect the following circuit to the FRDM-KL25Z development board.

2. The DigitalIn function is used to read the value of a digital input pin.

3. For example, to read the value of a pin:

DigitalIn button_press(D2);DigitalOut Led_out(LED_RED);int main(){

while(1) {if (!button_press)

Led_out = !Led_out;}

}

4. Create a new program using the “Blinky LED Hello World” template.

Page 8 of 11

Page 9: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

5. Modify the program with the example code from Step 3.6. Compile and download the binary file onto the KL25Z development.7. Once the code is downloaded, press the reset button on the board and observe the operation

when the button connected to D2 is pressed and released.8. Exercise 1.2: Analyze and enhance the code to control the RGB LED using the push-buttons

as follows:o Button 1: light up REDo Button 2: light up GREENo Button 3: light up BLUEo Button 4: light up WHITE (RED, GREEN and BLUE)

Experiment 1.3: Accessing Multiple Digital GPIOs

1. The BusIn interface is used to create a number of DigitalIn pins that can be read as one value.2. A group of GPIO pins can be used as a DigitalIn in the BusIn as shown in the following

example:BusIn My_Bus_In(D2, D3, D4); //D2 is LSB, D4 is MSBDigitalOut Led_out(LED_BLUE); int main(){

if(My_Bus_In == 0x03)//D2=1, D3=1, D4=0Led_out = 1;

}

3. The BusOut interface is used to create a number of DigitalOut pins that can be written as one value, as shown in the example below:BusOut My_Bus_Out(LED_RED, LED_GREEN, LED_BLUE); //LED_RED is LSB, LED_BLUE is MSBint main(){

My_Bus_Out = 0x04; //LED_RED=0, LED_GREEN=0, LED_BLUE=1}

4. Exercise 1.3: Modify the embedded C program in Exercise 1.2 to control the RGB LED using the push-buttons with the BusIn and BusOut functions.

Experiment 1.4: Universal Asynchronous Receiver Transmitter (UART)

1. UART is one of the popular protocols for serial communications.2. Install mbed serial driver by following the instructions on the following link:

https://os.mbed.com/handbook/Windows-serial-configuration

3. Launch “Device Manager” to check the COM port number for “mbed Serial Port”.

Page 9 of 11

Page 10: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

4. Install Termite RS232 Terminal from:http://www.compuphase.com/software_termite.htm

5. Launch Termite and click “Settings”.

6. Change the COM port (following the mbed serial port shown in Device Manager) and baud rate (9600) as shown below.

7. Launch a web browser and enter the following link:https://os.mbed.com/handbook/SerialPC

8. Copy the sample code for “Hello World!” and paste it as a new program in compiler created based on the “Blinky LED Hello World” template.

9. Compile and download the code to the KL25Z development board.10. Launch Termite on the computer and press the reset button on the KL25Z board.11. Analyze the code to understand how the KL25Z board communicates with the computer.12. Launch a web browser and enter the following link.

https://os.mbed.com/teams/NXP/code/frdm_tsi_slider/

Page 10 of 11

Page 11: foe.mmu.edu.myfoe.mmu.edu.my/lab/lab sheet/Labsheetsem22019-2020/ECE3186_…  · Web viewThe FRDM-KL25Z is a development board built on 32-bit ARM Cortex-M0+ core running at 48MHz

ECE3186 Embedded IoT Systems and Applications ESD1

13. Click “Import into compiler” as shown below (import as “program”).

14. Compile and download the code to the KL25Z development board.15. Press the reset button on the KL25Z board to execute the code.16. Study the code to understand how the touch sensor work.17. Exercise 1.4: Enhance the code to control the RGB LED using the touch sensor as follows:

Touch Sensor Value (x)

LED Color

0 < x ≤ 0.25 Red0.25 < x ≤ 0.5 Green0.5 < x ≤ 0.75 Blue0.75 < x ≤ 1 White

The value of the touch sensor should be sent to the computer via UART and printed via Termite. The LED should remain ON even when the finger is no longer touching the touch sensor. E.g. If the LED is showing green color just before the finger is removed from the touch sensor, it should remain on with green color even after the finger is removed.

Page 11 of 11