EET 203 MICROCONTROLLER SYSTEMS DESIGN LECTURE 2 INTRODUCTION

  • Slides: 24
Download presentation
EET 203 MICROCONTROLLER SYSTEMS DESIGN LECTURE 2: INTRODUCTION TO C PROGRAMMING

EET 203 MICROCONTROLLER SYSTEMS DESIGN LECTURE 2: INTRODUCTION TO C PROGRAMMING

PIC 16 C Program Basics Variables Looping Decisions The purpose of an embedded program

PIC 16 C Program Basics Variables Looping Decisions The purpose of an embedded program is (i) read in data or control inputs, (ii) process the data (iii) operate the outputs as required

Variables A variable name is a label attached to the memory location where the

Variables A variable name is a label attached to the memory location where the variable value is stored. In C, the variable label is automatically assigned to the next available location The variable name and type must be declared at the start of the program block Only alphanumeric characters (a–z, A–Z, 0– 9) and underscore, instead of space, can be used.

Variables x is variable int x; x= 99; PORTD = x;

Variables x is variable int x; x= 99; PORTD = x;

Looping to execute continuously until the processor is turned off or reset the program

Looping to execute continuously until the processor is turned off or reset the program generally jumps back at the end to repeat the main control loop implemented as a “ while ” loop While loop int x; while(1){ PORTD = x; x++; }

Decision Making To illustrate basic decision making is to change an output depending on

Decision Making To illustrate basic decision making is to change an output depending on the state of an input int x; PORTD=0 x 00; while(1) { x=RC 0; if(x==1) RD 0=1; } If statement for the decision making The value is then tested in the if statement and the output set accordingly.

PIC 16 C Data Operations Variable types Floating point numbers Characters Assignment operators A

PIC 16 C Data Operations Variable types Floating point numbers Characters Assignment operators A main function of any computer program is to carry out calculations and other forms of data processing Data structures are made up of different types of numerical and character variables, and a range of arithmetical and logical operations are needed Microcontroller programs do not generally need to process large volumes of data, but processing speed is often important

Variable Types Variable - to store the data values used in the program Variable

Variable Types Variable - to store the data values used in the program Variable labels are attached to specific locations when they are declared at the beginning of the program MCU can locate the data required by each operation in the file registers

Integers the default type is an unsigned 8 -bit number, giving a range of

Integers the default type is an unsigned 8 -bit number, giving a range of values of 0 – 255 Range of a number is determined by the number of different binary codes that can be represented

Signed Integers uses the most significant bit (MSB) as the sign bit, so the

Signed Integers uses the most significant bit (MSB) as the sign bit, so the range is accordingly reduced by half MSB 0 represents a positive number, MSB 1 indicates a negative number Therefore, the range for a 16 -bit signed integer is – 32767 to + 32767 The sign bit must be processed separately to get the right answer from a calculation

Character Variable represented by ASCII codes The basic set of 7 - bit characters

Character Variable represented by ASCII codes The basic set of 7 - bit characters includes the upper and lower case letters and the numerals and punctuation marks found on the standard computer keyboard

Character Variable For example, capital (upper case) A is 1000001 (6510 ) The statement

Character Variable For example, capital (upper case) A is 1000001 (6510 ) The statement answer = ' Y ' ; will assign the value 0 x 59 to the variable ‘ answer ’

Assignment Operations A range of arithmetic and logic operations are needed where single or

Assignment Operations A range of arithmetic and logic operations are needed where single or paired operands are processed The result is assigned to one of the operand variables or a third variable

Conditional Operations Where a logical condition is tested in a while, if, or for

Conditional Operations Where a logical condition is tested in a while, if, or for statement, relational operators are used One variable is compared with a set value or another variable, and the block is executed if the condition is true Examples : AND condition: if((a > b)& & ( c = d )) OR condition: if((a > b)||(c = d ))

PIC 16 C Sequence Control While loops Break, continue, goto If, else, switch

PIC 16 C Sequence Control While loops Break, continue, goto If, else, switch

While Loops while(condition) : provides a logical test at the start of a loop,

While Loops while(condition) : provides a logical test at the start of a loop, and the statement block is executed only if the condition is true do. . while(condition) : the loop block be executed at least once, particularly if the test condition is affected within the loop

While Loops The WHILE test occurs before the block and the DO WHILE after

While Loops The WHILE test occurs before the block and the DO WHILE after

Break, Continue, and Goto break the execution of a loop or block in the

Break, Continue, and Goto break the execution of a loop or block in the middle of its sequence The block must be exited in an orderly way, and it is useful to have the option of restarting the block (continue) or proceeding to the next one (break). It is achieved by assigning a label to the jump destination and executing a goto. . label

If. . Else and Switch. . Case if - allows a block to be

If. . Else and Switch. . Case if - allows a block to be executed or skipped conditionally. else - allows an alternate sequence to be executed, when the if block is skipped

If. . Else and Switch. . Case multichoice selection which is provided by the

If. . Else and Switch. . Case multichoice selection which is provided by the switch. . case syntax switch. . case : tests a variable value and provides a set of alternative sequences, one of which is selected depending on the test result

Array List of variables that are all of the same type and can be

Array List of variables that are all of the same type and can be referenced through the same name The individual variable in the array is called an array element Used to handle groups of related data Declaration for one-dimensional array type var_name [size] � type – valid C data type (char, int, long) � var-name – the array name � size – specifies how many elements are in the array � Example: int height[50]; The first element to be at an index of 0 (height[0]) The last element to be at an index size-1(height[49])

Array Can also being declare as multidimensional array � Unsigned int height[4][5] – 2

Array Can also being declare as multidimensional array � Unsigned int height[4][5] – 2 dimensional array 4 x 5 (4 rows, 5 columns) Assigning initial value � Int height[3] = { 23, 45, 67}; � Int height[] = {23, 45, 67}; � Char str[3] = {‘a’, ’b’, ’c’}; � Char name[6] = “Ernie”; equal to char name[6] = {‘E’, ’r’, ’n’, ’i’, ’e’, ’/0’}; ‘/0’ is a null terminator indicates the end of string

Pointer A memory location (variable) that holds the address of another memory location Declaration

Pointer A memory location (variable) that holds the address of another memory location Declaration type *var-name; � Type – valid C data types � Var-name – name of the pointer � * - indicates that the variable is a pointer variable � Example void main(void) () means value of { int *x; int height[4] = {1, 2, 3, 4}; a = &height; // assigned a to the first address of array (a) = height[0] a++; // set the pointer a to next array address (a) = height[1] printf(“%d”, *a); // print the array value as pointed by pointer a result is 2 } Suitable for accessing look-up table