The STM32F103 Arm Microcontroller and Embedded Systems Using Assembly and C First Edition Muhammad Ali Mazidi Sepehr Naimi Sarmad Naimi Naimi & Mazidi books Copyright © 2019-2021 Arm, Cortex, Keil, and uVision are registered trade mark of Arm Limited. Copyright © 2019-2021 Sepehr Naimi, Muhammad Ali Mazidi, and Sarmad Naimi No part of this book should be reproduced, stored in retrieval system, transmitted in any form or by any means, electronics, mechanical, photocopy, recording, web distribution, or likewise. To contact authors, use the following email addresses: Sepehr.com mazidibooks@gmail.com Visit our website at https://NicerLand.com ISBN-13: 978-1-970054-01-9 ISBN-10: 1-970054-01-8 2 "Regard man as a mine rich in gems of inestimable value. Education can, alone, cause it to reveal its treasures, and enable mankind to benefit therefrom." Baha'u'llah 3 Dedication To the faculty, staff, and students of BIHE university for their dedication and steadfastness.
4 Table of Contents Preface 10 Chapter 1: The History of Arm and Microcontrollers 11 Section 1.1: Introduction to Microcontrollers 11 Section 1.2: The Arm Family History 14 Section 1.3: STM32 Family 21 Problems 23 Answers to Review Questions 24 Chapter 2: Arm Architecture and Assembly Language Programming 25 Section 2.1: The General Purpose Registers in the Arm 25 Section 2.2: The Arm Memory Map 30 Section 2.3: Load and Store Instructions in Arm 33 Section 2.4: Arm CPSR (Current Program Status Register) 40 Section 2.5: Arm Data Formats and Assembler Directives 44 Section 2-6: Assembler data allocation directives 49 Section 2.7: Introduction to Arm Assembly Programming 55 Section 2.8: Creating an Arm Assembly Program 57 Section 2.9: The Program Counter and Program Memory Space in the Arm 60 Section 2.10: Some Arm Addressing Modes 62 Section 2.11: Pipelining and Harvard Architecture in Arm 64 Section 2.12: RISC Architecture in Arm 66 Problems 69 Answers to Review Questions 72 Chapter 3: Arithmetic and Logic Instructions and Programs 76 Section 3.1: Arithmetic Instructions 76 Section 3.2: Logic Instructions 87 Section 3.3: Shift and Rotate Instructions 91 Section 3.4: Rotate and Shift in Data Processing Instructions (Case Study) 95 Section 3.5: BCD and ASCII Conversion 97 Problems 100 Answers to Review Questions 102 5 Chapter 4: Branch, Call, and Looping in Arm 104 Section 4.1: Looping and Branch Instructions 104 Section 4.2: Calling Subroutine with BL 120 Section 4.3: Time Delay 125 Section 4.4: Stack in Arm Cortex 129 Section 4-5: Exploring the Startup File 134 Problems 136 Answers to Review Questions 139 Chapter 5: Signed Integer Numbers Arithmetic 141 Section 5.1: Signed Numbers Concept 141 Section 5.2: Signed Number Instructions and Operations 146 Section 5.3: Signed Number Comparison 150 Section 5-4: Sign Extension 152 Problems 156 Answers to Review Questions 157 Chapter 6: Arm Addressing Modes 158 Section 6.1: Arm Memory Access 158 Section 6.2: Advanced Indexed Addressing Modes 171 Section 6.3: ADR, LDR, and PC Relative Addressing 180 Section 6.4: Arm Bit-Addressable Memory Region 182 Problems 187 Answers to Review Questions 189 Chapter 7: C for Embedded Systems 191 Section 7.1: C Data types for Embedded systems 191 Section 7.2: Bit-wise Operations in C 197 Problems 202 Answer to Review Questions 203 Chapter 8: STM32F103 I/O Programming 204 Section 8.1: I/O Port Programming in STM32F1xx 204 Section 8.2: Seven-segment LED interfacing and programming 216 Section 8.3: Clock sources, Reset, and Power Supply Pins in STM32F10x 220 Section 8.4: GPIO Programming in Assembly Language 224 6 Problems 230 Answer to Review Questions 231 Chapter 9: LCD and Keyboard Interfacing 232 Section 9.1: Interfacing to an LCD 232 Section 9.2: Interfacing the Keyboard to the CPU 246 Problems 252 Answers to Review Questions 253 Chapter 10: UART Serial Port Programming 255 Section 10.1: Basics of Serial Communication 255 Section 10.2: Programming UART Ports 263 Problems 277 Answer to Review Questions 277 Chapter 11: STM32 ARM Timer Programming 279 Section 11.0: Introduction to counters and timers 279 Section 11.1: System Tick Timer 281 Section 11.2: Delay Generation with STM32 Timers 287 Section 11.3: Output Compare and TIM Channels 295 Section 11.4: Using Timer for Input Capturing 304 Section 11.5: Using Timer as a Counter 310 Problems 314 Answers to Review Questions 315 Chapter 12: Interrupt and Exception Programming 317 Section 12.1: Interrupts and Exceptions in ARM Cortex-M 317 Section 12.2: SysTick Programming and Interrupt 325 Section 12.3: STM32 I/O Port Interrupt Programming 327 Section 12.4: USART Serial Port Interrupt Programming 334 Section 12.5: Timer Interrupt Programming 337 Section 12.6: Interrupt Priority, nested interrupts, and latency 339 Section 12.7: ARM Cortex-M Processor Modes 343 Problems 346 Answer to Review Questions 347 Chapter 13: ADC, DAC, and Sensor Interfacing 349 7 Section 13.1: ADC Characteristics 349 Section 13.2: ADC Programming with STM32F1xx 354 Section 13.3: Sensor Interfacing and Signal Conditioning 364 Section 13.4: DAC Programming 368 Problems 371 Answers to Review Questions 372 Chapter 14: Relay, Optoisolator, and Stepper Motor Interfacing 374 Section 14.1: Relays and Optoisolators 374 Section 14.2: Stepper Motor Interfacing 380 Problems 387 Answers to Review Questions 388 Chapter 15: PWM and DC Motor Control 389 Section 15.1: DC Motor Interfacing and PWM 389 Section 15.2: Programming PWM in STM32 397 Section 15.3: DC Motor Control Using PWM 408 Problems 410 Answers to Review Questions 411 Chapter 16: I2C Protocol and RTC Interfacing 412 Section 16.1: I2C Bus Protocol 412 Section 16.2: I2C Programming in STM32F10x 420 Section 16.3: DS3231 RTC Interfacing and Programming 430 Problems 438 Answers to Review Questions 439 Chapter 17: SPI Protocol and Devices 440 Section 17.1: SPI Bus Protocol 440 Section 17.2: SPI programming in STM32 443 Section 17.3: MAX7219/MAX7221 SPI 7-Segment Driver 450 Problems 458 Answers to Review Questions 459 Chapter 18: Programming Graphic LCD 460 Section 18.1: Graphic LCDs 460 Section 18.2: Displaying Texts on Graphic LCDs 465 8 Problems 472 Answers to Review Questions 473 Chapter 19: Direct Memory Access (DMA) 474 Section 19.1: Introduction to DMA 474 Section 19-2: DMA in STM32F10x 476 Problems 484 Answers to Review Questions 485 Appendix A: ARM Cortex-M3 Instruction Description 486 Section A.1: List of ARM Cortex-M3 Instructions 486 Section A.2: ARM Instruction Description 489 Appendix B: ARM Assembler Directives 523 Section B.1: List of ARM Assembler Directives 523 Section B.2: Description of ARM Assembler Directives 523 Appendix C: Macros 528 What is a macro and how is it used? 528 Macros vs. subroutines 531 Appendix D: Passing Arguments into Functions 532 D.1: Passing arguments through registers 532 D.2: Passing through memory using references 532 D.3: Passing arguments through stack 533 D.4: AAPCS (ARM Application Procedure Call Standard) 534 Appendix E: ASCII Codes 536 Appendix F: Advanced C Programming 537 Section F.1: Preprocessor Directives 537 Section F.2: Manipulating Registers Using Defined Bit Names 540 Appendix G: Flowcharts and Pseudo-code (Web) Appendix H: IC Interfacing and System Design Issues (Web) See the following website to download the chapters which are labeled as “(Web)”: https://NicerLand.com 9 Preface The Arm processor is becoming the dominant CPU architecture in the computer industry. It is already the leading architecture in cell phones and tablet computers. With such a large number of companies producing Arm chips, it is certain that the architecture will move to the laptop, desktop and high-performance computers presently dominated by x86 architecture from Intel and AMD.
Currently the PIC and AVR microcontrollers dominate the 8-bit microcontroller market. The Arm architecture will have a major impact in this area too as designers become more familiar with its architecture. This book is intended as an introduction to STM32F103 Arm programming. To write programs for Arm microcontrollers, you need to know both Assembly and C languages.
Chapters 2 to 6 cover the Arm Assembly language. However, Chapter 6 contains more advanced topics and you can skip it if you like. Some general topics about embedded C programming are covered in Chapter 7. Then, the peripheral programming of the STM32F103 chip is discussed in Chapters 7 to 19.
You will learn interfacing to some real-world devices such as LCD, Keypad, Motor, 7-segment, relay, and sensors, as well. Prerequisites We assume no prior background in assembly language programming with other CPUs. But a basic knowledge of C programming is required. We also urge you to study Chapter 0 covering the fundamentals of digital systems such as hexadecimal numbers, various types of memory, memory and I/O interfacing, bus designing, and memory address decoding.
Chapter 0 is available free of charge on our website (https://NicerLand. Trainer board This book covers the STM32F103 microcontrollers and you can use any trainer boards with STM32F10x chips. But the Blue Pill board can be a very good choice. Since they are low priced and widely available around the world.
The pins are labeled on the board, as well. Keil tutorials We have used the Keil Compiler for the programs throughout this book. See our website (www.com) for the Keil step-by-step tutorial. You can freely download the Keil IDE and use it for programs which are less than 32KB.
Power Point, Source codes, and other materials The source codes, lab manuals, and Power points of the book are available on the website (https://NicerLand. If you are a professor using this book for a university course you can contact us to receive the solutions to the end-of-chapter problems. 10 Chapter 1: The History of Arm and Microcontrollers In Section 1.1 we look at the history of microcontrollers then we introduce some of the available microcontrollers. The history of Arm is provided in Section 1.1: Introduction to Microcontrollers The evolution of Microprocessors and Microcontrollers In early computers, CPUs were designed using a number of vacuum tubes.
The vacuum tube was bulky and consumed a lot of electricity. The invention of transistors, followed by the IC (Integrated Circuit), provided the means to put a CPU on printed circuit boards. The advances in IC technology allowed putting the entire CPU on a single IC chip. This IC was called a microprocessor.
Some of the microprocessors are the x86 family of Intel used widely in desktop computers, and the 68000 of Motorola. The microprocessors do not contain RAM, ROM, or I/O peripherals. As a result, they must be connected externally to RAM, ROM and I/O, as shown in Figure 1-1. Figure 1-1: A Computer Made by General Purpose Microprocessor In the next step, the different parts of a system, including CPU, RAM, ROM, and I/Os, were put together on a single IC chip and it was called microcontroller.
MCU (Micro Controller Unit) is another name used to refer to microcontrollers. Figure 1-2 shows the simplified view of the internal parts of microcontrollers. Figure 1-2: Simplified View of the Internal Parts of Microcontrollers (SOC) Since the microcontrollers are cheap and small, they are widely used in many devices. 11 Types of Computers Typically, computers are categorized into 3 groups: desktop computers, servers, and embedded systems.
Desktop computers, including PCs, tablets, and laptops, are general purpose computers. They can be used to play games, read and edit articles, and do any other task just by running the proper application programs. The desktop computers use microprocessors. In contrast, embedded systems are special-purpose computers.
In embedded system devices, the software application and hardware are embedded together and are designed to do a specific task. For example, digital camera, vacuum cleaner, mp3 player, mouse, keyboard, and printer, are some examples of embedded systems. It is interesting to note that embedded systems are the largest class of computers though they are not normally considered as computers by the general public. In most cases embedded systems run a fixed program and contain a microcontroller.
But sometimes microcontrollers are inadequate for a task. For this reason, sometimes general-purpose microprocessors are used to design embedded systems. In recent years many manufacturers of general-purpose microprocessors such as Intel, NXP (formerly Motorola), and AMD (Advanced Micro Devices, Inc.) have targeted their microprocessors for the high end of the embedded market. Currently, because of Linux and Windows standardization, in these embedded systems Linux and Windows operating systems are widely used.
In many cases, using the operating systems shortens development time because a vast library of software already exists for the Linux and Windows platforms.