Programming the HC12 in C. Some Key Differences – Note that in C, the starting location of the program is defined when you compile the program, not in.

Slides:



Advertisements
Similar presentations
Branches Two branch instructions:
Advertisements

7-5 Microoperation An elementary operations performed on data stored in registers or in memory. Transfer Arithmetic Logic: perform bit manipulation on.
EET 2261 Unit 5 Tables; Decision Trees & Logic Instructions
Revised: Aug 1, EE4390 Microprocessors Lesson 6,7 Instruction Set, Branch Instructions, Assembler Directives.
Addressing Modes & Instruction Set By: Prof. Mahendra B. Salunke Asst. Prof., Department of Computer Engg., SITS, Pune-41 URL:
Assembler Programming Chapter 6. EEL-4746 Best Practices.
68HC11 Polling and Interrupts
Bitwise Operators Pointers Functions Structs Interrupts (in C)
C Language Programming. C has gradually replaced assembly language in many embedded applications. Data types –C has five basic data types: void, char,
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)
Unsigned and Signed Numbers. Hexadecimal Number 217A 16 Position Digits A Value = 2x x x16 + Ax1 = 2x x x16.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
EECC250 - Shaaban #1 Lec # 20 Winter Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
9/20/6Lecture 3 - Instruction Set - Al Instruction Set (2)
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Computer Organization and Assembly Language Bitwise Operators.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
ECE 447: Lecture 12 Logic, Arithmetic, Data Test and Control Instructions of MC68HC11.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
6-1 EE 319K Introduction to Microcontrollers Lecture 6: Indexed Addressing Mode and Variants, Functional Debugging, Arrays, Strings.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
ELE22MIC Lecture 8 ASll Examples –16 Bit Counters –Buffalo Jump Table Interrupt processing (IRQ/RTI) Stack Frame & Base Pointer Wired OR.
ECE 265 – LECTURE 5 The M68HC11 Basic Instruction Set 12/8/ ECE265.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
1 ECE 372 – Microcontroller Design Assembly Programming HCS12 Assembly Programming Addressing Modes Stack Operations Subroutines.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
ECE Lecture 21 Typical Assembly Language Program Bugs.
Chapter 10 Instruction Sets: Characteristics and Functions Felipe Navarro Luis Gomez Collin Brown.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
LHO 22 C and the  The Silicon Labs ISE uses the Keil C51 compiler.  The code size is limiter to 2K  C has replaced PL/M (the original Intel high.
ECE 447: Lecture 16 Common Errors & Good Programming Style.
Advanced Assembly Language Programming
What is a program? A sequence of steps
ECE 447: Lecture 11 Introduction to Programming in Assembly Language.
Embedded Systems Lecture 5 January 25 th, 2016.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
Physics 413 Chapter 4: Advanced Assembly Programming.
ARM Embedded Programming Lecture 4 Accessing Memory Mapped Registers.
1 Subroutines Advanced Programming. 2 Top-Down approach to problem solving Algorithm step by step description of how to solve a problem Divide and Conquer.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
EE345 Chapter 2 Lecture 3 April Instruction and addressing modes 1.Extended Addressing 2.Direct Addressing 3.Inherent Addressing 4.Immediate Addressing.
ECE 447: Lecture 13 Assembler Directives. ECE 447: Defining a Constant #define PORTB 0x1004PORTB EQU $1004 #define DELAY 100 ………. #undef DELAY #define.
The Stack.
Processor Instructions set. Learning Objectives
Chapter 3 Bit Operations
CS 11 C track: lecture 8 Last week: hash tables, C preprocessor
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
MISP Assembly.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
AVR Programming in C Chapter 7
CS-401 Computer Architecture & Assembly Language Programming
AVR Programming in C Chapter 7
ECEG-3202 Computer Architecture and Organization
Homework Homework Continue Reading K&R Chapter 2 Questions?
CISC/CMPE320 - Prof. McLeod
ECEG-3202 Computer Architecture and Organization
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Operations and Arithmetic
MIPS Assembly.
Generalities for Assembly Language
Bit Manipulations CS212.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Programming the HC12 in C

Some Key Differences – Note that in C, the starting location of the program is defined when you compile the program, not in the program itself. – Note that C always uses the stack, so C automatically loads the stack pointer for you. Don’t need to worry as much about the actual location of variables

Some Command Differences ; Use a name instead of a num COUNT: EQU 5 /* Use a name instead #define COUNT 5 ;start a program CODE: section.text org $0800 lds #0x0A00 /* To start a program */ main() { } ;allocate two bytes for a signed number DATA: section.data org $0900 i: ds.w 1 j: dc.w $1A00 /* Allocate two bytes for a signed *number */ int i; int j = 0x1a00; ;allocate two bytes for an unsigned number i: ds.w 1 j dc.w $1A00 /* Allocate two bytes for * an unsigned number */ unsigned int i; unsigned int j = 0x1a00;

Compare C cont ; Use a variable to store ; an address i: ds.b 1 ptr: ds.b 1 ldd #$E000 std ptr ldx ptr ldaa 0,x staa i ldx ptr ldaa #$55 staa 0,x /* Use a variable as a pointer (address) */ unsigned char *ptr, i; ptr = (unsigned char *) 0xE000; i = *ptr; *ptr = 0x55;

Compare C ;Get a value from an address $E000. Store in i ‘i: DS.B 1 LDX $E000 LDAA 0,X STAA I /* Get a value from an address */ unsigned char i; i = *(unsigned char *) 0xE000; ;To return from a subroutine ldaa j rts /* To return from a function */ return j; ;Flow control blt bge /* Flow control */ if (i < j) if (i >= j)

Simple Program 1 COUNT EQU 5 org $0900 i: ds.w 1 org $0800 lds #$0A00 ldd #COUNT std i #define COUNT 5 unsigned int i; main() { i = COUNT; }

Simple Prog 2 org $0900 i: ds.b 1 org $0800 lds #$0A00 ldaa #16 jsr div staa i div: asra rts signed char div(signed char j); signed char i; main() { i = div(16); } signed char div(signed char j) { return j/2; }

Pointers in C To access a memory location: *address You need to tell compiler whether you want to access 8-bit or 16 bit number, signed or unsigned: *(type *)address – To read from an eight-bit unsigned number at memory location 0x2000: x = *(unsigned char *)0x2000; – To write an 0xaa55 to a sixteen-bit signed number at memory locations 0x2010 and 0x2011: *(signed int *)0x2010 = 0xaa55;

More Pointers To access consecutive locations in memory, use a variable as a pointer: unsigned char *ptr; ptr = (unsigned char *)0x2000; *ptr = 0xaa; /* Put 0xaa into address 0x2000 */ ptr = ptr+2; /* Point two further into table */ x = *ptr; /* Read from address 0x2002 */ To set aside ten locations for a table: unsigned char table[10]; Can access the third element in the table as: table[2] or as *(table+2) To set up a table of constant data: const unsigned char table[] = {0x00,0x01,0x03,0x07,0x0f};

Logic and Arithmetic Arithmetic operators: + (add) x = a+b; - (subtract) x = a-b; * (multiply) x = a*b; / (divide) x = a/b; % (modulo) x = a%b; (Remainder on divide) Logical operators & (bitwise AND) y = x & 0xaa; | (bitwise OR) y = x | 0xaa; ^ (bitwise XOR) y = x ^ 0xaa; << (shift left) y = x << 1; >> (shift right) y = x >> 2; ~ (1’s complement) y = ~x; - (2’s complement - negate) y = -x;

Bitwise Instructions To set bit 4 of PORTA leaving the other bits unchanged: – In assembly bset PORTA,#$10 – In C do a bitwise OR PORTA = PORTA | 0x10; To clear a bits 0 and 5 of PORTA) – In assembly bclr PORTA,#$21 – In C, do a bitwise AND of the register with a mask that has 0’s in the bits you want to clear: PORTA = PORTA & 0xDE; or PORTA = PORTA & ~0x21 ; ~ one’s complement

Waiting for a bit to be set or cleared Here is how to wait until Bit 3 of PORTB is set: – In assembly: L1: ldaa PORTB bita #$08 beq L1 OR l1: brclr PORTB,#$08,l1 – In C: while ((PORTB & 0x08) == 0) ; Here is how to wait until Bit 3 of PORTB is cleared: l1: brset PORTB,#$08,l1 – In C: while ((PORTB & 0x08) == 0) ; while ((PORTB & 0x08) != 0) ;

Delay Routine void delay(unsigned int num) { unsigned int i; while (num > 0) { i = XXXX; /* */ while (i > 0) /* */ {/* Want inner loop to delay */ i = i - 1;/* for 1 ms */ } /* */ /* */ num = num - 1; } What should XXXX be to make a 1 ms delay?