Eet203 Lecture 2 - Introduction to c Programming

  • Upload
    oh-oda

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    1/24

    EET203

    MICROCONTROLLERSYSTEMS DESIGN

    LECTURE 2: INTRODUCTION TO CPROGRAMMING

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    2/24

    PIC16 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

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    3/24

    Variables

    A variable name is a label attached to thememory location where the variable value isstored.

    In C, the variable label is automatically assignedto the next available location

    The variable name and type must be declared atthe start of the program block

    Only alphanumeric characters (az, AZ, 09)and underscore, instead of space, can be used.

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    4/24

    Variables

    x is variable

    int x;x= 99;PORTD = x;

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    5/24

    Looping

    to execute continuously until the processor isturned off or reset

    the program generally jumps back at the end

    to repeat the main control loop

    implemented as a while loop

    int x;while(1){

    PORTD = x;x++;

    }

    While loop

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    6/24

    Decision Making

    To illustrate basic decision making is tochange an output depending on the state of aninput

    int x;PORTD=0x00;while(1){

    x=RC0;

    if(x==1) RD0=1;}

    If statement for the decision making

    The value is then tested in the ifstatement and the output set

    accordingly.

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    7/24

    PIC16 C Data Operations

    Variable typesFloating point numbersCharactersAssignment operators

    A main function of any computer program is to carry outcalculations and other forms of data processing

    Data structures are made up of different types of numericaland character variables, and a range of arithmetical andlogical operations are needed

    Microcontroller programs do not generally need to processlarge volumes of data, but processing speed is oftenimportant

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    8/24

    Variable Types

    Variable - to store the data values used in the program

    Variable labels are attached to specific locations whenthey are declared at the beginning of the program

    MCU can locate the data required by each operation inthe file registers

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    9/24

    Integers

    the default type is an unsigned 8-bit number, giving arange of values of 0 255

    Range of a number is determined by the number ofdifferent binary codes that can be represented

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    10/24

    Signed Integers

    uses the most significant bit (MSB) as the signbit, 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 integeris 32767 to + 32767

    The sign bit must be processed separately toget the right answer from a calculation

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    11/24

    Character Variable

    represented by ASCII codes

    The basic set of 7- bit characters includes theupper and lower case letters and the numerals

    and punctuation marks found on the standardcomputer keyboard

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    12/24

    Character Variable

    The statement answer = 'Y ' ; will assign the value 0

    x 59 to the variable answer

    For example, capital(upper case) A is1000001 (6510 )

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    13/24

    Assignment Operations

    A range of arithmetic and logic operations areneeded where single or paired operands areprocessed

    The result is assigned to one of the operandvariables or a third variable

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    14/24

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    15/24

    Conditional Operations

    Where a logical condition is tested in a while,if, or for statement, relational operators areused

    One variable is compared with a set value oranother variable, and the block is executed ifthe condition is true Examples :

    AND condition:if((a > b)& & ( c = d ))

    OR condition:if((a > b)||(c = d ))

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    16/24

    PIC16 C Sequence Control

    While loops

    Break, continue, goto

    If, else, switch

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    17/24

    While Loops

    while(condition) : provides a logical test at thestart of a loop, and the statement block isexecuted only if the condition is true

    do..while(condition) : the loop block beexecuted at least once, particularly if the testcondition is affected within the loop

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    18/24

    While Loops

    The WHILE test occurs before the block and the DOWHILE after

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    19/24

    Break, Continue, and Goto

    break the execution of a loop orblock in the middle of itssequence

    The block must be exited in anorderly way, and it is useful tohave the option of restarting theblock (continue) or proceeding

    to the next one (break). It is achieved by assigning a

    label to the jump destination andexecuting a goto..label

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    20/24

    If..Else and Switch..Case

    if-allows a block to be executed or skippedconditionally.

    else- allows an alternate sequence to be

    executed, when the if block is skipped

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    21/24

    If..Else and Switch..Case

    multichoice selection -which is provided by theswitch..case syntax

    switch..case : tests avariable value andprovides a set ofalternative sequences,one of which is selecteddepending on the testresult

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    22/24

    Array

    List of variables that are all of the same type andcan be referenced through the same name

    The individual variable in the array is called anarray 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])

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    23/24

    Array

    Can also being declare as multidimensionalarray

    Unsigned int height[4][5] 2 dimensional array

    4x5 (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

  • 8/2/2019 Eet203 Lecture 2 - Introduction to c Programming

    24/24

    Pointer

    A memory location (variable) that holds the address ofanother memory location

    Declarationtype *var-name;

    Type valid C data types Var-name name of the pointer

    * - indicates that the variable is a pointer variable

    Examplevoid main(void)

    {

    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

    () means value of