Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Programming the HC12 in C

2 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

3 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;

4 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;

5 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)

6 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; }

7 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; }

8 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;

9 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};

10 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;

11 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

12 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) ;

13 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?


Download ppt "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."

Similar presentations


Ads by Google