“EMBEDDED SYSTEM”



    Tank Mitul G
What is Embedded Systems
 An embedded µc is a chip which has a computer processer
 with all it‟s support function memory and i/p o/p built in
 the device.



 It includes Hardware and Mechanical parts.
 Necessity
 Use in daily life.
Embedded systems
INTRODUCTION OF ‘C’
• C is a general purpose programming language.
• ‘C’ language is developed at AT & T’s
• Bell Laboratories of USA in 1972.
• Designed & written by Dennis Ritchie.
• C is relable,simple & easy to use.
• Applcation:-mobile phones
              Computer games

Steps in learning C
Constant & Variable
  •A variable can be considered as a name given to any memory location.
              Example: int i;
  • Here „i‟ is a name assigned to a particular memory location.
  •A constant is a quantity that does not change.
             Example: const int i = 500;
Keywords
    •The keyword can‟t be used as variable names.
    •The keywords are also called “Reserved Words”.
    •There are 32 keywords available in C.
    •Some example of keywords:
        auto, break, case, char, const, while.
Instructions
There are basically four types of instructions in C.
    •Declaration Instruction
    •Input / Output Instruction
    •Arithmetic Instruction
    •Control Instruction
Data type declaration
There are only a few basic data types in C.
1. Integer
2. Float
                   Data Type         Range                        Bytes   Format
3. Double
4. Character       signed char       -128 to + 127                1       %c
                   unsigned char        0 to 255                  1       %c
                   short signed int     -32768 to +32767          2       %d
                   short unsigned int   0 to 65535                2       %u
                   long signed int      -2147483648 to +2147483647 4      %ld
                   long unsigned int    0 to 4294967295           4       %lu
                   Float                -3.4e38 to +3.4e38        4       %f
                   Double               -1.7e308 to +1.7e308      8       %lf
                   long double          -1.7e4932 to +1.7e4932    10      %Lf
Operators
•Arithmetic Operators              •Assignment Operators                •Increment / Decrement Operators

             multiply               Operator Example Meaning             Operator          Meaning         When?
  *
                                            =         X=5       X=5     count++     count = count + 1;    After use
  /          divide
                                           +=        X += 5    X=X+5    ++count     count = count + 1;    Before
  %          reminder                      –=        X–=5      X=X–5                                      use
                                           /=         X /= 5   X=X/5    count--     count = count – 1;    After use
  +          Add
                                           *=        X *= 5    X=X*5
             Subtract
                                                                        --count     count = count – 1;    Before
  -                                        %=        X %= 5    X=X%5
                                                                                                          use



 Logical Operators                              Bitwise Operators       Exponentiation Operators
                                                &    bitwise AND        Exponentiation is not written as x**2 or x^2
 ! (not)                                        |    bitwise OR         C does not have an exponentiation operator.
 a != b is true if a and b are not equal        ^    bitwise X-OR       You can use the math function pow(a, b)
 && (and)                                       <<   Left Shift         You must include <math.h> in source code
 5<6 && 7>4 is true, but
                                                >>   Right Shift
 5>6 && 7>4 is not true (i.e. false)
 || (or)
                                                ~    one‟s complement
 5<6 || 7>4 is true, and
 5>6 || 7>4 is also true
CONDITIONAL STATEMENT
                                          •The if-else statement
•The if statement
(a) if ( condition )      (a) if ( condition )   (b) if ( condition )   (c) if ( condition )
     do this ;                     do this ;     do this ;                  {
(b) if ( condition )               else               else              if ( condition )
           {                       do this ;     {                      do this ;
           do this ;                             if ( condition )       else
           and this ;                            do this ;                 {
            }                                    else                   do this ;
                                                 {                      and this ;
                                                 do this ;                  }
 •The conditional operators                      and this ;
 expression 1 ? expression 2 : expression 3                                   }
                                                 }                      else
   char a ;                                      }                      do this ;
   int y ;
   scanf ( "%c", &a ) ;
   y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;
   Here 1 would be assigned to y if a >=65 && a <=90
    evaluates to true, otherwise 0 would be assigned.
LOOPS                   1.Using a while statement
                                       2.Using a for statement
                                       3.Using a do-while statement
                                                  •    As a rule the while must test a condition
1.The while Loop                                  that will eventually become false, otherwise the
    initialise loop counter ;                     loop would be executed forever, indefinitely.
    while ( test loop counter using a condition )      main( )
    {                                                   {
    do this ;                                          int i = 1 ;
    and this ;                                          while (i <= 10 )
    increment loop counter ;
                                                       printf ( " %dn", i ) ;
    }
                                                              }
 •     The statements within the loop may be a single line or a block of statements. In the first
 case the parentheses are optional. For example,
       while ( i <= 10 )
                                   i = i + 1 is same as
       while ( i <= 10)
        {
             i=i+1;
       }
The for Loop
The general form of for statement is as under:                     PROGRAM FOR CALCULATE
for( initialise counter ; test counter ; increment counter )        RATE OF INTEREST
{                                                              main ( )
                   do this ;                                   {
                   and this ;                                  int p, n, count ;
                   and this ;                                  float r, si ;
}                                                              for ( count = 1 ; count <= 3 ; count = count + 1 )
                                                               {
  Thus the following for loops are not ok.                     printf ( "Enter values of p, n, and r " ) ;
 for ( i = 10 ; i ; i -- )                                    scanf ( "%d %d %f", &p, &n, &r ) ;
  printf ( "%d", i ) ;                                         si = p * n * r / 100 ;
 for ( i < 4 ; j = 5 ; j = 0 )                                printf ( "Simple Interest = Rs.%fn", si ) ;
                                                               }
  printf ( "%d", i ) ;
                                                               }
 for ( i = 1; i <=10 ;
   printf ( "%d",i++ ) ;
 for ( scanf ( "%d", &i ) ; i <= 10 ; i++ )
  printf ( "%d", i ) ;
The do-while Loop

              The do-while loop looks like this:
              do
              {
              this ;
              and this ;
              and this ;
              and this ;
                      } while ( this condition is true ) ;


Difference between while and do-while loop

                                   {
                                   do
main( )
{
                                   {
while ( 4 < 1 )                    printf ( "Hello there n") ;
printf ( "Hello there n") ;       } while ( 4 < 1 ) ;
}                                  }
FUNCTIONS
A function is a self contained block of statements that perform a task of some kind.
Every C program is a collection of these functions
                                main() “calls” the function message()
   Example                      Control passes to the message() func.
   #include <stdio.h>           After execution of message statements, control returns to main and
   void main()                   begin executing the code at the exact point where it left.
   {                            main becomes the “calling funtion”
   message();                   Fnction ucan be called from any other function
   printf(“nUdaipur”);         A function can call itself. It is called Recursion.
   }                            A function can be called from any other function but
   message()                     a function can’t be defined in another function.
   {
   printf(“SSCE”);
                             •    General form of a function
   }                              function name (argument declaration)
   Output:                        {
   SSCE                           local variables declaration
   Udaipur                        executable statements
                                  ………………
                                  ………………
                                  return(expression)
                                      }
Function Types
              1) Library Functions
              2) User Defined Functions
                                                      Call by VExample : Call by Value
•   Methods used for declaration of arguments         int square (int x)
•   Method 1                                          {
    function ( a, b, c )                              return x*x;
    int a , b , c ;                                   }
    Here declaration is done in separate statement.
    This method is known as K & R method.             int main ( )
•   Method 2                                          {
    function ( int a, int b, int c )                  int num = 10;
                                                      int answer;
                                                      answer = square(num);
Passing the values to the functions                   printf(“Answer is : %d “, answer); // answer is 100
during function call                                  printf(“Value of a is : %d “, num); // num will be 10
                                                      }
•   Call by Value :                                   • alue :
    on calling a function we are passing                  on calling a function we are passing
     values of variables to it.                            values of variables to it.
•   Call by Reference :                               • Call by Reference :
    Instead of passing the value,                         Instead of passing the value,
     we pass the address of the variables                  we pass the address of the variables
ARRAY
  Definition : A set of similar data types is called array.
  An array is a collective name given to a group of similar quantities.
  Each member in the group is referred to by its position in its group.
  These similar elements can be all ints or all floats or all characters etc.
  Array Declaration :
   int marks[5];                                                  Entering the Data into an array
   Type of variables : Integer                                    Enter marks of 5 students.
   Dimension : 5
   marks : Name of the variable                                   for(i = 0;i<5;i++)
   [ ] : Indication of an array                                   {
 • Each member in the array is referred to by its position.       printf(“Enter the marks :”);
 • Position is from 0 to 4.                                       scanf(“%d”,&marks[i]);
Array Initialization                                              }
  Array Initialization :
  Int marks[5] = {32,45,56,67,78}                                 Reading Data from an array
  Int marks[ ] = {32,45,56,67,78}
  Array elements are always stored in
                                                                  Find average of 5 student marks
   contiguous locations.
                                                                  for(i = 0;i<5;i++)
       marks[0]   marks[1]   marks[2]   marks[3]   marks[4]       sum = sum + marks[i];
         32         45         56         67         78           average = sum/5;
        2004       2006       2008       2010       2012
                                                                  Example : arr0.c
Passing array element to a function
1. We can pass the array elements by value or by reference.
2. In call by value, we pass the values of array elements to the function.
3. In call by reference, we pass the address of array elements to the function.




• Call by Value                                        •    Call by Reference
  main()                                                    main()
  {                                                         {
  int i, marks[5] = {32,45,56,67,78}                        int i, marks[5] = {32,45,56,67,78}
  for(i = 0;i<5;i++)                                        for(i = 0;i<5;i++)
  findsum(marks[i]);                                        findsum(&marks[i]);
  }                                                         }
  findsum(int m)                                            findsum(int *m)
  {                                                         {
  static int sum;                                           static int sum;
  sum = sum + m;                                            sum = sum + *m;
  printf(“Sum = %d”,sum);                                   printf(“Sum = %d”,sum);
  }                                                         }
SWITCH CASE
• When decision from a number of choices is required, at that time switch – case – default is
  used.
• Syntax for the switch - case structure is as follows :
          Switch ( integer expression)
          {
          case value1 : action1 ;
          case value2 : action2 ;
          default : action3 ;
          }

• Execution of the Switch
1. First the expression is evaluated.
2. After getting the value it is matched one by one with constant values in case statements.
3. When a match is found, it executes the statements following that case and all subsequent case
   and default statements as well
Notes while using the SWITCH CASE
• Integer expression is any constant or expression that evaluates to an integer.
• The keyword case is followed by an integer or a character constant.
• Float is not allowed as case value.
• Each constant in each case must be different from all others.
• Order is not important while putting the case values.
• No enclosure is needed in case statements
• break statement must be used to prevent the execution of subsequent cases.
• Use of continue will not take the control to beginning of switch.


BREAK Statement
• Terminates the execution of the nearest enclosing do, for, switch, or while
  statement in which it appears.
• Control passes to the statement that follows the terminated statement.
Embedded systems
A typical 8051 contains:

 CPU with Boolean processor;
 5 interrupts: 2 are external, 2 priority levels;
 2 16-bit timer/counters;
 programmable full-duplex serial port (baud rate
  provided by one of the timers);
 32 I/O lines (four 8-bit ports);
 RAM and ROM/EPROM in some models.
8051 Architecture
Block Diagram
External interrupts

                      4K ROM
    Interrupt         program           128 bytes           Timer0     Counter
     Control            code
                                           RAM              Timer1      Inputs



      CPU



                       Bus                                  Serial
                                        4 I/O Ports         Port
      OSC             Control



                                                           TXD   RXD
                                   P0     P2    P1    P3
                                Address/Data
Pin Description of 8051

              P1.0   1        40   Vcc
              P1.1   2        39   P0.0(AD0)
              P1.2
              P1.3
                     3
                     4
                          8   38
                              37
                                   P0.1(AD1)
                                   P0.2(AD2)
              P1.4   5        36   P0.3(AD3)
              P1.5   6        35   P0.4(AD4)
                     7        34   P0.5(AD5)
              P1.6
              P1.7   8    0   33   P0.6(AD6)
              RST    9        32   P0.7(AD7)
      (RXD)P3.0      10       31   EA/VPP
       (TXD)P3.1     11       30   ALE/PROG
      (INT0)P3.2     12   5   29   PSEN
      (INT1)P3.3     13       28   P2.7(A15)
          (T0)P3.4   14       27   P2.6(A14)
          (T1)P3.5   15       26   P2.5(A13)
        (WR)P3.6
         (RD)P3.7
                     16
                     17
                          1   25
                              24
                                   P2.4(A12)
                                   P2.3(A11)
            XTAL2    18       23   P2.2(A10)
            XTAL1    19       22   P2.1(A9)
              GND    20       21   P2.0(A8)
Pins of 8051
 Vcc(pin 40) :
    Vcc provides supply voltage to the chip.
    The voltage source is +5V.


 GND(pin 20)- ground


 XTAL1 and XTAL2(pins 19,18)
    These 2 pins provide external clock.
    Way 1:using a quartz crystal oscillator.
    Way 2:using a TTL oscillator.
Pins of 8051
 RST(pin 9):reset
   It is an input pin and is active high (normally low).
       The high pulse must be high at least for 2 machine cycles.
   It is a power-on reset.
     High logical state on this input halts the MCU and

       clears all the registers.
     Bringing this pin back to logical state zero starts the program a new
        as if the power had just been turned on.
       In another words, positive voltage impulse on this pin resets the
        MCU.

   Way 1:Power-on reset circuit

   Way 2:Power-on reset with debounce
XTAL Connection to 8051
Using a quartz crystal oscillator
We can observe the frequency on the XTAL2 pin.

           C2
                                 XTAL2
          30pF

          C1
                                 XTAL1
          30pF

                                 GND
Embedded systems
Program Memory
                       64K              FFFFH




              4K             External
    0FFFH                     ROM

                              EA=0
            Internal
              ROM

            EA=1



    0000H                               0000H


                                  PSEN
Data Memory
                       64K                FFFFH




           128 bytes
                              External
     7FH                       RAM


            Internal
              RAM




     00H                                  0000H

                             WR          RD
Pins of 8051
ALE Pin


 The ALE pin is used for de-multiplexing the address and data by
  connecting to the G pin of the 74LS373 latch.

    When ALE=0, P0 provides data D0-D7.

    When ALE=1, P0 provides address A0-A7.

    The reason is to allow P0 to multiplex address and data.
Internal RAM allocation
7FH



         General             RS1 RS0   Register Bank   Address
       purpose RAM           0   0         0           00H-07H

                             0   1         1           08H-0FH

30H                    2FH   1   0         2           10H-17H

      Mixed Bit/Byte         1   1         3           18H-1FH
      Addressable

1FH                    20H

      Register Bank3
18H                    17H
      Register Bank2

0FH                    10H

      Register Bank1
08H                    07H   Stack
                             Pointer
      Register Bank0
                       00H
Internal RAM and SFR (special function register) region
               FFH                              FFH



          Upper                    Accessible
                     Accessible                  SFR
           128       by Indirect    by Direct
                     addressing    addressing
                                      only             Ports,
                        only
                                                       Timers,
                                                       Control
                                                       Registers,
               80H                              80H
                                                       Accumulator,
               7FH
                                                       Stack
                                                       Pointer,
                     Accessible
                                                       Etc,.
                     by Direct &
           Lower      Indirect
            128      addressing




               00H
Program status Word
       CY   AC      F0   RS1     RS0      OV        --       P



  CY        PSW.7         Carry Flag
  AC        PSW.6         Auxiliary Carry flag
  F0        PSW.5         Available to user for general purpose
  RS1       PSW.4         Register bank selector bit 1
  RS0       PSW.3         Register bank selector bit 0
  OV        PSW.2         Overflow flag
  --        PSW.1         Not defined
  P         PSW.0         Parity flag. Set/cleared by hardware to
                          indicate an odd/even number of „1‟ in the
                          accumulator.
Embedded systems
Assembly Language
 Data transfer instructions
 Push and pop opcode
 Data Exchange
 Rotate operations
 Logical operations
 Arithmetic operations
 Jump instructions
 Call instruction
Embedded ‘C’ Language

 Library files            - #include<reg51.h>

 Declaration data types   - int, char, Sbit.

 Body                     - statements
Embedded systems
Interfacing

• LED
• 7-segment
• LCD
• KEYBOARD
• LED connected to any port of microcontroller
LED   • In figure 8 LED are connected to PORT-2 of uc
      • Example:-
                  Program of running LED
                  #include<reg51.h>
                  run[]={0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81};
       void delay()
      {
                  long int i,k;
                  for(i=0;i<=1114;i++)
                  {
                   for(k=0;k<=0;k++);
                  }
      }
       void main()
      {
        int j;
        for(j=0;j<=7;j++)
                  {
                              P2=run[j];
                              delay();
                  }
      }
7-SEGMENT
•   7-segment display use to indicate number up to 0-9
•   7-segment display has 10-pin in this 7-pin for 7-led (a,b,c,d,e,f,g) and 1 for VCC , 1 for ground.
                          •    Example
                               #include<reg51.h>
                               sbit EN=P3^7;
                               sbit EN2=P3^6;
                               look_up[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
                               void delay()
                               {
                                     int j,k;
                                     for(j=0;j<=1000;j++)
                                     for(k=0;k<=100;k++);
                               }
                               void main()
                               {
                                     int i;
                                     EN=0;
                                     EN2=1;
                                     for(i=0;i<=9;i++)
                                     {
                                                 P2=look_up[i];
                                                 delay();
                                     }
                               }
LCD
•LCD is finding widespread use replacing LEDS
•The ability to display numbers, characters, and graphics
•Ease of programming for characters and graphics
                                                      • LCD pin description
                                                        PIN   SYMBOL   I/O           DESCRIPTION
                                                         1      VSS      -                Ground
                                                         2      VCC      -          +5V power supply
                                                         3      VEE      -    Power supply to control contrast
                                                         4       RS      I   RS=0 to select command register,
                                                                             RS=1 to select data register
                                                         5     R/W       I   R/W=0 for write, R/W=1 for read
                                                         6      E      I/O                Enable
                                                         7     DB0     I/O          The 8-bit data bus
                                                         8     DB1     I/O          The 8-bit data bus
                                                         9     DB2     I/O          The 8-bit data bus
                                                        10     DB3     I/O          The 8-bit data bus
                                                        11     DB4     I/O          The 8-bit data bus
                                                        12     DB5     I/O          The 8-bit data bus
                                                        13     DB6     I/O          The 8-bit data bus
                                                        14     DB7     I/O          The 8-bit data bus
• LCD Command Codes

     HEX CODE         Command to LCD Instruction Register
       0x01                    Clear display screen
       0x02                        Return home
       0x04            Decrement cursor (shift cursor to left)
       0x06            Increment cursor (shift cursor to right)
       0x05                     Shift display right
       0x07                      Shift display left
       0x08                   Display off, cursor off
       0x0A                   Display off, cursor on
       0x0C                   Display on, cursor off
       0x0E                  Display on, cursor blinking
       0x0F                  Display on, cursor blinking
       0x10                  Shift cursor position to left
       0x14                 Shift cursor position to right
       0x18               Shift the entire display to the left
       0x1C              Shift the entire display to the right
       0x80             Force cursor to beginning to 1st line
       0xC0             Force cursor to beginning to 2nd line
       0x38                    2 lines and 5x7 matrix
• Example:-
                                                void DATA (unsigned char val)
# include<reg51.h>                              {
sbit rs=P3^6;                                               rs=1;
sbit en=P3^7;                                               P2=val;
                                                            en=1;
void delay(int time)                                        delay(10);
{                                                           en=0;
             unsigned char i,j;                             delay(10);
             for(i=0;i<=time;i++)               }
             {                                  void main()
                          for(j=0;j<=55;j++);   {
             }                                              int k;
}                                                           char arr[15]=“microcontroller";
void cmd(unsigned char val)                                 cmd(0x38);
{                                                           cmd(0x06);
             rs=0;                                          cmd(0x0c);
             P2=val;                                        cmd(0x01);
             en=1;                                          cmd(0x80);
             delay(10);                                     for(k=0;k<4;k++)
             en=0;                                          {
             delay(10);                                                  DATA(arr[k]);
}                                                                        delay(100);
                                                            }
                                                }
MATRIX KEYBOARD
                  • A 4x4 matrix connected to PORT-1
                  • The rows are connected to an output
                    port and the columns are connected
                    to an input port
                   • Keyboards are organized in a
                     matrix of rows and columns
                  • When a key is pressed, a row
                    and a column make a contact
                  • Otherwise, there is no
                    connection between rows and
                    columns
Embedded systems
• The 8051 has two timers/counters, they can be used either as
     •Timers to generate a time delay
     •Event counters to count events happening outside the microcontroller
•Both Timer 0 and Timer 1 are 16 bits wide
     •Since 8051 has an 8-bit architecture, each 16-bits timer is accessed as two
      separate registers of low byte and high byte


   • Timer 0 Register
                           TH0                                        TL0

   D15   D14   D13   D12    D11   D10   D9   D8   D7   D6   D5   D4    D3   D2   D1   D0




    • Timer 1 Register
                           TH1                                        TL1

   D15   D14   D13   D12    D11   D10   D9   D8   D7   D6   D5   D4    D3   D2   D1   D0
•TMOD is a 8-bit register
        GATE      C/T             M1     M0        GATE           C/T             M1   M0
                        Timer 1                                         Timer 0


    •     The lower 4 bits are for Timer 0
    •     The upper 4 bits are for Timer 1
          In each case,
                The lower 2 bits are used to set the timer mode
                The upper 2 bits to specify the operation

 •To generate a time delay
• Example to calculate 10ms delay

                    1. Divide crystal frequency by 12
                               crystal frequency is 11.0592Mhz/12=0.9216
                    2. T=1/f so 1/0.9216=1.085us
                    3. Divide desired time delay by 1.085us
                               10ms/1.085us=9259.25
                    4. Then perform 65536-9259=56277
                    5. Convert result into Hex i.e dbd5
                    6. Then set TH=db & TL=d5
• Example:-
#include<reg51.h>
void delay()                                                                           void main()
{                                                                                      {
int i;                                                                                 while(1)
for (i=0;i<=200;i++)                                                                                 {
             {                                                                                       P2=0xff;
             TMOD=0x01;               // 8-bit control word                                          delay();
             TH0=0xee; // timer 0 higher byte                                                        P2=0x00;
             TL0=0x00; // timer 0 lower byte                                                         delay();
             TR0=1;                                                                                  }
             while (TF0==0);          // if overflow flag is 0 so come out from loop   }
             TR0=0;       // reset timer register
             TF0=0;       // reset overflow flag
             }
}
Embedded systems
What is an interrupt ?
• Interrupt - Unexpected Function Call

• Hardware interrupts generated by external signal

• Interrupt is a signal indicating that the CPU should suspend it‟s
  current task to service a designated activity

• It is some external event that overrides any current microcontroller
  action and causes certain special program to get executed.

• It is used by devices to transfer data to the microcontroller without
  wasting time.
Interrupt Service Routines

• CPUs have fixed number of interrupts
    Every interrupt has to be associated with a piece of code called
     “Interrupt Service Routine”, or ISR.
     If interrupt-x is received by CPU, the ISR-x is executed
• CPU architecture defines a specific “code address” for each ISR, which is
  stored in the,
• ISRs are basically “subroutines”, but they end with the RETI, instruction
  instead of RET
• When an interrupt occurs, the CPU fetches its ISR code address from the
  and executes it.
Interrupt Vectors

     Symbol   Address     Interrupt Source

    RESET      00H      Power Up or Reset
    EXTI0      03H      External Interrupt 0
    TIMER0     0BH      Timer 0 Interrupt
    EXTI1      13H      External Interrupt 1
    TIMER1     1BH      Timer 1 Interrupt
    SINT       23H      Serial Port Interrupt
8051 Interrupts

• There are 5 interrupts in the 8051.
    Two external interrupts (INT0 and INT1), two timer interrupts (TF0 and
    TF1) and one serial port interrupt (SI).

• Interrupts can be individually enabled or disabled. This is done in the IE
  (Interrupt Enable) register (A8H).
     IE is bit addressable.

• All interrupts correspond to bits in registers.
     Therefore, it is possible to cause an interrupt by setting the
     appropriate bit in the appropriate register.
     The end result is exactly as if the hardware interrupt occurred.
Interrupt Enable Register

 7                                                            0

 EA                           ES      ET1        EX1   ET0   EX0

EA= 1; //enables interrupts
EA=1; EX0=1; //enable interrupts from EX0 only
IE= 0x87; //enable interrupts from EX0, ET0, and EX1
Interrupts MUST be turned on to work. Default is off
• Example
                                            void main()
#include<reg51.h>
                                            {
void delay()
                                                        IE=0x86;
{
                                                        IP=0x04;
              int i,j;
                                                        //IT0=0x00;
              for(i=0;i<=1000;i++)
                                                        IT1=0x01;
              for(j=0;j<=100;j++);
                                                        TMOD=0x09;                // 8-bit control word
}
                                                        TH0=0x3c; // timer 0 higher byte
void a() interrupt 1 //TIMER 0
                                                        TL0=0xb0; // timer 0 lower byte
{
                                                        TR0=1;
              P2=0x77;
                                                        while(TF0==0);            // if overflow flag is 1
              delay();
                                            so come out from loop
              P2=0x88;
                                                        TR0=0;        // reset timer register
              delay();
                                                        TF0=0;                    // reset overflow flag
              return;
}
                                                          while(1)
void add() interrupt 2      // EXTERNAL 1
                                                          {
{
                                                                      P2=0xff;
              P2=0x11;
                                                                      delay();
              delay();
                                                                      P2=0x00;
//            TCON = 0x10;
                                                                      delay();
              P2=0x22;
                                                          }
              delay();
                                            }
              return;
}

More Related Content

PDF
Micro Blaze C Reference
PPT
9 cm604.12
PPTX
Programming Fundamentals
PDF
C++ Chapter III
PDF
C++ Chapter IV
PPTX
Claguage 110226222227-phpapp02
PPT
C C++ tutorial for beginners- tibacademy.in
Micro Blaze C Reference
9 cm604.12
Programming Fundamentals
C++ Chapter III
C++ Chapter IV
Claguage 110226222227-phpapp02
C C++ tutorial for beginners- tibacademy.in

What's hot (20)

PPTX
Very interesting C programming Technical Questions
PDF
yield and return (poor English ver)
PPT
PDF
Ch06
PDF
Topdown parsing
PPT
L3 control
PDF
Think sharp, write swift
PPTX
Decision statements in c language
PPT
PDF
C++ Chapter II
PDF
C++ Quick Reference Sheet from Hoomanb.com
PDF
Loops and conditional statements
PDF
PDF
Learning C programming - from lynxbee.com
PDF
Chapter 00 revision
PPTX
Selection Statements in C Programming
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
DOCX
Bit manipulation in atmel studio for AVR
Very interesting C programming Technical Questions
yield and return (poor English ver)
Ch06
Topdown parsing
L3 control
Think sharp, write swift
Decision statements in c language
C++ Chapter II
C++ Quick Reference Sheet from Hoomanb.com
Loops and conditional statements
Learning C programming - from lynxbee.com
Chapter 00 revision
Selection Statements in C Programming
CONDITIONAL STATEMENT IN C LANGUAGE
Bit manipulation in atmel studio for AVR
Ad

Similar to Embedded systems (20)

PDF
Arithmetic instructions
PPTX
Working with IDE
DOCX
รายงานการเขียนคำสั่งควบคุมแบบวนซ้ำ กลุ่ม 4 ม. 6 ห้อง2
PPT
C tutorial
DOC
Lab sheet 1
DOC
'C' language notes (a.p)
PDF
Workbook_2_Problem_Solving_and_programming.pdf
PDF
Simple C programs
PDF
2 1. variables & data types
PPTX
comp 122 Chapter 2.pptx,language semantics
PPT
Basic c operators
PPTX
การเขียนคำสั่งควบคุมแบบวนซ้ำ กลุ่ม 4 ม.6 ห้อง2
PPTX
การเขียนคำสั่งควบคุมแบบวนซ้ำ กลุ่ม 4 ม.6 ห้อง2
PPTX
What is c
PPTX
Lecture 03 Programming C for Beginners 001
PDF
Lecture15 comparisonoftheloopcontrolstructures.ppt
DOCX
Labsheet2 stud
PPTX
Intro to c chapter cover 1 4
PPT
9 cm604.12
Arithmetic instructions
Working with IDE
รายงานการเขียนคำสั่งควบคุมแบบวนซ้ำ กลุ่ม 4 ม. 6 ห้อง2
C tutorial
Lab sheet 1
'C' language notes (a.p)
Workbook_2_Problem_Solving_and_programming.pdf
Simple C programs
2 1. variables & data types
comp 122 Chapter 2.pptx,language semantics
Basic c operators
การเขียนคำสั่งควบคุมแบบวนซ้ำ กลุ่ม 4 ม.6 ห้อง2
การเขียนคำสั่งควบคุมแบบวนซ้ำ กลุ่ม 4 ม.6 ห้อง2
What is c
Lecture 03 Programming C for Beginners 001
Lecture15 comparisonoftheloopcontrolstructures.ppt
Labsheet2 stud
Intro to c chapter cover 1 4
9 cm604.12
Ad

Recently uploaded (20)

DOCX
Ibrahim Suliman Mukhtar CV5AUG2025.docx
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PPTX
Reproductive system-Human anatomy and physiology
PDF
faiz-khans about Radiotherapy Physics-02.pdf
PDF
THE CHILD AND ADOLESCENT LEARNERS & LEARNING PRINCIPLES
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PDF
Farming Based Livelihood Systems English Notes
PPTX
Macbeth play - analysis .pptx english lit
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PDF
Civil Department's presentation Your score increases as you pick a category
PDF
African Communication Research: A review
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
Nurlina - Urban Planner Portfolio (english ver)
PDF
Hospital Case Study .architecture design
PDF
Compact First Student's Book Cambridge Official
PDF
semiconductor packaging in vlsi design fab
PDF
PUBH1000 - Module 6: Global Health Tute Slides
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PPTX
BSCE 2 NIGHT (CHAPTER 2) just cases.pptx
PDF
Journal of Dental Science - UDMY (2020).pdf
Ibrahim Suliman Mukhtar CV5AUG2025.docx
Literature_Review_methods_ BRACU_MKT426 course material
Reproductive system-Human anatomy and physiology
faiz-khans about Radiotherapy Physics-02.pdf
THE CHILD AND ADOLESCENT LEARNERS & LEARNING PRINCIPLES
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
Farming Based Livelihood Systems English Notes
Macbeth play - analysis .pptx english lit
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
Civil Department's presentation Your score increases as you pick a category
African Communication Research: A review
Journal of Dental Science - UDMY (2021).pdf
Nurlina - Urban Planner Portfolio (english ver)
Hospital Case Study .architecture design
Compact First Student's Book Cambridge Official
semiconductor packaging in vlsi design fab
PUBH1000 - Module 6: Global Health Tute Slides
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
BSCE 2 NIGHT (CHAPTER 2) just cases.pptx
Journal of Dental Science - UDMY (2020).pdf

Embedded systems

  • 1. “EMBEDDED SYSTEM” Tank Mitul G
  • 2. What is Embedded Systems An embedded µc is a chip which has a computer processer with all it‟s support function memory and i/p o/p built in the device.  It includes Hardware and Mechanical parts.  Necessity  Use in daily life.
  • 4. INTRODUCTION OF ‘C’ • C is a general purpose programming language. • ‘C’ language is developed at AT & T’s • Bell Laboratories of USA in 1972. • Designed & written by Dennis Ritchie. • C is relable,simple & easy to use. • Applcation:-mobile phones Computer games Steps in learning C
  • 5. Constant & Variable •A variable can be considered as a name given to any memory location. Example: int i; • Here „i‟ is a name assigned to a particular memory location. •A constant is a quantity that does not change. Example: const int i = 500; Keywords •The keyword can‟t be used as variable names. •The keywords are also called “Reserved Words”. •There are 32 keywords available in C. •Some example of keywords: auto, break, case, char, const, while. Instructions There are basically four types of instructions in C. •Declaration Instruction •Input / Output Instruction •Arithmetic Instruction •Control Instruction
  • 6. Data type declaration There are only a few basic data types in C. 1. Integer 2. Float Data Type Range Bytes Format 3. Double 4. Character signed char -128 to + 127 1 %c unsigned char 0 to 255 1 %c short signed int -32768 to +32767 2 %d short unsigned int 0 to 65535 2 %u long signed int -2147483648 to +2147483647 4 %ld long unsigned int 0 to 4294967295 4 %lu Float -3.4e38 to +3.4e38 4 %f Double -1.7e308 to +1.7e308 8 %lf long double -1.7e4932 to +1.7e4932 10 %Lf
  • 7. Operators •Arithmetic Operators •Assignment Operators •Increment / Decrement Operators multiply Operator Example Meaning Operator Meaning When? * = X=5 X=5 count++ count = count + 1; After use / divide += X += 5 X=X+5 ++count count = count + 1; Before % reminder –= X–=5 X=X–5 use /= X /= 5 X=X/5 count-- count = count – 1; After use + Add *= X *= 5 X=X*5 Subtract --count count = count – 1; Before - %= X %= 5 X=X%5 use Logical Operators Bitwise Operators Exponentiation Operators & bitwise AND Exponentiation is not written as x**2 or x^2 ! (not) | bitwise OR C does not have an exponentiation operator. a != b is true if a and b are not equal ^ bitwise X-OR You can use the math function pow(a, b) && (and) << Left Shift You must include <math.h> in source code 5<6 && 7>4 is true, but >> Right Shift 5>6 && 7>4 is not true (i.e. false) || (or) ~ one‟s complement 5<6 || 7>4 is true, and 5>6 || 7>4 is also true
  • 8. CONDITIONAL STATEMENT •The if-else statement •The if statement (a) if ( condition ) (a) if ( condition ) (b) if ( condition ) (c) if ( condition ) do this ; do this ; do this ; { (b) if ( condition ) else else if ( condition ) { do this ; { do this ; do this ; if ( condition ) else and this ; do this ; { } else do this ; { and this ; do this ; } •The conditional operators and this ; expression 1 ? expression 2 : expression 3 } } else char a ; } do this ; int y ; scanf ( "%c", &a ) ; y = ( a >= 65 && a <= 90 ? 1 : 0 ) ; Here 1 would be assigned to y if a >=65 && a <=90 evaluates to true, otherwise 0 would be assigned.
  • 9. LOOPS 1.Using a while statement 2.Using a for statement 3.Using a do-while statement • As a rule the while must test a condition 1.The while Loop that will eventually become false, otherwise the initialise loop counter ; loop would be executed forever, indefinitely. while ( test loop counter using a condition ) main( ) { { do this ; int i = 1 ; and this ; while (i <= 10 ) increment loop counter ; printf ( " %dn", i ) ; } } • The statements within the loop may be a single line or a block of statements. In the first case the parentheses are optional. For example, while ( i <= 10 ) i = i + 1 is same as while ( i <= 10) { i=i+1; }
  • 10. The for Loop The general form of for statement is as under: PROGRAM FOR CALCULATE for( initialise counter ; test counter ; increment counter ) RATE OF INTEREST { main ( ) do this ; { and this ; int p, n, count ; and this ; float r, si ; } for ( count = 1 ; count <= 3 ; count = count + 1 ) { Thus the following for loops are not ok. printf ( "Enter values of p, n, and r " ) ;  for ( i = 10 ; i ; i -- ) scanf ( "%d %d %f", &p, &n, &r ) ; printf ( "%d", i ) ; si = p * n * r / 100 ;  for ( i < 4 ; j = 5 ; j = 0 ) printf ( "Simple Interest = Rs.%fn", si ) ; } printf ( "%d", i ) ; }  for ( i = 1; i <=10 ; printf ( "%d",i++ ) ;  for ( scanf ( "%d", &i ) ; i <= 10 ; i++ ) printf ( "%d", i ) ;
  • 11. The do-while Loop The do-while loop looks like this: do { this ; and this ; and this ; and this ; } while ( this condition is true ) ; Difference between while and do-while loop { do main( ) { { while ( 4 < 1 ) printf ( "Hello there n") ; printf ( "Hello there n") ; } while ( 4 < 1 ) ; } }
  • 12. FUNCTIONS A function is a self contained block of statements that perform a task of some kind. Every C program is a collection of these functions  main() “calls” the function message() Example  Control passes to the message() func. #include <stdio.h>  After execution of message statements, control returns to main and void main() begin executing the code at the exact point where it left. {  main becomes the “calling funtion” message();  Fnction ucan be called from any other function printf(“nUdaipur”);  A function can call itself. It is called Recursion. }  A function can be called from any other function but message() a function can’t be defined in another function. { printf(“SSCE”); • General form of a function } function name (argument declaration) Output: { SSCE local variables declaration Udaipur executable statements ……………… ……………… return(expression) }
  • 13. Function Types 1) Library Functions 2) User Defined Functions Call by VExample : Call by Value • Methods used for declaration of arguments int square (int x) • Method 1 { function ( a, b, c ) return x*x; int a , b , c ; } Here declaration is done in separate statement. This method is known as K & R method. int main ( ) • Method 2 { function ( int a, int b, int c ) int num = 10; int answer; answer = square(num); Passing the values to the functions printf(“Answer is : %d “, answer); // answer is 100 during function call printf(“Value of a is : %d “, num); // num will be 10 } • Call by Value : • alue : on calling a function we are passing on calling a function we are passing values of variables to it. values of variables to it. • Call by Reference : • Call by Reference : Instead of passing the value, Instead of passing the value, we pass the address of the variables we pass the address of the variables
  • 14. ARRAY Definition : A set of similar data types is called array. An array is a collective name given to a group of similar quantities. Each member in the group is referred to by its position in its group. These similar elements can be all ints or all floats or all characters etc. Array Declaration : int marks[5]; Entering the Data into an array Type of variables : Integer Enter marks of 5 students. Dimension : 5 marks : Name of the variable for(i = 0;i<5;i++) [ ] : Indication of an array { • Each member in the array is referred to by its position. printf(“Enter the marks :”); • Position is from 0 to 4. scanf(“%d”,&marks[i]); Array Initialization } Array Initialization : Int marks[5] = {32,45,56,67,78} Reading Data from an array Int marks[ ] = {32,45,56,67,78} Array elements are always stored in Find average of 5 student marks contiguous locations. for(i = 0;i<5;i++) marks[0] marks[1] marks[2] marks[3] marks[4] sum = sum + marks[i]; 32 45 56 67 78 average = sum/5; 2004 2006 2008 2010 2012 Example : arr0.c
  • 15. Passing array element to a function 1. We can pass the array elements by value or by reference. 2. In call by value, we pass the values of array elements to the function. 3. In call by reference, we pass the address of array elements to the function. • Call by Value • Call by Reference main() main() { { int i, marks[5] = {32,45,56,67,78} int i, marks[5] = {32,45,56,67,78} for(i = 0;i<5;i++) for(i = 0;i<5;i++) findsum(marks[i]); findsum(&marks[i]); } } findsum(int m) findsum(int *m) { { static int sum; static int sum; sum = sum + m; sum = sum + *m; printf(“Sum = %d”,sum); printf(“Sum = %d”,sum); } }
  • 16. SWITCH CASE • When decision from a number of choices is required, at that time switch – case – default is used. • Syntax for the switch - case structure is as follows : Switch ( integer expression) { case value1 : action1 ; case value2 : action2 ; default : action3 ; } • Execution of the Switch 1. First the expression is evaluated. 2. After getting the value it is matched one by one with constant values in case statements. 3. When a match is found, it executes the statements following that case and all subsequent case and default statements as well
  • 17. Notes while using the SWITCH CASE • Integer expression is any constant or expression that evaluates to an integer. • The keyword case is followed by an integer or a character constant. • Float is not allowed as case value. • Each constant in each case must be different from all others. • Order is not important while putting the case values. • No enclosure is needed in case statements • break statement must be used to prevent the execution of subsequent cases. • Use of continue will not take the control to beginning of switch. BREAK Statement • Terminates the execution of the nearest enclosing do, for, switch, or while statement in which it appears. • Control passes to the statement that follows the terminated statement.
  • 19. A typical 8051 contains:  CPU with Boolean processor;  5 interrupts: 2 are external, 2 priority levels;  2 16-bit timer/counters;  programmable full-duplex serial port (baud rate provided by one of the timers);  32 I/O lines (four 8-bit ports);  RAM and ROM/EPROM in some models.
  • 21. Block Diagram External interrupts 4K ROM Interrupt program 128 bytes Timer0 Counter Control code RAM Timer1 Inputs CPU Bus Serial 4 I/O Ports Port OSC Control TXD RXD P0 P2 P1 P3 Address/Data
  • 22. Pin Description of 8051 P1.0 1 40 Vcc P1.1 2 39 P0.0(AD0) P1.2 P1.3 3 4 8 38 37 P0.1(AD1) P0.2(AD2) P1.4 5 36 P0.3(AD3) P1.5 6 35 P0.4(AD4) 7 34 P0.5(AD5) P1.6 P1.7 8 0 33 P0.6(AD6) RST 9 32 P0.7(AD7) (RXD)P3.0 10 31 EA/VPP (TXD)P3.1 11 30 ALE/PROG (INT0)P3.2 12 5 29 PSEN (INT1)P3.3 13 28 P2.7(A15) (T0)P3.4 14 27 P2.6(A14) (T1)P3.5 15 26 P2.5(A13) (WR)P3.6 (RD)P3.7 16 17 1 25 24 P2.4(A12) P2.3(A11) XTAL2 18 23 P2.2(A10) XTAL1 19 22 P2.1(A9) GND 20 21 P2.0(A8)
  • 23. Pins of 8051  Vcc(pin 40) :  Vcc provides supply voltage to the chip.  The voltage source is +5V.  GND(pin 20)- ground  XTAL1 and XTAL2(pins 19,18)  These 2 pins provide external clock.  Way 1:using a quartz crystal oscillator.  Way 2:using a TTL oscillator.
  • 24. Pins of 8051  RST(pin 9):reset  It is an input pin and is active high (normally low).  The high pulse must be high at least for 2 machine cycles.  It is a power-on reset.  High logical state on this input halts the MCU and clears all the registers.  Bringing this pin back to logical state zero starts the program a new as if the power had just been turned on. In another words, positive voltage impulse on this pin resets the MCU.  Way 1:Power-on reset circuit  Way 2:Power-on reset with debounce
  • 25. XTAL Connection to 8051 Using a quartz crystal oscillator We can observe the frequency on the XTAL2 pin. C2 XTAL2 30pF C1 XTAL1 30pF GND
  • 27. Program Memory 64K FFFFH 4K External 0FFFH ROM EA=0 Internal ROM EA=1 0000H 0000H PSEN
  • 28. Data Memory 64K FFFFH 128 bytes External 7FH RAM Internal RAM 00H 0000H WR RD
  • 30. ALE Pin  The ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch.  When ALE=0, P0 provides data D0-D7.  When ALE=1, P0 provides address A0-A7.  The reason is to allow P0 to multiplex address and data.
  • 31. Internal RAM allocation 7FH General RS1 RS0 Register Bank Address purpose RAM 0 0 0 00H-07H 0 1 1 08H-0FH 30H 2FH 1 0 2 10H-17H Mixed Bit/Byte 1 1 3 18H-1FH Addressable 1FH 20H Register Bank3 18H 17H Register Bank2 0FH 10H Register Bank1 08H 07H Stack Pointer Register Bank0 00H
  • 32. Internal RAM and SFR (special function register) region FFH FFH Upper Accessible Accessible SFR 128 by Indirect by Direct addressing addressing only Ports, only Timers, Control Registers, 80H 80H Accumulator, 7FH Stack Pointer, Accessible Etc,. by Direct & Lower Indirect 128 addressing 00H
  • 33. Program status Word CY AC F0 RS1 RS0 OV -- P CY PSW.7 Carry Flag AC PSW.6 Auxiliary Carry flag F0 PSW.5 Available to user for general purpose RS1 PSW.4 Register bank selector bit 1 RS0 PSW.3 Register bank selector bit 0 OV PSW.2 Overflow flag -- PSW.1 Not defined P PSW.0 Parity flag. Set/cleared by hardware to indicate an odd/even number of „1‟ in the accumulator.
  • 35. Assembly Language  Data transfer instructions  Push and pop opcode  Data Exchange  Rotate operations  Logical operations  Arithmetic operations  Jump instructions  Call instruction
  • 36. Embedded ‘C’ Language  Library files - #include<reg51.h>  Declaration data types - int, char, Sbit.  Body - statements
  • 39. • LED connected to any port of microcontroller LED • In figure 8 LED are connected to PORT-2 of uc • Example:- Program of running LED #include<reg51.h> run[]={0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81}; void delay() { long int i,k; for(i=0;i<=1114;i++) { for(k=0;k<=0;k++); } } void main() { int j; for(j=0;j<=7;j++) { P2=run[j]; delay(); } }
  • 40. 7-SEGMENT • 7-segment display use to indicate number up to 0-9 • 7-segment display has 10-pin in this 7-pin for 7-led (a,b,c,d,e,f,g) and 1 for VCC , 1 for ground. • Example #include<reg51.h> sbit EN=P3^7; sbit EN2=P3^6; look_up[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; void delay() { int j,k; for(j=0;j<=1000;j++) for(k=0;k<=100;k++); } void main() { int i; EN=0; EN2=1; for(i=0;i<=9;i++) { P2=look_up[i]; delay(); } }
  • 41. LCD •LCD is finding widespread use replacing LEDS •The ability to display numbers, characters, and graphics •Ease of programming for characters and graphics • LCD pin description PIN SYMBOL I/O DESCRIPTION 1 VSS - Ground 2 VCC - +5V power supply 3 VEE - Power supply to control contrast 4 RS I RS=0 to select command register, RS=1 to select data register 5 R/W I R/W=0 for write, R/W=1 for read 6 E I/O Enable 7 DB0 I/O The 8-bit data bus 8 DB1 I/O The 8-bit data bus 9 DB2 I/O The 8-bit data bus 10 DB3 I/O The 8-bit data bus 11 DB4 I/O The 8-bit data bus 12 DB5 I/O The 8-bit data bus 13 DB6 I/O The 8-bit data bus 14 DB7 I/O The 8-bit data bus
  • 42. • LCD Command Codes HEX CODE Command to LCD Instruction Register 0x01 Clear display screen 0x02 Return home 0x04 Decrement cursor (shift cursor to left) 0x06 Increment cursor (shift cursor to right) 0x05 Shift display right 0x07 Shift display left 0x08 Display off, cursor off 0x0A Display off, cursor on 0x0C Display on, cursor off 0x0E Display on, cursor blinking 0x0F Display on, cursor blinking 0x10 Shift cursor position to left 0x14 Shift cursor position to right 0x18 Shift the entire display to the left 0x1C Shift the entire display to the right 0x80 Force cursor to beginning to 1st line 0xC0 Force cursor to beginning to 2nd line 0x38 2 lines and 5x7 matrix
  • 43. • Example:- void DATA (unsigned char val) # include<reg51.h> { sbit rs=P3^6; rs=1; sbit en=P3^7; P2=val; en=1; void delay(int time) delay(10); { en=0; unsigned char i,j; delay(10); for(i=0;i<=time;i++) } { void main() for(j=0;j<=55;j++); { } int k; } char arr[15]=“microcontroller"; void cmd(unsigned char val) cmd(0x38); { cmd(0x06); rs=0; cmd(0x0c); P2=val; cmd(0x01); en=1; cmd(0x80); delay(10); for(k=0;k<4;k++) en=0; { delay(10); DATA(arr[k]); } delay(100); } }
  • 44. MATRIX KEYBOARD • A 4x4 matrix connected to PORT-1 • The rows are connected to an output port and the columns are connected to an input port • Keyboards are organized in a matrix of rows and columns • When a key is pressed, a row and a column make a contact • Otherwise, there is no connection between rows and columns
  • 46. • The 8051 has two timers/counters, they can be used either as •Timers to generate a time delay •Event counters to count events happening outside the microcontroller •Both Timer 0 and Timer 1 are 16 bits wide •Since 8051 has an 8-bit architecture, each 16-bits timer is accessed as two separate registers of low byte and high byte • Timer 0 Register TH0 TL0 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 • Timer 1 Register TH1 TL1 D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
  • 47. •TMOD is a 8-bit register GATE C/T M1 M0 GATE C/T M1 M0 Timer 1 Timer 0 • The lower 4 bits are for Timer 0 • The upper 4 bits are for Timer 1 In each case, The lower 2 bits are used to set the timer mode The upper 2 bits to specify the operation •To generate a time delay
  • 48. • Example to calculate 10ms delay 1. Divide crystal frequency by 12 crystal frequency is 11.0592Mhz/12=0.9216 2. T=1/f so 1/0.9216=1.085us 3. Divide desired time delay by 1.085us 10ms/1.085us=9259.25 4. Then perform 65536-9259=56277 5. Convert result into Hex i.e dbd5 6. Then set TH=db & TL=d5 • Example:- #include<reg51.h> void delay() void main() { { int i; while(1) for (i=0;i<=200;i++) { { P2=0xff; TMOD=0x01; // 8-bit control word delay(); TH0=0xee; // timer 0 higher byte P2=0x00; TL0=0x00; // timer 0 lower byte delay(); TR0=1; } while (TF0==0); // if overflow flag is 0 so come out from loop } TR0=0; // reset timer register TF0=0; // reset overflow flag } }
  • 50. What is an interrupt ? • Interrupt - Unexpected Function Call • Hardware interrupts generated by external signal • Interrupt is a signal indicating that the CPU should suspend it‟s current task to service a designated activity • It is some external event that overrides any current microcontroller action and causes certain special program to get executed. • It is used by devices to transfer data to the microcontroller without wasting time.
  • 51. Interrupt Service Routines • CPUs have fixed number of interrupts Every interrupt has to be associated with a piece of code called “Interrupt Service Routine”, or ISR.  If interrupt-x is received by CPU, the ISR-x is executed • CPU architecture defines a specific “code address” for each ISR, which is stored in the, • ISRs are basically “subroutines”, but they end with the RETI, instruction instead of RET • When an interrupt occurs, the CPU fetches its ISR code address from the and executes it.
  • 52. Interrupt Vectors Symbol Address Interrupt Source RESET 00H Power Up or Reset EXTI0 03H External Interrupt 0 TIMER0 0BH Timer 0 Interrupt EXTI1 13H External Interrupt 1 TIMER1 1BH Timer 1 Interrupt SINT 23H Serial Port Interrupt
  • 53. 8051 Interrupts • There are 5 interrupts in the 8051. Two external interrupts (INT0 and INT1), two timer interrupts (TF0 and TF1) and one serial port interrupt (SI). • Interrupts can be individually enabled or disabled. This is done in the IE (Interrupt Enable) register (A8H). IE is bit addressable. • All interrupts correspond to bits in registers. Therefore, it is possible to cause an interrupt by setting the appropriate bit in the appropriate register. The end result is exactly as if the hardware interrupt occurred.
  • 54. Interrupt Enable Register 7 0 EA ES ET1 EX1 ET0 EX0 EA= 1; //enables interrupts EA=1; EX0=1; //enable interrupts from EX0 only IE= 0x87; //enable interrupts from EX0, ET0, and EX1 Interrupts MUST be turned on to work. Default is off
  • 55. • Example void main() #include<reg51.h> { void delay() IE=0x86; { IP=0x04; int i,j; //IT0=0x00; for(i=0;i<=1000;i++) IT1=0x01; for(j=0;j<=100;j++); TMOD=0x09; // 8-bit control word } TH0=0x3c; // timer 0 higher byte void a() interrupt 1 //TIMER 0 TL0=0xb0; // timer 0 lower byte { TR0=1; P2=0x77; while(TF0==0); // if overflow flag is 1 delay(); so come out from loop P2=0x88; TR0=0; // reset timer register delay(); TF0=0; // reset overflow flag return; } while(1) void add() interrupt 2 // EXTERNAL 1 { { P2=0xff; P2=0x11; delay(); delay(); P2=0x00; // TCON = 0x10; delay(); P2=0x22; } delay(); } return; }