me 4447/me 6405 - georgia institute of...

27
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME 6405 Microprocessor Control of Manufacturing Systems/ Introduction to Mechatronics Instructor: Professor Charles Ume Lecture on Codewarrior Integrated Development Environment

Upload: dinhtuong

Post on 12-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

ME 4447/ME 6405 Microprocessor Control of Manufacturing Systems/

Introduction to Mechatronics

Instructor: Professor Charles Ume

Lecture on Codewarrior Integrated Development Environment

Page 2: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Contents •  Overview of C compliers for HCS12 •  Overview of CodeWarrior

•  CodeWarrior Project File •  Project Configuration •  Data Type Size Implemented by

CodeWarrior •  Variables Associated with Registers •  asm Pseudo Function •  Pointers •  Standard I/O •  Function Modifiers •  Interrupts •  Wrap up with an Example

Page 3: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Overview of C compilers for HCS12 •  C compiler: C language assembly language

–  Offers a way to develop HC11 based system with C language.

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

Cosmic…

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

Page 4: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Overview of CodeWarrior

•  Integrated Development Environment –  An integrated tool that helps develop programs for embedded systems

using Freescale microcontrollers –  Offers full ANSI compliance, library source –  Including an assembler, a linker, a simulator, and a debugger –  Compatible with Background Debug Module (BDM) –  CodeWarrior supports all Freescale line (56800, 68K, ColdFire,

HCS12(X), RS08, HC08, MPC55xx, and more).

Page 5: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

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

Page 6: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

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? (explain later) –  Where is "Hello world" supposed to go? (Specify standard

I/O) –  How do I configure the debugger?

Page 7: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Project Configuration (Cont.)

•  Specify processor family and its variant •  Configure environment •  Configure memory

•  Refer to Mechatronics Lab website for detailed information on CodeWarrior configuration

Page 8: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Size of Data Type Implemented in Introl C Compiler

Note: Can be changed in complier settings

Type #Bits #Bytes char 8 1 short 16 2 int 16 2 long 32 4 float 32 4 double 64 8 long double 64 8

Page 9: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

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 do 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…

Page 10: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Inline Assembler

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. The function is used with the following form:

__asm { { <Assembly Instruction> [; Comment] \n} }

Examples: __asm {

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

It is also possible to access C functions and data from separate assembly language files, please refer the Compiler manual for detailed information.

Page 11: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointer

•  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

Page 12: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointer (cont.)

•  A simple example –  Assume variable x resides at

memorey location 2000, y at 3000, and ip at 5000

int x = 1, y = 2;

int *ip;

ip = &x;

y = *ip;

x = ip;

*ip = 3;

Address Data stored

2000 1

3000 2

5000 2000

x

y

ip

Page 13: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointer (cont.)

•  A simple example –  Assume variable x resides at

memorey location 2000, y at 3000, and ip at 5000

int x = 1, y = 2;

int *ip;

ip = &x;

y = *ip;

x = ip;

*ip = 3;

Address Data stored

2000 1

3000 1

5000 2000

x

y

ip

Page 14: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointer (cont.)

•  A simple example –  Assume variable x resides at

memorey location 2000, y at 3000, and ip at 5000

int x = 1, y = 2;

int *ip;

ip = &x;

y = *ip;

x = ip;

*ip = 3;

Address Data stored

2000 2000

3000 1

5000 2000

x

y

ip

Page 15: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointer (cont.)

•  A simple example –  Assume variable x resides at

memorey location 2000, y at 3000, and ip at 5000

int x = 1, y = 2;

int *ip;

ip = &x;

y = *ip;

x = ip;

*ip = 3;

Address Data stored

2000 3

3000 1

5000 2000

x

y

ip

Page 16: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointer (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;

Page 17: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointers (cont.)

•  In C programming, it is NOT recommended to directly assign an address value to a pointer like in the above example

•  If you need to access certain memory location in C, you need to make sure that compiler is not using that part of memory for something else

•  Example of pointers: Write a program to add odd numbers located in addresses $0000 through $0020

void main(void){ int sum = 0; char* mempointer; mempointer = (char*) 0x0000; while(mempointer <= (char*) 0x0020){ if(*mempointer & 0x01) sum = *mempointer + sum; mempointer++; } return; }

Page 18: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointers and Functions

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

void main(void){ int a = 5, b=6; swap(a,b); return; }

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

void main(void){ int a = 5, b = 6; swap(&a,&b); return; }

Example: if we want to write a function to swap variables around, the codes below on the left will not work (why?), but the one on the right will.

Page 19: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointers and Arrays

•  Pointers and arrays are closely related: int a[10], x; int *pa; pa = &a[0]; /* pa pointer to address of a[0] */ x = *pa;

•  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

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

int MyArray[80]; ArrayFunction(MyArray); �

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

Page 20: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

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][4] can be visualized as a two element array, with

each element being an array of four 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], in this case. –  Since the element multi[0] is also an array (with array namemulti[0]),

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.

•  For any n-dimensional arrays: –  The array name followed by n pairs of brackets evaluates as array data –  The array name followed by fewer than n pairs of brackets evaluates

as a pointer to an array element

Page 21: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Pointer Operations

•  Assignment: e.g. ptr=&x; ptr=array

•  Dereferencing: e.g. *ptr

•  Address of: e.g. &ptr •  Incrementing: e.g. ptr+=3

•  Decrementing: e.g. Ptr--

•  Differencing: e.g ptr1-ptr2

•  Comparison: e.g ptr1<ptr2, ptr1==ptr2, etc.

Page 22: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Standard I/O •  In order to use the standard IO functions, it is

necessary to initialize the SCI subsystem. This is made possible through direct modification of SCI registers, or by using termio.h

•  Termio.h and termio.c must be included in the Libraries folder of your codewarrior project. By default, the settings in termio.c will not work. The file must be modified to intitialize the SCI subsystem with the correct settings.

Page 23: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Function Modifiers

•  interrupt –  Specifies a function to be generated that can be used directly as an

interrupt handler (interrupt service routine). –  A function declared with the interrupt modifier must not be called

directly from C since the function performs a return from interrupt when it is finished. An interrupt function should return void and have a void parameter list.

–  The vector number determines which interrupt vector is associated with the function. Interrupt numbers are determined by the following equation: n = (0xFFFE – VectorAddress)/2

•  Example: #define TOF_ISR 16 //TOF vector 0xFFDE, n=16

interrupt TOF_ISR void TimerOverflow(void) { /* your code */ }

Page 24: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Helpful Macros

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

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

•  Example: void man(void) { DisableInterrupts; /* initialize registers */ EnableInterrupts; /* Rest of your code */ }

Page 25: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Interrupt •  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.

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

/* 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 */ /***************************************************/ /* The function modifier "interrupt" is required for a interrupt routine function. 16 is the Interrupt number for the Timer Overflow Interrupt */ /***************************************************/ #define TOF_ISR 16 interrupt TOF_ISR void Time(void) { counter++; /* Increment the global variable counter by 1 */ if(counter == 30) /* An over flow occurs every 33 ms so 30 times a sec */ { puts("Tick\n"); /* Put "Tick" on the screen followed by a newline */ 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 */ counter = 0; /* Initialize global counter variable */ EnableInterrupts; /* Enables maskable interrupts */ /* Your interrupts should start at this point */ while(1); /* infinite loop */ return; }

/**************** End of program ******************/

Page 26: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech

Wrap Up with Another Example •  LED lab with C code:

#include <hidef.h> /* common defines and macros */ #include <mc9s12c32.h> /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc9s12c32" void delay(unsigned long seconds) { /* function to delay 1 second*/

unsigned long clocks = seconds*200000; while(clocks > 0) {clocks = clocks-1;} return;

}

void main(void) { char i = 0xFC; /* FC = 1111 1100 */ DDRP = 0xFF; /* Initialize port P. */ while(1) { /* Loop to show binary increase. */ PTP = i; i = i - 4; delay(1); } return;

}

Page 27: ME 4447/ME 6405 - Georgia Institute of Technologyume.gatech.edu/mechatronics_course/IntroMech/C_part2.pdf · George W. Woodruff School of Mechanical Engineering, Georgia Tech ME 4447/ME

George W. Woodruff School of Mechanical Engineering, Georgia Tech