Top Banner
C and Assembler Subroutines: Using the LCD
17

C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

C and Assembler Subroutines: Using the LCD

Page 2: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

Outline

• Basic structure of CW-created C programs for the HC12• How to incorporate assembly code• How to use subroutines

– With no pass variables

– With pass variables

Page 3: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

Compiler selection

• Check both C and assembler to use both varieties of code when using the project wizard

Page 4: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

The Project Folder

• The project folder looks a lot like it did before with some new folders• Lets open some and see what’s inside• Most likely we will only need the prm and the sources folders

New

Page 5: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

The Bin Folder

• These are created after you compile. There are corresponding files created for the simulator and the monitor board.

• At this point these files are created after compiling for the simulator only

Parameters created during compilation inclusing RAM, ROM start/end addresses, all the variables used, program statistics

The raw machine code that will be pushed out to the board

Page 6: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

The S19 and S32 Source Files

• Here is the S19 file created by CW. This is the binary data (in hex) that will get pushed to the board.

• Many applications are available only in this form without the source code

• This code can be pushed with the simple debug program shipped by Technological Arts. We use CW.

Page 7: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

The PRM folderSimulator and Monitor Linkers

/* This is a linker parameter file for the MC9S12C32 */NAMES END /* CodeWarrior will pass all the needed files to the linker by command line. But here you may add your own files too. */

SEGMENTS /* here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */ RAM = READ_WRITE 0x3800 TO 0x3FFF; /* unbanked FLASH ROM */ ROM_4000 = READ_ONLY 0x4000 TO 0x7FFF; ROM_C000 = READ_ONLY 0xC000 TO 0xF77F;END

PLACEMENT /* here all predefined and user segments are placed into the SEGMENTS defined above. */ _PRESTART, /* Used in HIWARE format: jump to _Startup at the code start */ STARTUP, /* startup data structures */ ROM_VAR, /* constant variables */ STRINGS, /* string literals */ VIRTUAL_TABLE_SEGMENT, /* C++ virtual table segment *///.stackstart, /* eventually used for OSEK kernel awareness: Main-Stack Start */ SSTACK, /* allocate stack first to avoid overwriting variables on overflow */ //.stackend, /* eventually used for OSEK kernel awareness: Main-Stack End */END

STACKSIZE 0x100VECTOR 0 _Startup /* reset vector: this is the default entry point for a C/C++ application. *///VECTOR 0 Entry /* reset vector: this is the default entry point for an Assembly application. *///INIT Entry /* for assembly applications: that this is as well the initialization entry point */

Page 8: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

The PRM file

• No ORG statements are required in C. Absolute memory references are in the prm file

• Stack size (100 in this case) can be modified to be larger• The VECTOR statement can be used to identify the

location of interrupt service routines

Page 9: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

The Source Folder / Assembler subroutines

Main.c Main_asm.h

#include <hidef.h> /* common defines and macros */#include <mc9s12c32.h> /* derivative information */#pragma LINK_INFO DERIVATIVE "mc9s12c32"

#include "main_asm.h" /* interface to the assembly module */

void main(void) { /* put your own code here */ EnableInterrupts; asm_main(); /* call the assembly function */ Delay_100us(); DELAY_1MS();

for(;;) {} /* wait forever */ /* please make sure that you never leave this function */}

#ifndef _MAIN_ASM_H#define _MAIN_ASM_H

#ifdef __cplusplus extern "C" { /* our assembly functions have C calling convention */#endif

void asm_main(void); /* interface to my assembly main function */

void Delay_10us(void);void Delay_100us(void);void DELAY_1ms(void); void DELAY_10ms(void);

#ifdef __cplusplus }#endif

#endif /* _MAIN_ASM_H */

Declare routines

Page 10: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

Main.asm

; code sectionMyCode: SECTION; assembly routine called by the C/C++ applicationasm_main: MOVB #1,temp_byte ; demonstration code rts ; ---------------------------------------- ; ; ----------------------------------------; Subroutine DELAY: delays 10 uS ;; ----------------------------------------

DELAY_10us:Delay_10us:DELAY_10US: PSHA LDAA #55 ; for 10 uS delay

To execute assembly language routines simply use them as normal C functions,assembly file PLUS use XDEF in the assembly file to connect the two:

;*******************************************;*This is like the "old" utillib.asm file *;*******************************************; export symbols XDEF asm_main XDEF Delay_10us XDEF DELAY_100us xdef DELAY_1ms xdef DELAY_10ms ; 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 macros INCLUDE 'mc9s12c32.inc'

; variable/data sectionMY_EXTENDED_RAM: SECTION; Insert here your data definition. demonstration, temp_byte.temp_byte: DS.B 1

Page 11: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

How about passing parameters between C and ASM

• We have routines that pass data through a CPU register– The LCD routine uses B to pass charaters and commands

• CW uses the following registers to pass data

• If data is passed back the return values are stored in …

Page 12: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

More on passing

• Most of the routines we used, LCD_out, LCD_cmd used the register B, so we’re in good shape to use these again

• The Read_ATD subroutine that read the A2D and returned a value in D is also in good shape

Page 13: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

Passing Variables

Main_asm.h

#ifndef _MAIN_ASM_H#define _MAIN_ASM_H

#ifdef __cplusplus extern "C" { /* our assembly functions have C calling convention */#endif

void asm_main(void); /* interface to my assembly main function */

void Delay_10us(void);void Delay_100us(void); void DELAY_1MS(void); void DELAY_10ms(void);

char LCD_Out(char);

#ifdef __cplusplus }#endif

#include <hidef.h> /* common defines and macros */#include <mc9s12c32.h> /* derivative information */#pragma LINK_INFO DERIVATIVE "mc9s12c32"

#include "main_asm.h" /* interface to the assembly module */

extern char LCD_Out(char);void main(void) { /* put your own code here */ EnableInterrupts; asm_main(); /* call the assembly function */ Delay_100us(); DELAY_1MS(); LCD_Out(0x34);

for(;;) {} /* wait forever */ /* please make sure that you never leave this function */}

Main.c

Page 14: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

Main.asm; export symbols XDEF asm_main XDEF Delay_10us . . xdef DELAY_10MS xdef LCD_Out . .;=====================================================================; The 3 major 'low level' LCD routines: ; Init_LCD = no parameters, initializes the LCD (cursor and display on; clears and homes display, 2 line); LCD_CMD = takes value in reg. B and sends it as a command.; LCD_Out = takes value in reg. B and sends it as data (ASCII codes);=====================================================================LCD_Data: LCD_Out: PSHB

BCLR LCDCtrlPort, #RW_Bit ;it's data BSET LCDCtrlPort, #E_Bit+RS_Bit STAB LCDDataPort BCLR LCDCtrlPort,#E_Bit PULB RTS

RTS

Page 15: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

Global Variables

• If you want to share data between the two languages, one approach is to use global variables AND the extern keyword (on the C side) plus the XDEF directive (on the assembly side):

Page 16: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

LCD

• We will need to use the LCD as an indicator of what we are doing while the HC12 is running. We can’t always rely on the simulator

• We have three routines in our old utillib.asm file– LCD_init

– LCD_out

– LCD_cmd

• In C we want to create more, with more flexibility

Page 17: C and Assembler Subroutines: Using the LCD. Outline Basic structure of CW-created C programs for the HC12 How to incorporate assembly code How to use.

Routines we want for the LCD

• LCD_clear– Clears LCD and brings cursor home

• LCD_location(2,10) – Moves cursor to row 2, position 10 without clearing the screen

• LCD_writeline(buffer)– Clears the screen

– Prints the array of characters in buffer[]