0% found this document useful (0 votes)
36 views

Lab Manual ENGG3640 F22

ENGG 3640

Uploaded by

yamoon shash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lab Manual ENGG3640 F22

ENGG 3640

Uploaded by

yamoon shash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

ENGG3640: Microcomputer Interfacing


Laboratory Manual, F22
Developed by: Radu Muresan, August 2015
Revised and Updated 2016, 2018, 2021, 2022
School of Engineering, University of Guelph
NOTE: Do Not Copy Any Parts of this Lab Without Permission! There are many
hours of work invested in these labs.

References:
[1] Arm Assembly Language Fundamentals and Techniques, by William Hohl and Christopher
Hinds
[2] ARM Compiler V5.05u2
[3] uVision Assembler User Guide
[4] uVision Compiler User Guide
[5] NXP, K60 Family Product Brief
[6] NXP, K60 Sub-Family Data Sheet
[7] NXP, K60 Sub-Family Reference Manual
[8] NXP, TWR-K60D100M Tower Module, User’s Manual

I have developed this laboratory manual to train students in microcomputer interfacing


concepts. The manual contains six laboratories. This lab manual will teach students how to
design interfacing applications using the commercial microcontroller Kinetis K60_100 with an
ARM Cortex-M4 processor from NXP ( https://www.nxp.com/products/processors-and-
microcontrollers/arm-microcontrollers/general-purpose-mcus/k-series-cortex-
m4:KINETIS_K_SERIES ). We will use the Kinetis K60 Tower System base board from NXP to
teach ARM Cortex-M4 processor-based applications.
I would like to thank Nathaniel Groendyk for his support with developing the primary connector
interface board to the breadboard and the software tool setup and Hong Ma for soldering the
connectors.

Page |1 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

LAB 0: Lab Safety, Group Setup, Equipment Introduction and


Distribution … Week 1
OBJECTIVE
Lab 0 introduces safety rules regarding the laboratory activity, setup the lab groups that will work
together on the labs, and introduce the equipment.

SAFETY
Lab safety presentation by the lab instructor and teaching assistant (TA).
GROUP SETUP
The TAs will form groups and the lab instructor will distribute the equipment.

INTRODUCTION: ARM CORTEX M4 DEVELOPMENT SYSTEM


This laboratory manual aims to teach the students how to develop mixed assembly and C projects. I
have chosen to teach ARM assembly because ARM processors are used extensively in commercial
products such as portable devices, embedded designs, and controllers in many specialized interfaces. In
addition, working as an embedded engineer, you will likely program in assembly, so learning an
assembly language will significantly enhance your embedded engineering experience and help you
become a much better embedded designer.

NXP(FREESCALE) SYSTEM
The TWR‐K60D100M is a Tower Controller Module compatible with the NXP (Freescale) Tower
System. It can function as a stand‐alone, low‐cost platform for evaluating the Kinetis K10, K20, and
K60 microcontroller (MCU) devices. The TWR‐K60D100M features the Kinetis K60 low‐power
microcontroller based on the ARM Cortex-M4 architecture with a USB 2.0 full‐speed OTG controller
and 10/100 Mbps Ethernet MAC. The TWR‐K60D100M is available as a stand‐alone product or as a
kit (TWR‐K60D100M‐KIT) with the Tower Elevator Modules (TWR‐ELEV) and the Tower Serial
Module (TWR‐SER). The TWR‐K60D100M can also be combined with other Freescale Tower
peripheral modules to create development platforms for a wide variety of applications. Figure 20
provides an overview of the NXP (Freescale) Tower System.

Page |2 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Figure 20: (NXP)Freescale Tower System Overview

The TWR-K60D100M Tower system contains the TWR-K60D100M Tower MCU module shown in
Figure 2 below. Also, check the TWR-K60D100M Tower Module User’s Manual posted on the
University of Guelph Courselink system and online information at https://www.nxp.com/.

Page |3 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

The block diagram of the base board is shown in Figure 21 below.

Page |4 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Figure 21: TWR-K60D100M Block Diagram

K60DN512 Microcontroller
The TWR-K60D100M module features the MK60DN512VMD10 microcontroller. The K60
microcontroller family is part of the Kinetis portfolio of devices built around an ARM Cortex-M4 core.
Refer to the K60 Family Product Brief and the K60 Family Reference Manual for comprehensive
information on the microcontroller devices. The key features are listed here:
• 32-bit ARM Cortex-M4 core with DSP instructions
• 144 MAPBGA, 13mm × 13 mm, 1.0mm pitch package
• 1.71V-3.6V operating voltage input range
• 512 Kbytes of program flash, 128 Kbytes of static RAM
• External bus interface
• Power management controller with 10 different power modes
• Multi-purpose clock generator with PLL and FLL operation modes
• 16-bit SAR ADC, 12-bit DAC
• High-speed analog comparator with 6-bit DAC
• Programmable voltage reference
• USB full-speed/low-speed OTG/Host/Device controller with device charge detect
• 10/100 Mbps Ethernet MAC
• SPI, I2C (w/SMBUS support), UART (w/ISO7816 or IrDA), CAN, I2S
• SD Host Controller (SDHC)
• GPIO with pin interrupt support, DMA request capability, digital glitch filtering
• Capacitive touch sensing inputs (TSI)
• Debug interfaces: JTAG, cJTAG, SWD
• Trace: TPIU, FPB, DWT, ITM, ETM, ETB
For further information please check the “K60 Sub-Family Reference Manual” from Freescale (posted
on ENGG3640 Courselink).

Page |5 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
The block diagram below shows the many integrated interfaces and features supported by the K60
microcontrollers.

Figure 22: Block diagram representing integrated interfaces and features

Page |6 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
BOARD SETUP
Many jumpers are present on the TWR-K60D100M baseboard. First, make sure that the jumpers are set
in the default configuration. Next, connect the board; the drivers will be automatically installed if
needed. After connecting the board, under the device manager, mark the COM port number assigned to
the OSBDM/OSJTAG – CDC Serial Port. On my computer, this is COM5, and I will use this for the
work examples I present. When you start Keil uVision5, if the OSJTAG/OSBDM firmware is outdated,
it will ask you to update the firmware. To update the firmware, you need to unplug the board from the
USB connector and place a jumper on the J19 jumper called JM60 BOOT. This jumper is located on
the right corner of the board.

See: TWR‐K60D100M Tower Module User’s Manual page 5.

Then you plug the board again and continue with the update. After updating the firmware, unplug the
board again and remove the jumper from the JM60 Boot connector. Then, replug the board and
continue with the lab development.
To ensure everything works well, start the Keil uVision 5, if not yet started, and run an example. Click
Project >> Open Project, navigate to Demo.uvproj project, and open it.

Page |7 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Rebuild the project and load it to the board. After you load the project into the board, press the reset
button. The LED D7 should lit ON/OFF every second.
During the laboratory development, you will also need to use the terminal as a visual display to
communicate serially with the board. In order to connect a terminal to the board, you can use the
PEMicro terminal or the PuTTY terminal. The PuTTY terminal should work well; to start a PuTTY
communication terminal click the PuTTY icon and set the serial communication to the corresponding
COM port you identified for your board, as shown below.

Page |8 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
You can load one new project from the examples to check that your terminal works well. For example,
close the Demo project and load the dac_adc_demo.uvprojx project shown below.

The terminal should work, and you should be able to communicate with the program and run the demo,
as shown below.

Page |9 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

EXAMPLE LABS USED IN THIS MANUAL CAN BE FOUND IN THE DIRECTORY :


file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%20User'
s%20Guide/index.html
All projects from KSDK_1.2.0 package are installed in the following directory (or given to you by the
TA):
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\
The demo applications supported by the K60 boards that we have in our lab are found in the directory:
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\demo_apps\...\mdk\...
1. ADC Hardware Trigger Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__adc__hw__trigger.html
2. ADC Low Power Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__adc__low__power.html
3. DAC ADC Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__dac__adc__demo.html
4. Flash Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__flash__demo.html
5. Hello World Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__hello__world.html
6. Hardware Timer Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__hwtimer__demo.html
7. I2C Communication Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__i2c__comm.html
8. I2C Demo with RTOS:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__i2c__rtos.html
9. RTC Function Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__rtc__func.html
10. SAI Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__sai__demo.html
11. Heating, Ventilating, and AC demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__web__hvac.html
12. ADC 16 Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__adc16__example.html

P a g e | 10 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
13. CRC Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__crc__example.html
14. DAC Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__dac__example.html
15. DMA Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__dma__example.html
16. DSPI Example Demo:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__dspi__example.html
17. EDMA Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__edma__example.html
18. EWM Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__ewm__example.html
19. FLASH Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__flash__example.html
20. FlexCAN Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__flexcan__example.html
21. FTM Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__ftm__example.html
22. GPIO Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__gpio__example.html
23. I2C Example with other methods:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__i2c__example.html
24. LPTMR Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__lptmr__example.html
25. MPU Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__mpu__example.html
26. PDB Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__pdb__example.html
27. PIT Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__pit__example.html

P a g e | 11 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
28. RNGA Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__rnga__example.html
29. RTC Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__rtc__example.html
30. SDHC SdCard Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__sdhc__sdcard__example.html
31. UART Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__uart__example.html
32. WDOG Example:
file:///C:/Freescale/KSDK_1.2.0/doc/Kinetis%20SDK%20v.1.2.0%20Demo%20Applications%
20User's%20Guide/group__wdog__example.html

P a g e | 12 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

LAB 1: MDK Tools, ARM Assembly – Timer using Loop Delays …


Week 2
OBJECTIVE
In this lab, you will learn to develop mixed projects in ARM assembly and C using uVision tools
targeting the Kinetis K60 microcontroller.
IMPORTANT NOTE: As you develop your assembly program for this course, you might encounter a
memory fault problem. I understand that the memory fault exception is sometimes set when you
program a peripheral device such as ADC0 (Analog to Digital Converter) or a DAC0 (Digital to
Analog converter) and attempt to write the control registers before you have enabled the corresponding
clock gate control bit in the System Integration Module (SIM). The reason the system sets this memory
fault exception is because you are trying to write a memory location associated with one of the control
registers of a peripheral device. Still, since your clock is not enabled to the peripheral device, the
memory location will not be updated, and the system generates the fault to let you know that your value
was not, in fact, written in the memory. Therefore, always initialize first the SIM registers and then the
corresponding peripheral control registers.

Creating the Lab Template Project


ARM assembly laboratory
Start Keil uVision 5 and create a new project.
Create a directory called engg3640f23labs in the folder:
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m; Note that my folder in the examples is
engg3640f16labs.
Create a new directory called labtemplateea in the engg3640f16labs. All the paths that I give are
referenced to the labtemplateea directory.
CREATE A NEW PROJECT
Click Project >> Select New uVision Project … >> Navigate to the …/labtemplateea folder and type
the name (labtemplateea) of the project (Note: you should have a different folder for each project) >>
then click Save. In the new window >> Select Freescale >> Select K60 Series >> Select
MK60DN512xx10, as shown below and then click OK. Then click OK again to create the project. Note
that you will have in the left pane of the uVision5 window Project:labtemplateea and Target 1 as a
subfolder.

P a g e | 13 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Set up the Project Target


1. Highlight Target 1 then click Project tab >> then select Options for Target ‘target 1’
2. In Target tab window: check the correct settings for IROM1 (0x0; 0x80000,
IRAM1(0x20000000; 0x10000) and IRAM2(0x1FFF0000; 0x10000). Note that only IROM1,
IRAM1 and IRAM2 should have a check mark.
3. C/C++ tab window:
a. In Preprocessor Symbols >> Define type: DEBUG, CPU_MK60DN512VMD10,
TWR_K60D100M, TOWER
b. In Include Paths (depending on your installation directory; you could use absolute paths
here or relative to C:/Freescale/KSDK_1.2.0 ), type the following absolute paths (if
correct for your installation). To introduce these paths you can also use Folder Setup >>
Insert. If you use the Folder Setup you do not need to type the ; at the end of the path, it
will be automatically inserted. For my computer I am using the absolute paths:
C:/Freescale/KSDK_1.2.0/platform/osa/inc;
C:/Freescale/KSDK_1.2.0/platform/utilities/inc;
C:/Freescale/KSDK_1.2.0/platform/CMSIS/Include;
C:/Freescale/KSDK_1.2.0/platform/devices;
C:/Freescale/KSDK_1.2.0/platform/devices/MK60D10/include;
C:/Freescale/KSDK_1.2.0/platform/devices/MK60D10/startup;
C:/Freescale/KSDK_1.2.0/platform/hal/inc;
C:/Freescale/KSDK_1.2.0/platform/drivers/inc;

P a g e | 14 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
C:/Freescale/KSDK_1.2.0/platform/system/inc;
C:/Freescale/KSDK_1.2.0/platform/utilities/inc;
C:/Freescale/KSDK_1.2.0/platform/drivers/src/gpio;
../..
(Note that to build a relative path you would replace C:/Freescale/KSDK_1.2.0 with:
../../../)
c. In Misc Controls type: --library_type=standardlib --c99 --diag_suppress=1296,186 --
library_interface=armcc
Note: Avoid spaces in paths and file names. Also, in the above field there should not be
space between 1296,186
d. In Warnings: select All Warnings

4. In Asm tab window:


a. In Define type: DEBUG
b. In Misc Controls type: --cpreproc --diag_suppress=1609,1581
5. In Linker tab window:
a. Use Memory Layout from Target Dialog should be unchecked
b. R/O Base should have: 0x00000000
c. R/W Base should have: 0x20000000
d. Disable Warnings should be empty.
e. In Scatter File type the file path (check before to make sure the file exists in this
location):
C:\Freescale\KSDK_1.2.0/platform/devices/MK60D10/linker/arm/MK60DN512xxx10_flash.scf
f. In Misc controls type:
C:\Freescale\KSDK_1.2.0/lib/ksdk_platform_lib/mdk/K60D10/debug/libksdk_platform.lib
--remove
6. In Debug tab window: Should have checked the followings: Load Application at Startup; Run
to main(); Use: PEMicro Debugger
a. In Initialization File type:
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\driver_examples\gpio\mdk\pemicro_
connection_settings.ini
b. In CPU DLL should have: SARMCM3.DLL
c. In Dialog DLL should have: DCM.DLL then for the parameter should have –pCM4
7. In Utilities tab window: Should have checked the following: Use Debug Driver and Update
Target Before Debug
8. Click OK.

Set up the Groups within the Project


1. Right click on the Target 1 and select Manage Project Items …; delete the default Source Group
1 in Groups (by clicking X)
2. Add the following groups by selecting new (Insert) and typing each one’s name in order and
then enter: utilities; startup; board; source. At the end click OK.

P a g e | 15 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
3. Expend Target 1 folder. Within the utilities group add the following files. Please note that I
have indicated the paths of the files as they are on my computer. Right click utilities folder and
select Add Existing Files to Group ‘utilites’. Then chose the following files to add in this group:
a. fsl_misc_utilities.c (path:
C:\Freescale\KSDK_1.2.0\platform\utilities\src\fsl_misc_utilities.c)
b. fsl_debug_console.c (path:
C:\Freescale\KSDK_1.2.0\platform\utilities\src\fsl_debug_console.c)
c. print_scan.c (path: C:\Freescale\KSDK_1.2.0\platform\utilities\src\print_scan.c)
d. print_scan.h (path: C:\Freescale\KSDK_1.2.0\platform\utilities\src\print_scan.h)
e. fsl_debug_console.h (path:
C:\Freescale\KSDK_1.2.0\platform\utilities\inc\fsl_debug_console.h)
f. fsl_misc_utilities.h (path:
C:\Freescale\KSDK_1.2.0\platform\utilities\inc\fsl_misc_utilities.h)
4. Within the startup group add the following files:
a. startup_MK60D10.s (path:
C:\Freescale\KSDK_1.2.0\platform\devices\MK60D10\startup\arm\startup_MK60D10.s
)
b. system_MK60D10.h:
(C:\Freescale\KSDK_1.2.0\platform\devices\MK60D10\startup\system_MK60D10.h)
c. system_MK60D10.c:
(C:\Freescale\KSDK_1.2.0\platform\devices\MK60D10\startup\system_MK60D10.c)
d. startup.c (path: C:\Freescale\KSDK_1.2.0\platform\devices\startup.c)
e. startup.h (path: C:\Freescale\KSDK_1.2.0\platform\devices\startup.h)
f. fsl_device_registers.h (path: C:\Freescale\KSDK_1.2.0\platform\devices\fsl_device_registers.h)
5. Within the board group add the following files:
a. gpio_pins.c: (path: C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\gpio_pins.c)
b. gpio_pins.h: (path: C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\gpio_pins.h)
c. pin_mux.c: (path: C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\pin_mux.c)
d. pin_mux.h: (path: C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\pin_mux.h)
e. board.c: (path: C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\board.c)
f. board.h: (path: C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\board.h)
g. hardware_init.c: (path:
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\driver_examples\gpio\hardware_init.c)
6. Within the source create your new main.c file. Right click on the source folder and choose Add
New Item to Group ‘source’; then type main as the file name. For now just copy and paste into
your main.c file the main.c content found at:
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\driver_examples\gpio\main.c
a. NOTE: You can also in fact open the main.c file from the link above and save it as
main.c in your project directory. Then add the existing file main.c from your project
directory to your source group.

Your labtemplateea.uvprojx uVisoin final project structure should look like the structure shown in
figure below after you expend all the group folders:

P a g e | 16 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Running the labtemplateea.uvprojx Project.

Open a PuTTY terminal and set it to the right COMx port with serial communication and speed
115200.
Build (F7) the project, then load it into the board and verify that it executes as required. When
loading, it might ask to select the device target. Choose the K60DN512M10 device:

P a g e | 17 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

To run the project on the board, press the Reset button, and the LED D7 should lit. Then pressing the
switch button SW1 should turn the LED off; then, pressing again, the LED should turn on.

Using the Embedded Assembler


MODIFY the main to build your embedded assembler application as follows:
See Example 18.2 in the Arm Assembly Language book or the example in the Compiler User Guide.
Click Help >> uVision Help in uVision IDE and select Contents. You will find a link to the Compiler
User Guide. Review the Section "Using the Inline and Embedded Assembler of the AR" (Specifically,
Section 6.27 Embedded assembler syntax in C and C++) in this guide; it should be the same example as
in the book. See the Figure below:

P a g e | 18 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

P a g e | 19 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
STEPS to build the above example in your newly created project.
Step 1: Before your main function, type the assembly code shown in the Figure below. Also, within the
main type, the two declarations shown (for *a and b[30]). Don’t worry about the errors shown on
__asm line. The program will compile without errors. However, you will have one warning. To
eliminate the errors, you need to replace __asm with asm (). Then save the file, then go back and
change asm () to __asm. However, the errors will come back when you re-load the source.
Make sure there is no space before the label loop and also that you have two underscore characters _
before asm.

P a g e | 20 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Step 2. Add the following code within the while loop in your main:

Step 3. Rebuild your project and run it. First, make sure that your PuTTY terminal is active. Your
PuTTY terminal should display the following after resetting and pressing the SW1 (inner switch).

P a g e | 21 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Calling an Assembly Function from C Code
The following example is Example 18.4 in the ARM Assembly Language Book (Should read Section
18.4 Calling Between C and Assembly). I believe the uVision help content has this example as well.
NOTE: Before you continue the work, you should save your main as main_backup.c so you don’t lose
your previous work.
Step 1. Create a new assembly file as part of the source group that you call scopy.s (or whatever name
you like). Right click on source folder >> select Add New Item to Group ‘source’ >> select Asm File
(.s) >> type scopy for the file name. Type the program shown in Figure below.

Step 2. Delete the embedded assembly addition within your main.c program and make the following
additions before and after the int main (void) line, as shown in the figure below.

Step 3. Within your while loop in the main, make the additions shown in the figure below.

P a g e | 22 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Step 4. Start your PuTTY terminal if not started. Then, rebuild the project, load it into the board and
run it. Your terminal window should appear like in the figure below.

P a g e | 23 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Building a New main.c without the GPIO Functionality

If we don’t want the functionality of the GPIO in our main function, we can build a clean main.c that
just calls the strcopy assembly function. Keep the scropy.s file the same. Please note that you can just
rebuild the files you change in the project to save time.

NOTE: If you change a file in your project, you do not need to rebuild the entire project. Instead, you
can use build target, and only the files that have been updated will be recompiled and linked.

P a g e | 24 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Developing a Project with a Pseudo main.s Routine that can Call C
Functions
The following is the template project I created for working with a pseudo main.s function written in
assembly. I have experimented with other ways of developing the main assembly function. Still, I
found this template project more practical, especially if you need to call C functions such as printf. The
following are the steps to develop the pseudo main assembly language project. Please note that once
you build the project, you can just use build target to recompile and relink the changed files only.
1) Keep the folder and file structure the same as in the previous projects with the following two
files in the source folder: main.c and my_main.s;
2) First create your main.c file as shown in figure below.

Figure: main.c

3) Create a new item as an assembly language target that you include in the source group. You can
call this file mymain.s or pseudo_main.s or any other name you like. This file will be your main
working file for your assembly language implementation. The content of the main asm file is
shown in Figure: mymain.s. Ensure that for the DATA area, srcstr and dststr are labels, and

P a g e | 25 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
there is no space before; otherwise, you will get an “unknown code” error. Also, not to have the
“last line of file ends without a newline,” warning make sure you go to the last line of your
assembly file, type enter, then save the file.

Figure: mymain.s

4) Rebuild all target files for this project and download the project to the board. Please note that
you don’t need to rebuild all files next time you make some changes. Instead, you can use build,
and only files that have been modified will be recompiled.
5) The terminal PuTTY should display the following. However, you will most likely have only the
first two lines displayed. Note: If you right-click on the terminal header, a set of actions and
commands you can perform on the terminal are shown. You can reset the terminal if needed.

P a g e | 26 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
LAB1 REQUIREMENTS

1) For the Lab Report sample and organization, please see the Appendix.
2) The mymain.s program from the example above contains an intentional code error. Debug the
mymain.s program and have it display all four rows as shown in the PuTTY figure above.
Explain the program error in the Lab 1 report.
3) Your Lab 1 report should show the content of the addresses where your code and data are
loaded. Also, what hexadecimal addresses do “srcstr” and “dststr” data fields have? Present
your answer in the report.
4) At what hexadecimal addresses are your main.c function and mymain.s function located? Why
are the address regions for mymain.s much different than for the data? After all, the code and
data are present in the same program.
5) Study ENGG3640 week 1 and week 2 lecture notes. Specifically, for this lab requirement, study
the topic of “Program Loops,” slides 87 to 93. Using the template assembly develop a program
that counts by 10 seconds and displays the seconds repeatedly, wrapping after 60 s (10s, 20s, …
60s, 10s, 20s, …), displaying the seconds approximately 10 seconds apart. Use the loop delay
method to implement this program. To implement the delay, you are required to use a delay
loop. Identify the processor speed in MHz from the board data specifications and show your
calculations for the counter and the loop delay. Present the complete delay program and the
loop delay calculations in your Lab 1 report.
6) Answer all the questions completely in your lab report and explain your program
implementation in your report. For example, describe the time delay routine and how you can
obtain higher time delay precision with your implementation.

P a g e | 27 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

LAB 2: ARM Assembly – Simple Calculator … Weeks 3, 4


OBJECTIVES

Lab 2 builds on Lab 1 and teaches more assembly language concepts by implementing a Reverse Polish
Notation (RPN) calculator. Specifically, in this lab, you will define and use the stack and the functions
such as debug_getchar() and debug_putchar(). In addition, you will use ASCII code representation of
numbers and integer representation, converting numbers from ASCII to integer and from integer to
ASCII. Below are the definitions for console communication that you can use from
fsl_debug_console.h. So far, we have used in Lab1 PRINTF, and here in this lab, we will use PRINTF,
PUTCHAR, and GETCHAR.

To understand how these functions (PRINTF, SCANF, PUTCHAR, GETCHAR) are implemented, you
need to examine the fsl_debug_console.c file.

IMPORTANT NOTE: As you develop your assembly program for this course, you might encounter a
memory fault problem. I understand that the memory fault exception is sometimes set when you
program a peripheral device such as ADC0 (Analog to Digital Converter) or a DAC0 (Digital to
Analog converter) and attempt to write the control registers before you have enabled the corresponding
clock gate control bit in the System Integration Module (SIM). The system sets this memory fault
exception because you are trying to write a memory location associated with one of the control
registers of a peripheral device. Still, since your clock is not enabled to the peripheral device, the
memory location you intend to write will not be updated, and the system generates the fault to let you
know that your value was not, in fact, written in the memory. Therefore, always initialize first the SIM
registers and then the corresponding peripheral control registers.

P a g e | 28 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
LAB 2 Development
Numbers are represented in ASCII form when received from the keyboard and sent to the display. In
our case, we use the PuTTY terminal, and all our operands for the calculator will be communicated in
ASCII, and also, to display them, we must have them in ASCII. However, the assembly instructions
work with integer numbers represented in binary that are stored in registers. Some processors will have
special assembly language instructions for ASCII and BCD (Binary Coded Decimal) numbers. In ARM
assembly, we will use integer arithmetic to implement our calculator.
As a result, our calculator will work with data as follows:
1. Input data from the keyboard through the GETCHAR function (in ASCII) →
2. Store the characters in memory as an ASCII string null-terminated or fixed length →
3. Convert the ASCII string number into binary using the number conversion method given in
Appendix Lab2 →
4. Process data in binary →
5. Convert the binary result back to ASCII using the division by 10 procedure given in the
Appendix Lab 2 →
6. Output the result to PuTTY through the PUTCHAR function (in ASCII).

In ASCII representation, the numbers are stored as a string of ASCII characters. For example, the string
0123456789 is stored as 0x30 0x31 0x32 … 0x39. Also, to delimitate the end of the string, you can use
the null-terminated string method where the last character of the string is 0x00. In our implementation,
we will delimitate the end of an argument and an operator by typing enter (that will generate a carriage
return ASCII character).

Note that R13 (the main stack pointer register) is set to 0x20010000, the top memory address of the
IRAM1 memory area specified in the Target 1 project options (shown in the figure below). Likewise,
the SP (stack pointer) register is initialized to this value. You can use this setting, or you can define
your own stack region within the RAM area.

P a g e | 29 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

WHAT IS REVERSE POLISH NOTATION (RPN) CALCULATOR?


In regular algebraic notation, operations are placed between the arguments, for example, 30 + 100 =
130. However, in RPN, the operator is placed after the arguments, for example, 30 100 + = 130.
Some examples of expression calculations are given in the table below:

Standard Notation Reverse Polish Notation


30 + 50 × 7 30 50 7 × +
30 × (50 + 7) 30 50 7 + ×
(30 + 50) × (40 + 60) 30 50 + 40 60 + ×
30 + 50 + 7 30 40 + 7 =

The evaluation of the RPN calculator is simple if you implement a stack. First, each argument is
pushed on the stack. Then, when an operator is encountered, you pop two arguments from the stack and
perform the required operation, and next, you store the result back on the stack, and so on.
For our calculator in Lab 2, we will separate each argument and operation by a carriage return (CR)
ASCII character generated by pressing return on your keyboard after each argument. The ASCII code
for CR is 0x0D. So, for example, if you type on your keyboard 1234Enter, your generated ASCII string
that you will store in the memory will be the following 5 bytes: 0x31 0x32 0x33 0x34 0x0D.
So, how should our calculator work when using the RPN method? For example, if we want to calculate
the expression shown on row 4 of our table above, we will need to type the following:
30
50
+
40
60
+
*
=
The result displayed should be: 8000
Also, if you prefer, you could separate the arguments and the operators by spaces (0x20), and then you
will have:
30 50 + 40 60 + * =
8000

How will the stack method work for this example and how will you implement the calculator?

Step 1:
Your program must wait for characters and test for arguments and operators. We receive each character
and store them in a string in memory. When one argument is received, it must be converted from the
ASCII representation into integer representation. Then we push the integer value on the stack. In our
example, our first argument is 30, so our stack will be:
top
30
Bottom

Step 2:
Receive the following argument and push it on the stack. Now the stack will be:

P a g e | 30 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
top
50
30
bottom

Step 3:
When received, each character will be tested for operators such as +, -, *, and /, and the best practice
would be to do this in Step 1. When you encounter an operator, you will pop two arguments from the
stack, perform the operation, and then push the result on the stack again. At this step, in our example,
we encounter +, and we will pop from the stack 50 into a register (register_argument_1 = 50), then 30
into another register (register_argument_2 = 30); we will add the two registers and obtain 80
(register_result = 80). After this operation, we will push the result onto the stack. Now, our stack will
be:
top
80
bottom

Step 4:
Receive the following argument that, for our example, is 40. Push this argument onto the stack:
top
40
80
Bottom

Step 5:
Receive the following argument that is now entered as 60. Push this argument onto the stack:
top
60
40
80
Bottom

Step 6:
Receive the + operator. Pop the top two arguments in order (60, 40) from the stack and place them in
registers. Perform the addition and then store the result 100 onto the stack:
top
100
80
Bottom

Step 7:
Receive the * operator. Pop the top two arguments in order (100, 80) from the stack and place them in
registers. Then, perform the multiplication operation and store the result on the stack.
top
8000
Bottom

P a g e | 31 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Step 8:
Receive the = operator. Pop the result into one register. Then convert from binary to ASCII and store it
into a result string that you can send to the PUTCHAR function to display to the PuTTY terminal. The
stack now is empty:
top
bottom

Step 9:
Jump to the start again (Step 1).

The following are some general steps in developing this lab.


1. Create a new project called calculator. Then, you can follow the template from Lab1 and
change the main.c and mymain.s programs accordingly. Specifically, in main.c you should add
two new functions that you could call my_putchar and my_getchar. Or, if you prefer, you can
link directly to debug_getchar and debug_putchar, which can be found in the
fsl_debug_console.c file.
2. Develop within your mymain.s your application. The calculator you must implement for this lab
should be an RPN calculator, as shown in the example of this lab. Within your mymain.s, you
should have several subroutines that implement the various functions required for the
calculator. For example, you could have subroutines such as: ASCII string to integer
conversion; integer addition; integer subtraction; integer multiplication; integer division; integer
to ASCII string conversion; and others as needed.
3. The calculator must handle operands that are at least 4 digits long. Please note that you need to
convert the strings of characters you get from the PuTTY terminal into integer binary
representation to be able to use the ARM integer arithmetic instructions. Then, to display the
result to the PuTTY terminal, you need to convert it back from integer to ASCII characters.
4. Your implementation must use the stack in implementing the RPN calculator, as shown in the
example of this lab.
5. Display the expression and your result on the PuTTY terminal, with each character in the
expression being echoed to the terminal.
LAB 2 REQUIREMENTS

1. Prepare a demo of your calculator program that can perform at least the following integer
arithmetic operation (+, -, *, /).
2. Prepare a lab report detailing the implementation and the use of the stack.
3. Present a block diagram of your program in your report with detailed explanations of the use of
the stack.
4. Present a block diagram of your calculator and explain your subroutines.
5. Trace the stack for an example that uses your program and show the results in your report. The
stack trace should show the contents of the stack and the SP register values for the example.

APPENDIX LAB 2: RPN Example


Example of conversion from decimal to binary. If, for example, the decimal number is 347910, then
by performing the following binary operations, we will obtain the binary value for this number.
347910 = [(((3×10 + 4) ×10 + 7) × 10 + 9] = …

P a g e | 32 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Assembly implementation algorithm:
1. Initialize a register result such as r1 →
2. load digit 3 into a register such as r2 →
3. add register r2 to r1 and keep the result in r1 →
4. multiply r1 by 10 →
5. …

Example of conversion from binary to decimal. The conversion from binary to ASCII decimal can
be done by using the division by 10 algorithm.
Assembly implementation algorithm:
1. If the number is greater than 10, divide the number to be converted by 10, and preserve the
quotient and the remainder in some registers.
2. The first remainder is the least significant digit of the conversion. In order to get the ASCII
value, you must add 0x30 to the digit value and store the result as a byte.
3. Next, if the quotient is greater than 10, divide the quotient by 10, and get a new quotient and a
new remainder. This new remainder will be the second digit of the conversion.
4. Step 4. Continue until the quotient is less than 10.

Calculator Results Example. The following figure shows a screenshot of my calculator


implementation's terminal results. As you can see, I have considered negative results; however, my
calculator does not accept negative arguments in this version. You don't need to do signed arithmetic in
your calculator lab, but it would be a good experience. Keep in mind that the subtraction operation
could give you a negative result, and this result, if it is negative, is represented in 2's complement. So
you can simply test your result for a negative sign, and if it is negative, you would complement the
result and add 1 to get the corresponding value that you need to convert to ASCII. Then you can store
the '-' sign in front of the result and print it.

P a g e | 33 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

P a g e | 34 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

LAB 3: ARM Assembly – Interrupts … Weeks 5, 6


OBJECTIVES

Lab 3 builds on Labs 1 and 2 and teaches more assembly language concepts. Specifically, in this lab,
you will learn how to develop an interrupt handler routine in assembly.

IMPORTANT NOTE: As you develop your assembly program for this course, you might encounter a
memory fault problem. I understand that the memory fault exception is sometimes set when you
program a peripheral device such as ADC0 (Analog to Digital Converter) or a DAC0 (Digital to
Analog converter) and attempt to write the control registers before you have enabled the corresponding
clock gate control bit in the System Integration Module (SIM). The reason the system sets this memory
fault exception is that you are trying to write a memory location associated with one of the control
registers of a peripheral device. Still, since your clock is not enabled to the peripheral device, the
memory location will not be updated, and the system generates the fault to let you know that your value
was not, in fact, written in the memory. Therefore, always initialize first the SIM registers and then the
corresponding peripheral control registers.

• Really if you try to write to a READONLY section, you will get a memory fault exception.
When you get a memory fault exception, ensure that your program data is declared as a
READWRITE section.

EXCEPTIONS AND INTERRUPTS (SEE EDOCUMENT: CORTEX M4 DEVICES.PDF )


An exception is anything that breaks the normal flow of a program. An interrupt, or IRQ, is an
exception signaled by a peripheral or generated by a software request. In a general computing system,
the interrupts are used by a peripheral device to communicate with the processor. All interrupts are
asynchronous to the instruction execution.
The exception types for the Cortex M4 devices in decreasing order of their priority are: Reset, NMI,
HardFault, MemManage, BusFault, UsageFault, SVCall, PendSV, SysTick, Interrupt (IRQ).

P a g e | 35 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Table from: Cortex M4 Devices.pdf pages 2-22 to 2-23

EXCEPTION HANDLERS
Handlers are program routines. The processor handles exceptions using:
• Interrupt Service Routines (ISRs) – these routines handle the IRQ interrupts
• Fault Handlers – these are fault exception routines for: HardFault, MemManage fault,
UsageFault, and BusFault.
• System Handlers – these are fault exception routines for: NMI, PendSV, SVCall, SysTick.

The exception handlers are executed by the use of the vector table. The vector table contains the reset
value of the stack pointer, and the start addresses (exception vectors) for all exception handlers. The
figure below shows the order of the exception vectors in the vector table.
Note that the least-significant bit of each vector must be 1, indicating that the exception handler is
Thumb code.

P a g e | 36 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Note that a lower priority number indicates a higher priority level. There are configurable priorities in
software for all exceptions except Reset, HardFault, and NMI. For example, assigning a higher priority
value to the IRQ[0] and a lower priority value to the IRQ[1] (See pages 4-7) means that IRQ[1] has
higher priority than IRQ[0].
In order to increase priority control in a system with interrupts, the NVIC supports priority grouping by
dividing each interrupt priority register entry into two fields:
• Upper field defining the group priority
• Lower field defining the sub-priority within the group.
Note that only the group priority determines the preemption of interrupt exceptions.

P a g e | 37 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
ABOUT THE CORTEX -M4 PERIPHERALS
(See Chapter 4 in Cortex M4 Peripherals.pdf)

P a g e | 38 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
NESTED VECTORED INTERRUPT CONTROLLER (NVIC)
NVIC is the controller that interfaces the processor CPU with the interrupts. It implements various
functionalities through a set of memory-mapped programmable control registers. The table below gives
the summary of the NVIC registers.

CMSIS functions enable software portability between different Cortex-M profile processors. The table
below gives functions to access NVIC registers when using CMSIS.

For a better description of the NVIC registers and their bit usage, check Chapter 4 in “Cortex M4
Devices.pdf”.

P a g e | 39 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

NVIC PROGRAMMING HINTS


The software uses the CPSIE I and CPSID I instructions to enable and disable interrupts. The CMSIS
provides the following intrinsic functions for these instructions:
void __disable_irq(void) //Disable Interrupts
void __enable_irq(void) //Enable Interrupts

Plus, CMSIS provides various functions for NVIC control.

LAB 3 Development
Note that the vector address for the “low power timer” is 0x0000_0194; the vector number is 101, and
the IRQ number is 85 (i.e., 0x55) (See pp. 78 in K60 SFRM document (Section 3.2.2).
For this lab, you should reference the lptmr_example project from the MDK. Specifically, you can
consult the fsl_lptmr_driver.h (C:\Freescale\KSDK_1.2.0\platform\drivers\inc\fsl_lptmr_driver.h),
fsl_lptmr_driver.c (C:\Freescale\KSDK_1.2.0\platform\drivers\src\lptmr\fsl_lptmr_driver.c ), and
fsl_lptmr_hal.h (C:\Freescale\KSDK_1.2.0\platform\hal\inc\fsl_lptmr_hal.h) files.
Note that the hardware_init.c file within this project should be appropriate for your application.
However, it looks like they are placed in different locations.
IN MK60D10. H FILE WE HAVE :
▪ Line 259: LPTMR0_IRQn = 85, /**< Low power timer interrupt */
▪ Lines 11306-11449 the LPTMR peripheral access layer is defined.
▪ #define LPTMR0_BASE (0x40040000u) // this is the base address (See pp 1089 in the K60
Sub Family Reference Manual.pdf (K60SFRM)
▪ Line 19107: #define LPTMR0_BASE (0x40040000u)
▪ Line 19108: #define LPTimer_IRQHandler LPTMR0_IRQHandler
Also, the K60 Sub Family Reference Manual (K60SFRM) document describes the Low-Power Timer
(LPTMR) in Chapter 42.

P a g e | 40 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Working with hardware interrupts is essential. However, before you attempt to develop the assembly
language, you should implement the C-based application and then convert parts of it into the assembly.

LAB3 REQUIREMENTS

Please consult the Lab3 Requirement Notes below before attempting to implement the laboratory.
Requirement 1: Using the LPTMR0 timer, implement a hardware interrupt-based project in C that
counts on the terminal every 5 to 10 seconds from 0s to 60s. You can choose any second value greater
than 5 and smaller than 10. Do not use a callback function as the lptmr_example uses. Setup your
lptmr0 interrupt routine such that it does the counting. You can print a short message in your main ()
routine such as: 0, 10s, 20s, …, 60s. Note that you need to activate the NVIC for the proper interrupt
channel and initialize the timer to generate the hardware interrupts. Also, it would be best if you
cleared the lptmr interrupt flag.

Requirement 2: Using the C project developed at lab Requirement 1, perform changes such that your
lptmr IRQ service routine is entirely implemented in assembly. Your routine should perform the
clearing of the interrupt flag and keep track of the number of interrupts by incrementing a counter that
later you print as a value in seconds in the main loop. It is important to show how you generate the
appropriate address in the interrupt vector table in the assembly, clear the interrupt flag, and return
from the interrupt. For example, to calculate the aliased memory location of a bit within a control
register, you can use the following formula:
Bit-band alias = bit-band-aliased base + (byte offset × 32) + (bit number × 4).
For example, for your LPTMR0_CSR register bit TCF we will have:
Bit-band alias = 0x42000000 + [(0x40040000 – 0x40000000) × 32] + (7 × 4) = 0x4200001C.

Requirement 3: Using the C project developed at lab Requirement 1 and the things you learned at
Requirement 2, perform changes such that your LPTMR0 setup and the hardware interrupt service
routing are all implemented in assembly language. Explain all your LPTMR0 timer programming steps.

Requirement 4: Prepare your repot and demo. The report should clearly explain how the interrupt
system works for the K60 microcontroller, and you should present clear code examples of how the
interrupts are initialized and set.

LAB3 REQUIREMENTS NOTES


Requirement 1 Notes
For this requirement you will use the uVision project called lptmr_example found in the folder:
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\driver_examples\lptmr\mdk. You do not need to
use a callback function when you build your C project version as per requirement 1. You can do the
counter increment in the interrupt handler routine. However, it would be best to clear the interrupt flag
before incrementing your counter. If you do not clear the interrupt flag, your program will not work
well. This project should be your starting point for building an assembly language project.
The screenshot below shows a breaking point from my Lab 3 project to show the clearing of the
LPTMR0 interrupt flag.

P a g e | 41 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Requirement 2 Notes
Note 1: To implement the lptmr0 IRQ handler, you must understand how to clear or set a bit in a
peripheral device control register. In our case, we need to clear the Timer Compare Flag (TCF) in the
Low Power Timer Control Status Register (LPTMR0_CSR) register (See pp. 1089 in the K60 Sub
Family Reference Manual). The CSR register for LPTMR0 is at the Address: 4004_0000h base + 0h
offset = 4004_0000h. One way to do this is by writing a 0x01 in the corresponding location within the
Aliased to peripheral bridge (AIPS-Lite) and general-purpose input/output (GPIO) bitband memory
(See pp. 169 – 171 in the K60 Sub Family Reference Manual to understand the writing in this memory.
Another way is to perform a logical AND operation with that location (well, you need to first load it
into a register due to RISC architecture design for ARM) and clear that bit. However, by debugging the
C code project from Requirement 1, you can step through the assembly code generated by the C code,
and you can learn how you can do the writing in this memory. Also, you can view the status of the
control register when you debug by opening the LPTMR0 system viewer window (in the debug session
go to View >> System Viewer >> LPTMR0). Below is a screenshot from my C lab implementation for
Requirement 1. Note in the figure below the bits content of the CSR register as I show a screenshot
from my debug session going through the interrupt handler routine. To trace through the assembly
language, you need to click in the assembly language window and highlight an instruction; then, you
can step through. Please remember that when you are using interrupts, your debugging is more
complex since the interrupting device might do its work at a different rate while you are stepping
through the program.

P a g e | 42 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Note 2: The next important thing is understanding how the interrupts work for the Cortex M4 devices.
This mechanism is described in Section 2.3 of the Cortex M4 Devices manual. Specifically, it is
important to understand how exception entry occurs, and exception exit occurs (See pp. 2-26 to 2-28 in
the Cortex M4 Devices manual). The processor automatically stacks its state on exception entry and
unstacks this state on exception exit with no instruction overhead.
Exception Entry:
• Exception entry occurs when there is a pending exception with sufficient priority and either one
of the following is true:
o The processor is in thread mode
o The new exception is of a higher priority than the exception being currently handled.
• In the general case when the processor takes an exception, the processor pushes information
onto the current stack. This operation is referred to as stacking, and the structure of eight data
words stacked is referred to as stack frame. The content of the stack frame depends on the
processor mode.
• The stack frame includes the return address which is the address of the next instruction in the
interrupted program. This value is restored to the PC at exception return so that the interrupted
program can resume.
• In parallel with the stacking operation, the processor performs a vector fetch that reads the
exception handler start address from the vector table (See startup_MK60D10.s for the default
handler vector table).
• When the stacking is complete, the processor starts executing the exception handler. At the
same time, the processor writes an EXC_RETURN value to the LR (link register that is register
r14). This code value indicates which stack pointer corresponds to the stack frame and what
operation mode the processor was in before the entry occurred. See the value stored in LR in the
figure above (0xFFFFFFE9) as I entered by C interrupt handler.
Exception Return:
• Exception return occurs when the processor is in the handler mode and executes one of the
following instructions to load the EXC_RETURN value to the PC:
o An LDM or POP instruction to load the PC
o An LDR instruction with the PC as the destination
o A BX instruction using any register
• Note that EXC_RETURN value is the value loaded into the LR on exception entry. In this case
as you build your assembly IRQ handler you will need to save this value on the stack and when

P a g e | 43 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
you exit the handler you need to put this value into the PC. The exception mechanism relies on
this value to detect when the processor has completed an exception handler. The lowest 5 bits of
this value provide information on the return stack and processor mode. All EXC_RETURN
values have bits[31:5] set to one.
• When the EXC_RETURN value is loaded into the PC it indicates to the processor that the
exception is complete, and the processor will initiate the appropriate exception return sequence
(built in hardware sequence).

Requirement 3Notes
Note 1. To implement Requirement 3, you must understand how to program the LPTMR0 control
registers. For a description of the LPTMR registers, review Chapter 42 in the K60 Sub Family
Reference Manual. If you follow the C implementation that you developed at Requirement 1 or the
lptmr driver example, you will see that there are three important steps in programming the LPTMR
module:
• Initialize the LPTMR0
• Set the timer’s period
• Start the timer
If you go into the debug mode with your C implementation and step through the assembly, you will
learn how to write your main assembly program to initialize the timer. Below are some screenshots
from my debugging session through my C Lab 3 project.

P a g e | 44 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Screenshot1: The program here has a breakpoint before the LPTMR is initialized. Watch the LPTMR0
register values!

Screenshot2: The program here has a breakpoint after the LPTMR is initialized and before the timer’s
period is set. Watch the LPTMR0 register values!

P a g e | 45 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Screenshot3: The program here has a breakpoint after the LPTMR is initialized and after the timer’s
period is set. Watch the LPTMR0 register values!

Note 2. Important here is how to enable the NVIC and the interrupts. For this requirement, you can use
the CMSIS function: NVIC_EnableIRQ(IRQn_Type IRQn), where the IRQn is the IRQ number (i.e.,
85 or 0x55 for lptmr0). These NVIC functions are found in the core_cm4.h file. In order to use these
functions, you should use INT_SYS_EnableIRQ and INT_SYS_DisableIRQ instead, which are found
in the fsl_interrupt_manager.h file. I have called these functions in my implementation similar to the
my_fprint function built in Lab 1. However, I recommend you directly program the NVIC register for
this IRQ. I have programmed directly the NVIC_ISER2 register in order to enable the IRQ 85. Each
NVIC_ISERn register has 32 bits; the IRQ number corresponds to the bit position starting with
NVIC_ISER0 (0 to 31), then NVIC_ISER1 (32 to 63), etc.
• Table 4-2 NVIC register summary, gives the register addresses.
• NVIC_ISER2 has the address: 0xE000E108.
• You need to write a 0x00200000 value in this register to activate IRQ 85.
I have implemented my Lab 3 example all in assembly, and I have managed to write NVIC registers,
and the LPTM0 registers in assembly. My main function in main.c file source follows the basic
structure that I have introduced in Lab 1:

P a g e | 46 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Note 3. It did happen that I got a memory fault exception while implementing this lab, and my program
stalled. From what I figured out, this fault occurs when trying to access memory-protected locations for
data and instructions. Also, the MemManage fault on exception entry stack memory operation (failure
on a hardware save of context because of an MPU access violation) could be a cause. To solve this
problem, I just moved the assembly code around and checked the data memory addresses I used. My
lab 3 assembly implementation has about 180 lines of code, including the comment lines.

P a g e | 47 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

LAB 4: INTERFACING THE K60 MICROCONTROLLER – GPIO,


LEDs and 7 Segment interfacing … Weeks 7, 8
OBJECTIVES

In Labs 1, 2, and 3, we learned programming in ARM assembly and how to use interrupts. The main
objective of Lab 4 is to teach the students how to use the GPIO functionality to control LEDs and the
seven-segments display and use timers such as PIT to generate interrupts. You can implement this lab
in C or assembly.

IMPORTANT NOTE: As you develop your assembly program for this course, you might encounter a
memory fault problem. I understand that the memory fault exception is sometimes set when you
program a peripheral device such as ADC0 (Analog to Digital Converter) or a DAC0 (Digital to
Analog converter) and attempt to write the control registers before you have enabled the corresponding
clock gate control bit in the System Integration Module (SIM). The reason the system sets this memory
fault exception is that you are trying to write a memory location associated with one of the control
registers of a peripheral device. Still, since your clock is not enabled to the peripheral device, the
memory location will not be updated, and the system generates the fault to let you know that your value
was not, in fact, written in the memory. Therefore, always initialize first the SIM registers and then the
corresponding peripheral control registers.

LAB 4 Development
The project reference for this lab is gpio_example.uvprojx found in
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\driver_examples\gpio\mdk folder and the lecture
notes related to LEDs and 7-segments display interfaces. So, load this project, rebuild it, and test that it
runs on your station. Another project you can study to understand the GPIO functionality is the Blinky
project found at: C:\Keil_v5\ARM\Boards\Freescale\TWR-K60D100M\Blinky\Blinky.uvprojx.
The following explains the main characteristics of the Blinky.uvprojx project. Note that the project
Blinky is a simple project that you can efficiently study. However, it does not support the use of the
PuTTY terminal.
Working with gpio_example.uvprojx you need to note that the fsl_gpio_driver.c, fsl_gpio_irq.c files
are found in: C:\Freescale\KSDK_1.2.0\platform\drivers\src\gpio\fsl_gpio_driver.c and the
fsl_gpio_hal.c is found in: C:\Freescale\KSDK_1.2.0\platform\hal\src\gpio\fsl_gpio_hal.c. These files
have all the GPIO register functionalities used in the example.

GPIO
Note that all references here are made to the “K60 Sub Family Reference Manual.pdf” available from
nxp.com or ENGG3640 CourseLink system.
The key to understanding the example programs we have is understanding the interfaces and their
registers. The MK60D10.h header file describes all data types that reflect the interface registers.
In Lab 4 Requirement 1, you are required to run a simple GPIO example that blinks 3 LEDs. The
program is straightforward, but you must understand the interface and how to use the header file. We
need to know that the port pins can be multiplexed for different functions (See Chapter 10: Signal
Multiplexing and Signal Description), and some pre-programming steps must be taken before we can
read or write a port pin. To successfully program the GPIO, you need to do the following steps:

P a g e | 48 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
1. Enable the clock to the appropriate Port. In our case, it will be Port A. The clock control is done
through the System Integration Module (SIM), responsible for system control and chip
configuration. System clock gating control register 5 (SIM_SCGC5) is accountable for gating
the clock to the GPIO ports PORTA to PORTE. See Chapter 12: System Integration Module.
2. Configure the appropriate pin of the appropriate port as a GPIO. In our case, it will be PORTA
pins 11, 28, and 29. See Chapter 11: Port Control and Interrupts (PORT). If we write 001 in the
field 10-8 (Pin Mux Control) of the PORTA_PCRn (in our case, n will be 11, 28, and 29), then
the pin will be programmed as alternative 1, which is GPIO. Note that for each port (A to E)
and each port bit (0 to 31), there is a Pin Control Register called PORTx_PCRn (here, x can be
A, B, C, D, E, and n can be 0 to 31) that is a 32-bit register. Note that after calling
hardware_init() the following port pins are used: PORTA_PCR0,1,2,3,4,5 and
PORTE_PCR8,9. Avoid using these pins in your applications.
3. Program PORTA, see Chapter 55: General-Purpose Input/Output (GPIO). The registers to
program are: Port Data Direction Register (GPIOA_PDDR), Port Data Output Register
(GPIOA_PDOR), Port Clear Output Register (GPIOA_PCOR), and Port Set Output Register
(GPIOA_PSOR). In the LEDs example, first, you will program the direction register as output,
then turn the LEDs off, and then start a cycle of OFF and ON with a delay.

LAB4 REQUIREMENTS

To develop this lab, you can use C or assembly programming.


Requirement 1
Run and study the example project Blinky.uvproj found in the directory:
C:\Keil_v5\ARM\Boards\Freescale\TWR-K60D100M\Blinky. Rebuild the project and download it to
your board. The Flash has to be configured to use PEMicro Debugger. Running the application should
make the 3 LEDs blink every second. How to understand this simple program? If you follow the
Blinky.c program, you will see the instructions used to program the GPIOA port. To understand these
instructions, you need to follow the header file MK60D10.h.
Going step-by-step through the control ports used, we see the following ports:
• SIM->SCGC5 …: this port is used to enable the clock to Ports A, B, C, D, E (See pp. 306-307)
and check the SIM_Type structure in the MK60D10.h header file (Lines 7327-7355). Here
(1UL << 9) will move 1 in bit position 9 of the register SIM_SCGC5 (See pp. 306), enabling
the clock to Port A. You can do this in assembly by reading SIM_SCGC5 and performing a
logic OR to set bit 9 and then write back the updated content.
• PORTA->PCR[11], PORTA->PCR[28] and PORTA->PCR[29]: these are the
PORTA_PCR11, PORTA_PCR28, and PORTA_PCR29 control registers associated with
PORTA pins 11, 28, and 29, respectively (See pp. 277 – 279). These control registers are part of
the Port Control and Interrupt (PORT) module described in Chapter 11. Here we need to
program the MUX field to Alternative 1, the GPIO function, for each pin we want to use as
GPIO. If you check the schematics of the board (TWR-K60D100M_SCH.pdf) on page 7, you
will see that the LEDs are connected as follows: the orange LED D7 is connected to PTA11, the
yellow LED D8 is connected to PTA28, the green LED D9 is connected to PTA29, and the 4th
LED D11 is connected to PTA10. These PTA pins are set as GPIO through the PORTA_PCRn
registers. The PORT_Type structure used here is described in the MK60D10.h header file lines
6331 to 6342.

P a g e | 49 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
• PTA->PDOR …: the PDOR register configures the logic levels driven on each general-purpose
output pin (see pp. 1745). Also, the GPIO_Type is described in the MK60D10.h file in lines
4605 to 4612. Then further down in this header are all register masks and all registers fields. In
our example, this instruction here sets PTA bits 11, 28, and 29 to logic level 1, and as a result,
the LEDs will be off. Again, you can use here assembly to program this register.
• PTA->PDDR …: the PDDR register configures the individual pins for input or output (see pp.
1748). In our case, we write a 1 in the port A (PTA) fields 11, 28, and 29, and as a result, these
pins will be set as general purpose outputs.
• PTA->PCOR …: the PCOR register (see pp. 1456) configures whether to clear the fields of the
PDOR register. For example, if the bit is set to 1, then the corresponding bit in PDOR will be
clear. Note that the values in PDOR are the values that will be outputted in the case of the
general purpose output pin.
• PTA->PSOR …: the PSOR register (see pp. 1456) configures whether to set the fields of the
PDOR register. For example, if the bit is set to 1, then the corresponding bit in the PDOR will
be set. Note that the values in PDOR are the values that will be outputted in the case of the
general purpose output pin.
Requirement 2
Implement a new GPIO application that blinks the LEDs on the board where you control the delay of
the LEDs by using one of the following timer modules: Programmable Delay Block (PDB) – Chapter
39, FlexTimer Module (FTM) – Chapter 40, or Periodic Interrupt Timer (PIT) – Chapter 41. To
develop this application, you should use the interrupt project developed in Lab3 by replacing the
LPTMR0 device with the PIT timer or other timer you prefer (no LPTMR0 is allowed in this project). I
have used the PIT module and implemented this project in assembly. You can use either C or assembly.
For example, I have used the PIT module to generate interrupts every 2 seconds. For this, you need to
use the following registers (See pp. 1083-1084 in K60SFRM for an example):
• Enable PIT clock in SIM_SCGC6 register.
• Calculate the PIT_LDVAL1 (if you are using timer 1) that will give you the interrupt period
you desire. For example, assume the clock period to be 50 MHz.
• Program the PIT_MCR register.
• Program the PIT_LDVAL1.
• Enable Timer 1 interrupt.
• Start Timer 1.
• Program the NVIC for the appropriate PIT interrupt. Note that the IRQn values for all interrupts
can also be found in the MK60D10.h file at the beginning (PIT1_IRQn = 69, in my case).
Requirement 3
Design a voltmeter display module in incremental steps.
Note: Follow the design steps provided. Before you power any breadboard circuits, you need to
check the circuit with the TA or technician and get approval.
Implementation Requirements and Explanations. Develop a new application that implements the
display module of a voltmeter device. Here you need to use the 3-digits 7-segment display, the
resistors, and the transistors provided in your lab kit. Use the K60 board to control your display. You
can find many digital voltmeter designs online. One that is closed to what you need to develop is found
here: http://www.circuitstoday.com/voltmeter-using-arduino
Your breadboard circuit should be similar to the circuit found in this link. However, you are using the
K60 board, and all your connections and programming will differ. Also, keep in mind that our board
uses 3.3 V for Vdd.

P a g e | 50 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
For this application, you should store a 3-digit value with a decimal point (where required) in a
memory location (say volts_value); note that the ASCII value for the decimal dot is: 0x2E. You will
also need a table holding the 7-segment hex values for all digits. Then display this 3-digit value to your
7-segment display circuit on the breadboard. You should be able to change this field and display
different volts values as the TA will request during your demo. This lab project will be used to develop
a full voltmeter in Lab 5. The link to the 3-digits display from your lab kit is:
http://www.digikey.ca/product-detail/en/kingbright/BA56-12GWA/754-1656-5-ND/3084327
• Your device should be able to display using 3-digits from 0.01 to 5.00 with the decimal point
after any digit if required (assume the units to be V). As a note, you could connect the GPIO
pins directly to the display and control the base pin of the 2N222, and the collector pin of the
2N222 could be connected to a 5V voltage supply.
• To implement the voltmeter hardware, your electric circuit must be developed on the
breadboard provided using the components that you have in your Lab kit. First, you need to
connect the extension board to the primary connector (J11 Sides A and B), then you need to use
wires to connect the corresponding control signals that you will output from your K60 board to
the breadboard (See Appendix Lab 4 for pin connections and signal correspondence.
• The application should output the control signals for the 7-segments display to corresponding
GPIO pins that are connected to the primary connector J11 sides A and B (See Table 7 in TWR-
K60N512 Tower Module User’s Manual). You need to identify enough output GPIO pins that
you can connect to the breadboard circuit (See Appendix for pin availability on the board).
• Design the 3-digits voltmeter on the breadboard provided using appropriate resistors and
transistors as required. In the lab report, you need to justify all your design features by showing
the calculations and component selection process. Use the datasheet of the 7-display device to
explain the current calculations.
• Show your GPIO signals on the oscilloscope and ensure your program functions correctly. If
your device doesn’t work, you should just display the signals and plot them for the report.
For controlling your breadboard schematic, you need at least 11 GPIO lines. In my voltmeter
implementation, I used GPIO PORTC lines (PTC0 to PTC10) connected to segments a, b, c, d, e, f, g,
DP, Dig1, Dig2, and Dig 3, respectively.
Implementation Procedure Based on My Voltmeter Project
Step 1. Develop your voltmeter software using C or ARM assembly. I have used ARM assembly, and
it is easier to work with assembly and refer the control register programming needs for the needed
modules to the K60SF Reference Manual. You should develop the application in incremental steps and
debug the program as you go by verifying all your control registers and output values. For example, I
first programmed the GPIO port A (PTA), went through the program step by step, and verified the
NVIC, SIM, PORTC, PIT, PTA, and PTC control registers, and made sure that all were programmed as
I needed them. Then I programmed the GPIO port C (PTC) and checked all the outputs. Then
incrementally, I programmed the 7-segment display section with the ASCII to BCD and the BCD to 7-
segments table conversion. Here you need to make sure that your decimal point is in the right place and
you are outputting the right digit.

P a g e | 51 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Step 2. I first used a large PIT interrupt period (about 2 seconds), so I could easily verify what I was
displaying, and then I reduced the scanning time to 5 ms when I reached the last step.
Step 3. Disconnect the K60 board from the USB. Connect the break-out box to the primary connector
of your K60 board. Connect the board back and download your program and go step by step through
your program. See that you can measure the right signals on the connector pins that will be wired to the
PCB board. Here you can either use the oscilloscope or a voltmeter. Make sure you set the voltmeter to
measure volts. Keep in mind that the output voltage levels for our digital values for the K60 board are
0V for 0 logic and about 3.1 V for the 1 logic. Here the ground terminal of your scope or meter must be
connected to a ground pin on the board. For example, you can use pin 81 from the primary connector
(check the TWRK60 User Manual to verify that GND is outputted to pin 81 on the primary connector).
The measuring probe of the oscilloscope or multimeter can be connected to the break-out box where
you output the port signals and verify that the values are 0 logic or 1 logic as required.
Step 3. Disconnect the USB from your K60 board. Next, build your PCB circuit (See Figure below,
where I present the working system for my Lab 4). As shown, you can similarly arrange your PCB with
the transistors, resistors, and the 7-segments device.

P a g e | 52 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Step 4. VERY IMPORTANT!!! Now you should be ready to power your circuit.
• Do not place any power wires from your power supply on the breadboard. Instead, turn the
power supply ON and make sure that your output is regulated to 0V.
• Turn off the power supply. First, check that you have proper wires to connect the power supply
outputs to the breadboard; your power wires should have an end with a pin plugged into the
breadboard. Next, connect the plus (+) terminal of the power supply to one of the plus (+) lines
(which is marked with red) on your breadboard. Note that all the pins on the top + line of your
breadboard will carry the power voltage (if you are connected to the top line). Then connect the
COM output from your power equipment to one of the minuses (-) lines (marked with blue) on
your breadboard.
• After you connect the outputs of the power supply equipment to your breadboard, it is essential
to run a wire from a ground pin (GND) from your primary connector, specifically, from the
break-out box, to the minus (-) line on your breadboard. I used Pin 81 from the primary
connector as my common ground connection between the K60 board and the breadboard. If you
don’t make this common ground connection, your circuit will not work. Also, when you have
separate circuits that need to work together and are powered from different power sources, they
MUST have a common ground (K60 gets its Vdd from the PC through the USB cable, and the
breadboard gets its Vdd power from the power equipment).
Step 5. Verify your wiring and the circuit again and reconnect the USB cable to the K60 board.
Download the program and turn ON the power supply device. Slowly regulate the power output of your
device to reach 5V (not more than 5V). Run the program by pressing the reset button and watch your
display work. If not working, you will need to debug your program and verify the voltage levels that
come to the display.

P a g e | 53 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Requirement 4
• Demo all your implementation to the TA. Then, be ready to answer any theoretical and practical
questions related to the implementation.
• Prepare a good report that presents all the programs you experimented with in this lab. Explain
the implementations and back your implementations with screenshots showing results and
calculations for your timers, circuit design at point 3, etc. Specifically, for this report, you
should clearly present how you programmed your timers to control the delay for your LEDs;
explain the control registers used and how you programmed them; explain your 3-digits
voltmeter device control program and the circuit design.

APPENDIX LAB 4: Connectors and Devices


Below is your K60 primary connector interface. Note that this extension board has one large connector
(164 pins) that needs to be plugged into the primary connector of your K60. The smaller connectors can
be used to plug wires between the extension board and your breadboard.
Note that the J1, J2, J3, J4, and J9 are connected to Side A of the primary connector such that: A1 →
J1[1], A2 → J1[2], …, A21 → J2[1], A21 →J2[2], …, A61 →J4[1], A62 → J4[2], …, A79 → J4[19],
A80 → J4[20], A81 → J9[1], and A82 → J9[2].
And J5, J6, J7, J8, and J10 (that looks on the board to be J18) are connected to Side B of the primary
connector such that: B1 → J5[1], B2 → J5[2], …, B21 → J6[1], B21 →J6[2], …, B61 →J8[1], B62 →
J8[2], …, B79 → J8[19], B80 → J8[20], B81 → J10[1], and B82 → J10[2].

In the figure below, I show my Lab 4 voltmeter display module working:

P a g e | 54 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
For your reference, I am presenting Table 7 (Primary connector pinout) from the TWRK60 User
Manual. Please note the pin's usage indicated in the Usage column. In order to program the correct
usage, you need to choose the corresponding pin alternative in the PORTx_PCRn register (See Port
Control and Interrupts Module).

… table continues on the next page.

P a g e | 55 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

P a g e | 56 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
2N222 Transistor Pinout

7-Segments Circuit Diagram


Pin numbering starts from the bottom left with 1 to 6, then goes up from 7 to 12. Note that pin 6 is NC.
Then you need to identify the corresponding LED to the pin number using the diagram below to build
your PCB circuit.

P a g e | 57 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

LAB 5: INTERFACING THE K60 MICROCONTROLLER – Timers,


ADC and DAC Interfacing … Weeks 9, 10

OBJECTIVES
Lab 5 main objective is to teach the students the ADC and DAC interfaces using the K60
microcontrollers and the use of timers to control the ADC module. In this lab, you will finalize the
voltmeter design started in Lab 4 and use the DAC module to output the signal measured by the ADC
to an oscilloscope.

IMPORTANT NOTE: As you develop your assembly program for this lab, you might encounter a
memory fault problem. I understand that the memory fault exception is sometimes set when you
program a peripheral device such as ADC0 (Analog to Digital Converter) or a DAC0 (Digital to
Analog converter) and attempt to write the control registers before you have enabled the corresponding
clock gate control bit in the System Integration Module (SIM). The system sets this memory fault
exception because you are trying to write a memory location associated with one of the control
registers of a peripheral device. Still, since your clock is not enabled to the peripheral device, the
memory location will not be updated, and the system generates the fault to let you know that your value
was not, in fact, written in the memory. Therefore, always initialize first the SIM registers and then the
corresponding peripheral control registers.

LAB 5 Development
The lab references are Chapter 35 Analog-to-Digital Converter (ADC), Chapter 37 Digital-to-Analog
converter (DAC), and Chapter 39 Programmable Delay Block (PDB) in the K60 SFRM documentation.

INTRODUCTION
The MK60DN512VMD10 microcontroller that is part of the TWR-K60D100M base board has various
on-chip interfaces, and complex applications can be developed.

Analog to Digital Converter (ADC) – See Chapter 35 pp. 819 to 874


The block diagram of the ADC module is shown in the figure below. Please read the detailed
functionality from the Reference Manual.
ADC Triggers
The primary hardware trigger mechanism for triggering ADC is PDB. Also, note that PDB can be itself
triggered by other peripherals (See pp 125 in K60SRFM. Furthermore, it can be bypassed by using
ADCxTRGSEL bits in the SOPT7 register of the SIM module. Therefore, we will only use the PDB
trigger for our Lab 5 development.
From the block diagram below, to set up a continuous hardware trigger from PDB, we need to do the
following:
• We must assert the ADHWT signal by setting the ADTRG bit to 1 in the SC2 register.
• We need to choose a hardware trigger event ADHWTSn in the SOPT7 register, which should
be a PDB timer for us.
• We need to set up the desired signal period for the PDB timer to control the ADC conversions.
• We need to set the conversions to be hardware triggered by PDB and continuous mode.

P a g e | 58 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
• The interrupt for the PDB does not need to be enabled.
• If we enable the PDB0 interrupt, then you need to clear the interrupt flab PDBIF; otherwise, the
program keeps going to the PDB0 interrupt handler.
• We need to enable the ADC0 interrupt; this interrupt is generated when data is ready in the
result register. Here reading the ADC0_RA result register clears the COCO flag, which is the
interrupt flag.
• One way to implement this part is to set the PDB interrupt to interrupt continuously at a fixed
rate; then, you set the ADC0 as a software trigger. In the PDB0 interrupt handler, you write the
ADC0_SC1 register in order to start a new conversion. You can first check step-by-step by
connecting your AD channel (I used AD10, which is connected to the PTA7, which is
ADC0_SE10 which in turn comes from B40 primary connector pin).

P a g e | 59 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

ADC Initialization
The reference manual describes the initialization procedure that needs to be followed before using the
ADC. Here I present the primary initialization sequence from the manual. Please consult the manual for
further explanations.

P a g e | 60 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Figure 1: Initialization flowchart example

P a g e | 61 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Digital to Analog Converter (DAC) – See Chapter 37 pp. 903 to 912

The figure below shows the block diagram of the DAC module found on pp 904 in the reference
manual. Study further the functionality and the programming of the DAC module.

P a g e | 62 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
LAB 5 REQUIREMENTS

Requirement 1: Interfaces Demos. For implementing the lab requirements please also consult the
LAB 5 Implementation Notes section that follows the requirements sections.
1. Run the example project adc16_example.uvprojx found in the directory:
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\driver_examples\adc16\. Rebuild the project
and download it to your board. Analyze the application and understand how the ADC works
and how it was programmed.
2. Run the example project dac_example.uvprojx found in the directory:
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\driver_examples\dac\. Rebuild the project
and download it to your board. Analyze the application and understand how the DAC works
and how it was programmed.

Requirement 2: Lab Development


1. For this requirement, you need to develop a voltmeter that can measure DC voltages between
0V to 3.3 V. The design should sample an analog line where you input your DC voltage values
and display the measurement on your voltmeter display module (7-segments circuit) built in
Lab4. In fact, you can further build upon the Lab 4 implementation by adding the ADC part.
The circuit diagram for this lab should be the same, except for adding the input line to your
voltmeter. Use all the development steps you learned in Lab 4 to implement your circuit and
connect the measurement equipment.
2. For this requirement, you should still use your PIT timer for cycling the 7-segments digits. I
have implemented this lab, and there are various challenges with the programming of the timers
and the ADC module. You can use either C or assembly language; I have used the ARM
assembly since it was easier for me to follow the programming of the control registers.
3. The implementation should use interrupts for the PIT and interrupts for the ADC0 module. I
have used the hardware trigger for the ADC0 module through the PTB0 timer. Here I am OK if
you use the interrupts for the PTB0 timer and then trigger the ADC0 through software in the
PDB0_IRQHandler. Please note that you must program the ADC0 to sample a single-ended
analog channel (See Lab 5 Implementation Notes below). The ADC0 should be programmed
for 16-bit conversion, interrupts enable, and hardware trigger with a continuous trigger from the
PTB0 timer. A DC power supply can generate the input voltage to your voltmeter. If you have a
dual DC power supply, you can use this one to power the 7-segments circuit and to supply input
to your voltmeter. I used a dual power supply and connected one output to the + line of the PCB
to power the 7-segments circuit and the other output to the other + line of the PCB, and from
there, I took a wire to the B40 pin on the primary connector. Note that if we don’t have a dual
power supply, we could use two different power supplies. In this case, we need to connect the
grounds of all devices used to form a common ground (the TWR-K60D100M board GND must
be connected to the ground terminals of both power supplies). It is important that you can
regulate the voltage to the input of your voltmeter and show how your voltmeter displays the
correct result. However, the input signal to the analog line should not exceed 3.3 V; always
move the knobs slowly and make sure you are reading the correct channel voltage.
4. The ADC0 conversion register is in binary. In order to display this value to your 3-digits
voltmeter, you need to convert it to BCD by using the division by 10 algorithm and keeping the
remainders. The display can be in a fixed format, such as 0.00 to 3.30, where your decimal
point is set after the first digit.

P a g e | 63 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
Requirement 3— Lab Development
1. In this requirement, you must connect an input to your voltmeter from a function generator
device, such as BX Precision 4040A. Disconnect the power supply wire from your ADC0 input
(B40 pin if that was used) and connect the function generator's output. Before you connect the
function generator to the measuring input, view your signal on the oscilloscope and make sure
that it is within the input range allowable for the analog input (0V to 3.3V max). The input
signal should not be negative and should not have a peak greater than 3.3 V. Again, before you
connect the signal generator output to your breakout box, ensure that you verify its output level,
so it is between 0 V to 3.3 V.
2. Input your ADC measurements to the DAC module and measure the output signal of the DAC
with an oscilloscope. For example, you could use the DAC0 output connected on your K60
board to the A32 pin (See Table 7 in TWRK60 User Manual). Change the duty cycle and the
frequency of the input signal to the ADC and observe the measured signal. Your DAC0 can be
simply triggered by a software trigger after you read the result register in your ADC0 interrupt
service routine (I do the binary to BCD conversion in this routine and the averaging and the
activation of the DAC0). Also, for operating the DAC0 for this requirement, you don’t need to
enable the data buffer operation. However, keep in mind that at requirement 2, you have used
the 16-bit ADC conversion, and here the DAC is a 12-bit input; you will need to take the 12
most significant bits to get the correct conversions.
3. Plot both signals, the input to the ADC and the output from the DAC on your oscilloscope. One
signal on CH1 and one signal on CH2. Use the MATH button on your scope and measure the
difference between the signals. Analyze the error of the DAC conversion result versus the input
value. Comment on the magnitude of the error and the sources of error.
4. Average your ADC measurements and plot the average value as a DC measurement to your 7-
segments display. Then, change the signal's duty cycle and observe the average value (explain).
To average your measurements, you can keep an overall sum and a counter for the samples, but
keep in mind that the counter overruns, so you will need to start over after the counter overruns
by using the overall average to start with.
5. What is the maximum frequency of operation for your design such that your waveforms from
the output of the DAC0 correctly represent the input signal?
6. Can you measure the conversion time from the triggering of the ADC to the DAC output result?
Explain how we can do that and show the setup.

LAB 5 Implementation Notes


• The ADC0 interrupt number is ADC0_IRQn = 57 (See line 231 in the MK60D10.h file). This
interrupt is a higher value than the PIT interrupt I use for the 7-segments cycle scan, so I don’t
need to disable interrupts when I am in the ADC0 interrupt handler. To enable the interrupt for
the ADC0 module, we need to set bit 23 in NVIC_ISER1 register.
• The programmable delay block PDB0 has its interrupt number: PDB0_IRQn = 72. This
interrupt can be activated in assembly by setting bit 9 in the NVIC_ISER2 register (address:
0xE000E108).
• I am using ADC0_SE10 (PTA7) in my Lab5 implementation, and this signal comes from the
B40 pin of the primary connector. Therefore, the PORTA7 register in the PORT module should
be set to 0x00000000 to select the analog pin option (default setting, or ALT0).
• It is important to write the PDB0_SC register with LDOK set to 1 after you have set up all other
registers; otherwise, when you debug, you will not see the content of the control registers.

P a g e | 64 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
• Programming the PTB0 to be used as a hardware trigger for the ADC0 module requires the
following steps (note that you need to check the K60 SFRM register configurations to program
all required features; below, I am just highlighting some):
o Enable the clocks for PDB0 and ADC0 in SIM_SCGC6.
o Make sure that SIM_SOPT7 is set to 0x0, which is the reset value, to have PDB selected
as the hardware trigger for the ADC0 module.
o Set the analog pin input option for your PORTx pin; in my case was PORTA_PCR7.
o Set the PDB0_MOD, PDB0_IDLY, PDB0_CH0C1 (select and enable the CH0 pre-
trigger output), PDB0_POEN (enable output 0), PDB0_PO0DLY (sets the pulse duty
cycle), and PDB0_SC (at this stage, the interrupts should be disabled and LDOK set to 1
among other settings).
o Before the PDB0 starts, you must write the SETRG bit in the PDB0_SC register.
• Programming the ADC0 to be used as hardware triggered continuous mode ADC module:
o Follow the initialization algorithm presented in this lab.
o For ADC calibration, you can use the calibration routine from the example project, or if
you work in ARM assembly, you can use my calibration routine given in the Appendix.
However, the ADC works OK without the calibration routine; but it is advisable to do it
before starting the DAC0 for measurements.
o Program accordingly the following registers: ADC0_CFG1 (set the 16-bit conversion
single-ended, etc.), ADC0_CFG2 (I left it to 0x0), ADC0_SC1A (set the enable
interrupt bit; I used AD10, which is connected to the ADC0_SE10 that is Pin B40 on the
primary connector), ADC0_SC2 (set the hardware trigger bit), ADC0_SC3 (set
continuous conversion bit).
• Programming the DAC0 to perform software triggered digital to analog conversion.
o It is important to program first SIM_ SCGC2 to enable the clock gate for DAC0.
o Next, you can clear the DAC0_DATxL and DAC0_DATxH data registers.
o Then clear the DAC0_SR register (status flags) and C0, C1, and C2 registers. C2
actually should be loaded with 0x0F.
o You can start the DAC to work by setting the software trigger option. For example, you
can activate the software trigger at the beginning and set all your bit values for the
DAC0_C0 register, and then you only change the DAC0_DAT0L and DAC0_DAT0H
with the new values to be converted. You can do this in the ADC0 service routine; keep
in mind that the ADC returns a 16-bit value if you programmed it this way, and the
DAC needs only a 12-bit input.
• Trigger the PDB0 timer by writing the PDB0_SC register with the SWTRG bit set (software
trigger).
• Activate the ADC0_IRQ57 interrupt in the NVIC_ISER1 register. In the ADC0 interrupt
routine, you can read the result register, perform the binary to BCD conversion, and store the
result in a memory location, which the 7-segments display routine can access. Also, in the ADC
interrupt routine, you can perform the averaging of the measurements and activate the DAC0
for the Requirement 3 section. As it stands, you will have two active interrupts: the PIT and the
ADC. The PIT interrupt is used for cycling the 7-segments digits and ADC for the analog to
digital conversion.

P a g e | 65 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
PDB0 Note. It is essential to understand how the PDB0 can be used to work as a hardware trigger for
the ADC0 module. For this, please read Section 39.4 Functional Description in K60SFRM. If you look
at Figure 39-56 (printed here below as well), you can see that the "Ch n trigger," which is connected to
the ADC module as a trigger signal, is generated as a logical OR of the "Ch n pre-trigger 0 to M"
signals. Therefore, in order to have your signal valid to be used as a signal trigger for ADC0, you need
to have at least one "Ch n pre-trigger" signal (I used "Ch 0 pre-trigger 0"). When this signal is active,
the ADC0 module will use the ADC0_SC1A configuration settings to perform the conversion.

The ADC module has a selectable asynchronous hardware conversion trigger, ADHWT, that is enabled
when SC2[ADTRG] is set and a hardware trigger select event, ADHWTSn (in our case would be Ch 0
trigger), has occurred. However, you must start the PDB0 trigger before receiving the ADHWT signal.
The channel and status fields selected for conversion depend on the active trigger select signal:
ADHWTSA active selects SC1A, and ADHWTSn active selects SC1n.

P a g e | 66 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Figure below shows my setup for the DC voltmeter as per Requirement 2.

P a g e | 67 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

Figure below shows my setup for the working DC voltmeter as per Requirement 3.

Note: My implementation works decently and reproduces the input signal from the function generator
up to 1.7 kHz. However, you can see the delay of the conversion input >> ADC0 >> DAC0 when you
perform the MATH function on the oscilloscope. Also, you can see that the edges of my output signal
when a square waveform is inputted are not that good.

LAB 5 DEMO AND REPORT


Demo all your implementation to the TA. Be ready to answer any theoretical and practical questions
related to the implementation. Prepare a good report that presents all the programs you experimented
with in this lab. Answer all the questions from all Lab 5 requirements. Have clear Sections dealing with
the lab requirements. Explain the implementations and back your implementations with screenshots
showing results and calculations for your timers etc.
Specifically, for this report, you should clearly present how you programmed your PDB, ADC, and
DAC modules. Then, analyze the results, explain the lab application in detail, and justify your choices.
Finally, show photos with your 7-segments display and circuit on your breadboard and show
screenshots from your oscilloscope.

P a g e | 68 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

LAB 6/PROJECT: INTERFACING THE K60 MICROCONTROLLER – MOTOR


INTERFACING USING THE FTM MODULE … WEEKS 11, 12
OBJECTIVE
In this lab/project, you will gain further experience with the TWR-K60D100M board and the K60
microcontroller. Specifically, you will experiment with the Flex Timer Module (FTM) described in
Chapter 40 of the K60 SFRM and control a DC motor.
FlexTimer Module (FTM)
The FlexTimer module is a two-to-eight channel timer that supports input capture, output compare, and
the generation of PWM signals to control electric motor and power management applications. The
FlexTimer is built upon a simple timer, the HCS08 Timer PWM Module – TPM, which was used for
many years by Freescale. See Chapter 40 of K60SFRM for FTM module functionality and
programming.
Inter-Integrated Circuit (I2C) – See Chapter 51 pp 1453 to 1484
The inter-integrated circuit (I2C, I2C, or IIC) module provides a method of communication between
various devices. The interface is designated to operate up to 100 kbits/s with maximum bus loading and
timing. The block diagram of the module is presented below. Please study the functionality of this
module for the project application in this lab.

Figure 2: I2C Functional Block Diagram

P a g e | 69 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
LAB6/PROJECT REQUIREMENTS

REQUIREMENT 1 — Interfaces Demo


1. Run the demo project ftm_example.uvprojx found in the directory:
C:\Freescale\KSDK_1.2.0\examples\twrk60d100m\driver_examples\ftm\
2. Understand the FTM functionality for PWM modulation.
3. Build on your breadboard the DC motor control circuit. This control circuit should be a simple
circuit using either the 2N222 BJT transistor or the IRF9540 MOSFET transistor and a simple
1N914 diode. See the lecture notes for more on DC motor circuit control diagrams. Your lab kit
should contain all the components you need for this Lab. The figure below shows a simple
diagram I used in my lab design. In my design, I used the PWM0 signal connected to the A40
pin on the primary connector. The usage of the PWM0 signal is FTM0_CH0 or PTC1 (See the
Schematic diagram of the board). Note that the FTM0_CH0 output can be multiplexed to PTA3
and PTC1, but only PTC1 is connected to the primary connector on the board.

REQUIREMENT 2 — Implementation
1. Design a system that uses the FTM module to control a 5V DC motor. The implementation
should use a 5V DC motor and one FTM channel to control the DC motor circuit. The PWM
signal's duty cycle should be controlled through your PuTTY by entering a percentage value
such as 20%, etc., or you could use the potentiometer from the TWR-K60D100M board to
control the duty cycle. The potentiometer line is connected to the ADC1_DM1 signal that is
connected to the K2 pin of the microcontroller. As you change the percentage value or the
potentiometer, you should be able to observe the DC motor speed changing. Also, the PWM
signal should be plotted on the oscilloscope to show how the signal changes dynamically. For
the programming of the FTM channel, you can use the example presented in the Appendix of
this lab (See also ENGG3640 lecture notes).
2. Verify the DC motor circuit you have built, show the circuit to the TA before you power it, and
then connect it to the controller.
3. Plot on the scope the PWM signals controlling your motor and demonstrate the changing of the
duty cycle signal as you enter different values from the PuTTY
REQUIREMENT 3 — Demo and Report
Demo all your implementation to the TA. Be ready to answer any theoretical and practical questions
related to the implementation. Prepare a good report that presents all the programs you experimented
with in this lab. Explain the implementations and back your implementations with screenshots showing
results and calculations for your timers etc.
Specifically, for this report, you should clearly present how you programmed your FTM module. Then,
analyze the results, explain the lab application in detail, and justify your design.

P a g e | 70 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY
LAB6 APPENDIX
1. The TWR-K60D100M base board has a 3-D accelerometer chip on the board (MM8451Q, See
the schematic file page 7 of the TWR-K60D100M.pdf file (provided on Courselink system).
The link to the accelerometer documentation is:
http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8451Q.
The MM8451Q documentation is also presented in the ENGG3640 Courselink system.
2. The accelerometer is not used in this Lab, the link above is given for your information.

If you write your Lab code in ARM assembly, your main.c could be just a few lines as shown below:

int main(void)
{
// Init hardware.
CLOCK_SYS_EnablePortClock(PORTA_IDX);
CLOCK_SYS_EnablePortClock(PORTD_IDX);
CLOCK_SYS_EnablePortClock(PORTE_IDX);
/* Init board clock */
BOARD_ClockInit();
dbg_uart_init();
//call asmmain where we initialize lptmr
asmmain();
}

Note: For more information about programming the FTM and controlling a DC motor please follow the
examples presented in the ENGG*3640 lectures (Lectures Weeks 9-10).

P a g e | 71 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022


ENGG3640 LAB MANUAL– MICROCOMPUTER INTERFACING DO NOT COPY

APPENDIX
Lab Report Organization Example

The following is a general outline that you can use for your lab reports. However, this outline is not
mandatory; you can enhance it or tailor it to suit your goals.

1. Introduction
1.1 Lab description (problem definition)
1.2 System requirement (tools, equipment etc.)

2. Background
2.1 Short overview of the background related to the programming tools and/or the components and
devices used (depending on the lab). For example, if you use the oscilloscope, you should talk a bit
about it, what you use it for, etc. Same, if you use, for example, a transistor such as a 2N2222 or 7-
segments display, you should briefly discuss their characteristics.

3. Implementation
3.1 Present a top-down functional implementation of the lab.
3.1.1 Software implementation.
3.1.2 Hardware implementation.
3.2 Present specific implementation details answering the Lab Requirements. Highlight all the Lab
Requirement points. You should have them as subsections:
3.2.1 Lab Requirement 1
3.2.2 Lab Requirement 2, etc.
3.3 Simulation results – show any simulation results and oscilloscope graphs and display plots from
your experiments.

4. Conclusions – briefly describe your conclusions highlighting the challenges and the achievements of
your Lab experiments.

P a g e | 72 ENGG3640 LAB MANUAL – F22 VERSION, Updated August 2022

You might also like