C Compiler Basics_8051

73
C Compiler Training that Sticks 8051

Transcript of C Compiler Basics_8051

Page 1: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 1/73

C Compiler Training

that Sticks8051

Page 2: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 2/73

 Agenda

C Language Review 

Examples

Programs

Page 3: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 3/73

 What is C?

High-level programming language

One of the most popular general purposeprogramming languages available

First developed in 1972 by Dennis Ritchie at AT&T Bell Labs

 Advantages of CReadability 

Maintainability 

Portability  

Page 4: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 4/73

 A Word of Caution

C IS CASE

SENSITIVE Var ≠ VAR  

Use for, not FOR etc.

Page 5: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 5/73

 A Simple C Program

/*This is my 1st C program 

Port 1 will be loaded with 0xAA 

and Port 3 with 0xF6. Everything between

the star and forward slash is ignored by compiler*/

#include <stdio.h> 

void main(void){

P1 = 0xAA;

P3 = 0xF6;

 while(1){

asm{NOP;} //Assembler instruction

used }

Page 6: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 6/73

Dissecting a C Program

 The first line is a comment  

 A comment starts with a /* and ends with a */ 

Everything in between is ignored

Comments can cross more than one line

Document code, enhance readability

Comments affect only size of text file

Shortform notation for commenting out a single

line: two slashes, // 

Comments cannot be nested

Page 7: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 7/73

Dissecting a C Program

/* Comments do not increase the

size of the executable, nor

does it affect performance */ 

/* Comments do not increase the */

/* size of the executable, nor */

/* does it affect performance */

// Comments do not increase the

// size of the executable, nor

// does it affect performance

Page 8: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 8/73

Dissecting a C Program

#include <stdio.h>  

Line 2 is a preprocessor directive

 Tells the compiler to look for a file and placethe contents of that file at the directive

stio.h is a header file

Contains prototypes and macros for functionsand registers

Page 9: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 9/73

Dissecting a C Program

 Angle brackets <stdio.h> tell the

preprocessor to look in another directory for the

file

Double quotes “stdio.h” tell the

preprocessor to look in the current directory for

the file

Page 10: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 10/73

Dissecting a C Program

Every C program must have one,and only one, main( ) function

Can be anywhere in your source code Program execution starts with main( ) 

Programs always end with main( ) 

 The body of the function main( ) islocated between the curly braces { and the } 

Page 11: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 11/73

 The Basics of the C Program

 A C program is composed of basic elementssuch as:

keywords

constants

 variables expressions

statements and statement blocks

Page 12: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 12/73

 ANSI C Keywords

auto else register union

 break enum return unsinged 

case extern short void char float signed volatile

const for sizeof while

continue goto static

default if structdo int switch

double long typedef

Page 13: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 13/73

Expressions

 An expression  is a combination of constants, variablesand operators

Examples(2 + 3) * 10 10 * (4 + 5)

80/4 6

i 6 + i

return 0

Page 14: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 14/73

Precedence of Operators

 All expressions are evaluated according to the  precedence  of it‟s operators 

 What do the following expressionsreduce to?

2 + 3 * 10 (2 + 3) * 10

Page 15: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 15/73

Precedence of Operators

 All expressions are evaluated according to the  precedence  of it‟s operators 

 What do the following expressionsreduce to?

2 + 3 * 10 (2 + 3) * 10

= 2+(3*10) = 32 =5*10 =50* has higher precedence than +() has higher precedence than *

Page 16: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 16/73

Operator Precedence Table

( ) [ ] . ->  l to r ^ l to r

- ~ ! * & r to l | l to r

++ -- sizeof  r to l && l to r

* / %  l to r || l to r+ -  l to r ?: r to l

 << >>   l to r = *= /= r to l

 < > <= >= l to r %= += -=  r to l

== !=  l to r  <<= >>=  r to l

& l to r &= ^= |=  r to l

Page 17: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 17/73

Statements

 A statement is a complete instruction, ending  with a semicolon

 A statement is made up of one or moreexpressions

i = 1; Var1 = (2 + 3) * 10; 

j = 6 % 4; BinValue = i + j;

return 0;

Page 18: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 18/73

Statement Blocks

 A group of statements is a statement block  beginning with a curly brace ( { ) and ending with a

curly brace ( } )

 A statement block is treated like a single statement by the compiler

for(…)

{

s3 = s1 + s2;

 MulValue = s3 * c;

Page 19: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 19/73

Data Types

Defines the size and type of data that a variableholds

Only five data types in ANSI C void 

char -> 8 Bits ->1 Byte

int -> 16 Bits ->2 Bytes

long -> 32 Bits ->4 Bytes

float -> 32 Bits ->4 Bytes double

Page 20: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 20/73

void Data Type

Represents no data type

Used most commonly with functions

void CombineValues(void){

//Changes global values

Page 21: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 21/73

char Data Type

Represents a single character or an8-bit number, i.e., A or 7

 To define a variable as a char use:char variablename;

 To define multiple variables use:char variablename1;char variablename2 ;

 Another way of defining:char variablename3 , variablename4; 

Page 22: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 22/73

Character Constants

Single characters can be represented using singlequotes, such as: „A‟ 

 The ASCII value of the character is the value used inthe expression

x = „A‟; //0x41

Page 23: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 23/73

int Data Type

Integer numbers are whole numbers

Result of integer division is truncation

 The length of an integer varies from one machine to

another, typically 16-bits

 To define a variable as an int use:

int variablename; 

Page 24: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 24/73

float Data Type

Floating point numbers contain both real andfractional parts

Has at least 6 digits of precision

 To define a variable as float use:

float variablename; 

Supported as 32-bit value in MCS51

Page 25: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 25/73

Data Type Modifiers

 These four data type modifiers can be applied to datatypes char and int

signed 

unsigned 

short

long 

Page 26: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 26/73

Sign Bit

 The difference between a signed number and anunsigned number is the interpretation of the MSb

For signed numbers, the MSb indicates a positive

number when 0 and a negative number when 1 For unsigned numbers all bits are used to determine

the value

 The default value of data types is unsigned 

Page 27: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 27/73

Sign Bit

 The range of unsigned char is 0 to 255

{0, 1, …, 255} 

 The range of signed char is -128 to +127

{0x80, 0x81, …, 0xFE,0xFF,0x00,…,0x7F} 

Page 28: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 28/73

Changing Data Sizes

long and short are used to increase ordecrease the size used by the data type

short int x; reduces a 16-bit integer to 8-bits may be used without ‘int’: short x; 

long int x; doubles a 16-bit integer to 32-bits may be used without ‘int’: long x; 

Page 29: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 29/73

 Anatomy of a C Function

char add (char x, char y){ 

char result;

result = x + y;

return (result);}

Function

Type

Function

Name

 Argument List to

Function

Start of Function

Body of Function

End of Function Bodyand Function

Page 30: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 30/73

Function Type

 The function type is used to signify what typeof value the function returns

Can be any valid C data type

void functions do not return a value

 The function add( ) returns a char

Page 31: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 31/73

Function Names

 A function name should be chosen to reflect what

the function does

Function names cannot start with numbers

Function names cannot contain asterisk,

arithmetic signs, or dot (.)

Functions cannot contain minus signs or

apostrophes

Page 32: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 32/73

Function Arguments

Arguments are used to pass information TO a function

Multiple arguments are separated by commas

 A function that does not receive any arguments is

considered to be void  int status(void){

// This function will return data of

type int}

Page 33: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 33/73

Function Body

 The open curly brace ( { ) signifies the start of the

function body 

 The closed curly brace ( } ) denotes the end of the

function body   The statements in between are the statement block 

 The function completes when the statement block isfinished executing  

Page 34: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 34/73

Global Variables

Defined outside functions  Variable can be used by ALL functions 

unsigned char FlagReg; // Globally defined register

void TestFlags(void){FlagReg = FlagReg & 0x86;

}// end of function

void main(void){

-FlagReg = FlagReg + 5;TestFlags(); //Call function-

}

Page 35: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 35/73

Local Variables

Defined inside a functions e.g. Temp  Variable can be used ONLY by that function

unsigned char Online; // Globally defined register 

void ChangeVal(void){unsigned char Temp; // Local variable

Temp = P1 + 0x21;Online = Temp & 0x0F;

}// end of function

void main(void){-ChangeVal(); //Call function-

}

Page 36: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 36/73

Making Function Calls

#include <stdio.h> 

char add( char x, char y){ /* This function adds two chars */char result; //define a LOCAL result variable

result = x + y; /* add parameters */return (result); /* return result to caller */

}

void main(void){char sum; /* defines a local variable */

sum = add(5,12); // calculate sum of 5 and 12P1 = sum; // place result on Port 1

}

Page 37: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 37/73

Quiz

 What is wrong with this function?

int int_add(int x, int y){

char sum;

sum = x + y;

return (sum);

Page 38: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 38/73

Quiz

 Answer – Types different, answer limited tochar!

Change to same type

int int_add(int x, int y){int sum;

sum = x + y;return (sum);

Page 39: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 39/73

Quiz

 What is wrong with this function?

int AddUp(char x, int y){

int sum;

sum = x + y;

return (sum);

}

Page 40: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 40/73

Quiz

 Answer – Types different !

Change to same type

int int_add(char x, int y){

int sum;

sum = (int)x + y; //change x to an integer

return (sum);

Page 41: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 41/73

for Loop

General form:

for(expr1;expr2;expr3){

statement1;statement2;

. . .

}

Example

Page 42: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 42/73

for Loop

expr1

?-expr2

expr3

statement1

statement2

...

F

T

Page 43: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 43/73

 while Loop

General form: 

 while(expr1){

statement1;

statement2;. . .

Page 44: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 44/73

 while Loop

?-expr1

statement1

statement2

...

F

T

Page 45: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 45/73

do-while Loop

General form: 

do{

statement1;

statement2;. . .

} while(expr1); 

Page 46: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 46/73

do-while Loop

expr1

statement1

statement2

...

F

T

Page 47: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 47/73

 break Statement

 break is used to jump out of a loop 

General form:   break;

i=0;

do{if (i==10){ break;

}i++;

}while(1);

Page 48: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 48/73

 break Statement

 break is used to jump out of a loop 

General form:   break;

i=0;

do{if (i==10){ break;

}i++;

}while(1);

Page 49: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 49/73

continue Statement

continue allows you to stay in loop but skip over

statements within loop 

General form: continue; 

for (i=1;i<6;i++){if (i==3){

continue;}sum += i;

}

Page 50: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 50/73

continue Statement

continue allows you to stay in loop but skip over

statements within loop 

General form: continue; 

for (i=1;i<6;i++){if (i==3){

continue;}sum += i;

}

Page 51: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 51/73

if Statement

Executes a statement block based on a condition

General form: 

if (expr){

statement1;statement2;. . .

}

else{statement3;statement4;

} } Optional

Page 52: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 52/73

if Statement

Executes a statement block based on a condition

General form: 

if (Value == 0x34){

statement1;statement2;. . .

}

else{statement3;statement4;

} } Optional

 NB!! Not Value = 0x34

False

} True

Page 53: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 53/73

switch Statement

Replaces a group of if-else-if statements 

switch(expr){

case expr1: statement1; break;

case expr2: statement2; break;

. . .default: default-statement; //if no case

 break;}

Optional

Page 54: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 54/73

 Arrays

 An array  is a collection of values with the same data type

Each value in the array is an element  

 All elements are referenced by the name of the array with anoffset

Elements are stored in consecutive memory locations

Page 55: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 55/73

 Arrays

General form:

data-type Array-name[array_size];

data-type indicates what data type the elements in the

array will be 

 Array-name is the name of the array  

 Array_size defines how many elements are in the array 

E.g. char Keyboard[16];  Array name is Keyboard

 All data in Array is of type char

 Array has 16 locations (0 to 15)

Page 56: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 56/73

 Arrays

 Arrays are accessed using an index Index starts at 0 and ends at arraysize - 1   The elements of the array are:

day[0], day[1], day[2], day[3], day[4], day[5], day[6] 

Data[0], Data[1], Data[2], Data[3], Data[4], Data[5],Data[6] 

Elements may be initialized individually  day[0]=„S‟; 

Data[1]= 56; 

Or all together char day[7]={„S‟,‟M‟,‟T‟,‟W‟,‟T‟,‟F‟,‟S‟};

char Data[6]= {3,5,4,1,0,2};

Page 57: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 57/73

 Arrays

 Accessing arrays using an index variable

int List[10], i;

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

{

List[i] = i;

}

Array with name of List

and 10 elements  –int’s 

Page 58: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 58/73

 Arrays and Strings

 An array of characters is a string   Strings must be terminated by a null character \0 Null characters have a value of 0 char Greet[7]={„H‟,‟e‟,‟l‟,‟l‟,‟o‟,‟\0‟}; 

char Greet[7]={„H‟,‟e‟,‟l‟,‟l‟,‟o‟,0}; 

Strings may be initialized individually  as shown above

Or all togetherchar Greet[]=“Hello”; 

Compiler fill positions

Page 59: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 59/73

Strings

 What is the difference between the two statements?

char ch = „x‟; 

char str[ ] = „x‟; 

ch is a character constant

str is a string constant

str includes a null character

Page 60: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 60/73

Pointers

Used for indirect access to program elements single variables, arrays, structures, functions, etc.

Declared using “splat” notation: data-type *name;  where data-type is the data type of data the pointer is pointing to andname is the pointer name

Pointer functionality is similar to the @ use in assembler Pointers may be operated on just like any other variable

compiler automatically adjusts pointer registers to data size similar to array and index combination 

Page 61: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 61/73

Pointer Initialization

Pointers contain addresses

pointers must always be manually initialized

“&” notation used to initialize pointers to single variables

array name may be used to initialize array pointersint Var; //declare a variable

int *VPtr; //declare pointer to int variable

char Array1[] = “Hello”; //declare and init an array 

char *APtr; //declare pointer to array

 VPtr = &Var; //initialize variable pointer

 APtr = Array1; //initialize array pointer

 Aptr = &Array1[0]; //does same as previous line

OR char *APtr = Array1; 

Page 62: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 62/73

Pointer Operations

Use “splat” to get at the data pointed to by the pointer  interpret as “the contents of”  called „de-referencing‟ the pointer 

Suppose we have: char *Ptr1; 

Ptr1=0x20; sets the pointer to point to address 0x20 *Ptr1=0x34; sets the contents of address 0x20 to

0x34 Ptr1 += 5; adds 5 to the pointer to point to

address 0x25

*Ptr1=0x67; 0x67 loaded into 0x25 *Ptr1 = *Ptrl + 2; adds 2 to the contents of address

( *Ptr1 += 2)  0x25. Will become 0x67 +2 = 0x69

Page 63: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 63/73

Pointer Operations

Pointer arithmetic may increment or decrement pointers

may add values to or subtract values from pointers

pointer will always be modified by the number of bytes in the data type it

is pointing to:

int *IntPtr;char *ChPtr;IntPtr++; //pointer incremented by 2

 bytesChPtr++; //pointer incremented by 1

 byte 

Page 64: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 64/73

Pointers vs Arrays

Pointers are a more efficient way to pass data tofunctions

 Arrays are always passed by reference (i.e. using the

base address of the array)

 Array names may be used interchangeably withpointer names

array names used without indices are interpreted as thebase address of the array 

Page 65: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 65/73

Pointers vs Arrays

Code example:

void Add(char *Ptr1, char Length)

{ while(Length != 0x00) //check if all elements complete{

*Ptr1 += 0x03; //add 3 to array elementLength--; //decrement loop countPtr1++; //move to next element

}}void main(void){

char Array[] = {0,1,2,3,4}; //define and initialize arraychar *APtr = Array; //define and init pointer to array

 Add(Array,5); //call function using array variable Add(APtr,5); //call function using pointer Add(&Array[0],5); //call function using array element

}

Page 66: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 66/73

Structures

Structures provide a method of grouping data may contain data of different types, including pointers and arrays

Structure declaration

members may be of different types

may have multiple variables defined

struct struct-name {

type member1;type member2;. . .

} variable-name;

Structure Tag

Member variables}Variable

Page 67: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 67/73

Declaring Structures

1 - First declare the template, then the variable namestruct Data{

int Cntrl;int Speed;

char Fault;};

struct Data Motor1;

void main (void){

Motor1.Cntrl = 0x7854; Motor1.Speed = 0xF38C;

Motor1.Fault = 0x56;------; }

Structure Tag

Member variables

} No Variable

Page 68: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 68/73

Declaring Structures

2 - Declare the template + The variable namestruct Data{

int Cntrl;int Speed;

char Fault;} Motor1, Motor2;

void main (void){

Motor1.Cntrl = 0x7854; 

Motor1.Speed = 0xF38C;Motor1.Fault = 0x56;

Motor2.Speed = 0xF4CC;

------; }

Structure Tag

Member variables

} Variable Names

Page 69: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 69/73

 Accessing Structures

Structure members are accessed using “dot” notation

variable-name.memberx 

Member may be used as a variable of the type of   memberx struct MyStruct

{char Ch[20]; //character string variableint Length; //string length variable

} StrData;

StrData.Ch[0]=„H‟; //initialize element 0 of string

StrData.Ch[1]=„i‟; //initialize element 1 of stringStrData.Ch[2]=„\0‟; //initialize element 2 of string

StrData.Length=3; //initialize string length

Page 70: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 70/73

Casting Variables

 Allows variables of one type to be converted to a variable of another type “square peg in a round hole”  precede variable to be cast with the desired data type enclosed in parens

 What happens in this situation?

upper byte of x is unknown Different types!!!!

unsigned int x; //define 16-bit integerunsigned char y; //define 8-bit character

y=0x32; //initialize y

x=y; //set x equal to y

Page 71: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 71/73

Casting Variables

Previous situation can be avoided by casting 

y is now treated like an integer during assignment

upper byte of Y set to 0

unsigned int x; //define 16-bit integerunsigned char y; //define 8-bit character

y=0x32; //initialize y

x=(unsigned int)y; //set x equal to y

Page 72: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 72/73

Casting Variables

Situations in which to cast copying smaller data type to a larger one

char -> int

int -> long 

float -> double assigning a pointer to a constant

 when in doubt...

Page 73: C Compiler Basics_8051

7/27/2019 C Compiler Basics_8051

http://slidepdf.com/reader/full/c-compiler-basics8051 73/73

ASK !!!!!!