KEIL Tools by ARM Getting Started Creating Applications withμVision®4 Blinky-uVision 口▣☒ Eike Edt yiew Project Flash Debug Peripherals Iocis SVcs Window Help D3得造力地声座色安识胜监透 过*回●oe回☑ 陈@●性下下)中回国圆图深口2■圆·国··且· Project WorkSpace 91 AD value ADC Cosvertedvalue! 情Time Max Time: Range: Zoom t■a e言nky 0x00007004 0x0a000714 x080007005 LDRH c5,1r0.t0x001 59 atup Expo101462c6msF2070134s200a000s0.108000: In Out Smultor 色Startup Code 080007040 田5TM32F10x.5 08000706F9527B9DV 100 (AD_sosled 1-A 4257 日STmM32F100A 9x000071n Retarget.c Lbrary 1021 2网09 2700000 10 +日LCD_4bt.0 tBse似c KEIL s白rte2aton 出BsTM32ntc Tools by ARM 094 a□nky.c Source 05 fer (i ◆adc_Int (void Binky.c ◆nan (vod】 =白Documentation while uVision4 凸bstract.bxt 98 AD v Integrated Development Environment =日Gpo.c s台Gp0 39 AD s caled to 0. ◆man (void Smulator value differ ◆s2Pssed(o s台Startup Code 01 AD Cepyrght 1997-2005 2005-2009 ARM Ltd reerved ◆S3 Pressed(o STM32F10x.5 102 Tha productit peotacted by US and internabonal lwt rgraph accor LCD.4bc =白nitialsation 凸Retarget.c STM32_Int.c % ys_ext (int retum_co a白Source 105 /Print message with AD ◆ywrch (int ch) +由Gpoc t0 it (clockis) ◆ferror (FILE0 07 clockls 0: ◆getc (FIE*D 回Project国egen Command ,a×cal5tack Memory 1 5akFam9 Vale(Address A6 dress010000000 B◆main0 iehles LcD_Diaplay(h 0%100000001 MCBSTM32 DEHG Aneleg(3) AD_value 5×100000221 Analog 13.000000)entered AD scaled 000000036 5×10000644: 5×10600666 AD_scaled 55 IGN Br上Db1k高ab1 e Break11 Ccat stack ocalswath 1symbals 0×10000110: Smuetion u士2070139056t NUM For 8-bit,16-bit,and 32-bit Microcontrollers www.keil.com
www.keil.com Getting Started For 8-bit, 16-bit, and 32-bit Microcontrollers Creating Applications with µVision®4
2 Preface Information in this document is subject to change without notice and does not represent a commitment on the part of the manufacturer.The software described in this document is furnished under license agreement or nondisclosure agreement and may be used or copied only in accordance with the terms of the agreement.It is against the law to copy the software on any medium except as specifically allowed in the license or nondisclosure agreement.The purchaser may make one copy of the software for backup purposes.No part of this manual may be reproduced or transmitted in any form or by any means,electronic or mechanical,including photocopying,recording,or information storage and retrieval systems,for any purpose other than for the purchaser's personal use, without written permission. Copyright 1997-2009 Keil,Tools by ARM,and ARM Ltd. All rights reserved. Keil Software and Design,the Keil Software Logo,uVision,RealView", C51TM,C166TM,MDKTM,RL-ARMTM,ULINK,Device Database,and ARTXTM are trademarks or registered trademarks of Keil,Tools by ARM,and ARM Ltd. Microsoft and WindowsTM are trademarks or registered trademarks of Microsoft Corporation. PC is a registered trademark of International Business Machines Corporation. NOTE This manual assumes that you are familiar with Microsoft Windows and the hardware and instruction set of the ARM7,ARM9,Cortex-Mx,C166,XE166. XC2000,or 8051 microcontroller. Every effort was made to ensure accuracy in this manual and to give appropriate credit to persons,companies,and trademarks referenced herein
2 Preface Information in this document is subject to change without notice and does not represent a commitment on the part of the manufacturer. The software described in this document is furnished under license agreement or nondisclosure agreement and may be used or copied only in accordance with the terms of the agreement. It is against the law to copy the software on any medium except as specifically allowed in the license or nondisclosure agreement. The purchaser may make one copy of the software for backup purposes. No part of this manual may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or information storage and retrieval systems, for any purpose other than for the purchaser’s personal use, without written permission. Copyright © 1997-2009 Keil, Tools by ARM, and ARM Ltd. All rights reserved. Keil Software and Design® , the Keil Software Logo, µVision® , RealView® , C51™, C166™, MDK™, RL-ARM™, ULINK® , Device Database® , and ARTX™ are trademarks or registered trademarks of Keil, Tools by ARM, and ARM Ltd. Microsoft® and Windows™ are trademarks or registered trademarks of Microsoft Corporation. PC® is a registered trademark of International Business Machines Corporation. NOTE This manual assumes that you are familiar with Microsoft Windows and the hardware and instruction set of the ARM7, ARM9, Cortex-Mx, C166, XE166, XC2000, or 8051 microcontroller. Every effort was made to ensure accuracy in this manual and to give appropriate credit to persons, companies, and trademarks referenced herein
Getting Started:Creating Applications with uVision 3 Preface This manual is an introduction to the Keil development tools designed for Cortex-Mx,ARM7,ARM9,C166,XE166,XC2000,and 8051 microcontrollers. It introduces the u Vision Integrated Development Environment,Simulator,and Debugger and presents a step-by-step guided tour of the numerous features and capabilities the Keil embedded development tools offer. Who should Read this Book This book is useful for students,beginners,advanced and experienced developers alike. Developers are considered experienced or advanced if they have used u Vision extensively in the past and knowledge exists of how the u Vision IDE works and interacts with the debugger,simulator,and target hardware.Preferably,these developers already have a deep understanding of microcontrollers.We encourage this group of engineers to get familiar with the enhancements introduced and to explore the latest features in u Vision. Developers are considered students or beginners if they have no working experience with u Vision.We encourage this group of developers to start by reading the chapters related to the u Vision IDE and to work through the examples to get familiar with the interface and configuration options described. They should make use of the ample possibilities the simulator offers.Later on, they should continue with the chapters describing the RTOS and microcontroller architectures. However,it is assumed that you have a basic knowledge of how to use microcontrollers and that you are familiar with a few instructions or with the instruction set of your preferred microcontroller The chapters of this book can be studied individually,since they do not strictly depend on each other
Getting Started: Creating Applications with µVision 3 Preface This manual is an introduction to the Keil development tools designed for Cortex-Mx, ARM7, ARM9, C166, XE166, XC2000, and 8051 microcontrollers. It introduces the µVision Integrated Development Environment, Simulator, and Debugger and presents a step-by-step guided tour of the numerous features and capabilities the Keil embedded development tools offer. Who should Read this Book This book is useful for students, beginners, advanced and experienced developers alike. Developers are considered experienced or advanced if they have used µVision extensively in the past and knowledge exists of how the µVision IDE works and interacts with the debugger, simulator, and target hardware. Preferably, these developers already have a deep understanding of microcontrollers. We encourage this group of engineers to get familiar with the enhancements introduced and to explore the latest features in µVision. Developers are considered students or beginners if they have no working experience with µVision. We encourage this group of developers to start by reading the chapters related to the µVision IDE and to work through the examples to get familiar with the interface and configuration options described. They should make use of the ample possibilities the simulator offers. Later on, they should continue with the chapters describing the RTOS and microcontroller architectures. However, it is assumed that you have a basic knowledge of how to use microcontrollers and that you are familiar with a few instructions or with the instruction set of your preferred microcontroller. The chapters of this book can be studied individually, since they do not strictly depend on each other
Preface Chapter Overview "Chapter 1.Introduction",provides an overview of product installation and licensing and shows how to get support for the Keil development tools. "Chapter 2.Microcontroller Architectures",discusses various microcontroller architectures supported by the Keil development tools and assists you in choosing the microcontroller best suited for your application. "Chapter 3.Development Tools",discusses the major features of the uVision IDE and Debugger,Assembler,Compiler,Linker,and other development tools "Chapter 4.RTX RTOS Kernel",discusses the benefits of using a Real-Time Operating System(RTOS)and introduces the features available in Keil RTX Kernels “Chapter5.UsingμVision”,describes specific features of theμVision user interface and how to interact with them. "Chapter 6.Creating Embedded Programs",describes how to create projects, edit source files,compile,fix syntax errors,and generate executable code. "Chapter 7.Debugging",describes how to use the uVision Simulator and Target Debugger to test and validate your embedded programs. "Chapter 8.Using Target Hardware",describes how to configure and use third-party Flash programming utilities and target drivers. "Chapter 9.Example Programs",describes four example programs and shows the relevant features of u Vision by means of these examples
4 Preface Chapter Overview “Chapter 1. Introduction”, provides an overview of product installation and licensing and shows how to get support for the Keil development tools. “Chapter 2. Microcontroller Architectures”, discusses various microcontroller architectures supported by the Keil development tools and assists you in choosing the microcontroller best suited for your application. “Chapter 3. Development Tools”, discusses the major features of the µVision IDE and Debugger, Assembler, Compiler, Linker, and other development tools. “Chapter 4. RTX RTOS Kernel”, discusses the benefits of using a Real-Time Operating System (RTOS) and introduces the features available in Keil RTX Kernels. “Chapter 5. Using µVision”, describes specific features of the µVision user interface and how to interact with them. “Chapter 6. Creating Embedded Programs”, describes how to create projects, edit source files, compile, fix syntax errors, and generate executable code. “Chapter 7. Debugging”, describes how to use the µVision Simulator and Target Debugger to test and validate your embedded programs. “Chapter 8. Using Target Hardware”, describes how to configure and use third-party Flash programming utilities and target drivers. “Chapter 9. Example Programs”, describes four example programs and shows the relevant features of µVision by means of these examples
Getting Started:Creating Applications with uVision 5 Document Conventions Examples Description README.TXT' Bold capital text is used to highlight the names of executable programs, data files,source files,environment variables,and commands that you can enter at the command prompt.This text usually represents commands that you must type in literally.For example: ARMCC.EXE DIR LX51.EXE Courier Text in this typeface is used to represent information that is displayed on the screen or is printed out on the printer This typeface is also used within the text when discussing or describing command line items. Variables Text in italics represents required information that you must provide.For example,projectfile in a syntax string means that you must supply the actual project file name Occasionally,italics are also used to emphasize words in the text. Elements that repeat... Ellipses(...)are used to indicate an item that may be repeated Omitted code Vertical ellipses are used in source code listings to indicate that a fragment of the program has been omitted.For example: void main(void){ while (1); Optional Items》 Double brackets indicate optional items in command lines and input fields.For example: C51 TEST.C PRINT filenamex opt1 opt2 Text contained within braces,separated by a vertical bar represents a selection of items.The braces enclose all of the choices and the vertical bars separate the choices.Exactly one item in the list must be selected. Keys Text in this sans serif typeface represents actual keys on the keyboard. For example."Press Enter to continue It is not required to enter commands using all capital letters
Getting Started: Creating Applications with µVision 5 Document Conventions Examples Description README.TXT1 Bold capital text is used to highlight the names of executable programs, data files, source files, environment variables, and commands that you can enter at the command prompt. This text usually represents commands that you must type in literally. For example: ARMCC.EXE DIR LX51.EXE Courier Text in this typeface is used to represent information that is displayed on the screen or is printed out on the printer This typeface is also used within the text when discussing or describing command line items. Variables Text in italics represents required information that you must provide. For example, projectfile in a syntax string means that you must supply the actual project file name Occasionally, italics are also used to emphasize words in the text. Elements that repeat… Ellipses (…) are used to indicate an item that may be repeated Omitted code . . . Vertical ellipses are used in source code listings to indicate that a fragment of the program has been omitted. For example: void main (void) { . . . while (1); «Optional Items» Double brackets indicate optional items in command lines and input fields. For example: C51 TEST.C PRINT «filename» { opt1 | opt2 } Text contained within braces, separated by a vertical bar represents a selection of items. The braces enclose all of the choices and the vertical bars separate the choices. Exactly one item in the list must be selected. Keys Text in this sans serif typeface represents actual keys on the keyboard. For example, “Press Enter to continue 1 It is not required to enter commands using all capital letters
6 Contents Contents Preface.. .3 Document Conventions..... .5 0 ntents… Chapter 1.Introduction..... .9 Last-Minute Changes........ 11 Licensing. 11 nstallation.… 11 Requesting Assistance........... 13 Chapter 2.Microcontroller Architectures......14 Selecting an Architecture....... 15 Classic and Extended 8051 Devices........ .17 Infineon C166.XE166.XC2000 20 ARM7 and ARM9 based Microcontrollers 21 Cortex-Mx based Microcontrollers 23 Code Comparison.............. 26 Generating Optimum Code 28 Chapter 3.Development Tools.... .33 Software Development Cycle 33 μVision IDE. .34 u Vision Device Database 35 μVision Debugger.… 3 Assembler..... 37 C/C++Compiler.… 38 Object-HEX Converter 38 Linker/Locator.... 39 Library Manager 39 Chapter 4.RTX RTOS Kernel.. 40 Software Concepts............. 40 RTX Introduction..… 43 Chapter5.UsingμVision… .55 Menus.… 59 Toolbars and Toolbar Icons 63 Project Windows............. 69
6 Contents Contents Preface ................................................................................................................... 3 Document Conventions ........................................................................................ 5 Contents ................................................................................................................ 6 Chapter 1. Introduction...................................................................................... 9 Last-Minute Changes ....................................................................................... 11 Licensing .......................................................................................................... 11 Installation ....................................................................................................... 11 Requesting Assistance ..................................................................................... 13 Chapter 2. Microcontroller Architectures ...................................................... 14 Selecting an Architecture ................................................................................. 15 Classic and Extended 8051 Devices ................................................................ 17 Infineon C166, XE166, XC2000 ..................................................................... 20 ARM7 and ARM9 based Microcontrollers ...................................................... 21 Cortex-Mx based Microcontrollers .................................................................. 23 Code Comparison ............................................................................................ 26 Generating Optimum Code .............................................................................. 28 Chapter 3. Development Tools ......................................................................... 33 Software Development Cycle .......................................................................... 33 µVision IDE ..................................................................................................... 34 µVision Device Database ................................................................................ 35 µVision Debugger ............................................................................................ 35 Assembler ........................................................................................................ 37 C/C++ Compiler .............................................................................................. 38 Object-HEX Converter .................................................................................... 38 Linker/Locator ................................................................................................. 39 Library Manager .............................................................................................. 39 Chapter 4. RTX RTOS Kernel ........................................................................ 40 Software Concepts ........................................................................................... 40 RTX Introduction ............................................................................................. 43 Chapter 5. Using µVision ................................................................................. 55 Menus .............................................................................................................. 59 Toolbars and Toolbar Icons ............................................................................. 63 Project Windows .............................................................................................. 69
Getting Started:Creating Applications with uVision 7 Editor Windows............. Output Windows.… 73 Other Windows and Dialogs..... 74 On-line Help............ 74 Chapter 6.Creating Embedded Programs .75 Creating a Project File 75 Using the Project Windows....... 77 Creating Source Files....... 78 Adding Source Files to the Project 79 Using Targets,Groups,and Files. 79 Setting Target Options............. 81 Setting Group and File Options 82 Configuring the Startup Code.. 83 Building the Project............ 84 Creating a HEX File 85 Working with Multiple Projects 86 Chapter 7.Debugging..... 89 Simulation...... 91 Starting a Debug Session 91 Debug Mode........... 93 Using the Command Window.... 9 Using the Disassembly Window. 94 Executing Code....... 95 Examining and Modifying Memory .96 Breakpoints and Bookmarks.... 98 Watchpoints and Watch Window 100 Serial I/O and UARTs....... 102 Execution Profiler 103 Code Coverage........ 104 Performance Analyzer 105 Logic Analyzer 106 System Viewer...... 107 Symbols Window............ 108 Browse Window 109 Toolbox.... 110 Instruction Trace Window 111 Defining Debug Restore Views 111
Getting Started: Creating Applications with µVision 7 Editor Windows ............................................................................................... 71 Output Windows .............................................................................................. 73 Other Windows and Dialogs ............................................................................ 74 On-line Help .................................................................................................... 74 Chapter 6. Creating Embedded Programs ..................................................... 75 Creating a Project File ..................................................................................... 75 Using the Project Windows ............................................................................. 77 Creating Source Files ....................................................................................... 78 Adding Source Files to the Project .................................................................. 79 Using Targets, Groups, and Files ..................................................................... 79 Setting Target Options ..................................................................................... 81 Setting Group and File Options ....................................................................... 82 Configuring the Startup Code .......................................................................... 83 Building the Project ......................................................................................... 84 Creating a HEX File ........................................................................................ 85 Working with Multiple Projects ...................................................................... 86 Chapter 7. Debugging ....................................................................................... 89 Simulation ........................................................................................................ 91 Starting a Debug Session ................................................................................. 91 Debug Mode .................................................................................................... 93 Using the Command Window .......................................................................... 94 Using the Disassembly Window ...................................................................... 94 Executing Code ................................................................................................ 95 Examining and Modifying Memory ................................................................ 96 Breakpoints and Bookmarks ............................................................................ 98 Watchpoints and Watch Window .................................................................. 100 Serial I/O and UARTs.................................................................................... 102 Execution Profiler .......................................................................................... 103 Code Coverage ............................................................................................... 104 Performance Analyzer ................................................................................... 105 Logic Analyzer .............................................................................................. 106 System Viewer ............................................................................................... 107 Symbols Window ........................................................................................... 108 Browse Window ............................................................................................ 109 Toolbox .......................................................................................................... 110 Instruction Trace Window ............................................................................. 111 Defining Debug Restore Views ..................................................................... 111
8 Contents Chapter 8.Using Target Hardware........... 112 Configuring the Debugger...... .113 Programming Flash Devices 114 Configuring External Tools 115 Using ULINK Adapters.… 116 Using an Init File...... 121 Chapter 9.Example Programs........ …122 “Hello”Example Program… .123 "Measure"Example Program 127 “Traffic'”Example Program. 138 “Blinky”Example Program 142 Glossary …146 Index... 151
8 Contents Chapter 8. Using Target Hardware............................................................... 112 Configuring the Debugger ............................................................................. 113 Programming Flash Devices .......................................................................... 114 Configuring External Tools ........................................................................... 115 Using ULINK Adapters ................................................................................. 116 Using an Init File ........................................................................................... 121 Chapter 9. Example Programs ...................................................................... 122 “Hello” Example Program ............................................................................. 123 “Measure” Example Program ........................................................................ 127 “Traffic” Example Program ........................................................................... 138 “Blinky” Example Program ........................................................................... 142 Glossary ............................................................................................................ 146 Index .................................................................................................................. 151
Getting Started:Creating Applications with uVision 9 Chapter 1.Introduction Thank you for allowing Keil to provide you with software development tools for your embedded microcontroller applications. This book,Getting Started,describes the u Vision IDE,u Vision Debugger and Analysis Tools,the simulation,and debugging and tracing capabilities.In addition to describing the basic behavior and basic screens of uVision,this book provides a comprehensive overview of the supported microcontroller architecture types,their advantages and highlights,and supports you in selecting the appropriate target device.This book incorporates hints to help you to write better code.As with any Getting Started book,it does not cover every aspect and the many available configuration options in detail.We encourage you to work through the examples to get familiar with uVision and the components delivered. The Keil Development Tools are designed for the professional software developer,however programmers of all levels can use them to get the most out of the embedded microcontroller architectures that are supported. Tools developed by Keil endorse the most popular microcontrollers and are distributed in several packages and configurations,dependent on the architecture. MDK-ARM:Microcontroller Development Kit,for several ARM7,ARM9, and Cortex-Mx based devices PK166:Keil Professional Developer's Kit,for C166,XE166,and XC2000 devices DK251:Keil 251 Development Tools,for 251 devices PK51:Keil 8051 Development Tools,for Classic Extended 8051 devices In addition to the software packages,Keil offers a variety of evaluation boards, USB-JTAG adapters,emulators,and third-party tools,which completes the range of products The following illustrations show the generic component blocks of u Vision in conjunction with tools provided by Keil,or tools from other vendors,and the way the components relate
Getting Started: Creating Applications with µVision 9 Chapter 1. Introduction Thank you for allowing Keil to provide you with software development tools for your embedded microcontroller applications. This book, Getting Started, describes the µVision IDE, µVision Debugger and Analysis Tools, the simulation, and debugging and tracing capabilities. In addition to describing the basic behavior and basic screens of µVision, this book provides a comprehensive overview of the supported microcontroller architecture types, their advantages and highlights, and supports you in selecting the appropriate target device. This book incorporates hints to help you to write better code. As with any Getting Started book, it does not cover every aspect and the many available configuration options in detail. We encourage you to work through the examples to get familiar with µVision and the components delivered. The Keil Development Tools are designed for the professional software developer, however programmers of all levels can use them to get the most out of the embedded microcontroller architectures that are supported. Tools developed by Keil endorse the most popular microcontrollers and are distributed in several packages and configurations, dependent on the architecture. MDK-ARM: Microcontroller Development Kit, for several ARM7, ARM9, and Cortex-Mx based devices PK166: Keil Professional Developer’s Kit, for C166, XE166, and XC2000 devices DK251: Keil 251 Development Tools, for 251 devices PK51: Keil 8051 Development Tools, for Classic & Extended 8051 devices In addition to the software packages, Keil offers a variety of evaluation boards, USB-JTAG adapters, emulators, and third-party tools, which completes the range of products. The following illustrations show the generic component blocks of µVision in conjunction with tools provided by Keil, or tools from other vendors, and the way the components relate
10 Chapter 1.Introduction Software Development Tools Like all software based on Keil's u Vision IDE, the toolsets provide a powerful,easy to use and Software Development Tools easy to learn environment for developing embedded applications. C/C++Compiler They include the components you need to create, RTX RTOS Kernel Library debug,and assemble your C/C++source files, uVision and incorporate simulation for microcontrollers IDE Device Database and related peripherals. μ/ision Debugger Analysis Tools The RTX RTOS Kernel helps you to implement complex and time-critical software. Complete Device Simulation RTOS and Middleware Components These components are designed to solve RTOS and Middleware communication and real-time challenges of Components embedded systems.While it is possible to implement embedded applications without using RTX RTOS Source Code a real-time kernel,a proven kernel saves time and shortens the development cycle. TCPnet Networking Suite This component also includes the source code Flash File System files for the operating system. USB Device Interface CAN Interface Hardware Debug Adapters The u Vision Debugger fully supports several emulators provided by Keil,and other vendors. The Keil ULINK USB-JTAG family of adapters ☒KEIL ULINK A con nect the USB port of a PC to the target hardware.They enable you to download,test. and debug your embedded application on real hardware
10 Chapter 1. Introduction C/C++ Compiler RTX RTOS Kernel Library µVision IDE & Device Database µVision Debugger & Analysis Tools Complete Device Simulation Software Development Tools RTX RTOS Source Code TCPnet Networking Suite Flash File System USB Device Interface CAN Interface RTOS and Middleware Components Software Development Tools Like all software based on Keil’s µVision IDE, the toolsets provide a powerful, easy to use and easy to learn environment for developing embedded applications. They include the components you need to create, debug, and assemble your C/C++ source files, and incorporate simulation for microcontrollers and related peripherals. The RTX RTOS Kernel helps you to implement complex and time-critical software. RTOS and Middleware Components These components are designed to solve communication and real-time challenges of embedded systems. While it is possible to implement embedded applications without using a real-time kernel, a proven kernel saves time and shortens the development cycle. This component also includes the source code files for the operating system. Hardware Debug Adapters The µVision Debugger fully supports several emulators provided by Keil, and other vendors. The Keil ULINK USB-JTAG family of adapters con nect the USB port of a PC to the target hardware. They enable you to download, test, and debug your embedded application on real hardware