Microcontrollers and the Freescale/Motorola HC11 Summer 2008 What is a microcontroller? A computer on a chip used to control electronic devices A microprocessor Usually not cutting edge (4-bit to 32-bit) Dependable (all major bugs well known) Predictable (critical for real-time processing) On-chip peripherals and memory Parallel and serial digital I/O Analog I/O Counters and timers Internal ROM, RAM and/or EPROM CMPE12 Summer 2008 Slides by ADB 2 1
What are microcontrollers used in? Microcontrollers are used in Watches Microwaves Stereo Receivers ATMs PDAs, MP3 players Automobiles (between 9 and 50) Some products that you might know: NASA s Sojourner Rover 8-bit Intel 80C85 Palm Vx handheld 32-bit Motorola Dragonball EZ Sonicare toothbrush 8-bit Zilog Z8 The Vendo V-MAX 720 Soda Machine Motorola HC11 Miele dishwasher 8-bit Motorola 68HC05 Hunter 44550 Programmable Thermostat (4-bit cpu) CMPE12 Summer 2008 Slides by ADB 3 Microprocessor unit sales are 100 times higher than microcontroller sales Microprocessor: 220 million Microcontroller: 20 billion Microcontrollers are much, much cheaper Microprocessor unit sales CMPE12 Summer 2008 Slides by ADB 4 2
Microcontrollers (1/4) AMCC Until May 2004, these µcs were developed and marketed by IBM, whose 4xx family was sold to Applied Micro Circuits Corporation. 403 PowerPC CPU PPC 403GCX 405 PowerPC CPU PPC 405EP PPC 405GP/CR PPC 405GPr PPC NPe405H/L 440 PowerPC Book-E CPU PPC 440GP PPC 440GX PPC 440EP/EPx/GRx PPC 440SP/SPe Altera Nios II 32-bit configurable soft processor Nios 16-bit configurable soft processor Atmel AT89 series (Intel 8051 architecture) AT90, ATtiny, ATMega series (AVR architecture) (Atmel Norway design) AT91SAM (ARM architecture) AVR32 (32-bit AVR architecture) Charmed Labs Cypress MicroSystems Dallas Semiconductor CMPE12 Summer 2008 Slides by ADB 5 Microcontrollers (2/4) Freescale Semiconductor Until 2004, these µcs were developed and marketed by Motorola, whose semiconductor division was spun-off to establish Freescale. 8-bit 68HC05 (CPU05) 68HC08 (CPU08) 68HC11 (CPU11) 16-bit 68HC12 (CPU12) 68HC16 (CPU16) Freescale DSP56800 (DSPcontroller) 32-bit Freescale 683XX MPC500 MPC 860 (PowerQUICC) MPC 8240/8250 (PowerQUICC II) MPC 8540/8555/8560 (PowerQUICC III) Fujitsu Holtek Infineon 8-bit XC800 family C500/C800 family 16-bit XC166 family 32-bit family TRICORE family CMPE12 Summer 2008 Slides by ADB 6 3
Intel 8-bit Microcontrollers (3/4) MCS-48 (8048 family also incl. 8035, 8038, 8039, 8040, 8X42, 8X49, 8050; X=0 or 7) MCS-51 (8051 family also incl. 8X31, 8X32, 8X52; X=0, 3, or 7) 8xC251 16-bit MCS-96 (8096 family also incl. 8061) Intel MCS 296 Lattice Semiconductor Mico8 softcore 8 bit microcontroller Microchip Technology 8 and 16-bit microcontrollers with 12 to 24-bit instructions ability to include DSP function 12-bit instruction PIC 14-bit instruction PIC PIC16F84 16-bit instruction PIC National Semiconductor COP8 CR16 NEC Philips Semiconductors Rabbit Semiconductor CMPE12 Summer 2008 Slides by ADB 7 Microcontrollers (4/4) Renesas Technology (Renesas is a joint venture of Hitachi and Mitsubishi Electric.) Silabs Silicon Motion STMicroelectronics Texas Instruments Toshiba Western Design Center Ubicom Xemics Xilinx ZiLOG CMPE12 Summer 2008 Slides by ADB 8 4
Languages for microcontrollers Language 98-99 99-00 Assembly ~ 21% ~ 10% C ~ 69% ~ 80% C++ ~ 5% ~ 6% Java ~ 1 % ~ 2% Other ~ 3 % ~ 2% Source: TRON Association Survey 1998/99 & 1999/2000 CMPE12 Summer 2008 Slides by ADB 9 M6801 CPU core Memory: Motorola / Freescale MC68HC11A8 8KB ROM 512B EEPROM 256B RAM Counter/Timer system 8-channel, 8-bit A/D converter Analog to digital converter (external D/A in our kit) Parallel I/O port Two serial I/O ports: Asynchronous, SCI Synchronous, SPI Expansion bus for external memory CMPE12 Summer 2008 Slides by ADB 10 5
What are RAM and ROM? RAM: Random-access memory Write to any location with the same effort (Same amount of time) ROM: (Random-access) read-only memory PROM: Programmable ROM EPROM: Erasable PROM EEPROM: Electrically-erasable PROM Flash EEPROM: Faster EEPROM CMPE12 Summer 2008 Slides by ADB 11 High-level HC11 architecture Interrupt logic MEMORY Timer and counter M8601 CPU core Serial I/O A/D converter Port A Port B Port C Port D Port E CMPE12 Summer 2008 Slides by ADB 12 6
Our HC11 microkit LCD display brightness control LCD Serial cable to PC AC/DC Power adapter LEDs HC11 µc Switches CMPE12 Summer 2008 Slides by ADB 13 Serial (RS232C) connector Our HC11 microkit board LCD display brightness control HC11 µc External RAM HW Reset EEPROM LEDs Interrupt Switches CMPE12 Summer 2008 Slides by ADB 14 7
HC11 programmer-accessible hardware Buttons Reset Interrupt (IRQ) Switches 8 switches, MSb on far left and LSb on far right Accessed by reading from SWITCHES LEDs 8 LEDs, MSb on far left and LSb on far right Accessed by writing to LEDS Active low (writing a 0 turns a light on) LCD Two lines, 16 characters per line We provide some routines output to LCD CMPE12 Summer 2008 Slides by ADB 16 The HC11 instruction set architecture Mixed 8-bit/16-bit architecture CISC, with more than 100 opcodes One instruction can use several addressing modes, depending on how you use it Accumulator architecture CMPE12 Summer 2008 Slides by ADB 17 8
An aside: accumulator architecture One operand comes from memory The other comes from the accumulator CMPE12 Summer 2008 Slides by ADB 18 The HC11 accumulators and registers Two 8-bit accumulators, or you can use them together as one 16-bit accumulator Two 8-bit accumulators, A or B One 16-bit accumulator [A:B], called D Two index registers Two 16-bit index registers, X and Y, used for addressing memory or for counting Special registers 16-bit dedicated stack pointer, SP 16-bit program counter, PC 8-bit condition codes register, CCR CMPE12 Summer 2008 Slides by ADB 19 9
The HC11 registers CMPE12 Summer 2008 Slides by ADB 20 Accumulators: A, B, andd General-purpose 8-bit accumulators Source/destination of most operations Almost always interchangeable E.g., ABA, add B to A A (A + B) B is unchanged Note that there is no AAB instruction SBA, subtract B from A A (A B) Note that there is no SAB instruction 7 A 0 7 B 0 15 D 0 CMPE12 Summer 2008 Slides by ADB 21 10
Accumulators: A, B, andd Some instructions use [A:B] as a single 16-bit register D E.g. ADDD #2 LDD #1 MUL 7 A 0 7 B 0 15 D 0 CMPE12 Summer 2008 Slides by ADB 22 Index registers X and Y 15 X 0 15 Y 0 16-bit index registers X and Y: Used for indexed addressing mode Swap with register D to perform complex address operations Address space: 64KB LC-3: 64K words, which is 128 KB CMPE12 Summer 2008 Slides by ADB 23 11
The HC11 has a stack set up at start-up Memory is byte-addressable HC11 User Stack PSHA PSHY PULA PULY // push accumulator A onto stack // SP = SP 1 since a byte size // push index register Y onto stack // SP = SP 2 since a word size // Pop off a byte from the stack into A // SP = SP + 1 // Pop off a word from the stack into Y // SP = SP + 2 CMPE12 Summer 2008 Slides by ADB 24 An aside: the RPN calculator RPN is Reverse Polish Notation HP calculators E.g., A = CD+E Push C Push D Mult Push E Add Pop A A DE CD+E CCD TOP CMPE12 Summer 2008 Slides by ADB 25 12
Stack pointer: SP 16-bit stack pointer, SP Automatically incremented or decremented on pushes and pops Can be used for data and return addresses Return address for subroutines is automatically pushed with jsr and popped with rts 15 SP 0 CMPE12 Summer 2008 Slides by ADB 26 16-bit program counter, PC Program counter: PC 15 PC 0 CMPE12 Summer 2008 Slides by ADB 27 13
Condition code register: CCR Five status indicators H, half carry (used only for BCD operations) N, negative Z, zero S X H I N Z V C V, overflow (two s complement) C, carry/borrow many instructions set them all, but not all instructions Two interrupt masking bits I, disable all maskable interrupts (1 = disable) X, disable external interrupts (1 = disable) A STOP-disable bit disables the STOP instruction CMPE12 Summer 2008 Slides by ADB 28 Programming on the HC11 microkit Design environment: Edit the source file Assemble on UNIX machine > hc11build file.asm Will produce an object (executable) file called file.s19 Download program to the PC with terminal program TeraTerm Upload to the microkit using serial cable To use kits at home Get one at BELS Have a connection to ITS machines Get TeraTerm, which is free Or any program which is capable of serial communication CMPE12 Summer 2008 Slides by ADB 29 14
Programming on the HC11 microkit As part of your program, always have #include <v2_18g3.asm> What the include file does Sets up memory, including the stack pointer Jumps to the main label Includes a basic library of I/O routines CMPE12 Summer 2008 Slides by ADB 30 /* header comments */ Structure of a source program #include <v2_18g3.asm>.sect.data // your variables here.sect.text // your code here main: // must have a main: function CMPE12 Summer 2008 Slides by ADB 31 15
#include <v2_18g3.asm> Example program echo.asm.sect.data signon:.asciz "CMPE12C Welcome!" prompt:.asciz ">".sect.text main: ldx #signon jsr OUTSTRING // write a null-terminated // string to serial port echo_loop: ldx #prompt // address of prompt jsr OUTSTRING // write out to serial port jsr GETCHAR // getchar returns in ACCA jsr OUTCHAR // write ACCA to screen jsr OUTCRLF // write '\n' to serial port jmp echo_loop CMPE12 Summer 2008 Slides by ADB 32 Example program factorial The factorial function x! = x (x-1) (x-2) 3 2 1 Two ways to do it Recursively Non-recursively Draw a flow chart Write an algorithm CMPE12 Summer 2008 Slides by ADB 33 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Example program factorial.asm (1/2) #include <v2_18g3.asm>.sect.data welcome:.asciz " Factorial!" prompt:.asciz "Fact is " blanks:.asciz " " input:.byte 0.sect.text main: ldx #welcome ldaa #1 // line 1 jsr LCDSTRING // write to LCD, expects in X ldx #blanks inca // line 2 jsr LCDSTRING // write to LCD ldx #prompt 19 jsr LCDSTRING // write to LCD CMPE12 Summer 2008 Slides by ADB 34 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 Example program factorial.asm (2/2) ldab SWITCHES // B SWITCHES jsr fact_start jsr LCDINT // expects input in D stab LEDS // LEDS B always: jmp always // factorial: assume input in B, output in D fact_start: stab input // input B tba // A B fact: dec input // input (input 1) ldaa input // A input beq fact_done // Z? mul // D (A B) jmp fact fact_done: rts CMPE12 Summer 2008 Slides by ADB 35 17
Compiling the program CMPE12 Summer 2008 Slides by ADB 41 CMPE12 Summer 2008 Slides by ADB 42 18
Downloading the program Just drag & drop CMPE12 Summer 2008 Slides by ADB 43 Running the program Note the LEDs: is the number correct? CMPE12 Summer 2008 Slides by ADB 44 19
Recommended exercises Write and run the Fibonacci program. CMPE12 Summer 2008 Slides by ADB 45 20