embedded c advanced

Upload: abhishek-ek

Post on 06-Apr-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Embedded C Advanced

    1/15

    1/10/2012 Microcontroller 8051 1

    Advanced programming

    techniques withEmbedded C

  • 8/3/2019 Embedded C Advanced

    2/15

    1/10/2012 Microcontroller 8051 2

    InterruptsInterrupts

    The C51 compiler provides a method of callingThe C51 compiler provides a method of calling

    a C function when an interrupt occursa C function when an interrupt occurs

    This support allows you to create interruptThis support allows you to create interrupt

    service routines in Cservice routines in C

    The compiler automatically generates theThe compiler automatically generates the

    interrupt vector and entry and exit code forinterrupt vector and entry and exit code for

    the interrupt routinethe interrupt routine

  • 8/3/2019 Embedded C Advanced

    3/15

    1/10/2012 Microcontroller 8051 3

    InterruptsInterrupts

    The The interruptinterrupt function attribute, when function attribute, when

    included in a declaration, specifies that theincluded in a declaration, specifies that the

    associated function is an interrupt functionassociated function is an interrupt function

    The interrupt number and the register bankThe interrupt number and the register bank

    are specifiedare specified

    Additionally, you can specify the register bankAdditionally, you can specify the register bankused for that interrupt with the used for that interrupt with the usingusing

    function attributefunction attribute

  • 8/3/2019 Embedded C Advanced

    4/15

    1/10/2012 Microcontroller 8051 4

    InterruptsInterrupts

  • 8/3/2019 Embedded C Advanced

    5/15

    1/10/2012 Microcontroller 8051 5

    ExampleExample

    unsigned int interruptcnt;unsigned int interruptcnt;

    unsigned char second;unsigned char second;

    void timer0 (void) interrupt 1 using 2 {void timer0 (void) interrupt 1 using 2 {

    if (++interruptcnt == 4000) {if (++interruptcnt == 4000) {

    /* count to 4000 *//* count to 4000 */

    second++;second++; /* second counter *//* second counter */

    interruptcnt = 0;interruptcnt = 0; /* clear int counter *//* clear int counter */

    }}

  • 8/3/2019 Embedded C Advanced

    6/15

    1/10/2012 Microcontroller 8051 6

    Intrinsic Library RoutinesIntrinsic Library Routines

    The libraries included with the compiler includeThe libraries included with the compiler includea number of routines that are implemented asa number of routines that are implemented as

    intrinsic functionsintrinsic functions

    NonNon--intrinsic functions generate ACALL orintrinsic functions generate ACALL orLCALL instructions to perform the libraryLCALL instructions to perform the library

    routineroutine

    Intrinsic functions generate inIntrinsic functions generate in--line code (whichline code (whichis faster and more efficient) to perform theis faster and more efficient) to perform the

    library routinelibrary routine

  • 8/3/2019 Embedded C Advanced

    7/15

    1/10/2012 Microcontroller 8051 7

    Intrinsic Function Description

    _crol_crol_ Rotate character leftRotate character left_cror_ _cror_ Rotate character rightRotate character right

    _irol_ _irol_ Rotate integer leftRotate integer left

    _iror__iror_ Rotate integer rightRotate integer right

    _lrol_ _lrol_ Rotate long integer leftRotate long integer left

    _lror_ _lror_ Rotate long integer rightRotate long integer right

    _nop_ _nop_ No operation (8051 NOPNo operation (8051 NOP

    instruction)instruction)_testbit__testbit_ Test and clear bit (8051 JBCTest and clear bit (8051 JBCinstruction)instruction)

  • 8/3/2019 Embedded C Advanced

    8/15

    1/10/2012 Microcontroller 8051 8

    In-line assembly

    >ASM and ENDASM directivesare used along with

    #pragma directives.

  • 8/3/2019 Embedded C Advanced

    9/15

    1/10/2012 Microcontroller 8051 9

    Generating assembly source file

    >#pragama src directive is used togenerate assembly code for C

    program.

  • 8/3/2019 Embedded C Advanced

    10/15

    1/10/2012 Microcontroller 8051 10

    Disabling interrupts

    #pragma disableThis directive instructs the compiler

    to disable the interrupt for the

    duration of the function.

  • 8/3/2019 Embedded C Advanced

    11/15

    1/10/2012 Microcontroller 8051 11

    Function parameters

    By default c functions pass up tothree parameters in registers,theremaining in fixed memory

    locations. Parameter passing in registers can

    be disabled by calling #pragmaNOREGPARMS.

  • 8/3/2019 Embedded C Advanced

    12/15

    1/10/2012 Microcontroller 8051 12

    Function parameters

  • 8/3/2019 Embedded C Advanced

    13/15

    1/10/2012 Microcontroller 8051 13

  • 8/3/2019 Embedded C Advanced

    14/15

    1/10/2012 Microcontroller 8051 14

    Return values

  • 8/3/2019 Embedded C Advanced

    15/15

    I request Electronics andcommunication ENGINEERINGstudents to visit my blog for

    more

    abhishek1ek.blogspot.com

    awhengineering.blogspot.comTHANK YOU

    1/10/2012 Microcontroller 8051 15