50
H. Huang Transparency No.4-1 The 68HC11 Microcontroller Chapter 4: C Language Programming The 68HC11 Microcontroller Han-Way Huang Minnesota State University, Mankato

Chapter 4: C Language Programming

  • Upload
    billie

  • View
    109

  • Download
    6

Embed Size (px)

DESCRIPTION

Chapter 4: C Language Programming. The 68HC11 Microcontroller. Han-Way Huang. Minnesota State University, Mankato. Introduction to C - C has gradually replaced assembly language in many embedded applications. A summary of C language constructs that will be used in 68HC11 programming. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 4: C Language Programming

H. Huang Transparency No.4-1

The 68HC11 Microcontroller

Chapter 4: C Language Programming

The 68HC11 Microcontroller

Han-Way Huang

Minnesota State University, Mankato

Page 2: Chapter 4: C Language Programming

H. Huang Transparency No.4-2

The 68HC11 Microcontroller

Introduction to C

- C has gradually replaced assembly language in many embedded applications.- A summary of C language constructs that will be used in 68HC11 programming.- Books on C language

1. Kernighan & Ritchie, “The C Programming Language”, Prentice Hall, 1988.2. Deitel & Deitel, “C: How to Program”, Prentice Hall, 1998.3. Kelly & Pohl, “A Book on C: Programming in C”, Addison-Wesley, 1998.

- A C program consists of functions and variables.- A function contains statements that specify the operations to be performed.- Types of statements:

1. Declaration2. Assignment3. Function call4. Control5. Null

- A variable stores a value to be used during the computation.- The main () function is required in every C program.

Page 3: Chapter 4: C Language Programming

H. Huang Transparency No.4-3

The 68HC11 Microcontroller

A C Program Example

(6) #include <stdio.h> -- causes the file stdio.h to be included

(7) /* this is where program execution begins */ -- a comment

(8) main () -- program execution begins

(9) { -- marks the start of the program

(10) int a, b, c; -- declares three integer variables a, b, and c

(11) a = 3; -- assigns 3 to variable a

(12) b = 5; -- assigns 5 to variable b

(13) c = a + b; -- assigns the sum of a and b to variable c

(14) printf (“ a + b = %d\n”, c); -- prints the string a + b = followed by value of c

(15) return 0; -- returns 0 to the caller of main( )

(16) } -- ends the main( ) function

Types, Operators, and Expressions

- variables must be declared before they can be used.- A variable declaration must include the name and type of the variable and may optionally provide its initial value.

Page 4: Chapter 4: C Language Programming

H. Huang Transparency No.4-4

The 68HC11 Microcontroller

- The name of a variable consists of letters and digits.- The underscore character “_” can be used to improve readability of long

variables.

Data Types

- C has four basic data types: char, int, float, and double. - A variable of type char can hold a single byte of data.- A variable of type int is an integer that is the natural size for a particular machine.- The type float refers to a 32-bit single-precision floating-point number.- The type double refers to a 64-bit double-precision floating-point number.- Qualifiers short and long can be applied to integers. For ImageCraft C compiler,

short is 16 bits and long is 32 bits.- Qualifiers signed and unsigned may be applied to data types char and integer.

Declarations

- A declaration specifies a type, and contains a list of one or more variables of that type.

Page 5: Chapter 4: C Language Programming

H. Huang Transparency No.4-5

The 68HC11 Microcontroller

Examples of declarations

int i, j, k;char cx, cy;int m = 0;char echo = ‘y’; /* the ASCII code of letter y is assigned to variable echo.

Constants

- There are four kinds of constants: integers, characters, floating-point numbers, andstrings.

- A character constant is written as one character within single quotes, such as ‘x’.A character constant is represented by the ASCII code of the character.

- A string constant is a sequence of zero or more characters surrounded by doublequotes, as “68HC11 is made by Motorola” or “”, which represented an emptystring. Each individual character in the string is represented by its ASCII code.

- An integer constant like 1234 is an int. A long constant is written with a terminal l or L, as in 44332211L.

Page 6: Chapter 4: C Language Programming

H. Huang Transparency No.4-6

The 68HC11 Microcontroller

- A number in C can be specified in different bases. The method to specify the baseof a number is to add a prefix to the number:

base prefix example

decimaloctal

hexdecimal

none0

0x

1234043210x45

Arithmetic Operators

+ add and unary plus- subtract and unary minus* multiply/ divide -- truncate quotient to integer when both operands are integers.

% modulus (or remainder) -- cannot be applied to float or double

++ increment (by 1)-- decrement (by 1)

Page 7: Chapter 4: C Language Programming

H. Huang Transparency No.4-7

The 68HC11 Microcontroller

Bitwise Operators

- C provides six operators for bit manipulations; they may only be applied tointegral operands.

& AND| OR^ XOR~ NOT>> right shift<< left shift

- & is often used to clear one or more bits of an integral variable to 0.PORTD = PORTD & 0xBD /* clears bit 6 and bit 1 of PORTD to 0 */

/* PORTD is of type char */- | is often used to set one or more bits to 1.

PORTB = PORTB | 0x40; /* sets bit 6 to 1 (PORTB is of type char) */- ^ can be used to toggle a bit.

abc = abc ^ 0xF0; /* toggles upper four bits (abc is of type char) */

Page 8: Chapter 4: C Language Programming

H. Huang Transparency No.4-8

The 68HC11 Microcontroller

- >> can be used to shift the involved operand to the right for the specified numberof places.

xyz = xyz >> 3; -- shift right 3 places

- << can be used to shift the involved operand to the left for the specified numberof places.

xyz = xyz << 4; -- shift left 4 places

- The assignment operator = is often combined with the operator. For example,

PORTD &= 0xBD;PORTB |= 0x40;xyz >>= 3;xyz <<= 4;

Page 9: Chapter 4: C Language Programming

H. Huang Transparency No.4-9

The 68HC11 Microcontroller

Relational and Logical Operators

- Relational operators are used in expressions to compare the values of two operands. - The value of the expression is 1 when the result of comparison is true. Otherwise,

the value of the expression is 0.- Relational and logical operators:

== equal to!= not equal to> greater than>= greater than or equal to< less than<= less than or equal to&& and || or! not (one’s complement)

Page 10: Chapter 4: C Language Programming

H. Huang Transparency No.4-10

The 68HC11 Microcontroller

Examples of Relational and Logical Operators:

if (!(ADCTL & 0x80)statement1; /* if bit 7 is 0, then execute statement1 */

if (i < 10 && i > 0)statement2; /* if 0 < i < 10 then execute statement2 */

if (a1 == a2)statement3; /* if a1 = a2 then execute statement3 */

Control Flow

- The control-flow statements specify the order in which computations are performed.

- Semicolon is a statement terminator.- Braces { and } are used to group declarations and statements together into a

compound statement, or block.

Page 11: Chapter 4: C Language Programming

H. Huang Transparency No.4-11

The 68HC11 Microcontroller

If-Else Statement

if (expression)statement1

else -- The else part is optional.statement2

Example,

if (a != 0)r = b;

elser = c;

A more concise way for this statement is

r = (a != 0)? b : c;

Page 12: Chapter 4: C Language Programming

H. Huang Transparency No.4-12

The 68HC11 Microcontroller

Multiway Conditional Statement

if (expression1)statement1

else if (expression2)statement2

else if (expression3)statement3

…else

statementn

Example,

if (abc > 0) return 5;else if (abc = 0) return 0;else return -5;

Page 13: Chapter 4: C Language Programming

H. Huang Transparency No.4-13

The 68HC11 Microcontroller

Switch Statement Example

switch (expression) { switch (i) {case const_expr1; case 1: printf(“#”);

statement1; break;break; case 2: printf(“##”);

case const_expr2; break;statement2; case 3: printf(“###”);break; break;

… case 4: printf(“####”);default: break;

statementn; case 5: printf(“#####”);} break;

}

Page 14: Chapter 4: C Language Programming

H. Huang Transparency No.4-14

The 68HC11 Microcontroller

For-Loop Statement

for (expr1; expr2; expr3)statement;

where, expr1 and expr2 are assignments or function calls and expr3 is a relationalexpression.

Example

sum = 0;for (i = 1; i < 10; i++)

sum += i * i;

for (i = 1; i < 20; i++)if (i % 2) printf(“%d “, i);

Page 15: Chapter 4: C Language Programming

H. Huang Transparency No.4-15

The 68HC11 Microcontroller

While Statement

while (expression)statement

Example

int_cnt = 5;while (int_cnt); /* do nothing while the variable int_cnt 0 */

Do-While Statement Example

do int digit = 9;statement do

while (expression); printf(“%d “, digit--);while (digit >= 1);

Page 16: Chapter 4: C Language Programming

H. Huang Transparency No.4-16

The 68HC11 Microcontroller

Input and Output Examples

- Not part of the C language itself.- Four I/O functions will be discussed.

1. int getchar ( ). char xch;-- returns a character when called xch = getchar ();

2. int putchar (int). putchar(‘a’);-- outputs a character on a standard output device

3. int puts (const char *s). puts (“Welcome to USA! \n”);-- outputs the string pointed by s on a standard output device

4. int printf (formatting string, arg1, arg2, …).-- converts, formats, and prints its arguments on the standard output device.

Page 17: Chapter 4: C Language Programming

H. Huang Transparency No.4-17

The 68HC11 Microcontroller

Formatting String for Printf

- The arguments of printf can be written as constants, single variable or array names, or more complex expressions.- The formatting string is composed of individual groups of characters, with one

character group associated with each output data item.- The character group starts with %. - The simplest form of a character group consists of the percent sign followed by

a conversion character indicating the type of the corresponding data item.- Multiple character groups can be contiguous, or they can be separated by other

characters, including whitespace characters. These “other characters” are simplysent to the output device for display.

- Examples

printf (“this is a challenging course ! \n”);printf(%d %d %d”, x1, x2, x3); /* outputs x1, x2, and x3 using minimal

number of digits with one space separating each value */

printf(“Today’s temperature is %4.1d\n”, temp);

Page 18: Chapter 4: C Language Programming

H. Huang Transparency No.4-18

The 68HC11 Microcontroller

Rules for Conversion String

- Between the % and the conversion character there may be, in order:1. A minus sign, -- specify left adjustment.2. A number that specifies the minimum field width.3. A period that separates the field width from precision.4. A number, the precision, that specifies the maximum number of characters to

be printed from a string, or the number of digits after the decimal point, orthe minimum of digits for an integer.

5. An h if the integer is to be printed as a short, or l (letter ell) if as a long.

Conversioncharacter Meaning

cdefg

iosux

data item is displayed as a single characterdata item is displayed as a signed decimal numberdata item is displayed as a floating-point value with an exponentdata item is displayed as a floating-point value without an exponentdata item is displayed as a floating-point value using either e-type or f-type conversion, depending on value; trailing zeros, trailing decimalpoint will not be displayeddata item is displayed as a signed decimal integerdata item is displayed as an octal integer, without a leading zerodata item is displayed as a stringdata item is displayed as an unsigned decimal integerdata item is displayed as a hexadecimal integer, without the leading 0x

Table 4.1 Commonly used conversion characters for data output

Page 19: Chapter 4: C Language Programming

H. Huang Transparency No.4-19

The 68HC11 Microcontroller

Functions and Program Structure

- Every C program consists of one or more functions.- Definition of a function cannot be embedded within another function.- A function will process information passed to it from the calling portion of the

program, and return a single value.- Syntax of a function definition:

return_type function_name (declarations of arguments){

declarations and statements}

Example

char lower2upper (char cx){

if cx > ‘a’ && cx <= ‘z’) return (cx - (‘a’ - ‘A’));else return cx;

}

Page 20: Chapter 4: C Language Programming

H. Huang Transparency No.4-20

The 68HC11 Microcontroller

Example 4.1 Write a function to test if an integer is a prime number.Solution: A number is a prime if it is indivisible by any integer between 2 and half of itself.

/* this function returns a 1 if a is prime. Otherwise, it returns a 0. */char test_prime (int a){

int i;if (a == 1) return 0;for (i = 2; i < a/2; i++)

if ((a % i) == 0) return 0;return 1;

}

- A function must be defined before it can be called.- Function prototype declaration allows us to call a function before it is defined.- Syntax of a function prototype declaration:

return_type function_name (declarations of arguments);

Page 21: Chapter 4: C Language Programming

H. Huang Transparency No.4-21

The 68HC11 Microcontroller

Example 4.2 Write a program to find out the number of prime numbers between100 and 1000.Solution: #include <stdio.h>char test_prime (int a); /* prototype declaration for the function test_prime */main ( ){

int i, prime_count = 0;for (i = 100; i <= 1000; i++) {

if (test_prime(i))prime_count ++;

}printf(“\n The total prime numbers between 100 and 1000 is %d\n”, prime_count);

}

char test_prime (int a){

int i;if (a == 1) return 0;for (i = 2; i < a/2; i++)

if ((a % i) == 0) return 0;return 1;

}

Page 22: Chapter 4: C Language Programming

H. Huang Transparency No.4-22

The 68HC11 Microcontroller

Pointers and Addresses

- A pointer is a variable that holds the address of a variable.- Pointers provide a way to return multiple data items from a function via function

arguments. - Pointers also permit references to other functions to be specified as arguments to

a given function.- Syntax for pointer declaration:

type_name *pointer_name;

Examples

int *ax;char *cp;

- Use the dereferencing operator * to access the value pointed by a pointer.int a, *b;…a = *b; /* assigns the value pointed by b to a */

Page 23: Chapter 4: C Language Programming

H. Huang Transparency No.4-23

The 68HC11 Microcontroller

- Use the unary operator & to assign the address of a variable to a pointer. For example,

int x, y;int *ip;

ip = &x;y = *ip; /* y gets the value of x */

Page 24: Chapter 4: C Language Programming

H. Huang Transparency No.4-24

The 68HC11 Microcontroller

Example 4.3 Write a bubble sort function to sort an array of integers.Solution:

void swap (int *, int *); /* function prototype declaration */void bubble (int a[], int n) /* n is the array count */{

int i, j;for (i = 0; i < n - 1; i++)

for (j = n - 1; j > i; j--)if (a[j - 1] > a[j])

swap (&a[j - 1], & a[j]);}

void swap(int *px, int *py){

int temp;temp = *px;*px = *py;*py = temp;

}

Page 25: Chapter 4: C Language Programming

H. Huang Transparency No.4-25

The 68HC11 Microcontroller

Arrays

- An array consists of a sequence of data items that have common characteristics.- Each array is referred to by specifying the array name followed by one or more subscripts,

with each subscript enclosed in brackets. Each subscript is a nonnegative integer.- The number of subscripts determines the dimensionality of the array. For example,

x[i] is an element of an one-dimensional arrayy[i][j] refers to an element of a two-dimensional array

- Syntax for one-dimensional array declaration:

data-type array_name [expression];

- Syntax for two-dimensional array declaration:

data-type array_name [expr1] [expr2];

Page 26: Chapter 4: C Language Programming

H. Huang Transparency No.4-26

The 68HC11 Microcontroller

Pointers and Arrays

- Any operations that can be achieved by array subscripting can also be done with pointers.- The pointer version will in general be faster but, somewhat harder to understand.- For example,

int ax[20]; /* array of 20 integers */int *ip; /* ip is an integer pointer */

ip = &ax[0] /* ip contains the address of ax[0] */

x = *ip; /* copy the contents of ax[0] into x.

- If ip points to ax[0], then ip + 1 points to ax[1], and ip + i points to ax[i], etc.

Page 27: Chapter 4: C Language Programming

H. Huang Transparency No.4-27

The 68HC11 Microcontroller

Passing Arrays to a Function

- An array name can be used as an argument to a function.- To pass an array to a function, the array name must appear by itself, without brackets or

subscripts, as an actual argument within the function call.- When declaring a one-dimensional array as a formal argument, the array name is written with

a pair of empty square brackets.- When declaring a two-dimensional array as a formal argument, the array name is written with

two pairs of empty square brackets.- Example,

int average (int n, int arr[ ]);main ( ){

int n, avg;int arr[50];…avg = average (n, arr); /* function call with array name as an argument */…

}int average (int k, int brr [ ]) /* function definition */{

…}

Page 28: Chapter 4: C Language Programming

H. Huang Transparency No.4-28

The 68HC11 Microcontroller

External Variables

- A variable declared inside a function is called an internal variable.- A variable defined outside of a function is called a external variable. - An external variable is available to many functions.- External variables provide an alternative to function arguments and return values for

communicating data between functions.- Any function may access an external variable by referring to it by name if the the name has

been declared somewhere.

Scope Rules

- The functions and external variables that make up a C program can be compiled separately.- The source text of the program may be kept in several files.- The scope of a name is the part of the program within which the name can be used.- For a variable declared at the beginning of a function, the scope is the function in which the

name is declared. - Local (internal) variables of the same name in different functions are unrelated.- The scope of an external variable or a function lasts from the point at which it is declared

to the end of the file being compiled.

Page 29: Chapter 4: C Language Programming

H. Huang Transparency No.4-29

The 68HC11 Microcontroller

In the following program segment The use of external variables are illustratedin the following 2-file C program

… in file 1void f1 (…) extern int xy;{ extern long arr [ ];

… main ( )} {

…int a, b, c; }void f2(…) void foo (int abc) { … }{ long soo (void) { … }

…} in file 2

int xy;Variables a, b, and c are accessible to long arr [100];function f2 but not f1.

Page 30: Chapter 4: C Language Programming

H. Huang Transparency No.4-30

The 68HC11 Microcontroller

Using the ImageCraft C Compiler

- An Integrated Development Software (IDS) may consist of a text editor, a compiler, a linker,a librarian, an assembler, a simulator, and debugger, etc.

- An IDS uses project as the unit to control the software development.- A user creates C functions in one or multiple files using the editor provided by the integrated

software.- The compiler is then invoked to compile each individual file. In most cases, you will need to

eliminate syntax errors in this step.- Create a new project and add the files just created into this project.- Build the project. The IDS will combine all object modules into one executable module and

may translate it into appropriate format suitable for downloading into a demo board forexecution.

- Download the module into a demo board for execution.- In the following, we will use a project that consists of a single file to illustrate the process of

using the ImageCraft C compiler for program development:

Page 31: Chapter 4: C Language Programming

H. Huang Transparency No.4-31

The 68HC11 Microcontroller

Step 1. Invoke the ICC11 by clicking the icon of ICC1 program. A window appears asfollows:

Page 32: Chapter 4: C Language Programming

H. Huang Transparency No.4-32

The 68HC11 Microcontroller

Step 2. Click the file menu and select new to create a new file for entering a new program.The screen look like this:

Page 33: Chapter 4: C Language Programming

H. Huang Transparency No.4-33

The 68HC11 Microcontroller

Step 3. Type in the new program and save it in an appropriate directory. An example is shownin Figure 4.3.

Page 34: Chapter 4: C Language Programming

H. Huang Transparency No.4-34

The 68HC11 Microcontroller

Step 4. Create a new project by clicking the project menu and select new. The screen is shownin Figure 4.4. After selecting new, the screen changes to Figure 4.5. After this, we need to enter the project name and the result is shown in Figure 4.6.

Page 35: Chapter 4: C Language Programming

H. Huang Transparency No.4-35

The 68HC11 Microcontroller

Page 36: Chapter 4: C Language Programming

H. Huang Transparency No.4-36

The 68HC11 Microcontroller

Page 37: Chapter 4: C Language Programming

H. Huang Transparency No.4-37

The 68HC11 Microcontroller

Step 5. Add the program to the newly created project. Press the Add button on Figure 4.6.A list of files will be displayed and you can select those files that you want to include inthis project. Click on the file add.c and the result is shown in Figure 4.7.

Page 38: Chapter 4: C Language Programming

H. Huang Transparency No.4-38

The 68HC11 Microcontroller

Step 6. Setting appropriate options for the compiler, editor, and terminal program. The optionsavailable can be seen by pressing the Options menu shown in Figure 4.8.

Page 39: Chapter 4: C Language Programming

H. Huang Transparency No.4-39

The 68HC11 Microcontroller

Step 7. Set compiler preprocessor options. There are three sets of options that need to beset in the compiler: preprocessor, compiler, and linker. Select Compiler in Figure 4.8 andthen click on Preprocessor and the screen shown in Figure 4.9a will be brought up. Enterthe appropriate include path. Do not click on OK.

Page 40: Chapter 4: C Language Programming

H. Huang Transparency No.4-40

The 68HC11 Microcontroller

Step 8. Set compiler options. Click on the item compiler. The default options shown in Figure 4.9b are OK.

Page 41: Chapter 4: C Language Programming

H. Huang Transparency No.4-41

The 68HC11 Microcontroller

Step 9. Set linker options. Click the Linker button on Figure 4.9a to bring up the linker option screen. The appropriate option values for EVB demo board are shown in Figure 4.9c.The appropriate option values for CMD11A8 are shown in Figure 4.9d.

Page 42: Chapter 4: C Language Programming

H. Huang Transparency No.4-42

The 68HC11 Microcontroller

Page 43: Chapter 4: C Language Programming

H. Huang Transparency No.4-43

The 68HC11 Microcontroller

Step 10. Set terminal options. Go back to Option menu and select Terminal. This will bringup the terminal option screen as shown in Figure 4.10. The com port can be com1 or com2.Baud rate should be set to 9600 for both EVB and CMD11A8. Flow control should be setto none. Font also need to set properly. A popular font is Courier or Courier New (shown in Figure 4.11). Click on OK and the screen will change back to Figure 4.8.

Page 44: Chapter 4: C Language Programming

H. Huang Transparency No.4-44

The 68HC11 Microcontroller

Page 45: Chapter 4: C Language Programming

H. Huang Transparency No.4-45

The 68HC11 Microcontroller

Step 10. Build the project. Click the Build button and ICC11 will start to process your file.The Build button will be dimmed before it is done. When it is done, the Build button willchange back to bold. Minimize both the project and source code windows and maximize the status window and the result is shown in Figure 4.12.

Page 46: Chapter 4: C Language Programming

H. Huang Transparency No.4-46

The 68HC11 Microcontroller

Step 11. Download the program onto the demo board for execution. Click the Target menuand select Terminal and this will bring up the terminal window as shown in Figure 4.13. Typeload t followed by the enter key, and click on the Browse button. This would bring up a popup window (shown in Figure 4.14) for you to select a file to download. Select add.s19and click OK button. Click the ASCII Download button to start downloading. Thedownloading process looks like that in Figure 4.15. A successful download should look like Figure 4.16.

Page 47: Chapter 4: C Language Programming

H. Huang Transparency No.4-47

The 68HC11 Microcontroller

Select a File to Download

Page 48: Chapter 4: C Language Programming

H. Huang Transparency No.4-48

The 68HC11 Microcontroller

Download Process

Page 49: Chapter 4: C Language Programming

H. Huang Transparency No.4-49

The 68HC11 Microcontroller

A Successful Download

Page 50: Chapter 4: C Language Programming

H. Huang Transparency No.4-50

The 68HC11 Microcontroller

Step 12. Program Execution and Debugging. ICC11 terminal program allows you to use allthe Buffalo commands to debug the program when using an EVB and CMD11A8 demoboard. Type G 2000 to run this program on the CMD11A8 demo board and the result would look like Figure 4.17. (Type G C000 on the EVB board).