+ All Categories
Home > Documents > Chapter 8 C51

Chapter 8 C51

Date post: 04-Apr-2018
Author: m-suleman-murad
View: 229 times
Download: 0 times
Share this document with a friend
Embed Size (px)

of 17

  • 7/29/2019 Chapter 8 C51


    8051 C Programming

    Chapter 8

    The 8051 Microcontroller by

    Scott Mc Kenzie

  • 7/29/2019 Chapter 8 C51


    The RS-232 Standard

    Most widely used serial I/O interfacing standard Input and output voltage levels are not TTL


    1 bit is represented by -3 to -25 V

    0 bit is +3 to +25 V -3 to +3 is undefined

    To connect RS232 to a microcontroller system must

    use voltage converters such as MAX232 to convert

    the TTL logic levels to the RS232 voltage levels, andvice versa

    MAX232 IC chips are commonly referred to as line drivers

  • 7/29/2019 Chapter 8 C51


    The RS-232 DB-25 Connector

    RS232 Connector DB-25

  • 7/29/2019 Chapter 8 C51


    The RS-232 DB-9 Connector

    DB-9 9-Pin Connector

  • 7/29/2019 Chapter 8 C51


    The Line Driver- Max 232

    Max-232 Converts from RS232 voltage levels to TTL voltage levels

    Uses a +5 V power source

    MAX232 has two sets of line drivers for transferring and

    receiving data Line drivers used for TxD are called T1 and T2

    Line drivers for RxD are designated as R1 and R2

    T1 and R1 are used together for TxD and RxD of the

    8051 Second set is left unused

  • 7/29/2019 Chapter 8 C51


    Inside Max-232

  • 7/29/2019 Chapter 8 C51


    Why C51? Offers the benefits of high-level, structured programming languages

    such as C. Writing subroutines with ease

    Sequence of operations is simple to trace, facilitating debugging

    Finite number of structures with standardized terminology

    Structures lend themselves easily to building subroutines.

    The set of structures is complete that is all programs can be written using

    three structures Statements

    Assignment, call to a subroutine, etc

    Loops The while/do statement, Repeat/Until statements

    Choice If-then-else statement, the case statement, the goto statement

    Structured programming results in increased programming productivity

    Relieves the programmer of the hardware details

    Easier to write especially for large and complex programs

    Produces more readable program source codes.

  • 7/29/2019 Chapter 8 C51


    The downside of C51

    Generally generates larger machine codes May execute more slowly

    Require relatively large amount of memory

    Programmer has less control and less ability to

    directly interact with the hardware Some problems can be difficult to solve using only

    the three structures discussed earlier

    Nested structures can be difficult to follow

  • 7/29/2019 Chapter 8 C51


    C51 Data types

    Data Type Bits Bytes Value Range

    bit 1 - 0 to 1

    signed char 8 1 -128 to +127

    unsigned char 8 1 0 to 255

    signed short 16 2 -32768 to +32767

    unsigned short 16 2 0 to 65535

    signed int 16 2 -32768 to +32767

    unsigned int 16 2 0 to 65535

    signed long 32 4 -2147483648 to +2147483647

    unsigned long 32 4 0 to 4294967295float 32 4 +-1.175494E-38 to +-3.402823E+38

    sbit 1 - 0 to 1

    sfr and sfr16 8 & 16 1 & 2 0 to 255 & 0 to 65535

  • 7/29/2019 Chapter 8 C51


    C-51 Data types

    Data type bitcan be used to declare variables that reside in the 8051s bit-addressable locations

    bit flag = 0;

    Declares a bitvariable called flag and initializes it to 0.

    Data type sbit is similar to the bitdata type, except that it is normally used to

    declare 1-bit variables that reside in SFRs, for example:sbit P = 0xD0;

    Declares the sbitP and specifies that it refers to bit address D0H, which is

    really the LSB of PSW SFR.

    In case ofsbitdeclarations the assignment operator indicates what address

    the sbit resides in, while in bitdeclarations it is used to specify the initial

    value of the bitvariable. We can use a previously defined sfrvariable as

    base address and assign oursbitvariable to refer to a certain bit within that

    sfr.sfr PSW = 0xD0;

    sbit P = PSW^0;

  • 7/29/2019 Chapter 8 C51



    REG51.H contains the declarations for: All the special function registers in 8051

    All the individual flags, status and control bits in the bit

    addressable SFRs

    REG51.H is an important include in our C51programs.

    It enables us to refer to the SFRs and all the flags and

    control bit etc using their symbols i.e. PSW, ACC, B, SP,

    TR0, TR1 etc.

  • 7/29/2019 Chapter 8 C51


    Memory TypesMemory Type Description (Size)

    code Code memory (64 Kbytes). Objects

    declared in this segment must be initialized

    at compile time.

    data Directly addressable internal data memory

    (128 bytes)

    idata Indirectly addressable internal datamemory (256 bytes)

    bdata Bit-addressable internal data memory (16


    xdata External data memory (64 Kbytes)

    pdata Paged external data memory (256 bytes)

    MOVX A, @Ri, MOVX @Ri, A

    char code errormsg[] = An error occurred;

    signed int data num1;

    bit bdata numbit;

    unsigned int xdata num2;

  • 7/29/2019 Chapter 8 C51


    Accessing External Data and Code Memory



    The higher order address

    byte comes from P2, the

    lower order comes from Ri,so before using this

    instruction you have to

    initialize P2

  • 7/29/2019 Chapter 8 C51


    Memory Models

    Memory Model DescriptionSmall Variables default to the internal data memory (data)

    Compact Variables default to the first 256 bytes of external data

    memory (pdata)

    Large Variables default to external data memory (xdata)

    If you wish all the variables to be assigned a default memory type without

    having to specify them one by one then

    Use memory models by choosing anyone from the above.

    The default model is Small.Use the

    #pragmadirective to explicitly select any other memory model

    than the default Smallmodel.

    #pragma small

    #pragma compact

    #pragma large

  • 7/29/2019 Chapter 8 C51


    C51 Functions

    return_type function_name(arguments) [memory] [reentrant][interrupt] [using]

    return_type: refers to data type of the return value.function_name: is any name that you wish to call thefunction as.

    arguments: is the list of the type and number of inputvalues.

    memory: refers to an explicit memory model (small,compact or large)

    reentrant: refers to whether the function is reentrant(recursive)

    interrupt: indicates that the function is actually an ISRusing: explicitly specifies which register bank to use

  • 7/29/2019 Chapter 8 C51


    Parameter Passing

    Parameters to functions are passed through registersand memory

    Passing through registers is the faster and default way

    The registers used and their purpose are as follows.

    Since there are only 8 registers in available, any extra

    parameters may be passed using fixed memory locations.

    Number ofArguments



    Long/Float GenericPointer

    1 R7 R6 & R7 R4-R7 R1-R3

    2 R5 R4 & R5 R0-R3

    3 R3 R2 & R3

  • 7/29/2019 Chapter 8 C51


    Returning Values

    Unlike parameters output values must be returnedfrom functions via registers only.

    Return Type Register Description

    bit Carry Flag (C)

    char/unsigned char/1-

    byte pointer


    int/unsigned int/2-byte


    R6 and R7 MSB in R6 and LSB in R7

    long/unsigned long R4-R7 MSB in R4 and LSB in R7

    float R4-R7 32-bit IEEE format

    generic pointer R1-R3 Memory type in R3, MSB in R2,

    LSB in R1