microcontrollers and embedded systems - … to addressing modes addressing modes: lc-3 has:...

63
CMPE12 Cyrus Bazeghi Fun with Motorola/Freescale HC11 Microcontrollers and Embedded Systems

Upload: hanhu

Post on 30-May-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

CMPE12 Cyrus Bazeghi

Fun with Motorola/Freescale HC11

Microcontrollers and Embedded Systems

CMPE12 Cyrus Bazeghi2

•A microprocessor•Usually not cutting edge•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 and/or EPROM

What is a microcontroller?

CMPE12 Cyrus Bazeghi3

What are microcontrollers used in?

• Watches• Microwaves• Stereo Receivers

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)

Microcontrollers

• ATMs• PDA‟s, MP3 players• Automobiles

CMPE12 Cyrus Bazeghi4

Microcontrollers

Microcontroller unit sales are 15x higher than microprocessors.… and are MUCH,

MUCH cheaper.

CMPE12 Cyrus Bazeghi5

Microcontrollers

Microcontrollers are a large market•8-bit controllers are the largest, but not growing the fastest.

CMPE12 Cyrus Bazeghi6

Microcontrollers

• 8-bit microcontroller growth rate for 2003 is at 9.42%.

• Microcontroller growth rate in general is 11.5%.

• 8-bit controllers loosing market share in 2003. Was 62.36% in 1998 down to 56.76% in 2003.

Source: Cahners In-Stat Group

CMPE12 Cyrus Bazeghi7

• 16- and 32-bit and higher are on the rise. They will double their unit market share from 15.11% in 1998 up to 31.56% in 2003, decreasing 4-bit and 8-bit devices.

• But, in 2003, the 8-bit microcontrollers will outnumber the higher bit units by almost 80% in the market place.

Source: Cahners In-Stat Group

Microcontrollers

CMPE12 Cyrus Bazeghi8

Microcontrollers

So what languages are they being programmed in?

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 Cyrus Bazeghi9

•M6801 CPU core•ROM (8KB), EEPROM (512B), RAM (256B)•Counter/Timer system•A/D converter

•D/A in kit•Parallel I/O•Serial I/O (SPI and SCI)•Expansion bus

•To add more memory

Motorola/Freescale M68HC11

CMPE12 Cyrus Bazeghi10

HC11 Micro Kit

AC/DC Power adapterSerial

cable to PC

LCD Display

LEDs

Switches

HC11 µC

CMPE12 Cyrus Bazeghi11

Design environment:•Edit the source file on an PC•Assemble on UNIX machine

> “hc11build file.asm”•Download program to the PC with terminal program•Upload to the microkit using serial cable

To use kits at home•9-12V AC adapter•Serial download cable•Connection to ITS machines•Free serial communication software (e.g. TeraTerm)

HC11 Micro Kit

CMPE12 Cyrus Bazeghi12

Micro Kit Usage

Always have “#include <v2_18g3.asm>” as part of your program, this file:

•Sets up memory, including the stack pointer•Jumps to your “main” label•Also includes a basic library of I/O routines•Take a look at it!http://www.soe.ucsc.edu/classes/cmpe012/Fall09/In the handouts and links page under “HC11 Stuff”

CMPE12 Cyrus Bazeghi13

HC11 Architecture

HC11

CMPE12 Cyrus Bazeghi14

M8601 CPU Core • 8-bit• CISC• Accumulator-based• Results wind up in a 8-bit accumulator A or B• 16-bit results go to accumulator AB, called D• Two index registers for addressing memory or for counting - X and Y• Dedicated stack pointer• Push and Pop instructions use this – grows toward smaller memory addresses like in LC-3• Program Counter• Condition Codes

CMPE12 Cyrus Bazeghi15

Check out the lab manual

• It is online on the class website

• Contains a section on the HC11

• It is a great reference

Also get the programmer reference guide, free at BELS (basement of BE)

CMPE12 Cyrus Bazeghi16

M8601 CPU Core

ALU – Arithmetic Logic Unit

CMPE12 Cyrus Bazeghi17

HC11 Microcontroller

CMPE12 Cyrus Bazeghi18

•More present then in LC-3•Single-bit flags set appropriately for most instructions(several instructions do not, including push and pop)

C Carry/BorrowV OverflowZ ZeroN NegativeH Half-Carry

HC11 Microcontroller

Condition Codes

CMPE12 Cyrus Bazeghi19

HC11 code What it does

cmpa #4 Subtract AccA - 4, set CCR

bge mylabel Branch if

(CCR[Z]=1 OR CCR[N]=0)

Example of how condition codes are used

HC11 Microcontroller

CMPE12 Cyrus Bazeghi20

HC11 Assembly Syntax• Have separate sections for data declarations

and for instructions– Use “.sect .data” for data sections– Use “.sect .text” for instructions sections.

• Have more data types– “.space” for array declarations– “.byte” declares just a byte– “.word” declares 16-bits also– “.asciz” declares a string of characters– “.ascii” declares a string, no NULL terminator

• Comments use a “C++” style “//”

CMPE12 Cyrus Bazeghi21

Data Declaration Examples

.sect .data

myvar: .word 0x1000 // init a word

myarray: .space 20 // 20 entries

mychar: .byte „a‟ // a character

mybyte: .byte 23 // DEC integer

myhi: .asciz “Hi” // a string

myhi2: .ascii “Hi” // a string

.sect .text

// Instructions would start here.

// Can have many .text and .data sections.

CMPE12 Cyrus Bazeghi22

Bits set by the user to tell the processor how to dothings

I Interrupt MaskX XIRQ maskS Disable STOP instructions

HC11 Microcontroller

Configuration Registers

CMPE12 Cyrus Bazeghi23

Return to Addressing Modes

Addressing Modes:

LC-3 has:• PC-Relative• Base Displacement• Indirect

HC11 has many: • Check opcode listings to see what modes work

with what instructions• Also check what condition codes are set

CMPE12 Cyrus Bazeghi24

HC11 Addressing Modes

•Immediate (IMM)

•Extended (EXT)

•Direct (DIR)

•Indexed (INDX and INDY)

•Inherent (INH)

•Relative (REL)

Supports these addressing modes:

CMPE12 Cyrus Bazeghi25

Immediate addressing

• 1 or 2 byte immediate depending on register involved (LDAA vs. LDD)

• ALWAYS include a #• Several formats for different bases -- C-style

constants instead of what is in the HC11 manual (don‟t use !,$,@,%)

•Decimal: LDAA #245•Hexadecimal: LDAA #0x61•Octal: LDAA #041•Binary: LDAA #0b11000011•ASCII: LDAA #‟a‟

HC11 Addressing Modes

CMPE12 Cyrus Bazeghi26

• Access an absolute memory location• Essentially the same mode, but with 8-bit (direct)or 16-bit (enhanced or extended) addresses

• The assembler will decide on which to use basedon how many bits are needed

• Example// Your data starts at address 0x4000:.sect .datavar: .word 0x1000 // Allocate/initialize a word

// Note: a word is 16 bits!

.sect .textSUBD var // Subtract M[x4000] from DSUBD 0x4000 // Subtract M[x4000] from D

HC11 Addressing Modes

Extended and Direct addressing

CMPE12 Cyrus Bazeghi27

•Uses index register X or Y•Similar to LC-3 “LDR R0, R2, #3”

•But can access memory and use it all at once!!•Example

#define mydef 4 // c preprocessor usedldx #var // Like LC-3 load addressaddd 0,X // add contents of M[0+X] to D

// (Note “addd X” doesn‟t work)

addd 2,X // add contents of M[2+X] to Daddd mydef*2, X // add contents of M[8+X] to D

HC11 Addressing Modes

Indexed addressing

CMPE12 Cyrus Bazeghi28

•Opcode fully specifies operation; no addressing•Examples:

INCB increment accumulator BASLA Arithmetic Shift Left accumulator APSHY Push index register Y onto stack

HC11 Addressing Modes

Inherent addressing

CMPE12 Cyrus Bazeghi29

• Used for branches by the assembler• Offsets from –128 to +128 bytes• Use jumps for longer branches

HC11 Addressing Modes

Relative addressing

CMPE12 Cyrus Bazeghi30

HC11 Address Modes Review

ldab #0 // loads the number 0 into bldaa foo // loads the contents of byte

// variable foo into acc. Aldy #foo // loads the address of foo into Yldab 0, x // loads the byte at address X+0

// into acc. Bldx 0 // loads whatever is at memory

// address 0 into index reg. X // You don't ever want to do this in // CE12

CMPE12 Cyrus Bazeghi31

• HC11 Instructions have variable lengths (not like LC-3)• Careful coding can keep applications small and able to fit in the EPROM

•We don‟t have to worry about this since we‟re usingExpansion Mode, there is extra memory in the microkits.

• The opcode is always 1 byte• An additional 0-3 bytes specify the data to work with

Motorola 68HC11 Instructions

CMPE12 Cyrus Bazeghi32

•Accumulator and Memory Instructions

•Stack and Index Register Instructions

•Condition Code Register Instructions

•Program Control Instructions

Motorola 68HC11 Instruction Set

Four types of instructions available:

CMPE12 Cyrus Bazeghi33

Accumulator and Memory Instructions

Can be broken up into these 6 general types:

1. Loads, stores, and transfers

2. Arithmetic operations

3. Multiply and divide

4. Logical operations

5. Data testing and bit manipulation

6. Shifts and rotates

HC11 Instructions

CMPE12 Cyrus Bazeghi34

HC11 Instructions

Almost all MCU activities involve transferring data from memories orperipherals into the CPU or transferring results from the CPU intomemory or I/O devices.

CMPE12 Cyrus Bazeghi35

HC11 Instructions

This group of instructions supports arithmetic operations on a variety ofoperands; 8- and 16-bit operations are supported directly and can easilybe extended to support multiple-word operands. Twos-complement(signed) and binary (unsigned) operations are supported directly.

CMPE12 Cyrus Bazeghi36

HC11 Instructions

Compare instructions perform a subtract within the CPU toupdate the condition code bits without altering either operand. Althoughtest instructions are provided, they are seldom needed since almost allother operations automatically update the condition code bits.

CMPE12 Cyrus Bazeghi37

HC11 Instructions

One multiply and two divide instructions are provided. The 8-bit by 8-bitmultiply produces a 16-bit result. The integer divide (IDIV) performs a16-bit by 16-bit divide, producing a 16-bit result and a 16-bit remainder.The fractional divide (FDIV) divides a 16-bit numerator by a larger 16-bitdenominator, producing a 16-bit result (a binary weighted fractionbetween 0 and 0.99998) and a 16-bit remainder.

CMPE12 Cyrus Bazeghi38

HC11 Instructions

This group of instructions is used to perform the Boolean logicaloperations AND, inclusive OR, exclusive OR, and one‟s complement.

CMPE12 Cyrus Bazeghi39

HC11 Instructions

This group of instructions is used to operate on operands as small as asingle bit, but these instructions can also operate on any combination ofbits within any 8-bit location in the 64-Kbyte memory space.

CMPE12 Cyrus Bazeghi40

HC11 Instructions

All the shift and rotate functions in the M68HC11 CPU involve the carrybit in the CCR in addition to the 8- or 16-bit operand in the instruction,which permits easy extension to multiple-word operands.

CMPE12 Cyrus Bazeghi41

Stack and Index Register Instructions

HC11 Instructions

CMPE12 Cyrus Bazeghi42

HC11 Instructions

This table summarizes the instructions available for the 16-bit indexregisters (X and Y) and the 16-bit stack pointer.

CMPE12 Cyrus Bazeghi43

HC11 System Stack

• The HC11 has a stack set up for us• Memory is “byte” addressable

Example:

PSHA // pushes accumulator A onto stack

// SP = SP – 1 since a byte size

PSHY // pushes index register Y onto stack

// SP = SP – 2 since a word size

PULA // Pops off a byte from the stack into A

// SP = SP + 1

PULY // Pops off a word from the stack into Y

// SP = SP + 2

CMPE12 Cyrus Bazeghi44

HC11 Instructions

Condition Code Register Instructions

These instructions allow a programmer to manipulate bits of the CCR.

CMPE12 Cyrus Bazeghi45

Program Control Instructions

1. Branches2. Jumps3. Subroutine calls and returns4. Interrupt handling5. Miscellaneous

HC11 Instructions

CMPE12 Cyrus Bazeghi46

These instructions allow the CPU to make decisions based on thecontents of the condition code bits. All decision blocks in a flow chartwould correspond to one of the conditional branch instructions summarized here

HC11 Instructions

CMPE12 Cyrus Bazeghi47

HC11 Instructions

The jump instruction allows control to be passed to any address in the 64-Kbyte memory map.

CMPE12 Cyrus Bazeghi48

HC11 Instructions

These instructions provide an easy way to divide a programming taskinto manageable blocks called subroutines. The CPU automates theprocess of remembering the address in the main program whereprocessing should resume after the subroutine is finished. This addressis automatically pushed onto the stack when the subroutine is called andis pulled off the stack during the RTS instruction that ends thesubroutine

CMPE12 Cyrus Bazeghi49

This group of instructions is related to interrupt operations, we will get to there use later.

HC11 Instructions

CMPE12 Cyrus Bazeghi50

NOP is a do nothing instruction, just wastes an instruction cycle. STOP is used to put the CPU into a low power mode. TEST is a reserved instruction only used at the factory when making the chips.

HC11 Instructions

CMPE12 Cyrus Bazeghi51

“C” Conversions

“C” Addition

result = v0 + v1

// 8-bit variablesldab v0addb v1stab result

“HC11” Assembly// 16-bit variables

ldd v0addd v1std result

CMPE12 Cyrus Bazeghi52

“C” Conversions

“HC11” Assembly

ldab v0ldaa v1abastab v0

“C” Addition

char V0, V1;

v0 = v0 + v1

CMPE12 Cyrus Bazeghi53

“C” Multiplication

int Result // 16-bit char Val0, Val1 // 8-bit

Result = Val0 * Val1

“HC11” Assembly

ldaa Val0ldab Val1mulstd Result

“C” Conversions

CMPE12 Cyrus Bazeghi54

“C” if statement:

if(a > 10) y++;

“HC11” Assembly:

// using Acc A & index Y

ldaa acmpa #10ble endifinysty y

endif:

“C” Conversions

CMPE12 Cyrus Bazeghi55

“C” Conversions

“C” if/else statement:

char a; // 8-bitint y; // 16-bits

if(a > 10) y++;

else y--;

“HC11” Assembly:

cmpa #10bgt ifdeybra endif

if: inyendif:

CMPE12 Cyrus Bazeghi56

“C” Conversions

“C” if/else statement:

char foo; // 8-bitsint qux; // 16-bits

if(foo > 10) qux++;

else qux--;

“HC11” Assembly:

ldaa fooldy quxcmpa #10bgt ifdeybra endif

if: inyendif: sty qux

CMPE12 Cyrus Bazeghi57

“C” Conversions

“C” while statement:

char a, b;

while(a > 20) {a--; b *= 3;}

“HC11” Assembly:

// Using Acc A and Bwhile:

cmpa #20ble endwhiledecapshaldaa #3mulpulabra while

endwhile:

CMPE12 Cyrus Bazeghi58

“C” do/while statement:

int foo, bar, qux;

do {foo -= 3; bar += qux;}

while (foo > 7)

“HC11” Assembly:

do:ldd foosubd #3std foo

ldd baraddd quxstd bar

ldd foocpd #7bgt do

“C” Conversions

CMPE12 Cyrus Bazeghi59

Summing the first 10 elements of a 16-bit integer array, result in index register X:

array: .space 20

ldx #0ldab #0

while: cmpb #18bgt endwhile

ldy #arrayaby

xgdxaddd 0,yxgdx

incbincbbra while

endwhile:

HC11 Example

CMPE12 Cyrus Bazeghi60

HC11 Example

/************************************************************ Program 1.* A simple program to introduce the 68HC11 environment***********************************************************/

#include <v2_18g3.asm> // sets stuff up for you, especially I/O

.sect .data

SIGNON: .asciz “CMPE12C Simulator”PROMPT: .asciz “>”

// (continued…)

CMPE12 Cyrus Bazeghi61

HC11 Example Continued

/************************************************************** Your program starts where indicated by the label “main”. After startup * Code in v2_18g3.asm is done, it jumps to this label and continues running.**************************************************************/

.sect .text

main:ldx #SIGNONjsr OUTSTRING

loop: ldx #PROMPT // address of promptjsr OUTSTRING // write out to serial communications

// interface (to PC)jsr GETCHAR // get a character from SCI, returns in Ajsr OUTCHAR // write A to the SCIjsr OUTCRLF // write CR/LF to SCIjmp loop

CMPE12 Cyrus Bazeghi62

Example recursive routine

Fibonacci on the switches and LEDs

/* Input: A. Returns A‟th Fibonacci term in accumulator A */fib: cmpa #2

bgt fibnot1ldaa #1 // return 1 if n <= 2rts

fibnot1: pshb // caller save protocoldecapsha // save n-1deca // call with n-2jsr fibtab // transfer F(n-2) to bpula // a gets n-1jsr fibaba // add accumulatorspulb // restore Brts

main: ldx #SWITCHESldaa 0,X

jsr fibldx #LEDSstaa 0,xjmp main

CMPE12 Cyrus Bazeghi63

Microcontroller/HC11 Summary

•Microcontrollers are great little processors for I/O and data manipulation•The CISC-style programming makes assembly programmingeasier•Variable instruction length can be a problem for

•Word-aligned machines•Super-scalar machines•Pipelines

•The RISC philosophy is to•Use simple instructions and let the compiler optimize•Use loads and stores to support higher-speed registers

•Neither RISC, its CISC predecessors, nor CISCY RISCY hasthe definitive advantage

•Depends on application, architecture, and implementation.