21
ME4447/6405 ME 4447/6405 Introduction to Mechatronics Instructor: Professor Charles Ume Lecture on Codewarrior Integrated Development Environment

ME4447/6405 ME 4447/6405 Introduction to Mechatronicsume.gatech.edu/mechatronics_course/C_part2.pdf · ME4447/6405 ME 4447/6405 Introduction to Mechatronics Instructor: Professor

Embed Size (px)

Citation preview

ME4447/6405

ME 4447/6405Introduction to Mechatronics

Instructor: Professor Charles Ume

Lecture on Codewarrior Integrated Development Environment

ME4447/6405

Contents

• Overview of C Compliers for HCS12• CodeWarrior• Pointers• Interrupts

ME4447/6405

Overview of C compilers for HCS12

Machine Code

: A set of instructions executed directly by a computer’s CPU

Assembly Language

(Low-Level Programming Language)

Assembler

C/Java/… Compliers

C, Java, …

(High-Level Programming Language)

ME4447/6405

Overview of C compilers for HCS12 (cont.)

• C compiler: C language assembly language– Offers a way to develop HC12 based system with C

language

– Commercial C compilers:• CodeWarrior, Imagecraft, IAR Embedded Workbench,

Axiom, Cosmic …

– Freeware C compilers:• Gcc, ICCV7, Small C …

ME4447/6405

CodeWarrior: Overview

Code Warrior is an Integrated Development Environment (IDE):

• An integrated tool that helps develop programs for embedded systems

• Offers full ANSI compliance and library source

• Includes an assembler, a linker, a simulator, and a debugger

• Supports all Freescale product lines (56800, 68K, ColdFire, HCS12(X), RS08, HC08, MPC55xx, and more).

ME4447/6405

CodeWarrior: Project Files

• A project is: – a collection of files and rules for performing

operations on those files

• The files associated with a project can be: – source files, in either C or assembly language – libraries containing useful definitions or

functions

ME4447/6405

CodeWarrior: Project Configuration

We need to answer the following questions to configure a project: • What is the target processor? (MC9S12) • What on-chip resources does the processor have? (What variant:

C32, how to initialize registers) • What kind of memory resources exist on the target and where are

they? (memory configuration) • How do I set up the processor's interrupt and exception vectors?• Where is "Hello world" supposed to go? (Specify standard I/O)• How do I configure the debugger? • Refer to Mechatronics Lab website for detailed information on

CodeWarrior configuration

ME4447/6405

CodeWarrior: Data Types and Sizes

Note: Can be changed in complier settings

ME4447/6405CodeWarrior: Variables Associated to Registers

CodeWarrior associates variables to the HCS12 registers.• When you create your project, “mc9s12c32.h” is included to permit use of the variables.• The size of the variables match the actual size of their corresponding HCS12 registers.• Data Structures are defined to access the entire register or individual bits of the register.

/*** PTP - Port P I/O Register; 0x00000258 ***/ typedef union {

byte Byte; struct {

byte PTP0 :1; /* Port P Bit 0 */ byte PTP1 :1; /* Port P Bit 1 */ byte PTP2 :1; /* Port P Bit 2 */ byte PTP3 :1; /* Port P Bit 3 */ byte PTP4 :1; /* Port P Bit 4 */ byte PTP5 :1; /* Port P Bit 5 */ byte PTP6 :1; /* Port P Bit 6 */ byte PTP7 :1; /* Port P Bit 7 */

} Bits; } PTPSTR; extern volatile PTPSTR _PTP @(REG_BASE + 0x00000258); #define PTP _PTP.Byte#define PTP_PTP0 _PTP.Bits.PTP0 #define PTP_PTP1 _PTP.Bits.PTP1 #define PTP_PTP2 _PTP.Bits.PTP2 Etc…

ME4447/6405

CodeWarrior: Inline Assembler

CodeWarrior supports assembly language.

• The CodeWarrior compiler recognizes a keyword named “__asm.”– The compiler interprets it as a request to insert a string directly

into the assembler source file.

Example: __asm {

NOP ; // Does Nothing SEI ; // Sets I-bit, inhibits interrupts

}

ME4447/6405

Pointer: Concept

What is a Pointer?• A pointer is a variable which

contains the address in memory of another variable.

• We can have a pointer to any variable type.

• The operator & gives the “address of a variable”.

• The operator * gives the “contents of an object pointed to by a pointer”.

• To declare a pointer to a variable do: int *pointer

A simple example:• Assume variable x resides at memory

location 2000, y at 3000, and ip at 5000

Address Datastored

x        2000             1

y        3000             2

ip 5000             2000

… …

… …

1

20003

int x = 1;int y = 2; int *ip; ip = &x; y = *ip; x = ip; *ip = 3;

ME4447/6405 Pointer: Concept (cont.)• Note: When a pointer is declared it does not point to

anywhere. You must set it to point to somewhere before you use it.

• So below code may cause your program to crash: int *ip; *ip = 100;

• Correct use: int *ip; int x; ip = &x; *ip = 100;

Address Data stored

ip 1000 ???

Address Data stored

ip 1000

x 2000

2000

100

ME4447/6405

Pointers and Functions• Example: if we want to write a function to swap variables

around, the codes below will not work.

void swap(int a, int b){ int temp=a; a=b; b=temp; return; 

void main(void){ int x = 5, y=6; swap(x, y); return; 

}

Address Data stored

a 3000 5

b 4000 6

temp 5000 5

Address Data stored

x 1000 5

Y 2000 6

6

5

ME4447/6405

Pointers and Functions (cont.)

• Pointer can be used in the swap function.

void swap(int* a, int* b){ int temp=*a; *a=*b; *b=temp; return; 

void main(void){ int x = 5, y = 6; swap(&x, &y); return; 

}

Address Data stored

a 3000 1000

b 4000 2000

temp 5000 5

Address Data stored

x 1000 5

Y 2000 6

6

5

ME4447/6405

Pointers and Arrays• Pointers and arrays are closely related:

int x, a[10]; int *pa; a[0] = 5;a[1] = 10pa = &a[0]; (or pa = a;)x = *(pa+1);

• However they are different: – A pointer is a variable. We can do pa = a and pa++ – An array is not a variable. a = pa and a++ are illegal

• When an array is passed to a function what is actually passed is its initial elements location in memory

Facts:  pa=&a[0];  (pa=a;) pa+i ≡ &a[i] *(pa+i)  ≡ a[i] 

Address Data stored

x 1000 10

a[0] 2000 5

a[1] 2001 10… …

a[9] 2009 ??

pa 3000 2000

int MyArray[80];ArrayFunction(MyArray);

int MyArray[80]; ArrayFunction(&MyArray[0]);

ME4447/6405

Pointers and Multidimensional Arrays• A two dimensional array can be think of

as a one dimensional array with each element being an array – e.g. int multi[2][3] can be visualized as a two

element array, with each element being an array of three integers.

– Since the name of an array is a pointer to the first array element, multi is a pointer to the first element, multi[0][0], in this case.

– Since the element multi[0] is also an array, multi[0] is also a pointer to the first element, multi[0][0] in this case.

– While multi, multi[0] and &multi[0][0] all point to the same location, the sizes of the items they are pointing to are different.

Address Data stored

multi[0][0] 1000 1

multi[0][1] 1001 2

multi[0][2] 1002 3

multi[1][0] 1003 4

multi[1][1] 1004 5

multi[1][2] 1005 6

mul  ≡ 1000mul [0] ≡ 1000&multi[0][0] ≡ 1000

ME4447/6405

Pointer Operations

• Assignment: e.g. ptr=&x; ptr=array • Dereference: e.g. *ptr• Address of: e.g. &ptr• Increment: e.g. ptr+=3 • Decrement: e.g. Ptr--• Difference: e.g ptr1-ptr2 • Comparison: e.g ptr1<ptr2, ptr1==ptr2, etc.

ME4447/6405

Interrupt: Overview• An interrupt is a signal to the processor or an instruction in

software usually indicating an event (or a function) that needs immediate execution.

• An interrupt function should return void and have a void parameter list.

• The interrupt number determines which interrupt vector is associated with the interrupt function. – The interrupt number is determined by the following equation:

• n = (0xFFFE – VectorAddress)/2 • Example:

#define TOF_ISR 16 // TOF _IST vector 0xFFDE n=16 interrupt TOF_ISR void TimerOverflow(void) { /* your code */ }

ME4447/6405

Interrupt: Helpful Macros• DisableInterrupts

– Causes the I-bit to be cleared (equivalent to CLI)

• EnableInterrupts• Causes the I-bit to be set (equivalent to SEI)

• Example: void man(void) {

DisableInterrupts; /* initialize registers */ EnableInterrupts; /* Rest of your code */

}

ME4447/6405 Interrupt: Example

/* Include standard output and HCS12 register definitions */ #include <hidef.h>      /* common defines and macros */ #include <mc9s12c32.h>     /* derivative information */ #include <stdio.h> #include <termio.h> #pragma LINK_INFO DERIVATIVE "mc9s12c32" int counter; /* global variable */ 

#define TOF_ISR 16

/* Put "Tick" on the screen every 990ms using timer overflows*/ 

interrupt TOF_ISR void TimerOverflow(void) { 

counter++;    /* Increment the global variable counter by 1 */ if(counter == 30) { puts("Tick\n");    /* Put "Tick" on the screen */ counter = 0;    /* reset the counter */ 

} TFLG2 = 0x80;    /* Clear a the interrupt flag before exiting the function*/ return; 

}

/* Notes: Interrupt function takes no argument and returns nothing. */ /* The counter cannot be declared in the interrupt function because it  would reset it's value every time the interrupt is run. */ 

/* Also to clear the flag in TFLG2 you have write a one to that bit.  This goes for any other register that says to "write bits to clear" */ 

/*********************************************/ /* the main function that setup the interrupts*/ /*********************************************/ void main(void) { DisableInterrupts; /* It is recommended to do this before initializing 

/* any interrupts */ TERMIO_Init();    /* Initialize SCI subsystem */

TFLG2 = 0x80;    /* Clear the interrupt flag for Timer overflow */ TSCR1 = 0x80;    /* Turn on the Timer */ TSCR2 = 0x80;    /* Turn on the Timer Overflow interrupt itself */ 

/* The overflow occurs every 33ms */counter = 0;    /* Initialize global counter variable */ EnableInterrupts;    /* Enables interrupts */ /* Your interrupts should start at this point */ 

while(1);    /* infinite loop */ return; 

The following is an example of using interrupt when programming with CodeWarrior. You will have to make sure to add STDIO.H, TERMIO.H and TERMIO.C to the “Libraries” folder of your project. 

ME4447/6405

Thank you!