embeddedc_class1

Upload: tejassarwajna-degala

Post on 14-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 EmbeddedC_class1

    1/48

    9/7/2013 Microcontroller 8051 1

    Embedded C

  • 7/29/2019 EmbeddedC_class1

    2/48

    9/7/2013 Microcontroller 8051 2

    The development process

    Plan tasks and interaction

    Setup overall project file

    Edit

    Project.PRJ

    Task1.A51 Task2.C

    revise

    1 2

  • 7/29/2019 EmbeddedC_class1

    3/48

    9/7/2013 Microcontroller 8051 3

    The development process

    Task1.A51 Task2.C

    Assembled Compiled andassembled

    Task1.lstTask2.lst

    Link/LocateVarious.lib Project.M513

  • 7/29/2019 EmbeddedC_class1

    4/48

    9/7/2013 Microcontroller 8051 4

    The development process

    Link/LocateVarious.lib Project.M51

    Project.hexBurn in EPROM ordownload

    revise

  • 7/29/2019 EmbeddedC_class1

    5/48

    9/7/2013 Microcontroller 8051 5

    Example for EDE

  • 7/29/2019 EmbeddedC_class1

    6/48

    9/7/2013 Microcontroller 8051 6

    Intel HEX format

    Example::10400000755AFF111675907F11167590BF111675B0

    :10401000903F111680EA780379007A00DAFED9FA27

    :03402000D8F622AD

    :00000001FF

  • 7/29/2019 EmbeddedC_class1

    7/48

    9/7/2013 Microcontroller 8051 7

    An Example

    #include#include

    void main(void) {

    }

  • 7/29/2019 EmbeddedC_class1

    8/48

    9/7/2013 Microcontroller 8051 8

    Development Tools

    Debugging Simulator

    becomes less useful when there

    is a lot of time critical hardwareDownload to a target with amonitor

    Disadvantages: serial port is tiedup for downloading

    need for a software

  • 7/29/2019 EmbeddedC_class1

    9/48

    9/7/2013 Microcontroller 8051 9

    Development Tools

    In-circuit emulatorcombines monitor and simulator

    functions

    emulator plugs into the socket

    where the final processor would

    resideCostly

  • 7/29/2019 EmbeddedC_class1

    10/48

    9/7/2013 Microcontroller 8051 10

    What Language To Use for EmbeddedApplications

    Assembly - Most efficient

    but difficult to read and maintain

    C

    C++

    Java

  • 7/29/2019 EmbeddedC_class1

    11/48

    9/7/2013 Microcontroller 8051 11

    Why C

    Can control many machine level functionswithout resorting to assembly language

    Application can be written in C more easily

    than assembly language because the

    development software manages the details

    because of modularity,reusable codes can bedeveloped and maintained

  • 7/29/2019 EmbeddedC_class1

    12/48

    9/7/2013 Microcontroller 8051 12

    Why C

    The programmer need not be very thoroughwith the architecture of the processor

    Code developed in C is more portable

  • 7/29/2019 EmbeddedC_class1

    13/48

    9/7/2013 Microcontroller 8051 13

    Memory Models

    SMALL :

    all variables default to the internal data

    memory of the 8051

    same as if they were declared explicitly

    using the data memory type specifier

    variable access is very efficient

    However, all data objects, as well as the

    stack must fit into the internal RAM

  • 7/29/2019 EmbeddedC_class1

    14/48

    9/7/2013 Microcontroller 8051 14

    Memory Models

    Compact:

    all variables default to one page of external

    data memory

    same as if they were explicitly declared

    using the pdata memory type specifier

    can accommodate a maximum of 256 bytes

    of variables as it is accessed indirectly

    through R0 and R1

    variable access is not as fast as small model

  • 7/29/2019 EmbeddedC_class1

    15/48

    9/7/2013 Microcontroller 8051 15

    Memory Models

    Large:

    all variables default to external data memory

    same as if they were explicitly declaredusing the xdata memory type specifier

    Memory access through this data pointer is

    inefficient, especially for variables with alength of two or more bytes

    This type of data access generates morecode than the small or compact models

  • 7/29/2019 EmbeddedC_class1

    16/48

    9/7/2013 Microcontroller 8051 16

    Key differences between C and Keil C

  • 7/29/2019 EmbeddedC_class1

    17/48

    9/7/2013 Microcontroller 8051 17

    Key differences between C and Keil C

    The sbit, sfr, and sfr16 data types areincluded to allow access to the special

    function registers that are available on

    the 8051E.g: the declaration: sfr P0 = 0x80

    declares the variable P0 and assigns it the

    special function register address of0x80.This is the address of PORT 0 on the 8051.

  • 7/29/2019 EmbeddedC_class1

    18/48

    9/7/2013 Microcontroller 8051 18

    Memory Types

  • 7/29/2019 EmbeddedC_class1

    19/48

    9/7/2013 Microcontroller 8051 19

    Memory Types

    Frequently used variables are put in

    the internal memory in preference to

    the external RAM

    By including a memory type specifier

    in the variable declaration, you can

    specify where variables are stored

  • 7/29/2019 EmbeddedC_class1

    20/48

    9/7/2013 Microcontroller 8051 20

    Example

    char data var1;char code text[] = "ENTER PARAMETER:";

    unsigned long xdata array[100];

    float idata x,y,z;unsigned int pdata dimension;

    unsigned char xdata vector[10][4][4];

    char bdata flags;

  • 7/29/2019 EmbeddedC_class1

    21/48

    9/7/2013 Microcontroller 8051 21

    Default memory type

    If the memory type specifier is omitted in a

    variable declaration, the default or implicit

    memory type is automatically selected

    Function arguments and automatic variables

    which cannot be located in registers are alsostored in the default memory area

    The default memory type is determined by the

    SMALL, COMPACT and LARGE compiler controldirectives

  • 7/29/2019 EmbeddedC_class1

    22/48

    9/7/2013 Microcontroller 8051 22

    An Example 8 switches are connected to P0

    8 LEDs are connected to P2#include void msec (unsigned int);void main (){

    unsigned char array[10];

    unsigned char I;while (1){

    for (i=0; i

  • 7/29/2019 EmbeddedC_class1

    23/48

    9/7/2013 Microcontroller 8051 23

    Bitwise operators

    Logical operation Assembly CNOT CPL A ~

    AND ANL A,# &

    OR ORL A,# |

    XOR XRL A,# ^

  • 7/29/2019 EmbeddedC_class1

    24/48

    9/7/2013 Microcontroller 8051 24

    Assignment operators

    Unique to C is the shorthandrepresentation for modifying avariable and assigning it back

    portA = portA & 0xf7;

    portA &= oxf7;

  • 7/29/2019 EmbeddedC_class1

    25/48

    9/7/2013 Microcontroller 8051 25

    Delay by software looping

    Void msec (unsigned int x){

    unsigned char j;while (x-- > 0)

    {for (j=0; j

  • 7/29/2019 EmbeddedC_class1

    26/48

    9/7/2013 Microcontroller 8051 26

    Arrays andPointers

  • 7/29/2019 EmbeddedC_class1

    27/48

    9/7/2013 Microcontroller 8051 27

    Accessing an array element

    Unsigned int ary[20];unsigned int x;

    ary[9] =x;

    While defining arrays, it has to bemade sure that large amounts of

    memory is not tied up

  • 7/29/2019 EmbeddedC_class1

    28/48

    9/7/2013 Microcontroller 8051 28

    Structure

    As structure is made of contiguousmemory locations, it has to belooked into

    A structure could represent theinformation about the solenoids ofa sequencer

    ie a 1 for a bit means that asolenoid is on

  • 7/29/2019 EmbeddedC_class1

    29/48

    9/7/2013 Microcontroller 8051 29

    Pointers

    C51 compiler supports two differenttypes of pointers:

    memory specific pointers

    and generic pointers

  • 7/29/2019 EmbeddedC_class1

    30/48

    9/7/2013 Microcontroller 8051 30

    Generic Pointers

    char *s; /* string ptr */int *numptr; /* int ptr */

    long *state; /* long ptr */

    use three bytes,the first for the memory type,

    the second for the high-order byte of the offsetand the third for the low-order byte of theoffset

    Generic pointers may be used to access anyvariable regardless of its location in 8051memory space.

  • 7/29/2019 EmbeddedC_class1

    31/48

    9/7/2013 Microcontroller 8051 31

    Memory Specific Pointers

    char data *str; /* ptr to string in data */

    int xdata *numtab; /* ptr to int(s) in xdata */

    long code *powtab; /* ptr to long(s) in code */

    can be stored using only one byte (idata,

    data, bdata, and pdata pointers) or

    two bytes (code and xdata pointers)

  • 7/29/2019 EmbeddedC_class1

    32/48

    9/7/2013 Microcontroller 8051 32

    Comparison

  • 7/29/2019 EmbeddedC_class1

    33/48

    9/7/2013 Microcontroller 8051 33

    Unions

    A combination of different datatypes

    applies different names and typesfor the same space

  • 7/29/2019 EmbeddedC_class1

    34/48

    9/7/2013 Microcontroller 8051 34

    Passing Parameters

    Functions that go betweensoftware and hardware are calleddrivers

    As you call a function from aprogram ,it is needed to pass theparameters

    The transfer is specific to acompiler

  • 7/29/2019 EmbeddedC_class1

    35/48

    9/7/2013 Microcontroller 8051 35

    Reentrancy

    Normally,C51 functions are not reentrant

    The reentrant function attribute allows you to

    declare functions that may be reentrant and,

    therefore, may be called recursively

  • 7/29/2019 EmbeddedC_class1

    36/48

    9/7/2013 Microcontroller 8051 36

    Example

    int calc (char i, int b) reentrant

    {

    int x;

    x = table [i];

    return (x * b);

    }

    For each reentrant function, a reentrant stack

    area is simulated in internal or externalmemory depending on the memory model

  • 7/29/2019 EmbeddedC_class1

    37/48

    9/7/2013 Microcontroller 8051 37

    Interrupts

    The C51 compiler provides a method of callinga C function when an interrupt occurs

    This support allows you to create interrupt

    service routines in C

    The compiler automatically generates the

    interrupt vector and entry and exit code for

    the interrupt routine

  • 7/29/2019 EmbeddedC_class1

    38/48

    9/7/2013 Microcontroller 8051 38

    Interrupts

    The interrupt function attribute, whenincluded in a declaration, specifies that the

    associated function is an interrupt function

    The interrupt number and the register bank

    are specified

    Additionally, you can specify the register bank

    used for that interrupt with the using

    function attribute

    l

  • 7/29/2019 EmbeddedC_class1

    39/48

    9/7/2013 Microcontroller 8051 39

    Example

    unsigned int interruptcnt;

    unsigned char second;

    void timer0 (void) interrupt 1 using 2 {

    if (++interruptcnt == 4000) {/* count to 4000 */

    second++; /* second counter */

    interruptcnt = 0; /* clear int counter */

    }

    i

  • 7/29/2019 EmbeddedC_class1

    40/48

    9/7/2013 Microcontroller 8051 40

    Parameter passing

    The C51 compiler passes up to three function

    arguments in CPU registers

    Argument passing can be controlled with the

    REGPARMS and NOREGPARMS control

    directives

    The following table lists the registers used for

    different arguments and data types

    P i

  • 7/29/2019 EmbeddedC_class1

    41/48

    9/7/2013 Microcontroller 8051 41

    Parameter passing

    P t i

  • 7/29/2019 EmbeddedC_class1

    42/48

    9/7/2013 Microcontroller 8051 42

    Parameter passing

  • 7/29/2019 EmbeddedC_class1

    43/48

    9/7/2013 Microcontroller 8051 43

    Interfacing to assembly

    Can access assembly routines from C andvice versa

    Function parameters are passed via CPU

    registers or, if the NOREGPARMS controlis used, via fixed memory locations

    Values returned from functions are

    always passed in CPU registers

  • 7/29/2019 EmbeddedC_class1

    44/48

    9/7/2013 Microcontroller 8051 44

    Interfacing to assembly

    can use the SRC directive to direct the C51compiler to generate a file ready to assemblewith the A51 assembler instead of an objectfile.

    For example,the following C source file:

    unsigned int asmfunc1 (unsigned int arg)

    {

    return (1 + arg);

    }

  • 7/29/2019 EmbeddedC_class1

    45/48

    9/7/2013 Microcontroller 8051 45

    Interfacing to assembly

    ?PR?_asmfunc1?ASM1 SEGMENT CODE

    PUBLIC _asmfunc1RSEG ?PR? _asmfunc1?ASM1USING 0_asmfunc1:

    ;---- Variable 'arg?00' assigned to Register 'R6/R7' ----MOV A,R7 ; load LSB of the intADD A,#01H ; add 1MOV R7,A ; put it back into R7CLR A

    ADDC A,R6 ; add carry & R6MOV R6,A?C0001:RET ; return result in R6/R7

    bl

  • 7/29/2019 EmbeddedC_class1

    46/48

    9/7/2013 Microcontroller 8051 46

    Interfacing to assembly

    Can use the #pragma asm and

    #pragma endasm preprocessor directives

    to insert assembly instructions into your

    C source code

  • 7/29/2019 EmbeddedC_class1

    47/48

    9/7/2013 Microcontroller 8051 47

    Intrinsic Library Routines

    The libraries included with the compiler include

    a number of routines that are implemented as

    intrinsic functions

    Non-intrinsic functions generate ACALL or

    LCALL instructions to perform the library

    routine

    Intrinsic functions generate in-line code (which

    is faster and more efficient) to perform the

    library routine

    i i i i i

  • 7/29/2019 EmbeddedC_class1

    48/48

    9/7/2013 Microcontroller 8051 48

    Intrinsic Function Description

    _crol_ Rotate character left

    _cror_ Rotate character right

    _irol_ Rotate integer left

    _iror_ Rotate integer right

    _lrol_ Rotate long integer left

    _lror_ Rotate long integer right

    _nop_ No operation (8051 NOP

    instruction)

    _testbit_ Test and clear bit (8051 JBCinstruction)