+ All Categories
Home > Documents > C FORMAT

C FORMAT

Date post: 07-Apr-2018
Category:
Upload: lakshmidpa
View: 216 times
Download: 0 times
Share this document with a friend

of 103

Transcript
  • 8/4/2019 C FORMAT

    1/103

    1 Advance C Concepts

    CONTENTS

    C DATA TYPES

    OPERATORS

    ARRAYS

    PASSING ARRAYS TO FUNCTIONS

    STRUCTURES

    POINTERS

    MEMORY ALLOCATION

    FILE HANDLING

    MACROS

    UNION

    HARDWARE RELATED C-COMMUNICATING THROUGH UART

    DATA STRUCTURES

    C PROGRAMMING

  • 8/4/2019 C FORMAT

    2/103

    2 Advance C Concepts

    C is middle level language developed AT&T belllaboratory.

    CPL- combined programming language wasdeveloped at Cambridge university. It was so big and

    hard to learnBPCL- Basic combined programming language was

    developed by Martin Richards of Cambridgeuniversity. Too less powerful and too specific

    B was developed by Ken Thompson. Too specific

    Later Dennis Ritchie developed C inheriting theproperties of B and Bpcl

    Introduction to C

  • 8/4/2019 C FORMAT

    3/103

    3 Advance C Concepts

    Constants, variables and data types

    Constants entity whose value does not change duringprogramming execution

    Variables entity whose value changes with program execution

    Data types int, char, float, double, long,

  • 8/4/2019 C FORMAT

    4/103

    4 Advance C Concepts

    Processor directives

    Function declaration and definitions

    Any C program must have a main function which isthe entry point for all the program

    The main function can call other library functions

    and user defined functions.

    Structure of C Program

  • 8/4/2019 C FORMAT

    5/103

    5 Advance C Concepts

    Signed charater - %c

    Unsigned character - %c

    Short signed int - %d

    Short unsigned int - %ulong signed int - %ld

    long unsigned int - %lu

    float - %f double - %lf

    long double - %Lf

    Format control

  • 8/4/2019 C FORMAT

    6/103

    6 Advance C Concepts

    The binary arithmetic operators are +, -, *, /, and themodulus operator %. Integer division truncates any

    fractional part. The expression x % y produces the

    remainder when x is divided by y, and thus is zerowhen y divides x exactly.

    The *, / and % have same priority which has higher

    priority that binary operators + and - .

    In case of expression containing the equal

    precedence it gets evaluated from left to right

    Arithmetic Operators

  • 8/4/2019 C FORMAT

    7/103

    7 Advance C Concepts

    The relational operators are > >= <

  • 8/4/2019 C FORMAT

    8/103

    8 Advance C Concepts

    && - logical AND. Returns 1 when both end are same

    | | - logical OR . Returns 1 when both the end are

    non- zero.

    ! - inverting

    Expressions connected by && or || are evaluated left

    to right, and evaluation stops as soon as the truth or

    falsehood of the result is known.

    Most C programs rely on these properties.

    The Logical Operators

  • 8/4/2019 C FORMAT

    9/103

    9 Advance C Concepts

    ++ - increments the operand value by 1.

    -- - decrements the operand value by 1.

    Post fixing used after the variable. (eg) x++

    Prefixing used before the variable. (eg) ++x

    ?: is called ternary operator

    Exp1 ? Exp1 : Exp2

    Requires three operators.

    Can be taken as the alternate way for if condition.

    Unary and Ternary operator

  • 8/4/2019 C FORMAT

    10/103

    10 Advance C Concepts

    Binary operators like +,* have a correspondingassignment operator of the form

    op= where op is the one of +,-,*,/,%,&,|,^.

    Example if i=15

    i+=3 changes the value of i to 18. i-= 3 change the value of i to 12.

    i*= 3 change the value of to 45.

    I/= 3 change the value to 5

    Size of operator

    returns the number of bytes the operand occupies.

    Compound Assignment Operator

  • 8/4/2019 C FORMAT

    11/103

    11 Advance C Concepts

    Precedence of operation

  • 8/4/2019 C FORMAT

    12/103

    12 Advance C Concepts

    & - bitwise AND

    | - bitwise OR

    ^ - bitwise EXOR

    This can be applied to any data type.

    Bitwise operator

  • 8/4/2019 C FORMAT

    13/103

    13 Advance C Concepts

    When an operator has operands of different types, they

    are converted to a common type according to a small

    number of rules.

    Type Conversion

  • 8/4/2019 C FORMAT

    14/103

    14 Advance C Concepts

    Explicit type conversion can be forced in any

    expression with unary operator is called as cast.

    The expression is converted to the named type of

    conversion rules.

    Type Casting

  • 8/4/2019 C FORMAT

    15/103

    15 Advance C Concepts

    Control Statements

  • 8/4/2019 C FORMAT

    16/103

    16 Advance C Concepts

    Used to specify the conditional execution of a

    program statements enclosed in braces.

    If Condition

  • 8/4/2019 C FORMAT

    17/103

    17 Advance C Concepts

    If - Else Condition

  • 8/4/2019 C FORMAT

    18/103

    18 Advance C Concepts

    The switch is multi way decision that tests whether anexpression matches one of a number of constant

    integer values.

    Switch Case

  • 8/4/2019 C FORMAT

    19/103

    19 Advance C Concepts

    This allows a sequence of program statements to be executedseveral times, either for specific number of times or until a

    particular condition is satisfied.

    It has three parts loop variables, loop continuation condition,

    loop body and an exit point.

    There are three main loops

    The while loop

    Do while loop

    For loop

    Pre- test loop while and for loops

    Post test loop do while loops

    Loop Control

  • 8/4/2019 C FORMAT

    20/103

    20 Advance C Concepts

    If the expression evaluated is a non-zero the body loopis executed.

    After execution of body , it again evaluates the

    expression and if it is true again the body loop isexecuted.

    While loop

  • 8/4/2019 C FORMAT

    21/103

    21 Advance C Concepts

    In case of Do- while the body is executed once andthe expression is evaluated once.

    If expression is true the body is executed again.

    Do While

  • 8/4/2019 C FORMAT

    22/103

    22 Advance C Concepts

    The most preferred loop is for loop when we havesimple initialization and increment.

    One or more variable can be initialized and

    decremented.

    For loop

  • 8/4/2019 C FORMAT

    23/103

    23 Advance C Concepts

    Loop interruption are required when we want to exitfrom a loop other then by testing the loop terminationcondition at the top or bottom.

    There there are three interruption commands Break Continue Exit Goto is a control directive where the program

    control is transferred. C provides the infinitely-abusable goto statement, and

    labels to branch to. Formally, the gotostatement isnever necessary, and in practice it is almost alwayseasy to write code without it.

    Loop Interruption

  • 8/4/2019 C FORMAT

    24/103

    24 Advance C Concepts

    The break statement provides and early exit fromfor,do, while just as switch.

    Break Statement

  • 8/4/2019 C FORMAT

    25/103

    25 Advance C Concepts

    A function is a self-contained block of program thatperforms some specific, well-defined task.Separating a program into multifunction aid in

    maintenance and enhancement of programs bylocalizing the effect of changes.

    There are two types function Library function User-defined function

    Commonly used functions written,compiled,and

    placed in libraries are called Library function[eg Printf(), scanf()]

    User has the freedom to choose the function name,return data type and the arguments.

    Function

  • 8/4/2019 C FORMAT

    26/103

    26 Advance C Concepts

    The general form of a function declaration is

    Where return-type data type of the value returnedFunction name name of the function definedArgument deceleration types and names of the

    parameters of the function, separated by commas.

    Function declaration is also called as functionprototype.

    Ex. int func1(int);

    Function declaration and Prototype

  • 8/4/2019 C FORMAT

    27/103

    27 Advance C Concepts

    A function definition introduces a new function bydeclaring the type of the value it returns and itsparameters, and specifying the statements that areexecuted when the function is called.

    Function definition

  • 8/4/2019 C FORMAT

    28/103

    28 Advance C Concepts

    Function Call

  • 8/4/2019 C FORMAT

    29/103

    29 Advance C Concepts

    If the program contains multiple functions, their

    definitions may appear in any order. Though they mustbe independent of one another.

    The function cannot be embedded with another

  • 8/4/2019 C FORMAT

    30/103

    30 Advance C Concepts

    Functions which require a return value shoulduse the return statement.

    On executing the return statement, the value ofthe expression, which is just after the return keyword,

    is returned to the calling function. If the expression is not present, it returns an integer or

    void depending on the compiler thatyou use.

    Return Statement

  • 8/4/2019 C FORMAT

    31/103

    31 Advance C Concepts

    The function parameters are a means ofcommunication between the calling and the calledfunction.

    Conditions for function call The list of arguments in the function call and

    function declaration must be the same. The data type of each of the actual parameter

    must be the same as that of the formal parameter. The order of the actual parameters must be the

    same as the order in which the formal parametersare specified.ie

    Function Argument

  • 8/4/2019 C FORMAT

    32/103

    32 Advance C Concepts

    Passing arguments has two mechanism- Passing arguments by value (call by value)

    Passing arguments by address or pointers (call by

    reference)

    Call by value the contents of the arguments in thecalling function are not changed, as the contents of the

    variable are copied to the formal parameters of the

    function definition.

    Call by reference memory address of the variable ispassed to the function definition, so the argument

    value changes.

    Passing Arguments to a Function

  • 8/4/2019 C FORMAT

    33/103

    33 Advance C Concepts

    Recursion is a process by which the function can call itself

    repeatedly, until some specified condition is satisfied . Direct if the call to the function occurs inside the function

    itself. Indirect if a function calls another function and which in turn

    calls the first one.

    Recursion

  • 8/4/2019 C FORMAT

    34/103

    34 Advance C Concepts

    The storage class determines the life time of theshortage associated with the variable.

    There are two types locations store c can store

    Memory CPU registers

    There are four storage classes

    Automatic storage class Static storage class

    Register storage class

    External storage class

    Storage Glass

  • 8/4/2019 C FORMAT

    35/103

    35 Advance C Concepts

    A variable is said to automatic if it isallocated storage upon entry to a segmentof code, and the storage is reallocated uponexit from the segment.

    Syntax auto data type variable name;

    Automatic Variables

  • 8/4/2019 C FORMAT

    36/103

    36 Advance C Concepts

    A variable is said to static , if it is allocated storage atthe beginning of the program execution and thestorage remains allocated until the program executionterminates.Syntax static data type variable name;

    Static Variables

  • 8/4/2019 C FORMAT

    37/103

    37 Advance C Concepts

    Variables will be placed in the internal CPU registers, asaccessing internal registers take less time thanaccessing memory.

    When a variable is is used at many places in a program itis better to declare its storage class as registerSyntax register data type variable name;

    Register Variables

  • 8/4/2019 C FORMAT

    38/103

    38 Advance C Concepts

    If the declared variable is needed in another file, or inthe same file but at a point earlier than that at which ithas been defined , it must be declared of storage classexternal.Syntax extern data type variable name;

    External Variable

  • 8/4/2019 C FORMAT

    39/103

    39 Advance C Concepts

    An ordered finite collection of data items, each of thesame type, is called an array and the individual data

    items are its elements.

    Arrays, whose elements are specified by one subscript ,

    are called one dimensional arrays. They are commonly

    known as vectors.

    Arrays, whose elements are specified by more than one

    subscript , are called mulit dimensional arrays. They

    are commonly known as matrix.

    Arrays

  • 8/4/2019 C FORMAT

    40/103

    40 Advance C Concepts

    Elements of an array can be assigned a initial value by

    the following the array the array definition with a list

    of initializers enclosed braces and separated bycommas.

    Single Dimensional Array

  • 8/4/2019 C FORMAT

    41/103

    41 Advance C Concepts

    They are defined the same way as single dimensionalarrays, except that a separate pair of of square brackets is

    required for each subscript (dimension).

    Multi Dimensional Array

  • 8/4/2019 C FORMAT

    42/103

    42 Advance C Concepts

    Initializations of Two Dimensional Array

  • 8/4/2019 C FORMAT

    43/103

    43 Advance C Concepts

    A string is one dimensional array of charactersterminated by '\0' character.

    String are used to store text information and to

    perform manipulations on them.example char name[6];

    character arrays

    Char name[] = {'S', 'N' , 'A' , 'D', 'S' }

    Strings

  • 8/4/2019 C FORMAT

    44/103

    44 Advance C Concepts

    Built-in string functions

    The header file string.h provides a useful set of stringfunctions.

  • 8/4/2019 C FORMAT

    45/103

    45 Advance C Concepts

    Pointers

    Pointers enable us to achieve parameter by passing byreference , deal concisely and effectively either arrays,represent complex data structures and work withdynamically allocated memory.

    Pointer variable address of a data item is called pointer

    to the data item and a variable that holds an address iscalled a pointer variable.

    Uses of pointers

    Keep track of address of memory location

    By changing the address in the pointer type variableyou can manipulate data in different memorylocations.Allocation of memory can be done dynamically

  • 8/4/2019 C FORMAT

    46/103

    46 Advance C Concepts

    Pointers are also variables and hence, must be definedin program like any other variable.

    Pointer Declaration

  • 8/4/2019 C FORMAT

    47/103

    47 Advance C Concepts

    The declaration of the pointer may be accompaniedwith the initializer.

    Type * identifier = initializer

    E.g., float *fp=null;Pointer arithmetic can be performed only for addition

    and subtraction .

    Pointer Initialization

  • 8/4/2019 C FORMAT

    48/103

    48 Advance C Concepts

    A function can take a pointer to any data type, as

    argument and can return a point to any data type

    Pointer and Functions

  • 8/4/2019 C FORMAT

    49/103

    49 Advance C Concepts

    The values of the argument are used to initialize

    parameters of the called function.

    Call By Value

  • 8/4/2019 C FORMAT

    50/103

    50Advance C Concepts

    Here the address of the variable are supplied to the

    calling function and changes to the parameter valuesin the called function cause changes in the value of the

    variable in the calling function.

    Call By Reference

  • 8/4/2019 C FORMAT

    51/103

    51Advance C Concepts

    A pointer to function can be defined as a the addressof the code executed when the function is called.

    Pointer to function are used in

    Writing memory resident programsWriting viruses, or vaccines to remove the viruses

    Declaration of pointer to function -

    Pointer to Functions

  • 8/4/2019 C FORMAT

    52/103

    52Advance C Concepts

    Pointer to Functions

  • 8/4/2019 C FORMAT

    53/103

    53Advance C Concepts

    Pointer and array

    Any operation that can be done by array can also be

    done with pointers.

    Pointer and Array

  • 8/4/2019 C FORMAT

    54/103

    54Advance C Concepts

    Array as Function Arguments

  • 8/4/2019 C FORMAT

    55/103

    55Advance C Concepts

    An array of pointer is a collection of address.

    The address present in the array of pointers can be

    addressed of isolated variables or addresses of array

    elements or any otheraddress.

    Ex char *day[7];

    Char * day[7] = {sun,mon,tue,wed,thur,

    fri,sat};

    Array of Pointers

  • 8/4/2019 C FORMAT

    56/103

    56Advance C Concepts

    Functions Returning Pointers

  • 8/4/2019 C FORMAT

    57/103

    57Advance C Concepts

    Any operation that can be done by array can also be

    done with pointers.

    Pointer and Array

  • 8/4/2019 C FORMAT

    58/103

    58Advance C Concepts

    An array of pointer is a collection of address.

    The address present in the array of pointers can beaddressed of isolated variables or addresses of array

    elements or any other address.Ex char *day[7];

    Char * day[7] = {sun,mon,tue,wed,thur,fri,sat};

    Array of Pointers

  • 8/4/2019 C FORMAT

    59/103

    59Advance C Concepts

    A multidimensional array can be expressed in termsof an array of pointers rather than as a pointer to agroup of contiguous arrays.

    A two dimensional array can be defined as as one

    dimensional array of pointers like

    - data type-*array[exp1];

    rather thandata type array [exp1][exp2];

    Multi dimensional array as array of pointers

  • 8/4/2019 C FORMAT

    60/103

    60Advance C Concepts

    Functions returning pointers

  • 8/4/2019 C FORMAT

    61/103

    61 Advance C Concepts

  • 8/4/2019 C FORMAT

    62/103

    62 Advance C Concepts

    C provides a collection of dynamic memory management

    functions enable storage to be allocated as needed andreleased when no longer required.

    Header files required- malloc.h, alloc.h

    Void *malloc(size) to obtain storage for a data item

    Void *calloc(nitems,size) storage for more than on dataitem

    Void *realloc(void *block, size) allocates memory for thenew size. If the new size is larger than the older one , the

    original contents are preserved and the remaining space isuninitialized; if smaller the contents are unchanged up to thenew size.

    free(ptr) clear the memory allocated.

    Dynamic memory allocation

  • 8/4/2019 C FORMAT

    63/103

    63 Advance C Concepts

    Pointer Declaration

  • 8/4/2019 C FORMAT

    64/103

    64 Advance C Concepts

    Structure is collection of logically related data items

    grouped together under a single name called structure.

    Structure

  • 8/4/2019 C FORMAT

    65/103

    65 Advance C Concepts

    Individual members of a structure may be any of thecommon used data types (such as int,float, etc.),pointers, array, or even other structures.

    Structure variables -

    Structure variable can also be declared as

    Struct tag variable list

    ex struct student student1 , student2

    Declaration Individual Member of Structure

  • 8/4/2019 C FORMAT

    66/103

    66 Advance C Concepts

    With the help of dot operator (.), individual elementsof the structure can be accessed and syntax is of theform

    Structure variable . Member- nameEx student1.name

    Accessing structure members

  • 8/4/2019 C FORMAT

    67/103

    67 Advance C Concepts

  • 8/4/2019 C FORMAT

    68/103

    68 Advance C Concepts

    These are commonly used when a large number of

    similar records are required to be processed together.Ex struct item motor[100] ;

    Arrays of structure

    i S

  • 8/4/2019 C FORMAT

    69/103

    69 Advance C Concepts

    The beginning address of a structure can be accessed in

    the same manner as any other address, through the use

    of the & operator.

    The member variables can be accessed by

    Pointer name -> member variable

    Pointers to Structures

    S d F i

  • 8/4/2019 C FORMAT

    70/103

    70 Advance C Concepts

    A structure type definition may be local to a functionor it may be external to any function.

    Structures may be passed as function arguments andfunctions may be return structures.

    Structure as function arguments. There are threemethods

    Passing structure member to function

    Passing entire structure to function Passing structure pointers to function

    Structure and Functions

  • 8/4/2019 C FORMAT

    71/103

    71 Advance C Concepts

    P i ti t t t f ti

  • 8/4/2019 C FORMAT

    72/103

    72 Advance C Concepts

    Passing entire structure to a function

    St t F ti V l

  • 8/4/2019 C FORMAT

    73/103

    73 Advance C Concepts

    Structure as Functions Values

    P i t t i t t F ti

  • 8/4/2019 C FORMAT

    74/103

    74 Advance C Concepts

    Passing structure pointers to Functions

    Fil H dli

  • 8/4/2019 C FORMAT

    75/103

    75 Advance C Concepts

    C provides functions to get data from a file stored indisk.

    These function are classified as

    High level file I/O functions also called as standard

    I/O or stream I/O functions.Low-level file I/O functions also called as system

    I/O functions.

    Unformatted high level disk I/O functions.

    fopen () functionFILE *fp;

    fp=fopen(file_name, type/mode);

    File Handling

  • 8/4/2019 C FORMAT

    76/103

    76 Advance C Concepts

    Fil R l t d F ti l

  • 8/4/2019 C FORMAT

    77/103

    77 Advance C Concepts

    fclose function

    fclose(fp);Character I/O in files

    fgetc(fp);putc(c,fp);

    String line I/O in filesChar *fgets(char *s, int n, FILE *fp);Int fputs(const char *s, FILE *fp);

    Formatted high level disk I/O functionsfprintf(fp, format,s);

    fscanf(fp, format,s);Disk I/O

    fread(ptr,size,nitems,fp);fwrite(ptr,size,nitems,fp);

    File Related Functions-general

    Fil R l t d F ti l

  • 8/4/2019 C FORMAT

    78/103

    78 Advance C Concepts

    Error handling functions

    Int feof (FILE *fp);Int ferror (FILE *fp);

    void perror(const char *s);

    File positioning functions

    Int fseek (FILE *fp, long offset, int ptrname);

    File Related Functions-general

    C P P

  • 8/4/2019 C FORMAT

    79/103

    79 Advance C Concepts

    It is a program that process the source text of a C program

    before the program is passed to the compiler. It has four major functions-

    Macro replacement Conditional compilation File inclusion

    Error generation

    C Pre Processor

    C Preprocessor offers several features called Preprocessordirectives. Each of the preprocessor begin with #symbol.

    #define directive

    include directive undef directive #error directive Conditional compilation directives

    M S b tit ti

  • 8/4/2019 C FORMAT

    80/103

    80 Advance C Concepts

    # define macro-name sequence-of-tokensMacros with arguments

    #define AREA (r) (3.14*r*r)

    Nesting of macros

    #define SQUARE(x) x*x#define CUBE(x) (SQUARE(x) * x)

    We can use one macro in the definition of another

    macro.

    Undefining a Macro# undef identifier

    Macro Substitution

    File Inclusion

  • 8/4/2019 C FORMAT

    81/103

    81 Advance C Concepts

    This preprocessor directive causes one file to be

    included in another. This feature is used in two cases

    If we have a large program, it is good programming

    practice to keep different sections in separate file.

    These file are included at the beginning of mainprogram file.

    Many a times we need some functions or some

    macro definitions almost in all programs that we

    write. In such a case, commonly needed functionsand macro definitions can be stored in a file and

    that file can be included wherever necessary.

    File Inclusion

    Conditional Compilation

  • 8/4/2019 C FORMAT

    82/103

    82 Advance C Concepts

    This feature allows selective inclusion of lines ofsource text on the basis of computed condition.

    Conditional compilation is performed using the

    preprocessor directives

    #ifdef

    #ifndef

    #elif

    #else

    #endif

    Conditional Compilation

    Error generation

    Error Generation

  • 8/4/2019 C FORMAT

    83/103

    83 Advance C Concepts

    Error generation

    # error token sequence

    We can display the error message on occurrence of

    error.

    User defined data types

    typedef type new-type

    (Ex) typedef int age;

    Error Generation

    Enumerations

  • 8/4/2019 C FORMAT

    84/103

    84 Advance C Concepts

    This provide the facility to specify the possible values

    of a variable by meaningful symbolic means. This can

    help the program more readable.

    enum tag {member1, member2, ., member n};

    Enumerations

    Unions

  • 8/4/2019 C FORMAT

    85/103

    85 Advance C Concepts

    Unions are like structure, contain members whose

    individual data types may differ from one another.

    However, the members that compose a union all share

    the same storage area within the computers memory,

    whereas each member within a structure is assigned its

    own unique storage area.

    The compiler allocates sufficient memory to hold the

    Largest data item in the union.The member variable of union can be accessed same

    way as a structures member variable are accessed.

    Unions

  • 8/4/2019 C FORMAT

    86/103

    86 Advance C Concepts

    Asynchronous communication

  • 8/4/2019 C FORMAT

    87/103

    87 Advance C Concepts

    RS232 is an asynchronous communication protocol

    It does not have a seperate clock signal

    The clock recovery is done by including a known

    transition event in the data,so as to synchronise

    the transmitter and the receiver

    Asynchronous communication

    Bit Timing and Bit Rate

  • 8/4/2019 C FORMAT

    88/103

    88 Advance C Concepts

    The bit time is the time from the start of one serial

    bit date to the start of another data.

    The inverse of bit time is the Baud rate

    RS-232 are integer multiples and sub multiples of

    9600bps.

    Bit Timing and Bit Rate

    Electrical Specification

  • 8/4/2019 C FORMAT

    89/103

    89 Advance C Concepts

    The UART input/output 0V for logic 0 and 5V for

    logic 1.

    The RS-232 STANDARD uses +12v for logic 0

    and -12V for logic 1

    We convert this voltage levels by a level converter

    MAX232.

    Electrical Specification

    Parts of RS-232 Frame

  • 8/4/2019 C FORMAT

    90/103

    90 Advance C Concepts

    A FRAME transmits a single character and is

    composed of:

    1 start bit

    8 data bits

    1 stop bit

    1 parity bit

    Parts of RS 232 Frame

    Hardware Proporties

  • 8/4/2019 C FORMAT

    91/103

    91 Advance C Concepts

    Devices which use serial cables for their

    communication are split into two categories.

    DCE (Data Communications Equipment) and

    DTE(Data Terminal Equipment.)

    Data Communications Equipment are devices such

    as your modem Data Terminal Equipment is the Computer or

    Terminal.

    Hardware Proporties

    Flow Control

  • 8/4/2019 C FORMAT

    92/103

    92 Advance C Concepts

    Flow control has two basic types:Hardware and

    Software flow control.

    Software flow control ,buffer will transmit the

    data, and the control is accompanied by xon/xoff

    bit.

    Hardware flow control,two additional lines for

    RTS, CTS.When the terminal wants to be

    transmitted, pulls RTS high,dce sends the

    response back by sending CTS high.

    Flow Control

    Programming Addresses and IRQ

  • 8/4/2019 C FORMAT

    93/103

    93 Advance C Concepts

    32E8COM4

    43E8COM3

    32F8COM2

    43F8COM1

    IRQADRESSNAME

    Programming Addresses and IRQ

    Serial Port Registers

  • 8/4/2019 C FORMAT

    94/103

    94 Advance C Concepts

    The uart has only eight port address, but uart can able to access twelve

    registers with the help ofdlabbit.Registers:

    Interrupt Enable Register (IER)

    Interrupt Identification Register (idr)

    First In/First Out Control Register (FCR)

    Line Control Register (LCR)Modem Control Register (MCR)

    Line Status Register (LSR)

    Modem Status Register (MSR)

    Transmitor holding buffer

    Receiver Buffer

    Divisor Latch Low Byte

    Divisor latch high byte

    Scratch register

    Serial Port Registers

    Interrupt Enable Registers

  • 8/4/2019 C FORMAT

    95/103

    95 Advance C Concepts

    Enable Received Data Available InterruptBit0

    Enable TransmitterHolding Register Empty

    Interrupt

    Bit1

    Enable Receiver Line status InterruptBit2

    Enable Modem Status InterruptBit3

    Enables Sleep ModeBit4

    Enables Low Power ModeBit5

    ReservedBit6

    ReservedBit7NotesBit

    Interrupt Enable Registers

    Interrupt identification register

    Interrupt Identification Register

  • 8/4/2019 C FORMAT

    96/103

    96 Advance C Concepts

    p g

    No Interrupt Pending0

    Interrupt Pending

    16550 Time-out Interrupt Pending

    Reserved on 8250, 16450

    FIFO Enabled but Unusable10

    FIFO Enabled11

    00

    1

    0

    1

    0

    Receiver Line Status Interrupt1

    Received Data Available Interrupt1

    Transmitter Holding Register Empty

    Interrupt

    1

    0

    Modem Status Interrupt

    No FIFO

    0Bit 0

    0Bit 2:1

    0Bit 3

    ReservedBit 4

    Enable 64 Byte FIFO (16750 only)Bit 5

    BIT 7:6

    notesBit

    Interrupt Identification Register

    First in/First out control register

  • 8/4/2019 C FORMAT

    97/103

    97 Advance C Concepts

    Interrupt Trigger Level , 14BYTE

    Interrupt Trigger Level , 8 BYTE

    Interrupt Trigger Level , 4BYTE

    Interrupt Trigger Level , 1 BYTE

    1

    0

    1

    0

    1

    1

    0

    Enable FIFO'sBit0

    Clear Receive FIFOBit1

    Clear Transmit FIFOBit2

    DMA Mode Select. Change status of RXRDY & TXRDY pins

    from mode 1 to mode 2.

    Bit3

    RESERVEDBit4

    Enable 64 Byte FIFO (16750 only)Bit5

    0Bit7

    NotesBit

    First in/First out control register

    LINE CONTROL REGISTER

    Line Control Register

  • 8/4/2019 C FORMAT

    98/103

    98 Advance C Concepts

    8 BITS

    6 BITS

    11

    10

    LOW PARITY

    HIGH PATITY

    EVEN PARITY

    111

    101

    110

    ODD PARITY

    NO PARITY

    1

    0

    7 BITS

    5 BITS

    01

    0

    0

    X

    FIFO Enabled but Unusable0

    1

    2 Stop bits for words of length 8 Bits

    0

    0

    1 STOP BIT

    No FIFO

    0Bit 1:0

    0Bit 2

    XBit 3:5

    ReservedBit 5

    Enable 64 Byte FIFO (16750 only)Bit 6

    BIT 7

    notesBit

    Line Control Register

    Modem Control Register

  • 8/4/2019 C FORMAT

    99/103

    99 Advance C Concepts

    Force Data Terminal ReadyBit0

    Force Request to SendBit1

    Aux Output 1Bit2

    Aux Output 2Bit3

    Loop Back ModeBit4

    Auto flow Control Enabled (16750 only)Bit5

    ReservedBit6

    ReservedBit7

    NotesBit

    Modem Control Register

    Line Status Register

  • 8/4/2019 C FORMAT

    100/103

    10

    0

    Advance C Concepts

    Data readyBit0

    Overrun errorBit1

    Parity ErrorBit2

    Framing ErrorBit3

    Break InterruptBit4

    Empty Transmitter Holding RegisterBit5

    Empty Data Holding RegistersBit6

    Error in received fifoBit7

    NotesBit

    g

    MODEM STATUS REGISTER

    Modem Status Register

  • 8/4/2019 C FORMAT

    101/103

    10

    1

    Advance C Concepts

    MODEM STATUS REGISTER

    Delta Data Set ReadyBit0

    Delta Data Set ReadyBit1

    Trailing Edge Ring IndicatorBit2

    Delta Data Carrier DetectBit3

    Clear To SendBit4

    Data Set ReadyBit5

    Ring IndicatorBit6

    Carrier DetectBit7

    NotesBit

    g

    Programming Steps

  • 8/4/2019 C FORMAT

    102/103

    10

    2

    Advance C Concepts

    There are two methods of programming.

    Polling : to see if the new data is available.

    Interrupts :an interrupt handler to remove the data

    from the uart when it generates an interrupt.

    Polling the UART is a lot slower method, which isvery CPU intensive thus can only have a maximum

    speed of around 34.8 KBPSbefore you start losing

    data.

    The other option is using a Interrupt handler, andthat's what we have used here. It will very easily

    support 115.2K BPS, even on low end computers.

    g g p

  • 8/4/2019 C FORMAT

    103/103


Recommended