Programming the DRAGON12-Plus-USB in C and Assembly Language Using CodeWarrior
Teaches programming in both C and Assembly language for the Freescale MC9S12DG256 microcontroller on the DRAGON12-Plus-USB board from Wytec. Includes over 50 detailed examples. Uses CodeWarrior IDE. Provides LBE_DRAGON12_Plus Stationery project for quick start.
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. We will use a particular microcontroller, the Freescale MC9S12DG256, that it is one of the more powerful in the popular HCS12 family of microcontrollers from Freescale with lots of I/O capabilities. This microcontroller is available on the DRAGON12-Plus-USB™ development board from Wytec, Inc. The DRAGON12-Plus-USB™ has many built-in I/O devices including LEDs, switches, four 7-segment displays, a hex keypad, an LCD display, a hex keypad, a D/A converter chip, an on-board speaker, a built-in H-bridge for driving motors, 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.
A previous book from LBE Books, Learning By Example Using C – Programming the DRAGON12-Plus™ Using CodeWarrior showed how to write programs in C with a minimum of effort for this development board. We did this by providing you with a CodeWarrior stationery project that contained an assembly language file to do all the low-level interaction with the I/O registers. These assembly language routines become function calls for your C program. Thus, in this previous book you didn’t have to learn any assembly language to get sophisticated programs to work on this development board.
This current book provides these same CodeWarrior Stationery projects so that you can write C programs easily for the DRAGON12-Plus-USB™ development board. However, in this book we look under the hood to see how all of the assembly language routines that are included in the stationery project work. This way you will learn how to program the HCS12 microcontroller in assembly language and how to call these assembly language routines from your top-level C program. You will therefore be able to write your own assembly language subroutines that you can call from your C program, and in this way get the maximum performance from the MC9SDG256 microcontroller.
In Chapter 1 we introduce the DRAGON12-Plus-USB™ development board. Chapter 2 will include examples of using the parallel ports for outputs and Chapter 3 will include examples of using the parallel ports for inputs. Liquid crystal displays are described in Chapter 4. The important topic of interrupts are introduced in Chapter 5. Examples using the two 8-channel A/D converters that are built into the MC9S12DG256 microcontroller are given in Chapter 6. Examples that show how to use pulse-width modulation (PWM) to control the speed of a DC motor or the position of a servo are presented in Chapter 7. Chapter 8 includes examples of using the serial communication interface (SCI) and Chapter 9 shows how to use the serial peripheral interface (SPI). Examples using the builtin timer module are given in Chapter 10 and an example of using the MC9S12DG256 microcontroller for fuzzy control is included in Chapter 11.
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
1. Introduction – Microcontrollers and C
1.1 From Microprocessors to Microcontrollers
1.2 DRAGON12-Plus-USB-USB™ Board
1.3 The CodeWarrior Development Tools
2. Parallel Ports – Outputs
2.1 MC9S12DG256 Parallel I/O Ports
2.2 LEDs and 7-Segment Displays
Example 1 – Writing to Ports
Example 2 – C Function Calls
Example 3 – Delay Loops in C
Example 4 – Delay Loops in Assembly: ms_delay(int n)
Example 5 – Turning Single Bits On and Off
Example 6 – Hex Counter
Example 7 – Multiplexing the 7-Segment Displays
Problems
3. Parallel Ports – Inputs
3.1 Pushbuttons and DIP Switches
Example 8 – Switches on the DRAGON12-Plus-USB
3.2 Hex Keypad
Example 9 – Hex Keypad in C
Example 10 – Keypad C Function Calls Problems
Problems
4. Liquid Crystal Displays
4.1 Liquid Crystal Displays
Example 11 – Writing a Message on the LCD
4.2 Binary Number to ASCII String Conversion
Example 12 – Displaying Integers on the LCD
4.3 ASCII Number String to Binary Conversion
Example 13 – Calculator
Problems
5. Interrupts
5.1 Hardware Interrupts
5.2 Real-Time Interrupts
Example 14 – Blinking 7-Segment Display
Example 15 – Interrupt-Driven Traffic Light
Example 16 – Interrupt-Driven Blinking SOS
6. Analog-to-Digital Converter
6.1 Analog-to-Digital Conversion
6.2 Using the MC9S12DG256 A/D Converters
Example 17 – Reading the Potentiometer Value
6.3 Measuring Acceleration
Example 18 – Measuring the x–y–z Components of Acceleration
Example 19 – Measuring the Coefficient of Static Friction
6.4 Measuring Temperature
Example 20 – Displaying the Temperature on the LCD
7. Pulse-Width Modulation (PWM) – Motors and Servos
7.1 Connecting a Motor to a Microcontroller
Example 21 – Controlling the Speed of a DC Motor Using PWM
7.2 DC Servo Motors
Example 22 – Controlling the Position of a Servo Using PWM
8. Serial Communication Interface (SCI)
8.1 Asyncronous Serial I/O
8.2 The 68HCS12 SCI Interface
Example 23 – SCI Echo with LCD Display
8.3 A Circular Queue Data Structure
Example 24 – Keypad Input to LCD Using a Queue
8.4 SCI Interface Using Interrupts
Example 25 – Display SCI Input on LCD Using Receive Interrupts
Example 26 – Sending Periodic Acceleration Measurements to Matlab
Problems
9. The Serial Peripheral Interface (SPI)
9.1 Operation of the SPI
9.2 Programming the SPI in C and Assembly Language
Example 27 – Interfacing with the LTC1661 10-Bit DAC
9.3 Keypad Interfacing with 74165 Shift Registers
Example 28 – Reading Data from Shift Registers Using SPI
Problems
10. Timer
10.1 Output Compare
Example 29 – Interrupt-Driven Pulse Train
Example 30 – Playing Musical Notes with the Keypad
10.3 Input Capture
Example 31 – Measuring Input Pulse Widths
11. Fuzzy Control
11.1 Design of a Fuzzy Controller
11.2 Fuzzification of Inputs – MEM and fill_weights(…)
11.3 Processing the Rules – REV and fire_rules(…)
11.4 Output Defuzzification – WAV and calc_output(…)
11.5 Under the Hood – Fuzzy Control Assembly Routines
Problems
Appendix A – CodeWarrior Tutorial – DRAGON12-Plus-USB
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 – Phase Locked Loop
Appendix G – C Quick Reference Guide
Index
(211 pages)