Lecture 36 April 25, 2012 Review for Exam 3. Linking Assembly Subroutine with a C Program. A/D Converter

Similar documents
Linking Assembly Subroutine with a C Program

Review for Exam 3. Write 0x05 to ATD0CTL4 to set at fastest conversion speed and 10-bit conversions

Sample Problem Set #1

The MC9S12 IIC Interface

Dallas Semiconductor DS1307 Real Time Clock. The DS 1307 is a real-time clock with 56 bytes of NV (nonvolatile)

MC9S12 Assembler Directives A Summary of MC9S12 Instructions Disassembly of MC9S12 op codes. Summary of HCS12 addressing modes ADDRESSING MODES

Introduction to Programming the 9S12 in C Huang Sections 5.2 and 5.3. You will be able to use all of the Motorola data manuals on the exam.

Review for Exam III. Analog/Digital Converters. The MC9S12 has two 10-bit successive approximation A/D converters - can be used in 8-bit mode

ECE 4510/5530 Microcontroller Applications Week 10

Using the stack and the stack pointer

EE319K Final Fall 2005 Solution C. (3) Question 1. (3) Question 2. short function(const short in){ return in+5; } const

2. Arithmetic Instructions addition, subtraction, multiplication, divison (HCS12 Core Users Guide, Sections 4.3.4, and ).

HCS12 Inter-Integrated Circuit(IIC) Block Guide V02.06

Coe538 Final Study Guide 2016 (Questions & Answers)

ME4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 7

A B C D E F 0480 FE B F5 3B FC F3 E 1A 1D 2A 2D 3A 3D 4A 4D 5A 5D 6A 6D 7A 7D

Decimal, Hexadecimal and Binary Numbers Writing an assembly language program

Microcontrollers. 2IN60: Real-time Architectures (for automotive systems) Mike Holenderski,

HCS12 Inter-Integrated Circuit(IIC) Block Guide V02.08

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

Exam 1 Feb. 23, 25, 27?

EE 5340/7340 Motorola 68HC11 Microcontroler Lecture 1. Carlos E. Davila, Electrical Engineering Dept. Southern Methodist University

Lecture 9 Subroutines

CodeWarrior. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

ECE331 Handout 3- ASM Instructions, Address Modes and Directives

538 Lecture Notes Week 5

Exam I Review February 2017

Introduction to the MC9S12 Hardware Subsystems

538 Lecture Notes Week 5

Computer Systems Lecture 9

Parallel Data Transfer. Suppose you need to transfer data from one HCS12 to another. How can you do this?

Lecture 25 March 23, 2012 Introduction to Serial Communications

Addition and Subtraction of Hexadecimal Numbers Simple assembly language programming

ECE3120: Computer Systems Hardware & Software Development Tools

Addition and Subtraction of Hexadecimal Numbers Simple assembly language programming

CMPEN 472 Sample EXAM II

Most of the HC12 s instructions access data in memory There are several ways for the HC12 to determine which address to access

Introduction to Programming the 9S12 in C Huang Sections 5.2 and 5.3

Comparison of C and Assembly How to compile a C program using CodeWarrior

The 9S12 in Expanded Mode - How to get into expanded mode Huang Chapter 14

EE 308 Spring Lab on IIC Bus

EE4390 Microprocessors. Lessons 2, 3 68HC12 Hardware Overview, Subsystems, and memory System

Introduction to the 9S12 Microcontroller

Programming the Motorola MC68HC11 Microcontroller

Lab 7: Asynchronous Serial I/O

EE345L Spring 2004 Final Version 3 Page 1 of 8

; export symbols ; export 'Entry' symbol. ; include derivative specific macros PORTA EQU $0000 PORTB EQU $0001 DDRA EQU $0002 DDRB EQU $0003

Ryerson University Department of Electrical and Computer Engineering ELE 538 Microprocessor Systems Final Examination December 8, 2003

1. Memory Mapped Systems 2. Adding Unsigned Numbers

Exam 2 E2-1 Fall Name: Exam 2

Functional block diagram AD53x1 (Analog Devices)

Lecture #4 Microcontroller Instruction Set Embedded System Engineering Philip Koopman Monday, 25-Jan-2016

Quick Guide to Using Code Warrior

EE 308 Spring The HCS12 has 6 addressing modes

S12CPUV2. Reference Manual HCS12. Microcontrollers. S12CPUV2/D Rev. 0 7/2003 MOTOROLA.COM/SEMICONDUCTORS

MC68HC12 Parallel I/O

Asynchronous Data Transfer

N bit is set if result of operation in negative (MSB = 1) Z bit is set if result of operation is zero (All bits = 0)

ME 6405 Introduction to Mechatronics

EE345L Spring 2006 May 10, 2006, 2-5pm Page 1 of 8

Introduction the Serial Communications Parallel Communications Parallel Communications with Handshaking Serial Communications

Lab 2 Part 1 Assembly Language Programming and 9S12 Ports

Design of Embedded Systems Using 68HC12/11 Microcontrollers

Input and Output Ports. How do you get data into a computer from the outside?

EE 308 Spring A software delay. To enter a software delay, put in a nested loop, just like in assembly.

; export symbols XDEF Entry ; export 'Entry' symbol ABSENTRY Entry ; for assembly entry point

The 9S12 in Expanded Mode - How to get into expanded mode Huang Chapter 14

538 Lecture Notes Week 7

Fuzzy Logic Controllers. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

ECE 331: PC Lab 3 Stack and Subroutines

538 Lecture Notes Week 3

Chapter 4: Advanced Assembly Programming. EE383: Introduction to Embedded Systems University of Kentucky. Samir Rawashdeh

Chapter 2: HCS12 Assembly Programming. EE383: Introduction to Embedded Systems University of Kentucky. Samir Rawashdeh

TEMPERATURE SENSOR. Revision Class. Instructor / Professor LICENSE

Lecture 5 Assembly Programming: Arithmetic

Fredrick M. Cady. Assembly and С Programming forthefreescalehcs12 Microcontroller. шт.

Mask Set Errata for 68HC912DG128C, Mask 1M63Z

Comparison of C and Assembly How to compile a C program using CodeWarrior

LECTURE #21: G-CPU & Assembly Code EEL 3701: Digital Logic and Computer Systems Based on lecture notes by Dr. Eric M. Schwartz

EE 308 Spring 2013 The MC9S12 A/D Converter

EE 3170 Microcontroller Applications

MIGRATING TO THE 68HC12 IN C

Exam 2 E2-1 Fall Name: Exam 2

Menu Computer Organization Programming Model for the an example microprocessors (the G-CPU & Motorola 68HC11) Assembly Programming Look into my...

The MC9S12 in Expanded Mode Using MSI logic to build ports Using MSI logic to build an output port Using MSI logic to build an input port

EE 3170 Microcontroller Applications

Lecture 11: Advanced Arithmetic Instructions

Homework 12 Solutions

ECE 3120 Computer Systems Arithmetic Programming

EE 308 Spring Exam 1 Feb. 27

The MC9S12 Timer Input Capture Function

Module 1-D. Control Structure Applications. Tim Rogers 2017 [1.D]-1

EE 308 Spring 2011 The MC9S12 in Expanded Mode How to get into expanded mode

A Simple MC9S12 Program

538 Lecture Notes Week 2

COE538 Lecture Notes Week 3 (Week of Sept 17, 2012)

History of the Microprocessor. ECE/CS 5780/6780: Embedded System Design. Microcontrollers. First Microprocessors. MC9S12C32 Block Diagram

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-470/570: Microprocessor-Based System Design Fall 2014.

CHAPTER 8. Solutions for Exercises

Transcription:

Lecture 36 April 25, 2012 Review for Exam 3 Linking Assembly Subroutine with a C Program A/D Converter Power-up A/D converter (ATD1CTL2) Write 0x05 to ATD1CTL4 to set at fastest conversion speed and 10-bit conversions Write 0x85 to ATD1CTL4 to set at fastest conversion speed and 8-bit conversions Select number of conversions in a sequence (ATD1CTL3) Select type of conversion sequence and the analog channels sampled (ATD1CTL5) Right/left justified signed/unsigned Continuous Scan vs. Single Scan Multichannel vs. Single Channel conversions How to tell when conversion is complete - ATD1STAT0 register How to read results of A/D conversions - ATD1DR[7 0]H (8-bit left-justified conversions) How to read results of A/D conversions - ATD1DR[7 0]L (8-bit right-justified conversions) How to read results of A/D conversions - ATD1DR[15 6] (10-bit left-justified conversions) How to read results of A/D conversions - ATD1DR[9 0] (10-bit right-justified conversions) Be able to convert from digital number to voltage, and from voltage to digital number (need to know V RH and V RL ). How long does it take to make a conversion? 1

Pins used SDA and SCl Serial Communications and the IIC Bus Difference of use in Master and Slave mode IIC serial format for writing to slave Start condition, 7-bit slave address, R/W, wait for acknowledge Send eight data bits, wait for ACK, repeat, send stop condition IIC serial format for reading from slave Start condition, 7-bit slave address, R/W, wait for acknowledge Receive eight data bits, send ACK, repeat, after receiving last byte, send NACK instead of ACK, send stop condition IIC IBAD (Bus Address) register Set address when used as slave To use as master, write something like 0x01 (any address not assigned to a slave) IIC IBFD (Bus Frequency Divide) Register Set clock speed to match slave IIC IBCR (Bus Control Register) Register IBEN Enable IIC bus IBIE Enable interrupts MS/SL Switch to master mode TX/RX Switch between transmit and receive TKAK Send an acknowledge RSTA Send an restart (didn t discuss) IBSWAI Specify if IIC clock should operate in WAIT mode (didn t discuss) IIC IBSR (Bus Status Register) Register TCF Transmit Complete Flag IAAS Did not discuss IBB Did not discuss IBAL Did not discuss SRW Did not discuss IBIF Interrupt flag. Clear by writing a 1 to this bit. RXAK Did not discuss IIC IBDR (Bus Data Register) Register Write data to this register to send to slave Read data from this register to receive from slave 2

Interfacing Getting into expanded mode MODA, MODB, MDOC pins or MODE Register PEAR Register enable ECLK, LSTRB, R/W on external pins Ports A and B in expanded mode Port A AD 15-8 (Port A is for data for high byte, even addresses) Port B AD 7-0 (Port B is for data for low byte, odd addresses) E clock Address on AD 15-0 when E low, Data on AD 15-0 when E high Need to latch address on rising edge of E clock On write (output), external device latches data on signal initiated by falling edge of E On read (input), HCS12 latches data on falling edge of E E-clock stretch - MISC register R/W Line LSTRB line Single-byte and two-byte accesses 16-bit access of even address A0 low, LSTRB low accesses even and odd bytes 8-bit access of even address A0 low, LSTRB high accesses even byte only 8-bit access of odd address A0 high, LSTRB low accesses odd byte only A0 high and LSTRB high never occurs on external bus. Address Decoding interfacing using MSI chips Timing Be sure to meet setup and hold times of device receiving data For a write, meet setup and hold of external device For a read, meet setup and hold of HC12 Timing Be sure to meet address access time (length of time address needs to be on bus before external device is ready) 3

Linking Assembly Subroutine with a C Program To link an assembly subroutine to a C program, you have to understand how parameters are passed. For the CodeWarrior C compiler, one parameter is passed in the registers. The other parameters are passed on the stack. The left-most parameter is pushed onto the stack first, then the next-to-left, etc. The right-most parameter is passed in the registers An 8-bit parameter is passed in the B regisiter A 16-bit parameter is passed in the D register A 32-bit parameter is passed in the {X:D} register combination. A value returned from the assembly language program is returned in the registers: An 8-bit parameter is returned in the B regisiter A 16-bit parameter is returned in the D register A 32-bit parameter is returned in the {X:D} register combination. In the assembly language program, declare things the C program has to know about as XDEF: XDEF foo In the C program, declare things in the assembly program as you would an other functions: int foo(int x); In the assembly language program, use the stack to store local variables Need to keep close track of stack frame 4

Consider an assembly-language function fuzzy which uses two 8-bit arguments arg1 and arg2, and returns an 8-bit argument result. Declare the function in the C program as char fuzzy(char arg1, char arg2); Here is how the function may be called in the C program: char x,y,result; result = fuzzy(x, y); When the program is compiled, the value of the variable x is pushed onto the stack, the value of the variable y is loaded into the B register, and the function is called with a JSR instruction: 15: result = fuzzy(x,y); /* call the assembly function */ 000b f60000 [3] LDAB x 000e 37 [2] PSHB 000f f60000 [3] LDAB y 0012 160000 [4] JSR fuzzy In the assembly language function, you may need to use some local variables, which need to be allocated on the stack. If the fuzzy function needs two local 8-bit variables var1 and var2, you will need to allocate two bytes on the stack for them. Here s what the start of the assembly language program will look like: fuzzy: leas -2,sp ; Room on stack for var1 and var2 ; Stack frame after leas -2,sp ; ; SP -> var1 ; SP + 1 -> var2 ; SP + 2 -> Return address high ; SP + 3 -> Return address low ; SP + 4 -> 1st parameter of function (arg1) ; ; 2nd paramter (arg2) passed in B register In the assembly language program, you access arg1, var1 and var2 with indexed addressing mode: stab 1,SP ; Save arg2 into var2 ldaa 4,SP ; Put arg1 into ACCA staa 0,SP ; Save arg1 into var1 5

When you return from the assembly language function, put the value you want to return into B, add two to the stack (to deallocate var1 and var2), and return with an RTS. For example, it you want to return the value of the variable var2, you would do the following: ldab 1,SP ; Put var2 into B leas 2,SP ; Deallocate local variables rts ; Return to calling program Any global variables used by the program should be declared in a separate section: ; section for global variables FUZZY_RAM: SECTION ; Locations for the fuzzy input membership values I_E_PM: ds.b 1 I_E_PS: ds.b 1 Any global constants used by the program should be declared in a separate section: ; section for global variables FUZZY_RAM: SECTION ; Locations for the fuzzy input membership values I_E_PM: ds.b 1 I_E_PS: ds.b 1 FUZZY_CONST: SECTION ; Fuzzy input membership function definitions for speed error E_Pos_Medium: 170, 255, 6, 0 E_Pos_Small: 128, 208, 6, 6 The assembly language code should be put in its own section: ; code section MyCode: SECTION ; this assembly routine is called by the C/C++ application fuzzy: leas -2,sp ; Room on stack for ERROR and d_error 6

C program which calls fuzzy logic assembly function #include <hidef.h> /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ #include <stdio.h> #include <termio.h> int fuzzy(unsigned char e, unsigned char de); unsigned char ERROR[] = {128,128,128,100,150}; unsigned char d_error[] = {128,100,150,128,128}; void main (void) { char dpwm; int i; /* Set up SCI for using printf() */ SCI0BDH = 0x00; /* 9600 Baud */ SCI0BDL = 0x9C; SCI0CR1 = 0x00; SCI0CR2 = 0x0C; /* Enable transmit, receive */ } for (i=0;i<5;i++) { dpwm = fuzzy(error[i],d_error[i]); (void) printf("error = %3d, d_error = %3d, ", ERROR[i],d_ERROR[i]); (void) printf("dpwm: %4d\r\n", dpwm); } asm(" swi"); 7

The Assembly program ;************************************************************** ;* This stationery serves as the framework for a * ;* user application. For a more comprehensive program that * ;* demonstrates the more advanced functionality of this * ;* processor, please see the demonstration applications * ;* located in the examples subdirectory of the * ;* Freescale CodeWarrior for the HC12 Program directory * ;************************************************************** ; export symbols XDEF fuzzy ; we use export Entry as symbol. This allows us to ; reference Entry either in the linker.prm file ; or from C/C++ later on ; Include derivative-specific definitions INCLUDE derivative.inc ; Offset values for input and output membership functions E_PM equ 0 ; Positive medium error E_PS equ 1 ; Positive small error E_ZE equ 2 ; Zero error E_NS equ 3 ; Negative small error E_NM equ 4 ; Negative medium error de_pm equ 5 ; Positive medium differential error de_ps equ 6 ; Positive small differential error de_ze equ 7 ; Zero differential error de_ns equ 8 ; Negative small differential error de_nm equ 9 ; Negative medium differential error O_PM equ 10 ; Positive medium output O_PS equ 11 ; Positive small O_ZE equ 12 ; Zero output O_NS equ 13 ; Negative small O_NM equ 14 ; Negative medium output MARKER equ $FE ; Rule separator END_MARKER equ $FF ; End of Rule marker ; variable/data section FUZZY_RAM: SECTION ; Locations for the fuzzy input membership values for speed error I_E_PM: ds.b 1 I_E_PS: ds.b 1 I_E_ZE: ds.b 1 I_E_NS: ds.b 1 I_E_NM: ds.b 1 8

; Locations for the fuzzy input membership values for speed error diff I_dE_PM: ds.b 1 I_dE_PS: ds.b 1 I_dE_ZE: ds.b 1 I_dE_NS: ds.b 1 I_dE_NM: ds.b 1 ; Output fuzzy membership values - initialize to zero M_PM: ds.b 1 M_PS: ds.b 1 M_ZE: ds.b 1 M_NS: ds.b 1 M_NM: ds.b 1 FUZZY_CONST: SECTION ; Fuzzy input membership function definitions for speed error E_Pos_Medium: 170, 255, 6, 0 E_Pos_Small: 128, 208, 6, 6 E_Zero: 88, 168, 6, 6 E_Neg_Small: 48, 128, 6, 6 E_Neg_Medium: 0, 80, 0, 6 ; Fuzzy input membership function definitions for speed error de_pos_medium: 170, 255, 6, 0 de_pos_small: 128, 208, 6, 6 de_zero: 88, 168, 6, 6 de_neg_small: 48, 128, 6, 6 de_neg_medium: 0, 80, 0, 6 ; Fuzzy output memership function definition PM_Output: 192 PS_Output: 160 ZE_Output: 128 NS_Output: 96 NM_Output: 64 ; Rule Definitions Rule_Start: E_PM,dE_PM,MARKER,O_NM,MARKER E_PM,dE_PS,MARKER,O_NM,MARKER E_PM,dE_ZE,MARKER,O_NM,MARKER E_PM,dE_NS,MARKER,O_NS,MARKER E_PM,dE_NM,MARKER,O_ZE,MARKER E_PS,dE_PM,MARKER,O_NM,MARKER E_PS,dE_PS,MARKER,O_NM,MARKER E_PS,dE_ZE,MARKER,O_NS,MARKER E_PS,dE_NS,MARKER,O_ZE,MARKER E_PS,dE_NM,MARKER,O_PS,MARKER 9

E_ZE,dE_PM,MARKER,O_NM,MARKER E_ZE,dE_PS,MARKER,O_NS,MARKER E_ZE,dE_ZE,MARKER,O_ZE,MARKER E_ZE,dE_NS,MARKER,O_PS,MARKER E_ZE,dE_NM,MARKER,O_PM,MARKER E_NS,dE_PM,MARKER,O_NS,MARKER E_NS,dE_PS,MARKER,O_ZE,MARKER E_NS,dE_ZE,MARKER,O_PS,MARKER E_NS,dE_NS,MARKER,O_PM,MARKER E_NS,dE_NM,MARKER,O_PM,MARKER E_NM,dE_PM,MARKER,O_ZE,MARKER E_NM,dE_PS,MARKER,O_PS,MARKER E_NM,dE_ZE,MARKER,O_PM,MARKER E_NM,dE_NS,MARKER,O_PM,MARKER E_NM,dE_NM,MARKER,O_PM,END_MARKER ; code section MyCode: SECTION ; this assembly routine is called by the C/C++ application ; Stack frame after leas -2,sp ; ; SP -> ERROR ; SP + 1 -> d_error ; SP + 2 -> Return address high ; SP + 3 -> Return address low ; SP + 4 -> 1st parameter of function (d_error) ; ; 2nd paramter (ERROR) passed in B register fuzzy: leas -2,sp ; Room on stack for ERROR and d_error stab 1,sp ; d_error passed in B register ldab 4,sp ; ERROR passed on stack stab 0,sp ; Save in space reserved on stack ; Fuzzification LDX #E_Pos_Medium ; Start of Input Mem func LDY #I_E_PM ; Start of Fuzzy Mem values LDAA 0,SP ; Get ERROR value LDAB #5 ; Number of iterations Loop_E: MEM ; Assign mem value DBNE B,Loop_E ; Do all five iterations LDAA 1,SP ; Get d_error value LDAB #5 ; Number of iterations Loop_dE: MEM ; Assign mem value 10

DBNE B,Loop_dE ; Do all five iterations ; Process rules LDX #M_PM ; Clear output membership values LDAB #5 Loopc: CLR 1,X+ DBNE B,Loopc ; Defuzzification LDX #Rule_Start ; Address of rule list -> X LDY #I_E_PM ; Address of input membership list -> Y LDAA #$FF ; FF -> A, clear V bit of CCR REV ; Rule evaluation LDX #PM_Output ; Address of output functions -> X LDY #M_PM ; Address of output membership values -> Y LDAB #5 ; Number of iterations WAV ; Defuzzify EDIV ; Divide TFR Y,D ; Quotient to D; B now from 0 to 255 SUBB #128 ; Subtract offset from d_pwm ; dpwm returned in B; already there leas 2,sp ; Return stack frame to entry value RTS 11

Output of Program load ************** > >g 2029 hello, world ERROR = 128, d_error = 128, dpwm: 0 ERROR = 128, d_error = 100, dpwm: 22 ERROR = 128, d_error = 150, dpwm: -18 ERROR = 100, d_error = 128, dpwm: 22 ERROR = 150, d_error = 128, dpwm: -18 User Bkpt Encountered PP PC SP X Y D = A:B CCR = SXHI NZVC 38 2B9E 10FD 0005 0001 00:0C 1001 0100 xx:2b9e 1B83 LEAS 3,SP > 12