ARM
MICROCONTROLLER PROJECTS
Dogan Ibrahim
It is becoming important for microcontroller users to quickly learn and adapt to new technologies and architecture used in high performance 32-bit microcontrollers. Many manufacturers now offer 32-bit microcontrollers as general purpose processors in embedded applications. Prof Dr Dogan Ibrahim is a Fellow of the Institution of Electrical Engineers. He is the author of over 60 technical books, published by international famous publishers, such as Wiley, Butterworth, and Newnes. In addition, he is the author of over 250 technical papers, published in journals, and presented in seminars and conferences.
ISBN 978-1-907920-48-6
The architecture of the highly popular ARM Cortex-M processor STM32F107VCT6 is described at a high level, taking into consideration its clock mechanisms, general input/output ports, interrupt sources, ADC and DAC converters, timer facilities, and more. The information provided here should act as a basis for most readers to start using and programming the STM32F107VCT6 microcontroller together with a development kit. Furthermore, the use of the mikroC Pro for ARM integrated development environment (IDE) has been described in detail. This IDE includes everything required to create a project; namely an editor, compiler, simulator, debugger, and device programmer. Although the book is based on the STM32F107VCT6 microcontroller, readers should not find it difficult to follow the projects using other ARM processor family members.
DESIGN
www.elektor.com
This book makes use of the ARM Cortex-M family of processors in easy-to-follow, practical projects. It gives a detailed introduction to the architecture of the Cortex-M family. Examples of popular hardware and software development kits are described.
BEGINNER TO INTERMEDIATE
ARM
MICROCONTROLLER PROJECTS
LEARN
Elektor International Media BV
ARM provide 32 and 64-bit processors mainly for embedded applications. These days, the majority of mobile devices including mobile phones, tablets, and GPS receivers are based on ARM technology. The low cost, low power consumption, and high performance of ARM processors makes them ideal for use in complex communication and mixed signal applications.
ARM MICROCONTROLLER PROJECTS ● DOGAN IBRAHIM
BEGINNER TO INTERMEDIATE
Dogan Ibrahim LEARN
DESIGN
SHARE
SHARE
LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR
Table of Contents
Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Chapter 1 Microcomputer systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.2 Microcontroller Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.2.1 RAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.2.2 ROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.2.3 PROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.2.4 EPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.2.5 EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.2.6 Flash Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.3 Microcontroller Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.3.1 Supply Voltage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.3.2 The Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.3.3 Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.3.4 Watchdog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.3.5 Reset Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.3.6 Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.3.7 Brown-out Detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.3.8 Analog-to-digital Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.3.9 Sample and Hold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.3.10 RS232 Serial Input-Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.3.11 SPI and I2C Busses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.3.12 EEPROM Data Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.3.13 LCD Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.3.14 Analog Comparators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.3.15 Real-time Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.3.16 Sleep Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.3.17 Power-on Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.3.18 Low Power Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.3.19 Current Sink/Source Capability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
●7
ARM Microcontroller Projects: Beginner to Intermediate 1.3.20 Input/output (I/O) Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.3.21 USB Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.3.22 CAN Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.3.23 Ethernet Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.3.24 ZigBee Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.3.25 Multiply and Divide Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.3.26 Operating Temperature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.27 Pulse Width Modulated (PWM) Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.28 In-circuit Serial Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.29 Digital-to-analog Converter (DAC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.30 Debug Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.31 Package Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.32 DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.3.33 Temperature Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.4 Microcontroller Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.4.1 RISC and CISC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.5 8, 16, or 32 Bits ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Chapter 2 Why ARM? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.1 ARM Processors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.1.1 Cortex-M . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.1.2 Cortex-R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.1.3 Cortex-A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.2 Cortex-M Processor Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.3 Processor Performance Measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2.4 Cortex-M Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Chapter 3 Architecture of the STM32F107VCT6 ARM Microcontroller . . . . . . . . . . . . . 37 3.1 The STM32 Family of ARM Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 3.2 The STM32F107VCT6 Microcontroller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 3.2.1 Basic Features of the STM32F107VCT6 . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
●8
Table of Contents 3.2.2 Internal Block Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.2.3 The Power Supply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.2.4 Low Power Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 3.2.5 The Clock Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.2.6 General Purpose Inputs and Outputs (GPIOs) . . . . . . . . . . . . . . . . . . . . . . 48 3.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Chapter 4 Microcontroller Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4.1 ARM Hardware Development Kits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4.1.1 EasyMx Pro V7 for STM32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 4.1.2 Clicker 2 for STM32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.1.3 EasyMx Pro V7 for Tiva C Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.1.4 MCB1000 Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.1.5 MCBSTM32F200 development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.1.6 ARM7 Development Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.2 ARM Software Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4.2.1 mikroC Pro for ARM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.2.2 ARM DS-5 Development Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.2.3 ARM Compilation Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.2.4 Green Hills ARM Software Development Tools . . . . . . . . . . . . . . . . . . . . . . 59 4.2.5 MDK-ARM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.2.6 CrossWorks for ARM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.2.7 IAR Embedded Workbench for ARM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.2.8 JumpStart Software Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Chapter 5 Programming ARM Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 5.1 mikroC STM32F107VCT6 Microcontroller Specific Features . . . . . . . . . . . . . . . . . 63 5.2 The General Purpose Input-Output (GPIO) Library . . . . . . . . . . . . . . . . . . . . . . . 64 5.2.1 GPIO_Clk_Enable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 5.2.2 GPIO_Clk_Disable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 5.2.3 GPIO_Config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 5.2.4 GPIO_Set_Pin_Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 5.2.5 GPIO_Digital_Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.2.6 GPIO_Digital_Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
●9
ARM Microcontroller Projects: Beginner to Intermediate 5.2.7 GPIO_Analog_Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 5.2.8 GPIO_Alternate_Function_Enable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.3 Memory Type Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.4 PORT Input-Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.5 Accessing Individual Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 5.6 bit Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 5.7 Interrupts and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 5.7.1 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 5.7.2 Interrupt Service Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.8 Creating a New Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 5.9 Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 5.9.1 Setting Break Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.10 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 5.11 Other mikroC IDE Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 5.11.1 ASCII Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 5.11.2 GLCD Bitmap Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5.11.3 HID Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5.11.4 Interrupt Assistant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5.11.5 LCD Custom Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 5.11.6 Seven Segment Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 5.11.7 UDP Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 5.11.8 USART Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 5.11.9 USB HID Bootloader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 5.11.10 Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 5.11.11 The Library Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 5.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Chapter 6 Microcontroller Program Development . . . . . . . . . . . . . . . . . . . . . . . . . . 89 6.1 Using the Program Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 6.1.1 BEGIN – END . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 6.1.2 Sequencing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 6.1.3 IF – THEN – ELSE – ENDIF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 6.1.4 DO – ENDDO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
● 10
Table of Contents 6.1.5 REPEAT – UNTIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 6.1.6 Calling Subprograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 6.1.7 Subprogram Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 6.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 6.3 Representing for Loops in Flow Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 6.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 6.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Chapter 7 The EasyMx PRO v7 for STM32 Development Board . . . . . . . . . . . . . . . . 107 7.1 The Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 7.2 The Power Supply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 7.3 The CPU Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 7.4 On-board Programmer and Hardware Debugger . . . . . . . . . . . . . . . . . . . . . . . 109 7.5 The LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 7.6 mikroBUS Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 7.7 USB-UART Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7.8 USB Host Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7.9 USB Device Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7.10 Ethernet Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7.11 Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7.12 Audio I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 7.13 microSD card Slot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.14 320x240 Pixel TFT Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.15 Touch Panel Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.16 128x64 Pixel GLCD Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.17 Navigation Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.18 DS1820 Digital Temperature Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.19 LM35 Analog Temperature Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 7.20 Serial Flash Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 7.21 EEPROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 7.22 Piezo Buzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 7.23 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
● 11
ARM Microcontroller Projects: Beginner to Intermediate Chapter 8 Beginner ARM Microcontroller Projects . . . . . . . . . . . . . . . . . . . . . . . . . 115 8.1 PROJECT 1 – Flashing LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 8.2 PROJECT 2 – Complex Flashing LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 8.3 8.3 PROJECT 3 – Chasing LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 8.4 PROJECT 4 – Binary Counting LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 8.5 PROJECT 5 – Random Flashing LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 8.6 PROJECT 6 – Push-Button Switch With LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . 124 8.7 PROJECT 7 – Event Counter With LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 8.8 PROJECT 8 – Quiz Game Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 8.9 PROJECT 9 – Generating the SOS Morse Code . . . . . . . . . . . . . . . . . . . . . . . . . 131 8.10 PROJECT 10 – Generating Melody Using a Piezo Buzzer . . . . . . . . . . . . . . . . . 134 8.11 PROJECT 11 – Electronic Organ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 8.12 PROJECT 12 – Displaying Text on an LCD Display . . . . . . . . . . . . . . . . . . . . . 138 8.12.1 HD44780 Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 8.13 PROJECT 13 - Event Counter With LCD Display . . . . . . . . . . . . . . . . . . . . . . . 143 8.14 PROJECT 14 - LCD Font Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Chapter 9 Elementary ARM Microcontroller Projects . . . . . . . . . . . . . . . . . . . . . . . 149 9.1 PROJECT 1 – Voltmeter With LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 9.2 PROJECT 2 – Analog Temperature Measurement . . . . . . . . . . . . . . . . . . . . . . . 156 9.3 PROJECT 3 – Dice With LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 9.4 PROJECT 4 – 7-Seg Click Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 9.5 PROJECT 5 – Temperature and Humidity Measurement . . . . . . . . . . . . . . . . . . 171 9.6 PROJECT 6 – Simple Calculator With Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . 178 9.7 PROJECT 7 – DAC Converter Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 9.7.1 PROJECT 8 – Generating Square Waveform . . . . . . . . . . . . . . . . . . . . . . . 184 9.7.2 PROJECT 9 – Generating Sawtooth Waveform . . . . . . . . . . . . . . . . . . . . . 187 9.7.3 PROJECT 10 – Generating Sine wave . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Chapter 10 Intermediate ARM Microcontroller Projects . . . . . . . . . . . . . . . . . . . . . 191 10.1 PROJECT 1 – Event Counter Using An External Interrupt . . . . . . . . . . . . . . . . 191 10.2 PROJECT 2 – Car Park Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 10.3 PROJECT 3 – Pulse Width Modulation (PWM) Project . . . . . . . . . . . . . . . . . . . 200 10.4 PROJECT 4 – Controlling LED Brightness with PWM . . . . . . . . . . . . . . . . . . . . 204
● 12
Table of Contents 10.5 PROJECT 5 - TFT Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 10.6 PROJECT 6 – Displaying Temperature on TFT Display . . . . . . . . . . . . . . . . . . . 214 10.7 PROJECT 7 - Timer Interrupts - Chronograph . . . . . . . . . . . . . . . . . . . . . . . . 217 Appendix A Programming Listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 A.1 Flashing LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 A.2 Flashing LED (LED10.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 A.3 Complex Flashing LED (LEDCPLX.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 A.4 Chasing LEDs (LEDCHASE.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 A.5 Binary Counting LEDs (LEDCNT.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 A.6 Random Flashing LEDs (LEDRAN.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 A.7 Push-Button Switch With LEDs (SWITCH.c) . . . . . . . . . . . . . . . . . . . . . . . . . . 229 A.8 Event Counter With LEDs (EVENTLED.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 A.9 Event Counter With LEDs - Modified Listing (EVENTLED2.c) . . . . . . . . . . . . . . . 231 A.10 Quiz Game Controller - PDL Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 A.11 Quiz Game Controller (QUIZ.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 A.12 Quiz Game Controller (QUIZ2.c) - Modified Listing . . . . . . . . . . . . . . . . . . . . . 235 A.13 SOS Morse Code (SOS.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 A.14 Generating Melody Using a Piezo Buzzer (Melody.c) . . . . . . . . . . . . . . . . . . . . 239 A.15 Electronic Organ (ORGAN.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 A.16 Displaying Text on LCD Display (LCDTXT.c) . . . . . . . . . . . . . . . . . . . . . . . . . . 242 A.17 Event Counter With LCD Display (LCDEVNT.c) . . . . . . . . . . . . . . . . . . . . . . . . 243 A.18 Event Counter With LCD Modified (LCDEVNT2.c) . . . . . . . . . . . . . . . . . . . . . . 245 A.19 LCD Font Generation (FONT.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 A.20 Voltmeter With LCD (VOLTMETER.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 A.21 Analog Temperature Measurement (LM35.c) . . . . . . . . . . . . . . . . . . . . . . . . . 250 A.22 Dice With LCD (DICE.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 A.23 7-Seg Click Board (SEVENSEG.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 A.24 7-Seg Click Board - Modified (SEVENSEG2.c) . . . . . . . . . . . . . . . . . . . . . . . . 257 A.25 Temperature and Humidity Measurement PDL . . . . . . . . . . . . . . . . . . . . . . . . 260 A.26 Temperature and Humidity Measurement (HTU21D.c) . . . . . . . . . . . . . . . . . . 262 A.27 Simple Calculator With Keypad PDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 A.28 Simple Calculator With Keypad (KEYPAD.c) . . . . . . . . . . . . . . . . . . . . . . . . . . 266
● 13
ARM Microcontroller Projects: Beginner to Intermediate A.29 Generating Square Waveform (SQUARE.c) . . . . . . . . . . . . . . . . . . . . . . . . . . 270 A.30 Generating Sawtooth Waveform (SAWTOOTH.c) . . . . . . . . . . . . . . . . . . . . . . 271 A.31 Generating Sine wave (SINE.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 A.32 Event Counter Using An External Interrupt PDL . . . . . . . . . . . . . . . . . . . . . . . 273 A.33 Event Counter Using An External Interrupt (EVNTINT.c) . . . . . . . . . . . . . . . . . 274 A.34 Event Counter Using An External Interrupt (EVNTINT2.c) . . . . . . . . . . . . . . . . 276 A.35 Car Park Controller PDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 A.36 Car Park Controller (CARPARK.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 A.37 Pulse Width Modulation (PWM) - (PWM40.c) . . . . . . . . . . . . . . . . . . . . . . . . . 284 A.38 Controlling LED Brightness with PWM (PWMLED.c) . . . . . . . . . . . . . . . . . . . . . 285 A.39 TFT Displays (TFT1.c) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 A.40 Displaying Temperature on TFT Display PDL . . . . . . . . . . . . . . . . . . . . . . . . . 288 A.41 Displaying Temperature on TFT Display (TFTLM35.c) . . . . . . . . . . . . . . . . . . . 289 A.42 Timer Interrupts - Chronograph PDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 A.43 Timer Interrupts - Chronograph (CHRONO.c) . . . . . . . . . . . . . . . . . . . . . . . . 294 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
● 14
Preface
Preface The Internet of Things (IOT) is becoming a major application area of embedded systems and as a result more people are becoming interested in learning about embedded design and programming. Furthermore, we can see that more technical colleges and universities are moving away from the legacy 8-bit and 16-bit microcontrollers and introducing 32-bit embedded microcontrollers in their curriculums. Some IoT applications demand precision, high processing power, and very low power consumption. It is becoming important for microcontroller users to adapt to new technologies quickly and learn the architecture and use of high performance 32-bit microcontrollers. Several manufacturers offer 32-bit microcontrollers as general purpose processors in embedded applications. For example, Microchip Inc. offers the 32-bit PIC family of microcontrollers and development tools in addition to their highly popular 8-bit and 24-bit family. ARM offers 32-bit and 64-bit processors mainly for the embedded applications. Nowadays, the majority of mobile devices such as mobile phones, tablets, and GPS receivers are based on ARM processors. The low cost, low power consumption, and high performance of ARM processors make them ideal for use in complex communication and mixed signal applications. This book is about the use of the ARM Cortex-M family of processors in practical projects. The book gives a detailed introduction to the architecture of the Cortex-M family. Examples of popular hardware and software development kits are described. Using these kits simplifies the embedded design cycle considerably and makes it easier to develop, debug, and test a project. The architecture of the highly popular STM32F107VCT6 ARM Cortex-M processor is described at a high level by considering its clock mechanisms, general input/output ports, interrupt sources, ADC and DCA converters, timer facilities, and so on. The information given here should be sufficient for most readers to start using and programming the STM32F107VCT6 together with a development kit. Furthermore, the use of the mikroC Pro for ARM integrated development environment (IDE) has been described in detail. This IDE includes everything required to create a project - namely an editor, compiler, simulator, debugger, and a device programmer. Although the book is based on the STM32F107VCT6 microcontroller, readers should find it easy to use any other ARM processor family member. I hope that you will find the book helpful and enjoyable and will be able to create your next embedded project using an ARM Cortex-M microcontroller. Dogan Ibrahim London, 2016
â—? 15
Chapter 1 • Microcomputer systems
Chapter 1 • Microcomputer systems 1.1 • Introduction The term microcomputer is used to describe a system that includes a minimum of a microprocessor, program memory, data memory, and input-output (I/O) module. Some microcomputer systems include additional components such as timers, counters, interrupt processing modules, analog-to-digital converters, serial communication modules, USB modules and so on. Thus, a microcomputer system can be anything from a large system having hard disks, keyboards, monitors, floppy disks, and printers, to a single chip embedded controller. In this book we are going to only consider the type of microcomputers that consist of a single silicon chip. Such microcomputer systems are also called microcontrollers and they are used in many everyday household goods such as personal computers, digital watches, microwave ovens, digital TV sets, TV remote control units, cookers, hi-fi equipment, CD players, personal computers, fridges, etc. There are numerous different types of microcontrollers available on the market, developed and manufactured by many companies. In this book we shall be looking at programming and system design using the highly popular 32-bit ARM family of microcontrollers, manufactured by various semiconductor companies under the license of the Advanced RISC Machines (ARM).
1.2 • Microcontroller Systems A microcontroller is a single chip computer. Micro suggests that the device is small, and controller suggests that the device can be used in control applications. Another term used for microcontrollers is embedded controller, since most of the microcontrollers in industrial, commercial, and domestic applications are built into (or embedded in) the devices they control. A microprocessor differs from a microcontroller in many ways. The main difference is that a microprocessor requires several other external components for its operation as a computer, such as program memory and data memory, input-output module, and external clock module. A microcontroller on the other hand has all these support chips incorporated inside the same chip. In addition, because of the multiple chip concept, microprocessor based systems consume considerably more power than microcontroller based systems. Another advantage of microcontroller based systems is that their overall cost is much less than microprocessor based systems. All microcontrollers (and microprocessors) operate on a set of instructions (or the user program) stored in their program memories. A microcontroller fetches instructions from its program memory one by one, decodes them, and then carries out the required operations. Microcontrollers have traditionally been programmed using the assembly language of the target device. Although the assembly language is fast, it has several disadvantages. An assembly program consists of mnemonics and in general it is difficult to learn and maintain a program written using the assembly language. Also, microcontrollers manufactured by different firms have different assembly languages and the user is required to learn a new language whenever a new microcontroller is to be used. Microcontrollers can also be programmed using high-level languages, such as BASIC, PASCAL, and C. High-level languages have the advantage that they are much easier to learn than assembly languages. Also, very large and complex programs can easily be
● 17
ARM Microcontroller Projects: Beginner to Intermediate developed using high-level languages. In this book we will learn about the programming of 32-bit ARM microcontrollers using the popular C programming language: mikroC Pro for ARM, developed by mikroElektronika (www.mikroe.com). In general, a single chip is all that is required to have a running microcontroller based computer system. In practical applications, additional components may be required to allow a microcomputer to interface its environment. With the advent of the ARM family of microcontrollers, the typical development time of an electronic project has been reduced to several months, weeks, or even hours. Basically, a microcontroller (or a microprocessor) executes a user program which is loaded in its program memory. Under the control of this program, data is received from external devices (inputs), manipulated and then sent to external devices (outputs). For example, in a microcontroller based temperature control system the aim is to keep the temperature of an oven at a given set point. Here, the oven temperature is read by a microcomputer via a temperature sensor device. The program running inside the microcontroller then actuates the heater or fan through power amplifiers or by using relays in an attempt to control the temperature at the required value. As the simplest control algorithm - if the temperature inside the oven is lower than the desired set value, the microcomputer operates the heater to increase the temperature to the set value. If on the other hand the temperature inside the oven is higher than the desired set value, then the fan is operated in an attempt to lower the temperature (this is known as the ON/OFF control algorithm). Figure 1-1 shows the block diagram of a simple oven temperature control system.
Figure 1-1 Microcontroller based oven temperature control system The system shown in Figure 1-1 is a simplified temperature control system. In a more sophisticated system, a keypad may be used to set the desired temperature level, with an LCD to display the current temperature inside the oven. Figure 1-2 on page 19 shows the block diagram of this more sophisticated temperature control system. We can make our design even more sophisticated (see Figure 1-3 on page 20 by adding an audible alarm (e.g. a small buzzer) to inform us if the oven temperature is outside the desired set point. Also, the actual temperature readings at any time can be ● 18
Chapter 2 • Why ARM?
Chapter 2 • Why ARM? There are hundreds of types of microcontrollers manufactured by many companies across the world. Choosing a microcontroller for a particular application depends upon many factors such as the following: •
Cost
•
Speed
•
Power consumption
•
Size
•
Number of digital and analog input-output ports
•
Digital input-output port current capacity
•
Analog port resolution and accuracy
•
Program and data memory sizes
•
Interrupt support
•
Timer support
•
USART support
•
Special bus support (e.g. USB, CAN, SPI, I2C and so on)
•
Ease of system development (e.g. programming)
•
Working voltage
For example, if you need to develop a battery powered device such as a mobile phone or a games device then very high clock speed and long battery life are the main requirements. If you are developing a traffic light controller then very high performance is not a requirement. In general, as the clock speed goes up so does power consumption and as a result, a trade-off should be made in choosing a microcontroller for a specific application. ARM has been designing 32-bit processors for over 20 years and in the last few years they have also started to offer 64-bit designs. In actual fact ARM is a company specialised in designing processor architecture. They do not manufacture or sell processor chips. ARM makes money by licensing their designs to chip manufacturers. Manufacturers use the core ARM processors (e.g. the core CPU) and integrate them with their own peripherals in order to end up with a complete microcontroller chip. ARM is then given royalty fees for each chip manufactured by the third party companies. Companies using ARM core processors include Apple, Atmel, Broadcom, Cypress Semiconductors, Freescale Semiconductors, Analog Devices, Nvidia, NXP, Samsung Electronics, Texas Instruments, Qualcomm, Renesas, and many others. ARM was originally known as the Acorn Computers and they developed the first Acorn RISC Machine (ARM) architecture in the 1980's to use in their personal computers. The first ARM processors were co-processor modules used in the BBC Micro series. After failing to find suitable high performance microprocessor chips on the market, Acorn decided to design their own processors. In 1990, the research section of Acorn formed ARM Ltd. Currently ARM is the world's most widely used processor in terms of manufactured quantity. Over 50 billion ARM processors have been produced as of 2014, where 10 billion were produced in 2013 alone. ARM 32-bit is the most widely used architecture in mobile devices and about 98% of all mobile phones sold in the year 2005 used at least one ARM
● 31
ARM Microcontroller Projects: Beginner to Intermediate processor. ARM architecture is known to offer the smallest CPU size and best MIPS to Watts ratio as well as MIPS to $ ratio. Small size, low power consumption, and low cost make ARM an ideal processor in embedded applications. ARM processors are based on an instruction set called Thumb. With clever design, this instruction set takes 32-bit instructions and compresses them down to 16-bits, thus reducing hardware size and overall cost. The processor makes use of multi-stage pipelined architecture that is easier to learn, build, and program.
2.1 • ARM Processors ARM processors are based on the RISC (Reduced instruction Set Computer) architecture and are available as 32-bit or 64-bit multi-core structures. RISC processors, as opposed to CISC (Complex Instruction Set Computer) processors have a smaller number of instructions and fewer transistors (hence smaller die size) and as a result they can operate at higher speeds. Unimportant and not frequently used instructions are removed hence pathways are optimised resulting in superior performance. It is important to realise that ARM’s core architecture is only a processor and it does not include graphics, input-output ports, USB, serial communication, wireless connectivity, or any other form of peripheral modules. Chip manufacturers build their systems around the ARM core design and this is why different manufacturers offer different types of ARM based microcontrollers. Over the last 20 years or so, ARM has developed many 32-bit processors. Figure 2-1 shows some of the popular members of the ARM processor family. The first successful member was the ARM7TDMI which had high code density and low power consumption. This processor, based on the Von Neumann architecture was operating at 80MHz and was used in early mobile phones. ARM9 was developed in 1997 with Harvard architecture and operated at 150MHz, thus offering higher performance. ARM10 and ARM11 were developed in the years 1999 and 2003 respectively. Both of these processors were based on the Harvard architecture. ARM10 operated at 260MHz and ARM11 at 335MHz. Around the year 2003 ARM decided to increase their market share by developing a new series of high performance processors. As a result, the Cortex family or processors were created. The Cortex family consist of three processor families: The Cortex-M, Cortex-R, and Cortex-A. We shall now take a brief look at these families.
Figure 2-1 Overview of the ARM processor family
● 32
Chapter 2 • Why ARM?
2.1.1 • Cortex-M Cortex-M series are built around the ARMv6-M architecture (Cortex-M0 and Cortex-M0+) and the ARMv7-M architecture (Cortex-M3 and Cortex-M4). These processors are specifically designed for the microcontroller market, offering quick and deterministic interrupt responses, low power consumption, low cost, fairly high performance, and ease of use. Cortex-M3 and Cortex-M4 are very similar in architecture and have the same instruction set (Thumb 2) with the difference that the Cortex-M4 offers digital signal processing (DSP) capability and has an optional floating point unit (FPU). Cortex-M4 with its DSP and floating point capability is an ideal processor for IoT and wearable applications. For cost sensitive and lower performance applications, the Cortex-M0 or the Cortex-M0+ can be used. The Cortex-M0 processor has a small gate count (12K gates) and consumes only 12.5µW/MHz. The Cortex-M0+ consumes only 9.85µW/MHz and is based on a subset of the Thumb 2 instruction set and its performance is slightly above that of Cortex-M0 and below that of the Cortex-M3 and Cortex-M4. Cortex-M7 is a high performance processor capable of handling fast DSP and single or double precision floating point operations. It is mainly used in applications requiring higher performance than what the Cortex-M4 provides.
2.1.2 • Cortex-R Cortex-R series are real-time higher performance processors than the Cortex-M. Some family members are designed to operate at high clock rates in excess of 1GHz. These processors are commonly used in hard-disk controllers, network devices, automotive applications, and in specialised high speed microcontroller applications. The Cortex-R4 and Cortex-R5 are earlier members and can be used at clock speeds of up to 600MHz. Cortex-R7 is a newer member incorporating an 11-stage pipeline for high performance. It can operate in excess of 1GHz. Although Cortex-R processors are high performance, their architecture is complex. They have a high power consumption, making them unsuitable for use in mobile, battery powered devices.
2.1.3 • Cortex-A Cortex-A are the highest performance ARM processors designed for use with realtime operating systems in mobile applications such as in mobile phones, tablets, GPS devices and so on. These processors support advanced features for operating systems such as Android, ioS, Linux, Windows, etc. In addition, advanced memory management is supported with virtual memory. Early members of the family included processors such as the Cortex-A5 and Cortex-A17, based on the ARMv7-A architecture. The latest members of the family are the Cortex-A50 and Cortex-A72 series designed for low power consumption and very high performance mobile applications. These processors are built using the ARMv8-A architecture which offers 64-bit energy-efficient operation with the capability of more than 4GB of physical memory.
2.2 • Cortex-M Processor Comparison A comparison of the various Cortex-M series processors is given in Table 2-1 on page 34. As can be seen from this table, Cortex-M0 and Cortex-M0+ are used at low speed and low power consumption applications. The Cortex-M1 is optimised for use in programmable gate array applications. The Cortex-M3 and Cortex-M4 are medium power processors used in microcontroller applications with the Cortex-M4 supporting DSP and floating point arithmetic operations. The Cortex-M7 is a high performance member of the family used in applications requiring higher performance than the Cortex-M4.
● 33
Chapter 3 • Architecture of the STM32F107VCT6 ARM Microcontroller
Chapter 3 • Architecture of the STM32F107VCT6 ARM Microcontroller It is important to learn the basic architecture, advantages, disadvantages, and limitations of a microcontroller before it is used in a project. In this book we shall be using the highly popular 32-bit ARM microcontroller STM32F107VCT6. This microcontroller is a member of the STM32 family of ARM microcontrollers. A brief overview of the architecture of this microcontroller is provided in this Chapter and its basic features are described so that we can develop projects easily and efficiently. Clock configuration and input-outputs are used in almost all projects and as a result these are described in detail in this chapter.
3.1 • The STM32 Family of ARM Microcontrollers The STM32 family of 32-bit microcontrollers are based on the ARM Cortex and there are over 300 compatible devices in the family. As described below, this family includes microcontrollers with Cortex-M4, Cortex-M3, and Cortex-M0 architectures. Cortex-M4: The microcontrollers in this series start with the names STM32F4 and are designed for use in 32-bit applications. These are high performance microcontrollers having DSP and floating point arithmetic capabilities with a 168MHz clock, up to 1Mbyte of flash memory, up to 256Kbytes of SRAM, and large number of peripherals including USB, Ethernet, and a camera interface. Cortex-M3: The microcontrollers in this series start with the names STM32F1, STM32F2, STM32W, or STM32L1 and are designed for use in 16/32-bit applications. STM32F1 devices operate up to 72MHz, have up to 1Mbyte flash memory, up to 96Kbytes of RAM, and large number of peripherals, including an Ethernet interface. STM32F2 devices operate up to 120MHz, have up to 1Mbyte flash memory, up to 128Kbytes of SRAM, and have a large number of peripherals including Ethernet and a camera interface. STM32W are wireless (IEEE 802.15.4) microcontrollers with clock frequencies up to 24MHz, up to 256Kbytes of flash memory and 16Kbyte of SRAM. STM32L1 microcontrollers are ultralow power devices operating at up to 32MHz, having up to 384Kbyte flash memory, and up to 48Kbytes of SRAM. The operating voltage is down to 1.65V with standby current of only 0.3µA. Cortex-M0: The microcontrollers in this series start with the name STM32F0 and are entry level devices. The clock frequency is up to 48MHz, and they can have up to 128Kbytes of flash memory and 12Kbytes of SRAM.
3.2 • The STM32F107VCT6 Microcontroller In this book we shall be using the highly popular ARM microcontroller STM32F107VCT6 together with the EasyMxPro V7 for the STM32 development board. Details are provided in a later Chapter. In this Chapter we shall be looking at the features of the STM32F107VCT6 microcontroller. The internal architecture of this microcontroller is very complex. We shall only look at the important modules used in most projects, such as I/O, timers, ADC converter and DAC converter, interrupts, I2C, USART, and so on. Interested readers can get detailed information from the manufacturer’s data sheets that are available for download from the Internet.
3.2.1 • Basic Features of the STM32F107VCT6 The STM32F107VCT6 microcontroller is based on the Cortex-M3 architecture and has the following basic features:
● 37
ARM Microcontroller Projects: Beginner to Intermediate •
ARM Cortex-M3 32-bit RISC architecture
•
72MHz maximum operating frequency (3.32 CoreMark/MHz)
•
Single-cycle multiplication and hardware division
•
256Kbytes of flash memory
•
64Kbytes of SRAM
•
2.0 to 3.6V power supply
•
-40ºC to +105ºC operation
•
Clock PLL
•
Internal 8MHz, 32kHz, and 40kHz clock
•
Low power with sleep, stop, and standby modes
•
2x 12-bit 16 channel ADC converters with 0 to 3.6V reference
•
Sample and hold capability
•
Temperature sensor
•
2x 12-bit DAC converters
•
12-channel DMA controller
•
2x CAN bus interface (2.0B)
•
5x USART interface (with LIN and IrDA capabilities)
•
3x SPI interface (18Mbits/s)
•
2 x I2S interface
•
1x I2C interface
•
1x USB interface
•
1x 10/100 Ethernet interface
•
7x 16-bit timers
•
2x watchdog timers
•
Nested vectored interrupt controller
•
80 I/O (most of them 5V tolerant)
•
1x 16-bit PWM controller
•
Serial wire debug and JTAG interface
•
Cyclic Redundancy Check (CRC)
•
24-bit SysTick down counter timer
•
64 or 100 pin package
The basic features of the STM32F107VCT6 microcontroller are summarised in Figure 3-1 on page 39.
● 38
Chapter 3 • Architecture of the STM32F107VCT6 ARM Microcontroller
Figure 3-1 Basic Features of the STM32F107VCT6 microcontroller Figure 3-2 shows the pin layout (100 pin package) of the STM32F107VCT6 microcontroller.
3.2.2 • Internal Block Diagram The internal block diagram is shown in Figure 3-3 on page 40. In the top left corner is the 72MHz Cortex-M3 processor with the flash memory and SRAM, with DMA channels and the Ethernet module just below the processor. The voltage regulator and the external crystal inputs are shown in the top right hand corner of the figure. The internal AHB (Advanced High Speed Bus) bus is divided into a high-speed bus APB2 (Advanced Peripheral Bus 2), supporting the GPIO, Timer 1, and ADC converter modules on the left of the figure, and the low-speed bus APB1 (Advanced Peripheral Bus 1), supporting Timers 2 to 7, watchdog timer, RTC, USARTs, SPI bus, CAN bus, I2C bus, and the DAC converter on the right of the figure. Clock control circuitry is shown in the top middle part of the figure.
Figure 3-2 Pin layout of the STM32F107VCT6 microcontroller
● 39
Chapter 4 • Microcontroller Development Tools
Chapter 4 • Microcontroller Development Tools Perhaps the easiest and quickest way to learn to program and use a microcontroller system is to use a development system. Most microcontroller companies develop and manufacture various tools to help promote their products. Development tools can be divided into two categories: hardware and software tools. Furthermore, hardware tools can be divided into main development tools (or hardware development kits) and peripheral support tools (such as sensors, actuators, display devices and so on). Software development tools can be classified as either assembler or high-level language tools. In this Chapter we will be looking at some of the popular hardware and software development tools for ARM microcontrollers used in embedded applications.
4.1 • ARM Hardware Development Kits Some of the popular ARM hardware development kits are briefly described in this section.
4.1.1 • EasyMx Pro V7 for STM32 This development kit (Figure 4-1 on page 54) is designed and manufactured by mikroElektronika (www.mikroe.com). The kit is a full-featured development board for STM32 ARM Cortex-M3 and Cortex-M4 microcontrollers and is delivered with the STM32F107VCT6 processor on-board. The kit includes many on-board components necessary for the development of a variety of applications, including USB, CAN, RS232, multimedia, Ethernet, and many others. The on-board mikroProg programmer and debugger support the programming and debugging of over 180 STM32 type ARM microcontrollers. The kit includes the following components: •
STM32F107VCT6 Cortex-M3 ARM microcontroller operating at up to 72MHz with 256Kbytes of Flash memory, 64Kbytes of RAM.
•
mikroProg programmer and debugger
•
Flash and EEPROM memory
•
Ethernet module
•
USB UART modules (2 off)
•
Audio module with stereo MP3 Codec
•
DS1820 and LM35 temperature sensor sockets
•
Piezo buzzer
•
67 push-button switches
•
67 SMD LEDs
•
Navigation switch
•
TFT colour display
•
microSD card slot
•
mikroBUS connectors (2 off)
•
+3.3V power supply
EasyMx Pro V7 for STM32 ARM development kit can be programmed using the mikroC, mikroPascal, or mikroBASIC compilers developed by mikroElektronika.
● 53
ARM Microcontroller Projects: Beginner to Intermediate
Figure 4-1 EasyMx Pro V7 for STM32 development kit (www.mikroe.com)
4.1.2 • Clicker 2 for STM32 This is a small development board (Figure 4-2 on page 55) manufactured by mikroElektronika and delivered with an on-board STM32F407VGT6 Cortex-M4 chip that can operate at up to 168MHz. It also has 1MB of flash memory. The basic features of this development board are: •
STM32F407VGT6 Cortex-M4 ARM microcontroller
•
mikroBUS sockets (2 off)
•
2 LEDs
•
2 push-button switches
•
52 I/O pins
•
USB mini-B connector
•
Reset button
This board is programmed from a PC using a Bootloader program. A compatible Bootloader program is present on the program memory of the microcontroller.
● 54
Chapter 5 • Programming ARM Microcontrollers
Chapter 5 • Programming ARM Microcontrollers There are various programming languages and programming methods available for programming ARM microcontrollers. For example, ARM Cortex-M microcontrollers can be programmed in Java, Arduino, C, C++ and others. In this book we shall be using the mikroC Pro for ARM (called mikroC in the remainder of this book) programming language and the Integrated Development Environment (IDE) developed by mikroElektronika. mikroC Pro for ARM is a powerful, feature-rich development tool for ARM microcontrollers that is designed to provide programmers with the easiest possible solution for developing ARM based embedded system applications. In this chapter we shall be looking at the specific features of the mikroC programming language when mikroC is used to program the STM32 family of ARM microcontrollers (more specifically the STM32F107VCT6 microcontroller). The chapter is not intended to teach the C programming language as the readers are assumed to have practical knowledge of the C language in a microcontroller environment.
5.1 • mikroC STM32F107VCT6 Microcontroller Specific Features mikroC is very similar to the standard C language but has been developed specifically for programming microcontrollers. There are various versions of mikroC for programming PIC microcontrollers, the 8051 series of microcontrollers, the AVR family, ARM Cortex microcontrollers, and so on. mikroC allows a programmer to: •
Write the source code using the built-in text editor
•
Include all the libraries to speed up the development process
•
Manage your project easily
•
Monitor the program structure, variables, and functions
•
Generate assembly and HEX files for programming the target processor
•
Use an integrated simulator to debug code on your PC
•
Use the integrated hardware debugger to speed up program development and testing
•
Get detailed reports on memory usage, calling tree, assembly listing and more
•
Program the target processor using the integrated programming software
mikroC includes libraries on hardware, digital signal processing, ANSI C, and others. Some of the commonly used libraries are (there are over 60 libraries): •
ADC library
•
CAN library
•
EEPROM library
•
Ethernet library
•
GPIO library
•
LCD and Graphics LCD library
•
Keypad library
•
Sound library
● 63
ARM Microcontroller Projects: Beginner to Intermediate •
UART library
•
TFT display library
•
Touch panel library
•
USB library
•
Digital filter libraries (FIR and IIR)
•
FFT library
•
Matrices library
•
ANSI C Math library
•
Button library
•
Conversions library
•
Time library
•
Trigonometry library
mikroC includes a built-in, integrated help facility that helps programmers learn the format of various library statements and also to check the syntax of program statements. mikroC organises applications into projects, consisting of a single project file (extension .mcarm) and one or more source files (extension .c). The IDE helps programmers to create multiple projects. A project file contains the following: •
Project name
•
Target microcontroller device
•
Device clock
•
List of project source files
•
Header files
•
Binary files
•
Image files
•
Other files
Appendix A.1 on page 223 shows the structure of a mikroC program written for the STM32F107VCT6 microcontroller. Although comments are optional in a program, they are highly recommended as it makes a program easier to understand and maintain. This very simple program flashes an LED every second. In this chapter we shall see some of the STM32F107VCT6 specific features of the mikroC language. Most of the features described in this chapter are applicable to other members of the STM32 family. People familiar with the standard C language will notice in Appendix A.1 on page 223 that there are no library include files at the beginning of the program. This is because all the library files are automatically included by the compiler when a new file is created. In the remainder of this chapter the important GPIO library (which is used in almost all projects) and some of the ARM specific features of the mikroC are described.
5.2 • The General Purpose Input-Output (GPIO) Library The GPIO library includes a set of routines for easier handling of the General Purpose Input/Output (GPIO) pin functions. The library contains the following functions (Only the STM32 processor specific features are described in this section):
● 64
Chapter 5 • Programming ARM Microcontrollers •
GPIO_Clk_Enable
•
GPIO_Clk_Disable
•
GPIO_Config
•
GPIO_Set_Pin_Mode
•
GPIO_Digital_Input
•
GPIO_Digital_Output
•
GPIO_Analog_Input
•
GPIO_Alternate_Function_Enable
5.2.1 • GPIO_Clk_Enable This function enables the clock on the desired port. In the following example code, the clock is enabled on PORTE:
GPIO_Clock_Enable(&GPIO_BASE) 5.2.2 • GPIO_Clk_Disable This function disables the clock on the desired port. In the following example code, the clock is disabled on PORTE:
GPIO_Clock_Disable(&GPIO_BASE) 5.2.3 • GPIO_Config This function is used to configure port pins according to the specified parameters. The function has the following format:
void GPIO_Config(unsigned long *port, unsigned int pin_mask, unsigned long config) where, port is the PORT we wish to use, pin_mask is the pin we wish to configure and config is the desired configuration of the port pin. The function returns a 0 if there are no errors. In the following example, PORTA pins 0 and 7 are configured as digital inputs with no pull-up or pull-down resistors:
GPIO_Config(&GPIOA_BASE,_GPIO_PINMASK_0 | _GPIO_PINMASK_7, _GPIO_CFG_MODE_INPUT | _GPIO_CFG_PULL_NO); Similarly, the following example configures all pins of PORTB as digital outputs with pushpull output transistors:
GPIO_Config(&GPIOB_BASE,_GPIO_PINMASK_ALL, _GPIO_CFG_MODE_OUTPUT |_GPIO_CFG_OTYPE_PP); pin_mask can take the following values:
● 65
ARM Microcontroller Projects: Beginner to Intermediate
_GPIO_PINMASK_0 pin
0 mask
_GPIO_PINMASK_1 pin
1 mask
…………………………………………….............…... _GPIO_PINMASK_15 pin
15 mask
_GPIO_PINMASK_LOW
low 8 port pins
_GPIO_PINMASK_HIGH
high 8 port pins
_GPIO_PINMASK_ALL
all pins masked
config can take different values depending upon the port usage. The following values are valid: Basic
_GPIO_CFG_PULL_UP
configure pins as pull-up
_GPIO_CFG_PULL_DOWN
configure pins as pull-down
_GPIO_CFG_PULL_NO
configure pins as floating (no pull-up/down)
_GPIO_CFG_MODE_ALT_FUNCTION
pins have alternate functions (non GPIO)
_GPIO_CFG_MODE_ANALOG
configure pins for analog
_GPIO_CFG_OTYPE_OD
configure pins as open-drain
_GPIO_CFG_OTYPE_PP
configure pins as push-pull
_GPIO_CFG_SPEED_400KHZ
configure pins for 400kHz clock
_GPIO_CFG_SPEED_2MHZ
configure pins for 2MHz clock
_GPIO_CFG_SPEED_10MHZ
configure pins for 10MHz clock
_GPIO_CFG_SPEED_25MHZ
configure pins for 25MHz clock
_GPIO_CFG_SPEED_40MHZ
configure pins for 40MHz clock
_GPIO_CFG_SPEED_50MHZ
configure pins for 50MHZ clock
_GPIO_CFG_SPEED_100MHZ
configure pins for 100MHZ clock
_GPIO_CFG_SPEED_MAX
configure pins for maximum clock
_GPIO_CFG_DIGITAL_OUTPUT
configure pins as digital output
_GPIO_CFG_DIGITAL_INPUT
configure pins as digital input
_GPIO_CFG_ANALOG_INPUT
configure pins as analog input
Timer These are timer functions and the function name changes depending upon the timer used. For example, for Timer 1 the following functions are available (similar functions are available for other timers, see the HELP file for more details):
_GPIO_CFG_AF_TIM1
Timer 1 alternate function mapping
_GPIO_CFG_AF2_TIM2
Timer 1 alternate function 2 mapping
_GPIO_CFG_AF6_TIM1
Timer 1 alternate function 6 mapping
_GPIO_CFG_AF11_TIM1
Timer 1 alternate function 11 mapping
● 66
Chapter 6 • Microcontroller Program Development
Chapter 6 • Microcontroller Program Development Before writing a program it is always helpful first to think about and plan the structure (or the algorithm) of the program. Although simple programs can easily be developed by writing the code directly without any preparation, the development of complex programs almost always becomes easier if an algorithm is derived first. Once the algorithm is ready, the coding of the actual program is rarely a difficult task. A program algorithm can be described in a variety of graphical and text-based methods, such as flow charts, structure charts, data flow diagrams, program description languages, and so on. There are three basic operations in every program: sequencing, selection, and iteration. These operations can either be shown graphically or in text form. The problem with graphical techniques is that it can be very time consuming to draw shapes manually with text inside them. That said, there are computer programs that help to draw such diagrams. Also, it can often be a tedious task to modify an algorithm described using graphical techniques. Flow charts have been used for many years by programmers and they can be very useful in describing the flow of control and data in small programs where there are only a handful of diagrams, usually not extending beyond a page or two. Some of the problems associated with flow charts are: •
Drawing flow charts is often a tedious task
•
It is difficult and can be very time consuming to modify flow charts
•
It is almost impossible to draw flow charts extending over many pages
There are some computer programs that help the programmer draw flow charts. One such program is called the Raptor. This can be downloaded free of charge from the developer web site: http://raptor.martincarlisle.com/. Using Raptor, a programmer can draw complex flow charts and even see the logic of the program and the program outputs at every stage by simulating the operation of the program step by step. In addition, breakpoints can be set in the flow chart and program execution can be analysed by executing the program up to the selected breakpoints. Raptor also generates program code for popular programming languages such as C#, C++ and so on. Figure 6-1 shows a simple screen shot from Raptor.
Figure 6-1 Screen shot from a simple Raptor flow chart The program description language (PDL) can be useful in describing the flow of control
● 89
ARM Microcontroller Projects: Beginner to Intermediate and data in small to medium size programs. The main advantages of using PDL are: •
There are no graphical shapes to draw
•
Creating a PDL is very easy and may take little time
•
Complex programs can easily be described using PDL
•
It is very easy to modify a PDL
Structure charts (sometimes called Jackson Structure Charts, or JSC) is another method of showing a program algorithm using graphical techniques. All the elements of a structure chart consist of rectangular blocks with text inside. The main advantage of using structure charts is that unlike flow charts, code generated using these charts is structured and easy to follow. i.e. there are no goto type loops in these program. In this book, we will mainly be using the program description language. Flow charts or structure charts will also be provided where it is felt to be useful. The next sections briefly describe the basic building blocks of the program description language and its equivalent algorithm using flow charts and structure charts. It is left to the reader to decide which method to use during the development of their programs.
6.1 • Using the Program Development Tools Program description language (PDL) is free-format English-like text which describes the flow of control and data in a program. PDL is not a programming language. It is a collection of keywords that enable a programmer to describe the operation of a program in a stepwise and logical manner. In this section we will look at the basic PDL statements and their flow chart and structure chart equivalents. The superiority of PDL over the other two techniques will become obvious when we have to develop medium to large size programs.
6.1.1 • BEGIN – END Every PDL program description should start with a BEGIN and end with an END statement. The keywords in a PDL description should be highlighted to make reading easier. Program statements should be indented and described between PDL keywords. An example is shown in Figure 6-2 together with the equivalent flow chart.
Figure 6-2 BEGIN – END statement and equivalent flow chart
6.1.2 • Sequencing For normal sequencing in PDL, program statements should be written in English text to describe the operations to be performed. An example is shown in Figure 6-3 on page 91 together with the equivalent flow chart. Figure 6-4 on page 91 shows the equivalent structure chart.
● 90
Chapter 6 • Microcontroller Program Development
Figure 6-3 Sequencing and equivalent flow chart
Figure 6-4 Structure chart for the example in Figure 6.3
6.1.3 • IF – THEN – ELSE – ENDIF IF, THEN, ELSE, and ENDIF should be used to conditionally change the flow of control in a program. Every IF keyword should be terminated with a THEN, and every IF block should be terminated with an ENDIF keyword. Use of the ELSE statement is optional and depends on the application. Figure 6-5 provides an example of using IF – THEN – ENDIF both as a PDL and flow chart. Figure 6-6 on page 92 shows the equivalent structure chart. Notice the two lines are joined to indicate that this is a selection statement and the condition is written on one of the lines.
Figure 6-5 Using IF – THEN – ENDIF statements
● 91
Chapter 7 • The EasyMx PRO v7 for STM32 Development Board
Chapter 7 • The EasyMx PRO v7 for STM32 Development Board The EasyMx PRO v7 for STM32 development board is a highly popular ARM development board based on Cortex-M3 architecture, using the STM32F107VCT6 microcontroller. The board is developed and manufactured by mikroElektronika (www.mikroe.com) and is used in all projects in the later chapters of this book. This chapter describes the basic features of the EasyMx PRO v7 for STM32 development board and shows the layout of the various interface devices and jumpers located on the board. Readers should be familiar with this board and its various jumper positions before it is used in the projects in later chapters.
7.1 • The Features Figure 7-1 shows a picture of the EasyMx PRO v7 for STM32 development board (it will be called the EasyMx board from now on). The board has the following features:
Figure 7-1 The EasyMx PRO v7 for STM32 development board ARM Cortex-M3, 72MHz STM32F107VCT6 microcontroller •
67 push-button switches
•
67 LEDs
•
320x240 colour TFT display
•
mikroProg in-circuit debugger
•
8x256 bytes EEPROM
•
8 Mbit serial flash memory
•
3.3V power regulator
•
DS1820 and LM35 temperature sensor sockets
•
USB UART connectors
•
CAN support
•
Piezo buzzer
•
Tri-state DIP switches for all port pins
•
microSD card slot
•
Ethernet connector
● 107
ARM Microcontroller Projects: Beginner to Intermediate •
Audio connector
•
Stereo MP3 codec
•
mikroBUS connectors
•
navigation switch
•
power from external power supply or from USB port
Some of the commonly used features of this development board are described in detail in the following sections.
7.2 • The Power Supply The EasyMx board accepts a 7-23V AC or 9-32V DC external power supply. Power can also be provided from a 5V supply via a USB cable. Figure 7-2 shows how the board can be powered using a USB cable. In this configuration, jumper J9 must be set to USB position.
Figure 7-2 Powering using a USB cable Figure 7-3 shows how the board can be powered using an external adapter. Here, jumper J9 must be set to EXT position.
Figure 7-3 Powering using an external adapter A laboratory power supply can also be used as shown in Figure 7-4. In this configuration, jumper J9 must be set to EXT position.
Figure 7-4 Powering using a laboratory power supply
● 108
Chapter 7 • The EasyMx PRO v7 for STM32 Development Board In this book, the board is powered from the USB port of a laptop.
7.3 • The CPU Card The board is shipped with the ARM Cortex-M3 type STM32F107VCT6 microcontroller on a CPU card. Users can replace this card with another one containing the STM32F407VGT6 microcontroller belonging to the ARM Cortex-M4 family. Figure 7-5 shows the default CPU card. In addition to the microcontroller, this card contains a 25MHZ crystal, a 32768Hz crystal, USB communications lines, and an Ethernet transceiver module.
Figure 7-5 The default CPU card with the STM32F107VCT6 microcontroller
7.4 • On-board Programmer and Hardware Debugger The development board contains a fast programmer and hardware debugger called the mikroProg, based on the ST-LINK V2 programmer (see Figure 7-6). This programmer allows over 180 ARM Cortex-M3 and Cortex-M4 STM32 processors to be programmed. The programmer driver and the mikroProg suite for ARM software (both available at the mikroElektronika website, www.mikroe.com) must be installed before the programmer can be used.
Figure 7-6 mikroProg programmer and debugger
7.5 • The LEDs 67 LEDS and 67 push-button switches are provided on the board. Each port group has its own port header, tri-state pull up/down DIP switch, push-buttons, and LEDs. Figure 7-7 on page 110 shows a typical I/O group (for PORT A high group). Low-current SMD type LEDs and resistors are used on the board. The typical current consumption of the LEDs are 0.2mA or 0.3mA. An active LED indicates that logic High is present on the pin.
● 109
Chapter 8 • Beginner ARM Microcontroller Projects
Chapter 8 • Beginner ARM Microcontroller Projects In this chapter we shall be looking at the design of simple ARM microcontroller based projects. The EasyMx PRO v7 for STM32 development board (see Chapter 7 on page 107) is used in all projects. The software for the projects has been developed using the mikroC PRO for ARM compiler (called mikroC from now on) and the IDE, developed by mikroElektronika (www.mikroe.com). In order to become familiar with the process of developing projects using mikroC and the ARM development board, it is recommended that readers first try the simple projects provided in this chapter. The following are provided for each project: •
Project title
•
Project description
•
Block diagram of the project
•
Circuit diagram of the project
•
Description of the hardware
•
Algorithm of the project
•
Program listing in mikroC Pro For ARM
•
Suggestions for future work (optional)
The microcontroller operates at 72MHz in all of the projects in this Chapter.
8.1 • PROJECT 1 – Flashing LED Project Description In this simple project, an LED is connected to port pin PD0 (bit 0 of PORTD) of an STM32F107VCT6 type microcontroller. In this project, the LED flashes 10 times with a one second delay between each flash. It then stops. Block Diagram Figure 8-1 shows the block diagram of this project. The LED is connected to port pin PD0.
Figure 8-1 Block diagram of the project Circuit Diagram In this project the EasyMx PRO v7 for STM32 development board is used and thus there was no need to build any hardware. Switch SW15 position 7 (PORTD/L) should be set to position ON to enable PORTD low byte LEDs.
● 115
ARM Microcontroller Projects: Beginner to Intermediate An LED can be connected to a microcontroller output port in two different modes: current-sinking mode and current-sourcing mode. Current-sinking As shown in Figure 8-2, in current-sinking mode the anode leg of the LED is connected to the +5V supply, and the cathode leg is connected to the microcontroller output port through a current limiting resistor.
Figure 8-2 LED connected in current-sinking mode The voltage drop across an LED varies between 1.4V and 2.5V, with a typical value of 2V. The brightness of an LED depends on the type of LED used. Small SMD type LEDs require around 0.12mA current, while bigger LEDs may require 1 – 16mA current to be bright. The LED is turned ON when the output of the microcontroller is at logic 0 so that current flows through the LED. Assuming that the microcontroller output voltage is about 3.2V when the output is at logic 1, also assuming LED current is 0.12mA, we can calculate the value of the required resistor as follows:
R=
VS − VLED I LED
(8.1)
where,
VS is the supply voltage (5V) VLED is the voltage drop across the LED (2V) I LED is the current through the LED (0.12mA)
substituting the values into equation (8.1) we get,
= R
3.2 − 2 = 10 K 0.12
Current-sourcing As shown in Figure 8-3 on page 117, in current-sourcing mode, the anode leg of the LED is connected to the microcontroller output port and the cathode leg to ground through a current limiting resistor.
● 116
Chapter 9 • Elementary ARM Microcontroller Projects
Chapter 9 • Elementary ARM Microcontroller Projects In this chapter we shall be looking at the design of more complex ARM microcontroller based projects than the ones given in Chapter 8. The EasyMx PRO v7 for STM32 development board (see Chapter 7 on page 107) is again used in all the projects in this Chapter. The software for the projects has been developed using the mikroC Pro for ARM compiler (called mikroC from now on) and the IDE, developed by mikroElektronika (www. mikroe.com). It is recommended that the readers first study the simple projects provided in Chapter 8 on page 115 before looking at the projects in this Chapter.
9.1 • PROJECT 1 – Voltmeter With LCD Project Description This project describes the design of a voltmeter with an LCD display. Analog voltage is applied to one of the analog inputs (PC0, which can also be configured as analog input AN10 of ADC module 1) of the STM32F107VCT6 microcontroller and the voltage is displayed on the LCD every second. Block Diagram The block diagram of the project is shown in Figure 9-1.
Figure 9-1 Block diagram of the project Circuit Diagram Figure 9-2 on page 150 shows the circuit diagram of the project. An LCD is connected to PORTD low-byte. In addition, analog voltage that is to be measured is connected to analog input AN10 (pin 0 of PORTC, PC0). A 10K potentiometer is used to vary the input voltage between 0V and 1.8V during the testing.
● 149
ARM Microcontroller Projects: Beginner to Intermediate
Figure 9-2 Circuit diagram of the project Project Hardware In this project the EasyMx PRO v7 for STM32 development board is used and thus there was no need to build any hardware. A jumper is placed in PC0 position of Jumper J8 so that the potentiometer is connected to analog input. The EasyMx PRO v7 for STM development board jumper J8 connection is shown in Figure 9.3.
Figure 9-3 EasyMx PRO v7 for STM board jumper J8 selection Project PDL The PDL of this project is very simple and is given in Figure 9-4 on page 151.
● 150
Chapter 9 • Elementary ARM Microcontroller Projects
BEGIN
Define LCD interface
Configure PORTD as digital output
Initialize LCD library
Initialize ADC library
Set input channel 10
DO FOREVER
Get sample from analog channel 10
Convert into millivolts
Convert into string
Remove leading spaces
Clear LCD screen
Display on LCD
Wait 1 second
ENDDO
END Figure 9-4 PDL of the project An ADC converter converts an analog input voltage into a digital number so it can be processed by a microcontroller or any other digital processor. As far as the input and output voltages types are concerned, ADC converters can be classified as either unipolar or bipolar. Unipolar ADC converters accept unipolar input voltages which are always positive, and bipolar ADC converters accept bipolar input voltages which are positive and negative voltages. Bipolar converters are frequently used in digital signal processing applications, where the signals by nature are bipolar. Unipolar converters are cheaper, easier to use, and are used in many control and instrumentation applications. An ADC converter usually has one analog input and a digital parallel output. The conversion process is basically as follows: •
Apply the analog signal to the ADC input
•
Start the conversion process
•
Wait until the conversion is complete (this step can be interrupt driven)
•
Read the converted digital data
The ADC conversion starts by triggering the converter to start the conversion process. Depending upon the speed of the converter, the conversion process itself can take several microseconds. At the end of the conversion, the converter either raises a flag or generates an interrupt to indicate that the conversion is complete. The converted parallel output data can then be read and processed as required. The STM32F107VCT6 microcontroller has two 12-bit ADC converters with a conversion time of 1µs. The reference voltage can be in the range 0 to +3.6V. Each converter shares 16 external channels and two internal channels of analog data. The result of a conversion is stored in a left-aligned or right-aligned 16-bit data register. The conversion can be performed in single, continuous, scan, or discontinuous mode. In this project, the reference voltage is +1.8V. With this reference voltage and 12-bit
● 151
Chapter 10 • Intermediate ARM Microcontroller Projects
Chapter 10 • Intermediate ARM Microcontroller Projects In this Chapter we shall be looking at the design of more advanced projects, such as projects using timers, interrupts, graphics LCDs, flash memory cards, and so on. Some of the projects given in this Chapter may not be complex or advanced themselves, but they are in this Chapter because the concepts they use are more difficult to understand.
10.1 • PROJECT 1 – Event Counter Using An External Interrupt Project Description The aim of this project is to show how the external interrupts of the STM32F107VCT6 microcontroller can be used. In this project, pin 0 of PORTA (PA0) is configured as an external interrupt input. External events occur when this pin goes from logic 1 to logic 0. The event count is shown on an LCD connected to PORTD of the microcontroller. Block Diagram The block diagram of the project is as shown in Figure 10-1.
Figure 10-1 Block diagram of the project Circuit Diagram The circuit diagram of the project is shown in Figure 10-2. External events are simulated using a push-button switch connected to port pin PA0. The LCD is connected to PORTD as shown in the figure.
Figure 10-2 Circuit diagram of the project
● 191
ARM Microcontroller Projects: Beginner to Intermediate Project Hardware In this project the EasyMx PRO v7 for ARM development board is used. Position 1 of switch SW10 is set to the bottom position (i.e. GND), so that when any of the pushbuttons connected to PORTA pins are pressed, then logic 0 is output from that pin. Thus, for example pressing button PA0 simulates the occurrence of an external event. Project PDL The project PDL is shown in Appendix A.32 on page 273. Before going into the details of external interrupt programming, it is worthwhile to consider the external interrupt structure and interrupt registers of the STM32F107VCT6 microcontroller in some detail. All the I/O lines of the STM32F107VCT6 microcontroller are configurable as external interrupt lines. Up to 80 GPIOs can be connected to 16 external interrupt lines. Each line can be independently configured to select the trigger event as rising edge or falling edge, and can be masked independently. A pending register maintains the status of the interrupt requests. To use an I/O pin as an external interrupt pin it must be configured as a digital input pin. Figure 10-3 shows the external interrupt controller block diagram. The following registers are used in external interrupt setup: •
External Interrupt Configuration Register (AFIO_EXTICRx)
•
Rising Edge Trigger Selection Register (EXTI_RTSR)
•
Rising Edge Trigger Selection Register (EXTI_FTSR)
•
Interrupt Mask Register (EXTI_IMR)
•
Interrupt Event Mask Register (EXTI_EMR)
•
Interrupt Pending Register (EXTI_PR)
•
Software Interrupt Event Register (EXTI_SWIER)
Figure 10-3 External interrupt controller
● 192
Appendix A • Programming Listings
Appendix A • Programming Listings A.1 • Flashing LEDs /*************************************************************************** FLASHING LEDs ============= In this project 16 LEDs are connected to PORTD of a STM32F107VCT6 type ARM Cortex-M3 microcontroller. The program flashes the LEDs every second.
Author: Dogan Ibrahim File
: LEDS.c
Date
: November, 2015
****************************************************************************/ void main() { GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_ALL); // Set PORTD as digital output GPIOD_ODR = 0; while(1)
// Turn OFF LEDs to start with
// Do Forever
{ GPIOD_ODR = ~GPIOD_ODR; Delay_ms(1000);
// Toggle PORTD // Wait 1 second
} }
● 223
ARM Microcontroller Projects: Beginner to Intermediate
A.2 • Flashing LED (LED10.c) /*************************************************************************** FLASHING LED ============ In this project an LED is connected to bit 0 of PORTD (PD0) of a STM32F107VCT6 type ARM Cortex-M3 microcontroller. The program flashes the LED 10 times with 1 second delay between each flash Author: Dogan Ibrahim File
: LED10.c
Date
: November, 2015
****************************************************************************/ #define LED GPIOD_ODR.B0
// Bit 0 of PORTD
void main() { unsigned char i; GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_0); // Set PD0 as digital output LED = 0; for(i=0; i< 10; i++)
// Do 10 times
{ LED = 1; Delay_ms(1000); LED = 0; Delay_ms(1000); } }
● 224
// LED ON // Wait 1 second // LED OFF // Wait 1 second
Index
Index
A Absolute maximum ratings 52 ADC channels 155 ADC library 63 ADC prescaler 44 ADC_Set_Input_Channel 154 ADCx_Get_Sample 154 ADCx_Read 154 ADXx_Init 153 Analog Comparators 25 Analog Temperature Sensor 112 Analog-to-digital Converter 24 ANSI C 63 ANSI C Math library 64 APB high-speed prescaler 44 APB low-speed prescaler 44 ARM7 Development Kit 57 ARM Compilation Tools 59 ARM Cortex-M3 107 ARM Microcontrollers 37, 63 ASCII chart tool 83 Audio I/O 111
B BEGIN - END 90 bit Data Type 71 bit patterns 164 Bit Sending Hierarchy 169 Brown-out 24 Button library 64
C Calling Subprograms 95 CAN 67 CAN Interface 26 CAN library 63 CISC 28 Clock 23 Clock Circuit 41 Clock configuration 43, 47 Clock Polarity 168 Clock Pulse 169 Clock security system 44
Configurable Exceptions Bus Fault 71 Interrupts (IRQ) 72 Memory Management 71 PendSV 72 SVCall 72 SysTick 72 Usage Fault 71 Configuring the Clock 43 Conversions library 64 Cortex-M3 37 Cortex-M4 37 CPU Card 109 Current Sink 26 Current-sinking 116 Current-sourcing 116
D DAC1_Dual_Output 186 DAC Converter 183 DACx_Advanced_CHy_Output 185 DACx_Chy_Output 186 DACx_Deinit 187 DACx_Init 185 Data Length Size 168 DEBUG 68 Debug Features 27 Debugging 82 Development Tools 53 Digital Temperature Sensor 112 Digital-to-analog Converter 27 DIP switch 109 Divide Hardware 26 DMA 27 DO - ENDDO 93 DS-5 Development Studio 59
E EasyMx Pro V7 for STM32 53 EEPROM 22, 113 EEPROM Data Memory 25 EEPROM library 63 Electrically Erasable Programmable Read Only Memory 22 embedded controller 17 EPROM 22
â&#x2014;? 299
ARM Microcontroller Projects: Beginner to Intermediate
Erasable Programmable Read Only Memory 22 Ethernet Communication 111 Ethernet Interface 26 Ethernet library 63 Event Counter 191 Exceptions 71 Exception vector table 72 External Clock Sources High Speed External (HSE) 42 Low Speed External (LSE) 42 External high-speed clock bypass 44 External high-speed clock enable 44
F FFT library 64 Flash Memory 22 Frequencies of musical notes 134
G GLCD Bitmap Editor 84 GLCD Interface 112 GPIO_Clk_Disable 65 GPIO_Clk_Enable 65 GPIO_Config 65 GPIO library 63 GPIO Library 64 GPIOs 48 Graphics LCD library 63
H Hardware Debugger 109 Hardware Development Kits 53 Harvard architecture 28 HD44780 Controller 138 HID Terminal 84 High Speed External (HSE) 42 HTU21D Communication Protocol Sending a Command 176 Start Sequence 176 Stop Sequence 176 HTU21D sensor 177
I
â&#x2014;? 300
I2C 67 I2C Bus 172 I2C Busses 25 I2Cn_Get_Status 175 I2Cn_Init 174 I2Cn_init_Advanced 175 I2Cn_Is_Idle 175 I2Cn_Read 175 I2Cn_Start 175 I2Cn_Write 176 I2S 67 I2S2 clock source 45 I2S3 clock source 45 IF - THEN - ELSE - ENDIF 91 In-circuit Serial Programming 27 Internal Clock Sources 43 High Speed Internal (HIS) 43 Low Speed Internal (LSI) 43 Internal high-speed clock enable 44 Internal Slave Select 169 Interrupts 24, 71 Interrupt Service Routine 74, 197
K Keypad 178 Keypad library 63
L Lcd_Chr 142 Lcd_Chr_Cp 142 Lcd_Cmd 142 LCD Custom Character 85 LCD Drivers 25 Lcd_Init 142 Lcd_Out 142 Lcd_Out_Cp 142 LCD standard character set 140 LEDs 109 Library Manager 88 licker 2 54 Low Power Modes 40 Sleep mode 40 Standby mode 40 Stop mode 40 Low Power Operation 26 Low Speed External (LSE) 42
ARM
MICROCONTROLLER PROJECTS
Dogan Ibrahim
It is becoming important for microcontroller users to quickly learn and adapt to new technologies and architecture used in high performance 32-bit microcontrollers. Many manufacturers now offer 32-bit microcontrollers as general purpose processors in embedded applications. Prof Dr Dogan Ibrahim is a Fellow of the Institution of Electrical Engineers. He is the author of over 60 technical books, published by international famous publishers, such as Wiley, Butterworth, and Newnes. In addition, he is the author of over 250 technical papers, published in journals, and presented in seminars and conferences.
ISBN 978-1-907920-48-6
The architecture of the highly popular ARM Cortex-M processor STM32F107VCT6 is described at a high level, taking into consideration its clock mechanisms, general input/output ports, interrupt sources, ADC and DAC converters, timer facilities, and more. The information provided here should act as a basis for most readers to start using and programming the STM32F107VCT6 microcontroller together with a development kit. Furthermore, the use of the mikroC Pro for ARM integrated development environment (IDE) has been described in detail. This IDE includes everything required to create a project; namely an editor, compiler, simulator, debugger, and device programmer. Although the book is based on the STM32F107VCT6 microcontroller, readers should not find it difficult to follow the projects using other ARM processor family members.
DESIGN
www.elektor.com
This book makes use of the ARM Cortex-M family of processors in easy-to-follow, practical projects. It gives a detailed introduction to the architecture of the Cortex-M family. Examples of popular hardware and software development kits are described.
BEGINNER TO INTERMEDIATE
ARM
MICROCONTROLLER PROJECTS
LEARN
Elektor International Media BV
ARM provide 32 and 64-bit processors mainly for embedded applications. These days, the majority of mobile devices including mobile phones, tablets, and GPS receivers are based on ARM technology. The low cost, low power consumption, and high performance of ARM processors makes them ideal for use in complex communication and mixed signal applications.
ARM MICROCONTROLLER PROJECTS ● DOGAN IBRAHIM
BEGINNER TO INTERMEDIATE
Dogan Ibrahim LEARN
DESIGN
SHARE
SHARE
LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR