Learning By Example Using C – Programming the DRAGON12-Plus Using CodeWarrior
C programming only (no Assembly explanation) for the Freescale MC9S12DG256 on DRAGON12-Plus board. Over 50 detailed examples. Uses CodeWarrior IDE. Provides LBE_DRAGON12_Plus Stationery project.
More in C / Microcontrollers
Preface
Microcontrollers such as the Freescale MC9SDG256 are remarkable devices. They contain not only a sophisticated microprocessor with a rich set of instructions and addressing modes, but also contain built-in RAM, EEPROM, and flash memory as well as numerous useful I/O ports, including parallel I/O, several different types of serial I/O, timers, and A/D converters.
This extensive list of features makes the programming of these microcontrollers, particularly using assembly language, a daunting task. A quick look at the many textbooks (or the many datasheets) that describe these microcontrollers will confirm this. These books and datasheets spend a great deal of time explaining how to program the many I/O registers to perform the wide variety of different I/O tasks. However, it is usually something fairly simple that you are trying to do; e.g. turn on a light, read a switch, turn on a motor at some speed, read the value of an A/D converter, or measure some time interval. If you just want to learn how to do these simple things without getting bogged down in the minutia of the microcontrollers I/O and internal operation, then this book is for you.
Compare this book to your automobile. Most of us want to use our car to get from one place to another easily and in comfort. We don’t want to have to learn how to design and build an internal combustion engine or automatic transmission in order to drive our car. By the same token you should not have to learn a particular assembly language or the details of what each bit in dozens of different I/O registers do in order to quickly and effectively program a microcontroller to do what you want it to do. This book will show you how to do just that. Of course, some people like to look under the hood of their car to see how things really work. If you really want to see how the assembly language routines that we provide for you actually work you can study the entire source listing that is included in our stationery project. But it isn’t necessary to get your microcontroller to work.
This book assumes no previous knowledge of either microcontrollers or C. We will use a particular microcontroller, the Freescale MC9S12DG256, on a popular, low-cost development board, the DRAGON12-Plus™ from Wytec, Inc. The reason for using this microcontroller is that it is one of the more powerful in the popular HCS12 family with lots of I/O capabilities, and the reasons for using the DRAGON12-Plus are the many built-in I/O devices including LEDs, switches, four 7-segment displays, a hex keypad, an LCD display, and a hex keypad. There is also an on-board speaker, a D/A converter chip, a built-in H-bridge for driving motors and many other features including convenient headers for connecting servos and an accelerometer board available from Wytec, and female headers for connecting to your circuits on the built-in protoboard. You will quickly be able to do lots of fun stuff with this board.
In this book you will learn to write programs in C and compile them to HCS12 code using CodeWarrior™, an industry-strength integrated development environment for microcontrollers. CodeWarrior itself can be daunting, but we will make it easy for you to learn what you need to quickly get your microcontroller to work. We do this by providing you with an LBE_DRAGON12_Plus Stationery project from which you can easily build your own particular application. This project will contain an assembly language file that we have written to do all the low-level interaction with the I/O registers. You can then concentrate on driving your car in C rather than wasting your time figuring out how the internal combustion engine works! So let’s get started.
Many colleagues, students and reviewers have influenced the development of this book. Their stimulating discussions, probing questions, and critical comments are greatly appreciated. Special thanks go to Michael Latcha and Osamah Rawashdeh with whom we have had many useful discussions related to the contents of this book.
Richard E. Haskell
Darrin M. Hanna
Table of Contents
Introduction – Microcontrollers and C
Example 1 – LEDs and 7-Segment Display: Parallel Port
Example 2 – Binary Count of LEDs: Delay Loops
Example 3 – Controlling Single LEDs
Example 4 – 7-Segment Display: Hex Counter
Example 5 – Pushbutton and DIP Switches
Example 6 – Hex Keypad
Example 7 – Liquid Crystal Displays
Example 8 – Serial Communcation Interface (SCI)
Example 9 – Binary to ASCII String Conversion
Example 10 – ASCII String to Binary Conversion
Example 11 – Analog-to-Digital Converter
Example 12 – Pulse-Width Modulation (PWM): Motors and Servos
Example 13 – Serial Peripheral Interface (SPI)
Example 14 – Real-time Interrupts
Example 15 – Interrupt-Driven Controller
Example 16 – Circular Queue
Example 17 – SCI Receive Interrupts
Example 18 – Pulse Train Using Interrupts
Example 19 – Measuring the Period of a Pulse Train Using Interrupts
Example 20 – Using Accelerometers
Example 21 – Fuzzy Control
Appendix A – CodeWarrior Tutorial
Appendix B – HCS12 Assembly Language Essentials
Appendix C – Summary of C Function Calls to main.asm
Appendix D – MC9S12DG256B Interrupt Vectors
Appendix E – Introduction to Fuzzy Control
Appendix F – C Quick Reference Guide
Index
(150 pages)