I SEM Lab - 8051, AVR

download I SEM Lab - 8051, AVR

of 138

Transcript of I SEM Lab - 8051, AVR

  • 7/22/2019 I SEM Lab - 8051, AVR

    1/138

    1. MICROCONTROLLER

    General description

    The P89V51RD2 is an 80C51 microcontroller with 64 kB Flash and 1024 bytes of data

    RAM. A key feature of the P89V51RD2 is its X2 mode option. The design engineer can

    choose to run the application with the conventional 80C51 clock rate (12 clocks per machine

    cycle) or select the X2 mode (6 clocks per machine cycle) to achieve twice the throughput at

    the same clock frequency. Another way to benefit from this feature is to keep the same

    performance by reducing the clock frequency by half, thus dramatically reducing the EMI.

    The Flash program memory supports both parallel programming and in serial In-System

    Programming (ISP). Parallel programming mode offers gang-programming at high speed,

    reducing programming costs and time to market. ISP allows a device to be reprogrammed inthe end product under software control. The capability to field/update the application

    firmware makes a wide range of applications possible.

    The P89V51RD2 is also In-Application Programmable (IAP), allowing the Flash program

    memory to be reconfigured even while the application is running.

    Features

    80C51 Central Processing Unit

    5 V Operating voltage from 0 to 40 MHz

    64 kB of on-chip Flash program memory with ISP (In-System Programming) and IAP

    (In-Application Programming)

    Supports 12-clock (default) or 6-clock mode selection via software or ISP

    SPI (Serial Peripheral Interface) and enhanced UART

    PCA (Programmable Counter Array) with PWM and Capture/Compare functions

    Four 8-bit I/O ports with three high-current Port 1 pins (16 mA each)

    Three 16-bit timers/counters

    Programmable Watchdog timer (WDT)

    Eight interrupt sources with four priority levels

    Second DPTR register

    Low EMI mode (ALE inhibit)

    TTL- and CMOS-compatible logic levels

  • 7/22/2019 I SEM Lab - 8051, AVR

    2/138

    Pin diagram

    Figure 1.1 Pin diagram of 8051

  • 7/22/2019 I SEM Lab - 8051, AVR

    3/138

    P89V51RD2 pin description

    Symbol: P0.0 toP0.7

    Pin : 39-32I/O Port 0: Port 0 is an 8-bit open drain bi-directional I/O port. Port 0 pins that have 1s

    written to them float, and in this state can be used as high-impedance inputs. Port 0 is also the

    multiplexed low-order address and data bus during accesses to external code and data

    memory. It uses strong internal pull-ups when transitioning to 1s External pull-ups is

    required during program verification or as a general purpose I/O port.

    Symbol: P1.0 toP1.7

    Pin : 40-44I/O Port 1: Port 1 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 1 pins

    are pulled high by the internal pull-ups when 1s are written to them and can be used as

    inputs in this state. As inputs, Port 1 pins that are externally pulled LOW will source current(IIL)because of the internal pull-ups. Port 1 also receives the low-order address bytes during

    the external host mode programming and verification.

    P1.0 I/O T2: External count input to Timer/Counter 2 or Clock-out from Timer/Counter 2

    P1.1 I T2EX: Timer/Counter 2 capture/reload trigger and direction control

    P1.2 I ECI: External clock input. This signal is the external clock input for the PCA.

    P1.3 I/O CEX0: Capture/compare external I/O for PCA Module 0.Each capture/compare

    module connects to a Port 1 pin for external I/O. When not used by the PCA, this pin can

    handle standard I/O.

    P1.4 I/O SS: Slave port select input for SPI

    CEX1: Capture/compare external I/O for PCA Module 1

    P1.5 I/O MOSI: Master Output Slave Input for SPI

    CEX2: Capture/compare external I/O for PCA Module 2

    P1.6 I/O MISO: Master Input Slave Output for SPI

    CEX3: Capture/compare external I/O for PCA Module 3

    P1.7 I/O SCK: Master Output Slave Input for SPI

    CEX4: Capture/compare external I/O for PCA Module 4

    Symbol: P2.0 toP2.7

    PIN : 21-28(I/O with internal pull-up)

    Port 2: Port 2 is an 8-bit bi-directional I/O port with internal pull-ups. Port 2 pins are pulled

    HIGH by the internal pull-ups when 1s are written to them and can be used as inputs in this

    state. As inputs, Port 2 pins that are externally pulled LOW will source current (IIL) because

    of the internal pull-ups. It uses strong internal pull-ups when transitioning to 1s. Port 2 also

    receives some control signals and a partial of high-order address bits during the external host

    mode programming and verification.

    Symbol: P3.0 toP3.7

    Pin : 11-17(I/O with internal pull-up)

    Port 3: Port 3 is an 8-bit bidirectional I/O port with internal pull-ups. Port 3 pins are pulled

    HIGH by the internal pull-ups when 1s are written to them and can be used as inputs in this

    state. As inputs, Port 3 pins that are externally pulled LOW will source current (IIL)because

  • 7/22/2019 I SEM Lab - 8051, AVR

    4/138

    of the internal pull-ups. Port 3 also receives some control signals and a partial of high-order

    address bits during the external host mode programming and verification.

    P3.0 RXD: serial input port

    P3.1TXD: serial output portP3.2 INT0: external interrupt 0 input

    P3.3 INT1: external interrupt 1 input

    P3.4 T0: external count input to Timer/Counter 0

    P3.5 T1: external count input to Timer/Counter 1

    P3.6 WR : external data memory write strobe

    P3.7 RD : external data memory read strobe

    PSEN29 I/O Program Store Enable: PSEN is the read strobe for external program memory.

    When the device is executing from internal program memory, PSEN is in active(HIGH).

    When the device is executing code from external program memory, PSEN is activated twice

    each machine cycle, except that two PSEN activations are skipped during each access toexternal data memory. A forced HIGH-to-LOW input transition on the PSEN pin while the

    RST input is continually held HIGH for more than 10 machine cycles will cause the device to

    enter external host mode programming.

    RST 9 I Reset: While the oscillator is running, a HIGH logic state on this pin for two

    machine cycles will reset the device. If the PSEN pin is driven by a HIGH-to-LOW input

    transition while the RST input pin is held HIGH, the device will enter the external host mode;

    otherwise the device will enter the normal operation mode

    EA31 I External Access Enable: EA must be connected to VSS in order to enable the device

    to fetch code from the external program memory. EA must be strapped to VDD for internalprogram execution. However, Security lock level 4 will disable EA, and program execution is

    only possible from internal program memory. The EA pin can tolerate a high voltage of 12

    V.

    ALE/PROG30 I/O Address Latch Enable: ALE is the output signal for latching the low

    byte of the address during an access to external memory. This pin is also the programming

    pulse input (PROG) for flash programming. Normally the ALE is emitted at a constant rate of

    1/6 the crystal Frequency and can be used for external timing and clocking. One ALE pulse is

    skipped during each access to external data memory. However, if AO is set to 1, ALE is

    disabled.

    XTAL1 19 I Crystal 1: Input to the inverting oscillator amplifier and input to the internal

    clock generator circuits.

    XTAL2 18 O Crystal 2: Output from the inverting oscillator amplifier.

    VDD 40 I Power supply

    VSS20 I Ground

  • 7/22/2019 I SEM Lab - 8051, AVR

    5/138

    2. KEIL

    Keil software is used to write the microcontroller code and to simulate it on the computer

    itself. It is also used to generate the hex code for the code written in ALP or C.

    User Vs machine (microcontroller): as the microcontroller know only about the digital value

    (i.e. either logic zero or one), so we need to convert the written code in the sets of zero n one.

    The KEIL environment provide us the facility to convert the code written in C or ALP to sets

    of zero n one (i.e. called hex code).

    How to use the keil

    Create a new project

    To create a new project go to project option then click the new project option. It will ask

    to save the project in the desired folder. All the project is saved in the Uv2 format with the

    extension of Uv2.

    Selecting the device

    After saving the project the window will appear which will ask for selecting the device, then

    select the company PHILIPS or NXP and then select the device number P89V51RD2.

    Figure 2.1 selecting device

  • 7/22/2019 I SEM Lab - 8051, AVR

    6/138

    After selection of device, one more window will appear to copy the start up code .After

    clicking ok option the project format is ready to simulate the code.

    Writing Code

    clickthe new file in the FILE menu. Then a new file will appear in which code must bewritten in any programming language like C or ALP. The window will appear as shown

    below

    Figure 2.2 writing code in editor window

    Saving the code

    Code written in C should be saved with the extension .c (i.e. the file is stored as

    filename.c). The code written in ALP(assembly language programming) has to save with the

    extension of .a.

    Adding the code file in the project

    Right click on source group 1 ,the option Add files to group source group1 will appear.

  • 7/22/2019 I SEM Lab - 8051, AVR

    7/138

    Figure 2.3 Adding files to group

    Simulating code and checking

    Click the debug option and click start/stop debug session then the the window will appear

    as follows:

    Figure 2.4 Starting debug session

  • 7/22/2019 I SEM Lab - 8051, AVR

    8/138

    The keil we use having limitation to simulate the maximum code size 2KB.this window is

    just for a warning message, so, just press ok for that option. After that the project is ready to

    run.

    The microcontroller is having 4 ports. Each port is having 8 pins. Each port can be used

    either as input or output port. By using KEIL integrated development environment, we can

    check all the input and output on computer itself.

    For that when your project is ready to run then click the option peripheral then go to I/O

    ports and then select the ports which all are required.

    Figure 2.5 Testing of code

    Testing of code

    Code can be tested on computer itself. After selecting the port, just press run on the

    window and give the input to the port and the corresponding output will automatically appear

    on other port.

  • 7/22/2019 I SEM Lab - 8051, AVR

    9/138

    Figure 2.6 Simulation

    Compilation

    Compilation can be done by clicking translate current file and then building by clicking built

    target.It will generate the hex code .But before that settings must be changed, for that project option

    selected and then Options for Target target 1must be clicked.

    Figure 2.7 To select target

  • 7/22/2019 I SEM Lab - 8051, AVR

    10/138

    A new window will appear as sho

    Here,the frequency of crystal whiROM(0x0-0xFFFF)option must

    option Create HEX Filemust be

    automatically.

    wn below, here we need to change the Xtal

    Figure 2.8 To set Xtal(MHZ)

    ch we are going to use must be filled. For thae clicked. Then the option output must be

    selected. Then build target will generate the

    (MHz):

    t Use On-chipclicked and the

    HEX code

  • 7/22/2019 I SEM Lab - 8051, AVR

    11/138

    3. ISIS PROTEUS

    ISIS incorporates a number of features which aid in the management of large designs. It is

    used to produce designs containing many thousands of components.

    ISIS provides the means to enter the design in the first place, the architecture for real time

    interactive simulation and a system for managing the source and object code associated with

    each project. In addition, a number of graph objects can be placed on the schematic to enable

    conventional time, frequency and swept variable simulation to be performed.

    FEATURES

    The simulator runs on Windows 98/Me/2k/XP and later. It allows automatic wire routing and

    dot placement/removal. It contains powerful tools for selecting objects and assigning their

    properties. It ensures full support for buses including component pins, inter-sheet terminals,

    module ports and wires. It includes bill of materials and electrical rules check reports. Net list

    outputs to suit all popular PCB layout tools.

    ISIS AND NETWORKS

    ISIS is fully network compatible. The library files can be set to Read Only. This prevents

    users from messing with symbols or devices that may be used by others. Since the registry

    determines the location of library files, it follows that users can have individual

    USERDVC.LIB files in their personal or group directories.

    DESCRIPTIONS

    ISIS program must be started.The start button must be clicked. Along the top of the screen isthe menu bar. The largest area of the screen is called the editing window, and it acts as a

    window on the drawing - this is where the wire-up components can be placed. The smaller

    area at the top right of the screen is called the overview window. In normal use the overview

    window displays an overview of the entire drawing - the blue box shows the edge of the

    current sheet and the green box shows the area of the sheet currently displayed in the editing

    window. However, when a new object is selected from the object selector the overview

    window is used to preview the selected object.

    The area of the drawing is displayed in the editing window to simply 'pan' the editing window

    up, down, left or right, position of the mouse pointer over the desired part of the editing

    window and the F5 key is pressed. The SHIFT key is pressed and the mouse is bumpedagainst the edges of the editing window to pan up, down, left or right.F8 key is pressed to

    display the whole drawing. The SHIFT key is pressed down and dragged out a box around

    the area to zoom in to .Zoom In, Zoom Out, Zoom Full or Zoom Area icons on the toolbar

    also can be used for zooming. Below the Overview Window is the Object Selector which is

    used to select devices, symbols and other library objects.

    STEPS TO DRAW THE DESIGN

    The p button at the top left corner of the object selector is selected. Clicking left causes the

    device selector dialogue box to appear and select the various devices from the libraries.Theobjects selector displays all the parts in the currently selected library according to the settings

  • 7/22/2019 I SEM Lab - 8051, AVR

    12/138

    in the extensions selector. Click left once on a part to browse or double-click a part to pick

    it in to the design. The browser displays the last selected part in the parts selector as a means

    of browsing the contents of the library. Whenever a device is selected in the device selector,

    the selected device is previewed in the overview window.

    By clicking left or right on the Rotation and/or Mirror icons, the device is redrawn topreview the new orientation. The preview remains until the device is placed or until another

    command or action is performed. It should be ensured that the device is selected and the

    mouse pointer is moved into the middle of the editing window. The left mouse button must

    be pressed down. An outline of the device will appear which can be moved around by

    dragging the mouse. When the button is released, the component will be placed and is drawn

    in full. The rotation of the last object tagged can be adjusted in this way; the mirror icon can

    similarly be used to reflect the last object tagged. ISIS senses a component pin and deduces

    that a wire is needed to connect from it. To signify this, it displays a green line which goes

    from the pin to the pointer. Manually a wire can be routed by simply clicking left on the first

    pin, clicking left at each point along the required route and then finished by clicking left on

    the second pin. To complete this section of the drawing, place the two generic and oneground terminals and wired. To do this, the terminal icon is selected. The object selector

    changes to a list of the terminal types available. The ground terminal is selected and placed it.

    Work must be saved at any time by means of the Save command on the File menu. Save as

    option also allows saving it with a different filename to run the program, the play button must

    be clicked and to terminate the program, the stop button is clicked.

  • 7/22/2019 I SEM Lab - 8051, AVR

    13/138

    4. FLASHMAGIC

    Flash Magic is a PC tool for programming flash based microcontrollers from NXP using a

    serial or Ethernet protocol while in the target hardware. There are five simple steps to erase

    and programme a device and setting key options. It programs Intel Hex Files. There is

    Automatic verifying option after programming.

    MAIN WINDOW

    After flashmagic software start-up, the main window will be as shown below:

    Figure 4.1 Main window

    Communication

    For this select device by selecting select and select P89v51RD2device

    Then specify COM1 in COMPORT row.

    Specify the baud rate as 9600 Fill interface row as NONE(ISP).

    Specify operator frequency as 12MHz.

    Erase

    Enable the Erase block used by HEX file, as shown below:

  • 7/22/2019 I SEM Lab - 8051, AVR

    14/138

    Figure 4.2 Erasing blocks used by Hex file

    Hex File selection

    Select the required/generated hex file by pressing Browse button, as shown below:

    Figure 4.3 Choosing Hex file

  • 7/22/2019 I SEM Lab - 8051, AVR

    15/138

    Then select hex file and its path specified in the Hex File.

    Options

    Go to options menu from that select Advanced option and in that press Hardware

    Config

    Here, Enable the use DTR to control RST which is normally disabled as shown below:

    Figure 4.4 Hardware configure

    Then press ok

    Press the start button from main window as shown below:

    Figure 4.6 Start dumping into specified IC

    So by pressing start button the hex file will be start dumping into the specified

  • 7/22/2019 I SEM Lab - 8051, AVR

    16/138

    5. UNIVERSAL PROGRAMMER-TOPWIN6

    The TOP3100 programmer has compact size, lower power consumption, and high reliability.

    This is an universal equipment especially designed for MCU and EPROMs programming.USB universal serial port is used to connect with the PC, communications, support

    2000/xp//Vista/Win7, software upgrades and flexible.

    Features

    Full driving with 48 pin(Vpp/Vcc/GND/TTL), good for future upgrading.

    Support 2.5V to 6.5V device.

    Selectable to use USB or external 5V power supplier to supply power.

    Transmission speed is 12MHz/s.

    Suitable to work with Desktop and Laptop. With current protection function, effectively protects the programmer and devices.

    Automatic Pin status detection.

    48 pins self-lock sockets.

    Support operation system: WINDOWS7/vista/2000/2003/XP;

    Plastic cabinet, small size and light weight.

    Auto detects company and type of the IC.

    Software support: TopWin ver6.0 or newer.

    Specifications

    Software: Topwin6 (forWindows7/Vista/2003/2000//xp)

    Actual tested Speed(PIII/800M, Windows98se,USB1.1):28F320 write and check 104seconds,29LF320 write and check 112 seconds, p89c58 write and check 5 seconds.

    Size: 155mm*110mm*26mm

    Weight for the programmer only : 275g

    Power: < 2.5W (5V/500mA)

    48 Pin self-lock socket (Changeable)

    This auction includes

    New Top3100 USB Universal programmer; Software: TopWin6 (for Windows7/Vista/xp);

    USB cable.

    Main window:

    After TopWin start-up, Main window of topwin6 will be showed on the screen as follows:

  • 7/22/2019 I SEM Lab - 8051, AVR

    17/138

    Figure 5.1 Main window

    Select device by multiple list

    Choose "Run" ,and from that select select chip then appeared the dialog box as follows:

  • 7/22/2019 I SEM Lab - 8051, AVR

    18/138

    Figure 5.2 Select chip

    Select device

    Select "Type of Device" in the "Type" menusuch as "MPU".

    Select a manufacturer from the list of "Manufacturer", like "PHILIPS".

    Select a device type from the list of "Device", like select"P89V51RD2".Then dialog box appears as follows:

  • 7/22/2019 I SEM Lab - 8051, AVR

    19/138

    Figure 5.3 Select P89v51RD2

    Erasing previous content:

    To erase the previous content from buffer, go to Run menu and from that selectErase, as shown in the dialog box:

  • 7/22/2019 I SEM Lab - 8051, AVR

    20/138

    Figure 5.4 Erase before loading

    Now after erasing it is ready to load the new data.

    After being erased the device shows "FF".

    Loading file to buffer

    Window under the buffer will show "data from xxxx to xxxx".The function is helping you to

    write in the correct address.

    Loading step is the same with "Open file".

    Select the empty device before setup.

    To load the new data go to File menu and select Load Data option, as shown in the

    below dialog box:

  • 7/22/2019 I SEM Lab - 8051, AVR

    21/138

    Figure 5.5 Select load data

    Then select the required/generated hex file with filename.hex extension from the library

    and open it, as shown below:

    Figure 5.6 Choosing the Hex file

  • 7/22/2019 I SEM Lab - 8051, AVR

    22/138

    1Choose the file format in "project file", for example a hexadecimal file, .HEX

    2. Choose byte. Many devices should set byte while shown the dialog.

    3Select the way to fill, if it is 00, then the buffer zone so if it is FF, so is the blank or No

    clear fill mode.

    4Select the address of buffer zone. Normally starting address will be o.

    Figure 5.7 Select the address of buffer zone

    Operation

    Operation box on the left hand beside buffer will spring the five functions of the device.

    Select the function which is needed, then press[Go], programmer will operate "Erase""Blank" "Write code" "Verify" "Config Secure are the operation done by pressing

    start and GO buttons as shown below:

  • 7/22/2019 I SEM Lab - 8051, AVR

    23/138

    Figure 5.8 Select start and go

    The verify menu is selected by choosing secure from Run and is chiefly used to

    test the common static storage SRAM. Press "Start"[Go], and the programmer test eachbyte of memory to ensure the test is performed stably, as shown below.

    Figure 5.9 To verify

  • 7/22/2019 I SEM Lab - 8051, AVR

    24/138

    Ex. No : 1

    Date :

    ARITHMETIC OPERATIONS USING 8051

    AIM:To design and implement arithmetic operations by interfacing KEYPAD and LCD using 8051

    microcontroller.

    HARDWARE REQUIRED:

    1. VSM DLL model keypad.2. 16X2 Line LCD (LM016L).

    3. PHILIPS P89C51RD2XX.

    4. TOP2048 Programmer kit.5. Power supply(5v).

    6. Connecting wires.

    SOFTWARE REQUIRED:1. Keil uvision 3.

    2. ISIS Proteus Professional 7.

    3. Topwin VER5.49.

    ALGORITHM:

    1. Initialize the ports using cct_init() function.

    2. Initialize the Port1 for input interfacing with keypad.(Rows are assigned toP1.0,P1.1,P1.2,P1.3& columns are assigned to P1.4,P1.5,P1.6&P1.7)

    3. Initialize the Port2 for data pins of LCD.

    4. Initialize the P3.6 for enable and P3.7 for RS of LCD.

    5. Get the key value and store it in num1 using get_key() function.

    6. Check the num1 with all cases in get_num() if not display wrong input using DispError()

    function.

    7. Display the num1 on LCD using writedata().

    8. Get the operator using get_func();

    9. Check the operator with all cases in get_func() if not display wrong input using DispError()

    function.

    10. Similarly for num2.

    11. Press= key and perform the operation .

    12. Check whether num less than zero then perform the operation and display the number with

    signed value.

  • 7/22/2019 I SEM Lab - 8051, AVR

    25/138

    FLOWCHART:

    START

    Initialize the ports using

    cct_init()

    Initialize the P1 for input

    interfacing with keypad

    Initialize the P2 for data pins

    of LCD

    Initialize the P3.6 for E &

    P3.7 for RS of LCD

    Get the key value

    Store the num1 using get_key()

    Check num1

    in get_num()

    Display wrong input

    using disp error()

    Display the num1 on LCD usingwrite data()

    Get the operator using get_fun()

    *

    NO

    YES

  • 7/22/2019 I SEM Lab - 8051, AVR

    26/138

    *

    Checkoperator in

    get_fun()Display wrong function using

    Disp error()

    store the num2 using

    get_key()

    Check

    num2 in

    get_fun()

    Press = key & perform the

    operation

    Display wrong input using

    Disp error()

    Display the number with

    signed value

    Check the

    num < 0

    YES

    NO

    NO

    NO

    YES

    YES

    Stop

    Display the result

    Fig 1.2 Arithmetic Operation Using 8051 Microcontroller

  • 7/22/2019 I SEM Lab - 8051, AVR

    27/138

    Program:

    #include#include

    #define Error 13

    void cct_init(void);

    void delay(int);void lcdinit(void);

    void writecmd(int);void writedata(char);

    void writeline(char[]);

    void ReturnHome(void);char READ_SWITCHES(void);

    char get_key(void);

    int get_num(char);

    char get_func(char);void DispError(int);

    void disp_num(int);sbit RowA = P1^0;sbit RowB = P1^1;

    sbit RowC = P1^2;

    sbit RowD = P1^3;sbit C1 = P1^4;

    sbit C2 = P1^5;

    sbit C3 = P1^6;

    sbit C4 = P1^7;sbit E = P3^6;

    sbit RS = P3^7;

    int main(void){

    char key;

    int num1 = 0;char func = '+';

    int num2 = 0;

    cct_init();

    lcdinit();while(1)

    {

    key = get_key();writecmd(0x01);

    writedata(key);

    num1 = get_num(key);

    if(num1!=Error)

    {key = get_key();

    writedata(key);

  • 7/22/2019 I SEM Lab - 8051, AVR

    28/138

    func = get_func(key);

    if(func!='e'){

    key = get_key();

    writedata(key);

    num2 = get_num(key);

    if(num2!=Error){

    key = get_key();

    writedata(key);

    if(key == '=')

    {

    switch(func){

    case '+': disp_num(num1+num2); break;case '-': disp_num(num1-num2); break;case 'x': disp_num(num1*num2); break;

    case '/': disp_num(num1/num2); break;

    }}

    else

    {

    if(key == 'C')writecmd(0x01);

    else

    DispError(0);}

    }

    }}

    }

    }

    void cct_init(void)

    {P0 = 0x00;

    P1 = 0xf0;

    P2 = 0x00;P3 = 0x00;

    }

    void delay(int a)

    {

  • 7/22/2019 I SEM Lab - 8051, AVR

    29/138

    int i;

    for(i=0;i

  • 7/22/2019 I SEM Lab - 8051, AVR

    30/138

    void writeline(char Line[]){

    int i;

    for(i=0;i

  • 7/22/2019 I SEM Lab - 8051, AVR

    31/138

    {

    char key = 'n';

    while(key=='n')

    key = READ_SWITCHES();

    return key;

    }

    int get_num(char ch)

    {switch(ch)

    {

    case '0': return 0; break;

    case '1': return 1; break;case '2': return 2; break;

    case '3': return 3; break;case '4': return 4; break;case '5': return 5; break;

    case '6': return 6; break;

    case '7': return 7; break;case '8': return 8; break;

    case '9': return 9; break;

    case 'C': writecmd(0x01); return Error; break;

    default: DispError(0); return Error; break;}

    }

    char get_func(char chf)

    {

    if(chf=='C'){

    writecmd(0x01);

    return 'e';

    }

    if( chf!='+' && chf!='-' && chf!='x' && chf!='/' )

    {DispError(1);

    return 'e';

    }

    return chf;

    }

  • 7/22/2019 I SEM Lab - 8051, AVR

    32/138

    void DispError(int numb)

    {writecmd(0x01);

    switch(numb)

    {case 0: writeline("Wrong Input"); break;

    case 1: writeline("Wrong Function"); break;default: writeline("Wrong Input"); break;

    }

    }

    void disp_num(int numb)

    {

    unsigned char UnitDigit = 0;unsigned char TenthDigit = 0;

    if(numb

  • 7/22/2019 I SEM Lab - 8051, AVR

    33/138

    Circuit diagram:

    Output:

    VSM DLL model keypad

    P89C51RD2

    LCD LM016L

    Fig. 1.3 8051 Microcontroller interfacing with LCD & Keypad Connection

    Fig 1.4 Sample Output for Addition

  • 7/22/2019 I SEM Lab - 8051, AVR

    34/138

    Fig 1.5 Sample Output for Subtraction

    Fig 1.6 Sample Output for Multiplication

  • 7/22/2019 I SEM Lab - 8051, AVR

    35/138

    Hardware Diagram:

    Result:Therefore, arithmetic operations by interfacing keypad and lcd using 8051 microcontroller has

    been executed successfully.

    Fig 1.7 Sample Output for Division

    Fig 1.8 Hardware Setup for Arithmetic Operation

  • 7/22/2019 I SEM Lab - 8051, AVR

    36/138

    EX NO: 2

    DATE:

    SERIAL COMMUNICATION WITH UART OF 8051:

    AIM

    To establish serial communication with UART of 8051 and simulate the output

    through Kiel software.

    COMPONENTS REQUIRED

    Microcontroller IC P89V51RD2FN

    Max 232 IC & 9pin RS232 female connector.

    Bread board & connecting wires.

    Resistors 1K,8.2k,10k-10nos

    Crystal Oscillator 11.0592 MHz

    Capacitor 1f, 10f, 33pf, 0.1f,150pf-6nos

    BLOCK DIAGRAM

    Figure 2.1 Block Diagram-Serial Communication

  • 7/22/2019 I SEM Lab - 8051, AVR

    37/138

    SERIAL COMMUNICATION

    PC/compatible computers (Pentium) microprocessors normally have two COM

    ports

    Both ports have RS232-type connectors

    COM ports are designated as COM 1 and COM 2 (replaced by USB ports)

    Serial communication is often used either to control or to receive data from an

    embedded microprocessor.

    Serial communication is a form of I/O in which the bits of a byte begin transferred

    appear one after the other in a timed sequence on a single wire.

    Serial communication has become the standard for inter-computer

    communication. In this lab, we'll try to build a serial link between 8051 and PCusing RS232 and MAX 232 to make it TTL compatible.

    MAX-232

    Converts from RS232 voltage levels to TTL voltage levels

    Uses a +5 V power source

    MAX232 has two sets of line drivers for transferring and receiving data

    Line drivers used for TxD are called T1 and T2 Line drivers for RxD are designated as R1 and R2

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

    Second set is left unused

    (a) Inside MAX232 b) its Connection to the 8051 (Null Modem)

    Figure 2.2 - MAX-232

  • 7/22/2019 I SEM Lab - 8051, AVR

    38/138

    RS232 STANDARDS

    Most widely used serial I/O interfacing standard

    Input and output voltage levels are not TTL compatible

    1 bit is represented by -3 to -25 V

    0 bit is +3 to +25 V

    -3 to +3 is undefined

    To connect RS232 to a microcontroller system must use voltage converters such as

    MAX232 to convert the TTL logic levels to the RS232 voltage levels, and vice versa

    9 PIN RS232 FEMALE CONNECTOR

    Data communication classification

    DTE (data terminal equipment)

    DCE (data communication equipment)

    DTE - Terminals and computers that send and receive data

    DCE - Communication equipment responsible for transferring the data

    Simplest connection between a PC and microcontroller requires a minimum of

    three pins, TxD, RxD, and ground

    Figure 2.3

    PC/compatible computers (Pentium) microprocessors normally have two COM

    ports

    both ports have RS232-type connectors

    COM ports are designated as COM 1 and COM 2 (replaced by USB ports)

    can connect the 8051 serial port to the COM 2 port

  • 7/22/2019 I SEM Lab - 8051, AVR

    39/138

    9 PIN RS232

    Figure 2.4- RS232 DB9 Female Connector

    DESCRIPTION

    PROGRAMMING 8051 TO TRANSFER DATA SERIALLY:

    1) The TMOD register is loaded with the value 20H, indicating the use of timer1 in mode2 toset the baud rate.

    2) The TH1 is loaded with one of the baud rate values for serial data transfer.

    3) The SCON register is loaded with value 50H, indicating serial mode1,where an 8-bit data

    is framed with start and stop bits

    4) TR1 is set to 1 to start timer1

    5) TI is cleared by the CLR TI instruction

    6) The character byte to be transferred serially as written into the SBUF register

    7) The TI flag bit is monitored with the use of instruction JNB TI, XX to see if the

    character has been transferred completely

    8) To transfer the next character, go to step 5.

  • 7/22/2019 I SEM Lab - 8051, AVR

    40/138

    CODING

    MOV TMOD,#20H ;Timer 1 mode 2(auto reload)

    MOV TH1,#-3 ;9600 baud rate

    MOV SCON,#50H ;serial mode 1,REN enabled

    SETB TR1 ;to start timer 1

    MOV A,#"H" ;transfer H to A

    ACALL TRANS ;call subroutine

    MOV A,#"I" ;transfer I to A

    ACALL TRANS ;call subroutine

    MOV A,#" " ;transfer to A

    ACALL TRANS ;call subroutine

    MOV A,#"H" ;transfer H to A

    ACALL TRANS ;call subroutine

    MOV A,#"O" ;transfer O to A

    ACALL TRANS ;call subroutine

    MOV A,#"W" ;transfer W to A

    ACALL TRANS ;call subroutine

    MOV A,#" " ;transfer to A

    ACALL TRANS ;call subroutine

    MOV A,#"A" ;transfer A to A

    ACALL TRANS ;call subroutine

    MOV A,#"R" ;transfer R to A

    ACALL TRANS ;call subroutine

    MOV A,#"E" ;transfer E to A

    ACALL TRANS ;call subroutine

    MOV A,#" " ;transfer to A

    ACALL TRANS ;call subroutine

  • 7/22/2019 I SEM Lab - 8051, AVR

    41/138

    MOV A,#"Y" ;transfer Y to A

    ACALL TRANS ;call subroutine

    MOV A,#"O" ;transfer O to A

    ACALL TRANS ;call subroutine

    MOV A,#"U" ;transfer U to A

    ACALL TRANS ;call subroutine

    MOV A, #"?" ;transfer ? to A

    ACALL TRANS ;call subroutine

    AGAIN:SJMP AGAIN ;be in this loop always

    TRANS: MOV SBUF,A ;transfer contents of A serially

    HERE: JNB TI,HERE ; wait for transmission to be over

    CLR TI ;clear TI for next transmission

    RET ;go back to next instruction

  • 7/22/2019 I SEM Lab - 8051, AVR

    42/138

    CIRCUIT DIAGRAM

    Figure 2.5 Circuit Diagram

  • 7/22/2019 I SEM Lab - 8051, AVR

    43/138

    FLOW CHART

    Figure 2.6 Flow Chart

    Start

    Start the timer TR1

    Transfer data to SBUF register

    Check TI

    flag

    Set TMOD for

    mode selection

    Set TH1 to set

    baud rate

    Load the value to

    SCON register

    Send next data to be transmitted

    Transfer current data

    Stop

  • 7/22/2019 I SEM Lab - 8051, AVR

    44/138

    SIMULATION OUTPUT

    Figure 2.7 Simulation Output

    HYPER-TERMINAL OUTPUT

    Figure 2.8 Hyper terminal Output

  • 7/22/2019 I SEM Lab - 8051, AVR

    45/138

    HARDWARE SETUP

    Figure 2.9 Hardware Setup

    RESULT

    Thus the Serial Transmission of data from Microcontroller to HyperTerminal was set

    by using RS 232 Cable

  • 7/22/2019 I SEM Lab - 8051, AVR

    46/138

    EXNO:3

    DATE:

    TRAFFIC CONTROL IMPLEMENTATION USING 8051 MICROCONTROLLER

    AIM

    To implement traffic control using 8051 microcontroller interfacing seven segment display.

    COMPONENTS REQUIRED:

    Microcontroller (P89V51RD2)

    Seven segment display (anode)- 2

    Development board

    Universal programmer

    connecting wires

    ALGORITHM

    Include the header file using the comment #include

    Define RED led, YELLOW led, GREEN led,RED1 led,YELLOW1 led,GREEN1 led

    for the port P1 ,pin 0,pin 1,pin 2,pin 5,pin 6,pin 7 respectively

    Declare function call display1(), display(),delay()

    In the main program give input to RED led (port1^0) and GREEN1 led(port1^7) and

    other all led is off

    Call the display function to decrement the counter from 9 to 0

    >declare the 7 segment code in array.The code is written for common anode.

    >call the delay function to execute the delay by the timer between the decrement

    TMOD=0x01 set timer 1 and mode 1 of 8 bit microcontroller

    TH0=0xa0 load the timer higher 8 bit with the value a0

    TL0=0x30 load the timer lower 8 bit with the value 30

    TR0=1 turn on T0

    while(TF0==0) wait for TF0 to roll over

    TR0=0 turn OFF T0

    TF0=0 clear TF0

    Display function executed the program returns to the main program,

    Give input to YELLOW led(port P1^1) call for msdelay for 10 secs. and

    simultaneously RED1 led (port P1^5) is ON.

    YELLOW led goes low. GREEN led (port P1^2) is ON .Now Display1 function is

    called for display the counter value from 9-0

  • 7/22/2019 I SEM Lab - 8051, AVR

    47/138

    Display1 function has different time delay from display function since YELLOW

    led goes from low to high.

    The loop continues to ON the led in the sequence RED -GREEN1-YELLOW-

    RED1-GREEN-YELLOW1-GREEN1-RED with the counter decrement from 9 to 0.

    FLOW CHART

    Fig 3.1 Flowchart of Traffic Control Implementation using 8051 Microcontroller

    INITIALIZE DISPLAY,DISPLAY1,

    DELAY, MSDELAY FUNCTION

    SET PORT PIN FOR

    RED,RED1,YELLOW,

    YELLOW1,GREEN,GREEN1 LED

    COUNTER DECREMENT

    FUNCTION

    DELAY

    FUNCTION

    GIVE INPUT TO RED ,GREEN1 LED &

    CALL DISPLAY FUNCTION

    START

    YELLOW LED =1 & CALLS MSDELAY

    FUNCTION

    GREEN =1 & RED1=1 & CALL DISPLAY1

    FUNCTIONCOUNTER DECREMENT

    FUNCTION

    DELAY

    FUNCTION

    WHILE(1)

  • 7/22/2019 I SEM Lab - 8051, AVR

    48/138

    CIRCUIT DIAGRAM

    Fig 3.2 Circuit Diagram of Traffic Control Implementation Using 8051 Microcontroller

    Seven segment

    display

  • 7/22/2019 I SEM Lab - 8051, AVR

    49/138

    CODING

    #include

    sbit rlight=P1^0;

    sbit ylight=P1^1;

    sbit glight=P1^2;sbit r1light=P1^5;

    sbit y1light=P1^6;

    sbit g1light=P1^7;

    void display1();

    void display();

    void delay();

    void msdelay(unsigned int);

    void main()

    {

    unsigned char i;

    while(1){

    ylight=0;

    r1light=0;

    y1light=0;

    glight=0;

    rlight=1;

    g1light=1;

    display();

    rlight=0;

    glight=0;

    g1light=0;

    r1light=1;

    display1();

    r1light=0;

    glight=0;

    ylight=0;

    rlight=1;

    y1light=1;

    for(i=0;i

  • 7/22/2019 I SEM Lab - 8051, AVR

    50/138

    } } }

    void display1()

    code unsigned char pattern[10 ]={0x98,0x80,0Xf8,0X02,0X12,

    0X99,0Xb0,0Xa4,0Xf9,0Xc0};

    unsigned int n,i;

    ylight=1;for(i=0;i

  • 7/22/2019 I SEM Lab - 8051, AVR

    51/138

    ISIS STIMULATION OUTPUT

    Fig 3.3 ISIS Stimulation of Traffic Control Implementation with 8051 Microcontroller

    HARDWARE OUTPUT

    Fig3.4 Hardware Output of Traffic Control Implementation with 8051 Microcontroller

  • 7/22/2019 I SEM Lab - 8051, AVR

    52/138

    RESULT

    Traffic control using 8051 microcontroller with seven segment display is implemented and

    output is verified.

  • 7/22/2019 I SEM Lab - 8051, AVR

    53/138

    EX NO: 4

    DATE:

    COUNTER PROGRAMMING WITH TIMERS IN 8051

    MICROCONTROLLER

    AIM

    To establish and develop an counter application for P89v51RD2FN microcontroller, by

    programming in keil with timers using delay.

    COMPONENTS REQUIRED

    Microcontroller (P89V51RD2)

    Led-4(Yellow)

    Development board Universal programmer

    Connecting wires

    ALGORITHM

    Include the header file and declare the delay function.

    Assign the characters x and i

    Initialize the port 1 as output by assigning port 1=x.

    Initialize for loop with x=0 and the count increment for x

  • 7/22/2019 I SEM Lab - 8051, AVR

    54/138

    FLOWCHART

    No

    Yes

    No

    Yes

    Figure 4.1 Upcounter Implementation Using 8051 Microcontroller

    Initialize Unsigned character(x=0,i=0)

    Assign (P1=x)

    If

    i

  • 7/22/2019 I SEM Lab - 8051, AVR

    55/138

    CIRCUIT DIAGRAM

    Figure 4.2 Circuit Diagram for Upcounter Implementetion

  • 7/22/2019 I SEM Lab - 8051, AVR

    56/138

    CODING

    #include

    void T1M1delay(void);

    void main()

    {

    unsigned char x,i;

    while(1)

    {

    for(x=0;x

  • 7/22/2019 I SEM Lab - 8051, AVR

    57/138

    0-OFF

    1-ON

    Table 4.1 Truth Table of Upcounter Implementation

    RESULT

    Thus the upcounter application with timer using delay was successfully executed and

    verified.

    UPCOUNT

    LED 4 LED 3 LED 2 LED 1

    0 0 0 0

    0 0 0 1

    0 0 1 0

    0 0 1 1

    0 1 0 0

    0 1 0 1

    0 1 1 0

    0 1 1 1

    1 0 0 0

    1 0 0 1

    1 0 1 0

    1 0 1 1

    1 1 0 0

    1 1 0 1

    1 1 1 0

    1 1 1 1

  • 7/22/2019 I SEM Lab - 8051, AVR

    58/138

    ISIS SIMULATION OUTPUT

    Figure 4.3 ISIS Stimulation of Upcounter Implementation Using 8051 Microcontroller

    HARDWARE OUTPUT

    Figure 4.4 Hardware Output of Upcounter Implementation Using 8051 Microcontroller

  • 7/22/2019 I SEM Lab - 8051, AVR

    59/138

    4.1 KEYPAD INTERFACING USING 8051 MICROCONTROLLER

    CIRCUIT DIAGRAM

    Figure 4.1.1 Circuit Diagram of Keypad Interfacing

    CODING

    #include

    sbit led0=P1^0;

    sbit led1=P1^1;

    sbit led2=P1^2;

    sbit led3=P1^3;

    sbit led4=P1^4;

    sbit led5=P1^5;

    sbit led6=P1^6;

    sbit led7=P1^7;

    sbit col1=P3^0;

    sbit col2=P3^1;

  • 7/22/2019 I SEM Lab - 8051, AVR

    60/138

    sbit col3=P3^2;

    sbit row1=P2^0;

    sbit row2=P2^1;

    sbit row3=P2^2;

    void delay(unsigned int msec) //Time delay function

    {

    int i,j ;

    for(i=0;i

  • 7/22/2019 I SEM Lab - 8051, AVR

    61/138

    delay(100);

    led1=1;

    delay(100);

    led2=1;

    delay(100);

    led0=led1=led2=0;

    delay(100);

    break;

    case 4:

    led0=1;

    delay(100);

    led1=1;

    delay(100);

    led2=1;

    delay(100);

    led3=1;

    delay(100);

    led0=led1=led2=led3=0;

    delay(100);

    break;

    case 5:

    led0=1;

    delay(100);

    led1=1;

    delay(100);

    led2=1;

    delay(100);

    led3=1;

  • 7/22/2019 I SEM Lab - 8051, AVR

    62/138

    delay(100);

    led4=1;

    delay(100);

    led0=led1=led2=led3=led4=0;

    break;

    case 6:

    led0=1;

    delay(100);

    led1=1;

    delay(100);

    led2=1;

    delay(100);

    led3=1;

    delay(100);

    led4=1;

    delay(100);

    led5=1;

    delay(100);

    led0=led1=led2=led3=led4=led5=0;

    break;

    case 7:led0=1;

    delay(100);

    led1=1;

    delay(100);

    led2=1;

    delay(100);

    led3=1;

    delay(100);

  • 7/22/2019 I SEM Lab - 8051, AVR

    63/138

    led4=1;

    delay(100);

    led5=1;

    delay(100);

    led6=1;

    delay(100);

    led0=led1=led2=led3=led4=led5=led6=0;

    break;

    case 8:

    led0=1;

    delay(100);

    led1=1;

    delay(100);

    led2=1;

    delay(100);

    led3=1;

    delay(100);

    led4=1;

    delay(100);

    led5=1;

    delay(100);

    led6=1;

    delay(100);

    led7=1;

    delay(100);

    led0=led1=led2=led3=led4=led5=led6=led7=0;

    break;

    }

  • 7/22/2019 I SEM Lab - 8051, AVR

    64/138

    }

    void check_col1() //Function for checking column one

    {

    row1=row2=row3=1;

    row1=0;

    if(col1==0)

    display(1);

    row1=1;

    row2=0;

    if(col1==0)

    display(2);

    row2=1;

    row3=0;

    if(col1==0)

    display(3);

    }

    void check_col2() //Function for checking column two

    {

    row1=row2=row3=1;

    row1=0;

    if(col2==0)

    display(4);

    row1=1;

    row2=0;

    if(col2==0)

    display(5);

    row2=1;

    row3=0;

  • 7/22/2019 I SEM Lab - 8051, AVR

    65/138

    if(col2==0)

    display(6);

    }

    void check_col3() //Function for checking column three

    {

    row1=row2=row3=1;

    row1=0;

    if(col3==0)

    display(7);

    row1=1;

    row2=0;

    if(col3==0)

    display(8);

    }

    void main()

    {

    P1=0x00;

    col1=col2=col3=1; //Input Port

    while(1)

    {

  • 7/22/2019 I SEM Lab - 8051, AVR

    66/138

    if(col1==0)

    check_col1();

    else

    if(col2==0)

    check_col2();

    else

    if(col3==0)

    check_col3();

    }

    }

    ISIS SIMULATION OUTPUT

    Figure 4.1.2 ISIS Simulation of Keypad Interfacing With LED

  • 7/22/2019 I SEM Lab - 8051, AVR

    67/138

    EX:NO: 5

    DATE:

    DISPLAYING THE POSITION OF KEY PRESSED USING 7 SEGMENT

    DISPLAY

    AIM

    To display the position of the key pressed using 7 segment display in the

    microcontroller

    COMPONENTS REQUIRED

    Microcontroller P89V51RD2-1

    Development board

    Bread board

    Universal programmer

    connecting wires

    Keypad-1

    BCD to 7 segment Decoder, IC 7447-2

    7 Segment display(common anode type)-2

    AND 74LS21-1

    ALGORITHM

    Initializing port 2 as output port and port 1 as input port by giving P1=0x0F

    External interrupt pin is enabled.

    When the interrupt occurred is detected by pressing the key, external interrupt

    is disabled, in order to avoid any interrupt until the key press is detected

    Flag is also set (flag=0xff) to indicate that there is an external interrupt

    Now scanning of keypad starts to detect the position of key pressed

    Initially scancode value is given as 0xef and this value is assigned to port1 and

    keycode

  • 7/22/2019 I SEM Lab - 8051, AVR

    68/138

    The following two conditions are checked if scancode value is not equal to

    0xff;

    a) ((keycode&0x0f)!=0x0f); if this condition is true, no change in the

    keycode value and detects the column value

    b) else the following operation is done

    scancode=(keycode

  • 7/22/2019 I SEM Lab - 8051, AVR

    69/138

    FLOWCHART

    NO

    YES

    YES

    NO

    NO

    YES

    START

    Initialize

    IE=0; EX1=1; EA=1

    If key

    press

    Flag=0xff

    INT1

    =1

    EX1=0,Scancode=0XEF

    If

    scancode!=

    0xXFF

    P1=Scancode

    Keycode=p1

  • 7/22/2019 I SEM Lab - 8051, AVR

    70/138

    YES

    NO

    NO

    YES

    NO

    YES

    Figure 5.1: Flowchart to scan the position of key pressed by making keypad as external

    interrupt

    (keycode&0x0f)

    !=0x0f

    scancode=(keycode>1)|0x0f

    Keycode=~keycode

    P2=keycode

    P1=0x0F

    INT1==1

    Flag=~flag

    Flag==0

    EXT=1

    END

    C

  • 7/22/2019 I SEM Lab - 8051, AVR

    71/138

    CIRCUIT DIAGRAM

    Figure 5.2: Circuit diagram

    displays

    to display the position of key pressed using 7 segment

  • 7/22/2019 I SEM Lab - 8051, AVR

    72/138

    CODING

    #include

    #include

    #define byte unsigned char

    //Interrupt service routine

    Void keyinterrupt() interrupt 2

    {

    int t;

    byte keycode, scancode, flag=0xff;

    t=5000;

    while(t--);

    if(INT1==1)

    return;

    EX1=0;

    scancode=0xef;

    while (scancode!=0xff)

    {

    P1=scancode;

    Keycode=P1;

    if((keycode&0x0f)!=0x0f)

    break;

    scancode=(keycode

  • 7/22/2019 I SEM Lab - 8051, AVR

    73/138

    P1=0X0F;

    while(1)

    {

    if(INT1==1)

    {

    flag=~flag;

    if(flag==0)

    break;

    }

    t=10000;

    while(t--);

    }

    EX1=1;

    return;

    }

    Void main(void)

    {

    IE=0;

    EX1=1;

    EA=1;

    P2=0XFF;

    P1=0X0F;

    while(1)

    {

    }}

  • 7/22/2019 I SEM Lab - 8051, AVR

    74/138

    ISIS SIMULATON OUTPUT

    Figure 5.3: ISIS simulation output for displaying the position of key pressed using the

    seven segment display

    RESULT

    Thus, keypad is used as the external interrupt and the position of the key pressed is

    displayed using two 7 segment display and verified

  • 7/22/2019 I SEM Lab - 8051, AVR

    75/138

    EX.NO: 6

    DATE:

    CONTROL FOUR LED USING SWITCH IN 8051 MICROCONTROLLER

    AIM

    To control four LED in different sequence using switch in microcontroller P89V51RD2.

    COMPONENTS REQUIRED

    Microcontroller P89V51RD2

    Universal programmer

    LED-4

    Switch-1

    Connecting wires

    ALGORITHM

    Initialize port1 as output port by assigning 0 to port1

    Initialize pin3.4(T0-timer0 input) of port3 as input port by assigning 1 to pin3.4

    TMOD(Timer/counter mode control register) is an 8-bit register, both timers 0 and 1 use

    the same register to set various timer operation mode.

    Set TMOD with value 0x16.

    Timer0 is used as counter with mode2 8-bit auto-reload.Mode2 is an 8-bit timer it allows

    only values of 00 to FFH to be loaded into the timers register TH0.After TH0 is loaded

    with the 8-bit value the 8051 gives a copy of it to TL0.

    Set values in Timer0 (16-bit register) as (timer0 low byte register )TL0=0x0FB and(

    high byte register)TH0=0x0FB and set TR0=1(Timer0 run control bit) to start the counter

    Within infinite while loop different sequence for each input count(TL0) is defined using

    switch case. Each value in each sequence is assigned to output port1 with a delay of 2

    seconds.

    Timer1 is used as timer with mode1 (16-bit timer) for generating delay. Initiate for loop

    with i=0.

    Set values in Timer1(16-bit register) as TL1=0x0A5 and TH1=0x0FE.start Timer1 by

    setting TR1=1(Timer1 run control bit is ON) and continuously monitor timer1 overflow

    flag (TF1).If TF1==1 then, clear by setting TF1=0 and TR1=0(Timer1 run control bit is

    OFF)and, increment i in for loop.

    Repeat the aboveuntil i

  • 7/22/2019 I SEM Lab - 8051, AVR

    76/138

    FLOWCHART

    Figure 6.1 flowchart for controlling four LED using switch in microcontroller

    Start

    Initialize port1 as output P1=0x0000;

    Initialize pin3.4 (T0) as input T0=1;

    Initialize TMOD=0x016, TL0=0xFB, TH0=0xFB and, set

    TR0=1to start counter

    Interrupt using switch connected to T0

    if(TL0==0Xfc)

    if(TL0==0Xfd)

    if(TL0==0Xff)

    if(TL0==0Xfe)

    P1=0x01;delay();

    P1=0x03;delay();

    P1=0x05;delay()

    P1=0x09; delay();

    P1=0x02;delay();

    P1=0x03;delay();

    P1=0x06;delay();

    P1=0x0A; delay();

    P1=0x04;delay();

    P1=0x05;delay();

    P1=0x06;delay();P1=0x

    0C; delay();

    P1=0x08;delay();P1=0x09;delay();

    P1=0x0A;delay();

    P1=0x0C;delay();

    Yes

    NO

    Yes

    NO

    Yes

    NO

    YesNO

  • 7/22/2019 I SEM Lab - 8051, AVR

    77/138

    Figure 6.2 Flowchart for delay

    Set TL1=0x0A5 and

    TH1=0x0FE

    Set TR1=1

    while(TF1==0);

    Yes

    TR1=0; TF1=0;

    increment i;

    i=0

    i

  • 7/22/2019 I SEM Lab - 8051, AVR

    78/138

    CIRCUIT DIAGRAM

    Figure 6.3 Circuit diagram to control four LED using switch in microcontroller

  • 7/22/2019 I SEM Lab - 8051, AVR

    79/138

    CODING

    #include

    void delay();

    void main(void)

    {P1=0X00;

    T0=1;

    TMOD=0x16;

    TL0=0xFB;

    TH0=0xFB;

    TR0=1;

    while(1)

    {

    switch(TL0)

    {

    case 0xfc:P1=0x01;

    delay();

    P1=0x03;

    delay();

    P1=0x05;

    delay();

    P1=0x09;

    delay();

    break;

    case 0xfd:

    P1=0x02;delay();

    P1=0x03;

    delay();

    P1=0x06;

    delay();

    P1=0x0A;

    delay();

    break;

    case 0xfe:

    P1=0x04;

    delay();

    P1=0x05;

    delay();

    P1=0x06;

    delay();

    P1=0x0C;

    delay();

    break;

    case 0xff:

    P1=0x08;

    delay();P1=0x09;

  • 7/22/2019 I SEM Lab - 8051, AVR

    80/138

    delay();

    P1=0x0A;

    delay();

    P1=0x0C;

    delay();

    break;}

    }

    }

    void delay()

    {

    inti;

    for(i=0;i

  • 7/22/2019 I SEM Lab - 8051, AVR

    81/138

    SAMPLE OUTPUT

    Figure 6.4 ISIS simulation output control four led using switch in 8051 microcontroller

    HARDWARE OUTPUT

    Figure 6.5 Hardware Implementation of Control four led using Switch in 8051

    Microcontroller

  • 7/22/2019 I SEM Lab - 8051, AVR

    82/138

    SAMPLE OUTPUT

    TL0=0XFC TL0=0XFD

    TL0=0XFE TL0=0XFF

    RESULT

    Thus four LEDs in different sequence using switch in microcontroller P89V51RD2 was

    implemented and verified.

    LED

    1 2 3 4

    1 0 0 0

    1 1 0 0

    1 0 1 0

    1 0 0 1

    LED

    1 2 3 4

    0 1 0 0

    1 1 0 0

    0 1 1 0

    0 1 0 1

    LED

    1 2 3 4

    0 0 0 1

    1 0 0 1

    0 1 0 1

    0 0 1 1

    LED

    1 2 3 4

    0 0 1 0

    1 0 1 0

    0 1 1 0

    0 0 1 1

  • 7/22/2019 I SEM Lab - 8051, AVR

    83/138

    EXNO:7

    DATE:

    INTERFACING STEPPER MOTOR WITH 8051

    AIM

    To write a Program to Interface a stepper motor with 8051c and to rotate in clockwise and

    anti-clockwise direction using keil software.

    HARDWARE REQUIRED

    Microcontroller IC P89C51RD2XXPHILIPS1No

    Crystal Oscillator 11.0592 MHz

    Capacitor 1f, 10f, 33pf, 0.1f,150pf-6nos

    Bread board & connecting wires. Resistors 1K,8.2k,10k,4.7k-10nos

    Micro Switch1 Pin

    ULN2003-1 No

    Stepper motor

    SOFTWARE REQUIRED

    Keil vision 3

    ISIS Proteus Professional 7 Topwin VER5.49

    ALGORITHM

    Define the delay function.

    Initialize the switch, SW.

    Making the condition as an infinite loop.

    If SW = 0, motor get rotates step by step in a clockwise direction with a delay of 100 ms.

    If SW = 1, motor get rotates step by step in a counter-clockwise direction with a delay of

    100 ms. This process gets repeated until the power off.

  • 7/22/2019 I SEM Lab - 8051, AVR

    84/138

    FLOWCHART

    Yes No

    Figure 7.1 Flow Chart

    Start

    Define delay function (MSDelay)

    Set port pin for switch (SW)

    Initialize switch, SW=1

    While(1)

    If

    SW==0

    Assign

    P1=0x06

    Assign

    P1=0x33

    Assign

    =

    Assign

    P1=0xcc

    Assign

    =

    MSDelay(100)

    Assign

    P1=0xcc

    Assign

    P1=0x99

    Assign

    P1=0x33

    MSDelay(100)

    MSDelay(100)MSDelay(100)

    MSDelay(100) MSDelay(100)

    MSDelay(100) MSDelay(100)

  • 7/22/2019 I SEM Lab - 8051, AVR

    85/138

    CIRCUIT DIAGRAM

    Fig 7.7:STEPPER MOTOR INTERFACE

    Figure 7.2 Circuit Diagram of Stepper Motor Interface

    PROCEDURE

    1. Write the stepper motor coding for (clock wise rotation) in keil software and generate a

    hex code.

    2. Dump the hex code in the controller chip using flash magic software.

    3. Give the circuit connections as given above.

    4. Interface the 8051 controller, ULN2003 and the stepper motor.5. Switch on the power supply and view the clock-wise rotation of stepper motor.

    6. Repeat the steps 1-5 for anti-clock-wise rotation of the motor.

    To

    ULN2003

    5V

    4.7K

  • 7/22/2019 I SEM Lab - 8051, AVR

    86/138

    STEP SEQUENCES

    Normal 4-Step Sequence (90 )

    CLOCK

    WISE

    Step # Winding A Winding B Winding

    C

    Winding

    D

    Counter

    Clockwise

    1 1 0 0 1

    2 1 1 0 0

    3 0 1 1 0

    4 0 0 1 1

    Table 7.1 4-Step Sequence

    STEP ANGLE

    Step angle Steps per revolution

    0.72 500

    1.8 200

    2.0 180

    2.5 144

    5.0 72

    7.5 48

    15 24

    Table 7.2 Step Angle

  • 7/22/2019 I SEM Lab - 8051, AVR

    87/138

    CODING

    #include

    void MSDelay(unsigned int value);

    sbit SW=P2^7;

    void main(){

    SW=1;

    while(1)

    {

    if(SW==0)

    {

    P1=0x06;

    MSDelay(100);

    P1=0x0c;

    MSDelay(100);

    P1=0x09;MSDelay(100);

    P1=0x03;

    MSDelay(100);

    }

    else

    {

    P1=0x06;

    MSDelay(100);

    P1=0x03;

    MSDelay(100);

    P1=0x09;MSDelay(100);

    P1=0x0c;

    MSDelay(100);

    }

    }

    }

    void MSDelay(unsigned int value)

    {

    unsigned int x,y;

    for(x=0;x

  • 7/22/2019 I SEM Lab - 8051, AVR

    88/138

    ISIS SIMULATION OUTPUT

    Figure 7.3 Simulation of Stepper Motor Interfacing

    HARDWARE SNAP SHOT

    Figure 7.4 Interfacing Stepper Motor

  • 7/22/2019 I SEM Lab - 8051, AVR

    89/138

    RESULT

    Thus the program is written to interface stepper motor with 8051c was verified and

    executed.

  • 7/22/2019 I SEM Lab - 8051, AVR

    90/138

    Ex No:8

    Date:

    INTERFACING ADC0804 WITH TEMPERATURE SENSOR

    AIM

    To design and verify the ADC 0804 conversion of data given, using Philips AT89C5

    Microcontroller with temperature sensing as a parameter.

    COMPONENTS REQUIRED

    Philips AT89C51RD2XX Microcontroller

    ADC 0804

    LM 35 Temperature sensor

    Resistor - 10 K ohm

    Capacitor- 150 Pico farad

    LCD Display

    Kiel Simulator

    ISIS Emulator

    BLOCK DIAGRAM

    Figure 8.1 Block Diagram of the Experiment

  • 7/22/2019 I SEM Lab - 8051, AVR

    91/138

    CIRCUIT DIAGRAM

    Figure 8.2 Circuit Diagram

  • 7/22/2019 I SEM Lab - 8051, AVR

    92/138

    ALGORITHM

    1. START

    2. Select the specific ports of microcontroller.

    3. Initialize the interrupts.

    4. Move the 8-bit data into ACC and store the data from accumulator into any register

    5. Write the commands of LCD display for output data.

    6. Move again data from register to accumulator for division operation.

    7. Convert the data from ASCII values.

    8. Display the data through ADC output pins.

    9. Generate delay where ever necessary.

    10. STOP

    ALGORITHM BETWEEN THE DATA FROM LM35 AND ADC0804

    1. START

    2. Temperature sensors LM35 sense the output temperature.

    3. Initialize the CS=0 pin and low to high transition, to enable the data to be write to

    ADC, which starts the conversion.

    4. Keep the INTR pin always low to ensure that process of conversion is finished.

    5. Initialize the CS=1 pin and pass high to low transition, to read the data at output of

    ADC.

    6. STOP

  • 7/22/2019 I SEM Lab - 8051, AVR

    93/138

    CODING

    #include

    //#define port P3

    #define adc_input P1

    //#define dataport P0

    #define sec 100

    sbit rs = P3^0;

    sbit rw = P3^1;

    sbit e = P3^2;

    sbit wr= P3^3;

    sbit rd= P3^4;

    sbit intr= P3^5;

    int a,b,c;

    void delay(unsigned int msec );

    void lcd_cmd(unsigned int item);

    void lcd_data(unsigned int item);

    void lcd_data_string(unsigned char *str);

    void delay(unsigned int msec)

    {

    int i ,j ;

    for(i=0;i

  • 7/22/2019 I SEM Lab - 8051, AVR

    94/138

    for(j=0; j

  • 7/22/2019 I SEM Lab - 8051, AVR

    95/138

    {

    P2 = item;

    rs= 1;

    rw=0;

    e=1;

    delay(1);

    e=0;

    //delay(100);

    return;

    }

    void main()

    {

    //adc_input=0xff;

    lcd_cmd(0x38);

    lcd_cmd(0x0E); //Display On, Cursor Blinking

    lcd_cmd(0x01); // Clear Screen

    lcd_cmd(0x06);

    lcd_cmd(0x80);

    rd=1;

    wr=0;

    wr=1;

    while(intr==1);

  • 7/22/2019 I SEM Lab - 8051, AVR

    96/138

    rd=0;

    a=adc_input;

    delay(1);

    intr=1;

    b=0x30+(a/0x0A);

    c=0x30+(a%0x0A);

    lcd_data(b);

    lcd_data(c);

    }

  • 7/22/2019 I SEM Lab - 8051, AVR

    97/138

    FLOW CHART

    Figure 8.3 Implementation of Interfacing ADC with Temperature Sensor

  • 7/22/2019 I SEM Lab - 8051, AVR

    98/138

    PROTOTYPE

    ISIS OUTPUT

    Figure 8.5 Screen Shot of ISIS Emulator

    INTERFACING LCD

    Figure 8.4 Prototype of Experiment

  • 7/22/2019 I SEM Lab - 8051, AVR

    99/138

    RESULT

    Thus the Analog-Digital conversion using 8051 was successfully completed and the analog dataswere converted to its digital values.

  • 7/22/2019 I SEM Lab - 8051, AVR

    100/138

    EX.NO:9

    DATE:

    DIGITAL CLOCK

    AIM

    To implement digital clock using seven segment display in microcontrollerP89V51RD2.

    COMPONENTS REQUIRED

    MicrocontrollerP89V51RD2

    Universal programmer

    Seven segment display-6

    Switch-2

    4x4 Keypad

    Connecting wires

    ALGORITHM

    Initialize Port 2(P2.0-P2.5) as enablepins to 6 seven segments.

    Set Port3 for keypad scanning.

    Initialize P2.6 and P2.7 as input which is used as switch for set mode and run mode

    Initialize timer0 and timer1 in timer mode.

    Set TL0=0X99 & TH0=0X0f1 to generate 0.004 second and set IE=0x82 to enabletimer0 interrupt and start timer0(TR0=1)

    If run=0;within infinite loop clock starts running from 00:00:00

    If set=0;enter second, minute, hour using keypad and assigned to s0,s1,m0,m1,h0 and

    h1.

    Within infinite loop clock starts running from the value i.e., set via keypad

    Set TL1=0X0FD & TH1=0X4B to generate 50ms and repeat using for loop for about

    20 times to generate one second

    Each value in second is incremented with time delay of one second using timer1

    When timer0 overflow ,it set TF0=1 which is raised as interrupt then executes

    interrupt routine

  • 7/22/2019 I SEM Lab - 8051, AVR

    101/138

    Within interrupt routine, again reload TL0=0x36 & TH0=0x0F6 and post increment

    count variable

    Then depending upon count ,it enable one of the 6 seven segment and display the

    content in s/m/h variable

    FLOWCHART

    Figure9.1 Flowchart for digital clock

    START

    Initialize P2.0-P2.5 as enable pins to 6 seven

    segments.Set Port3 for keypad scanning.

    Initialize P2.6 and P2.7 as input connected to

    switch for set mode and run mode

    Initialize timer0 and timer1 in timer modeSet

    TL0=0X99 & TH0=0X0f1set IE=0x82 and set

    TR0=1

    if (run=0)

    if (set=0)

    Start from 00:00:00

    Enter s0, s1, m0, m1, h0, h1 using keypad andset values in for loop and starts from set value

    callclockdelay()

    & increment

    callclockdelay() &

    increment

    YES

    YES

    NO

    NO

  • 7/22/2019 I SEM Lab - 8051, AVR

    102/138

    Figure9.2 Flowchart for display

    Start ISR

    Set TL0=0X99 & TH0=0X0f1

    and initialize P1=0xff

    Set P2.0 toP2.5 as zero.

    Count++

    Count=count%6

    If count=0

    If count=1

    If count=2

    If count=3

    If count=4

    P1=digi[s0];

    P2.0 = 1;

    P1=digi[s1]; P2.1

    = 1; break;

    P1=digi[m0];

    P2.2 = 1;

    break;

    P1=digi[m1];

    P2.3 = 1;

    break;

    If count=5

    P1=digi[h0]; P2.4

    = 1; break;

    P1=digi[h1];

    P2.5 = 1;

    break;

    YES

    NO

    YES

    YES

    YES

    YES

    YES

    NO

    NO

    NO

    NO

    Return

  • 7/22/2019 I SEM Lab - 8051, AVR

    103/138

    Figure9.3 Flowchart for one second delay

    START

    clockdela

    Set TL1=0x0FD and

    TH1=0x4B

    Set TR1=1

    while(TF1==0);

    Yes

    TR1=0; TF1=0;

    increment i;

    i=0

    i

  • 7/22/2019 I SEM Lab - 8051, AVR

    104/138

    CIRCUIT DIAGRAM

    Figure9.4 Digital clock circuit diagram

  • 7/22/2019 I SEM Lab - 8051, AVR

    105/138

    CODING

    #include

    voidclockdelay();

    unsigned char count=0;

    sbit enable_1 = P2^0;

    sbit enable_2 = P2^1;

    sbit enable_3 = P2^2;

    sbit enable_4 = P2^3;

    sbit enable_5 = P2^4;

    sbit enable_6 = P2^5;

    sbit set = P2^6;

    sbit run = P2^7;

    sbit r1 = P3^0;

    sbit r2 = P3^1;

    sbit r3 = P3^2;

    sbit r4 = P3^3;

    sbit c1 = P3^4;

    sbit c2 = P3^5;

    sbit c3 = P3^6;

    sbit C4 = P3^7;

    voiddisp(int );

    int keypad(void);

    intgetkey();

    unsigned char digi[]={0x0c0,0x0f9,0x64,0x70,0x59,0x52,0x42,0x0f8,0x40,0x50,0x46};

  • 7/22/2019 I SEM Lab - 8051, AVR

    106/138

    int s0,s1,m0,m1,h0,h1;

    inta,b,c,d,e,f;

    int flag=0;

    void main()

    {

    TMOD=0x11;

    TL0=0x99;

    TH0=0x0f1;

    IE=0x82;

    TR0=1;

    if(set==0)

    {

    while(set==0);

    do{

    flag=0;

    a=getkey();

    if(a>=10)

    { a=10;flag=1;}

    s0=a;

    }while(flag==1);

    do{

    flag=0;

    b=getkey();

  • 7/22/2019 I SEM Lab - 8051, AVR

    107/138

    if(b>5)

    { b=10;flag=1;}

    s1=b;

    }while(flag==1);

    do{

    flag=0;

    c=getkey();

    if(c>=10)

    { c=10;flag=1;}

    m0=c;

    }while(flag==1);

    do{

    flag=0;

    d=getkey();

    if(d>5)

    {d=10;flag=1;}

    m1=d;

    }while(flag==1);

    do{

    flag=0;

    e=getkey();

  • 7/22/2019 I SEM Lab - 8051, AVR

    108/138

    if(e>=10)

    {e=10;flag=1;}

    h0=e;

    }while(flag==1);

    do{

    flag=0;

    f=getkey();

    if(e>3&&e=2)

    { f=10;flag=1;}

    }

    if(e2)

    {f=10;flag=1;}

    }

    h1=f;

    }while(flag==1);

    while(1)

    {for(h1=f;h1

  • 7/22/2019 I SEM Lab - 8051, AVR

    109/138

    {for(s1=b;s1

  • 7/22/2019 I SEM Lab - 8051, AVR

    110/138

    {

    for(s1=b;s1

  • 7/22/2019 I SEM Lab - 8051, AVR

    111/138

    {

    for(h0=0;h0

  • 7/22/2019 I SEM Lab - 8051, AVR

    112/138

    {

    for(m0=0;m0

  • 7/22/2019 I SEM Lab - 8051, AVR

    113/138

    while(TF1==0);

    TR1=0;

    TF1=0;

    }

    }

    voidclockdisp() interrupt 1

    {

    TL0=0x99;

    TH0=0xf1;

    P1=0xff;

    enable_1 = enable_2 = enable_3 = enable_4 = enable_5 = enable_6 = 0;

    count++;

    count=count%6;

    switch(count)

    {

    case 0:

    P1=digi[s0];

    enable_1 = 1;

    break;

    case 1:

    P1=digi[s1];

    enable_2 = 1;

    break;

  • 7/22/2019 I SEM Lab - 8051, AVR

    114/138

    case 2:

    P1=digi[m0];

    enable_3 = 1;

    break;

    case 3:

    P1=digi[m1];

    enable_4 = 1;

    break;

    case 4:

    P1=digi[h0];

    enable_5 = 1;

    break;

    case 5:

    P1=digi[h1];

    enable_6 = 1;

    break;

    }

    }

    intgetkey()

    { int key=99;

    while(key==99)

  • 7/22/2019 I SEM Lab - 8051, AVR

    115/138

    key=keypad();

    return(key);

    }

    int keypad(void)

    { r1=0; r2=1;r3=1; r4=1;

    if(c1==0){ while(c1==0); return 7; }

    if(c2==0){ while(c2==0); return 8; }

    if(c3==0){ while(c3==0); return 9; }

    r1=1; r2=0; r3=1; r4=1;

    if(c1==0){ while(c1==0); return 4;}

    if(c2==0){ while(c2==0); return 5;}

    if(c3==0){ while(c3==0); return 6;}

    r1=1;r2=1;r3=0;r4=1;

    if(c1==0){ while(c1==0); return 1;}

    if(c2==0){ while(c2==0); return 2;}

    if(c3==0){ while(c3==0); return 3;}

    r1=1;r2=1;r3=1;r4=0;

    if(c2==0){ while(c2==0); return 0;}

    return 99;

    }

  • 7/22/2019 I SEM Lab - 8051, AVR

    116/138

    SAMPLE OUTPUT

    Figure 9.5 ISIS simulation output

    RESULT

    Thus digital clock using seven segment display in microcontroller P89V51RD2 was

    simulated using ISIS and verified

  • 7/22/2019 I SEM Lab - 8051, AVR

    117/138

    EX. NO:10

    DATE : OBSTACLE DETECTOR USING IR SENSOR

    AIM:

    To design and implement obstacle detection by interfacing LCD and IR sensor using 8051

    microcontroller.

    Hardware Required:

    16X2 Line LCD (LM016L).

    IR Sensor.

    PHILIPS P89v51RD2XX.

    TOP2048 Programmer kit.

    Power supply(5v).

    Connecting wires.

    Software Required:

    Keil uvision 3.

    ISIS Proteus Professional 7.

    Topwin VER5.49.

    IR (Infrared) Sensor

    The basic concept of IR(infrared) obstacle detection is to transmit the IR signal(radiation) in a

    direction and a signal is received at the IR receiver when the IR radiation bounces back

    from a surface of the object. Here in the figure the object can be any thing which has certain

    shape and size, the IR LED transmits the IR signal on to the object and the signal is reflected

    back from the surface of the object. The reflected signals is received by an IR receiver. The

    IR receiver can be a photodiode / phototransistor or a ready made module which decodes the

    signal.

    Fig 10.1 IR Sensor

  • 7/22/2019 I SEM Lab - 8051, AVR

    118/138

    IR Transmitter

    In general, the basic building block of any IR transmitter is modulation of the information

    signal with carrier signal, because the receiver modules which are available off-the-shelf are

    made for a particular carrier frequency. So it is clear that when you chose a particular IR

    receiver module, you also need to transmit the the modulated wave with the same carrierfrequency of that of a IR receiver module.

    IR Receiver

    It is quite simple to construct a IR receiver with readily available off-the-shelf modules.

    These modules are nothing but the IC packages, referred as TSOP(Thin small-outline

    package). In this document, the receiver is designed for 38 kHz carrier signal, hence the IC

    selected should work for the same frequency. The IC TSOP4838 will serve as a receiver

    module, which is compatible with both TTL and CMOS logic. This means that we can

    directly get digital signal from the receiver module and then connect it to the microcontroller.

    ALGORITHM:

    Initialize the Port1 for data pins of LCD.

    Initialize the P2.4 for RS ,P2.5 for enable and P2.6 for RW pins of LCD.

    Initialize the Port 2.7 for output pin of IR SENSOR.

    Declare function delay(), lcdcmd(), lcddata(), lcddata_string() and IRSENSOR().

    Call the command function lcdcmd() to send the commands to LCD

    Call the IRSENSOR() function to check the obstacle present

    If any obstacle present means the IR (P 2.7 ) output will be high signal and then

    lcddata_string() will be called to display obstacle in LCD

    Else the IR (P 2.7 ) output will be low signal and then lcddata_string() will be called

    to display no obstacle in LCD

  • 7/22/2019 I SEM Lab - 8051, AVR

    119/138

    Flow chart

    Start

    Initialize the Port1 for data pins of LCD

    Initialize the P2.4 for RS ,P2.5 for enable and

    Initialize the Port 2.7 for output pin of IR

    Call the command function

    lcdcmd

    Call the

    IRSENSOR

    If output is

    high?

    Display Obstacle in LCD

    Display no Obstacle in LCD

    YES

    NO

    Stop

  • 7/22/2019 I SEM Lab - 8051, AVR

    120/138

    Program:

    #include

    #define cmdport P2

    #define dataport P1

    sbit rs = cmdport^4; //register select pin

    sbit rw = cmdport^6; // read write pin

    sbit e = cmdport^5; //enable pin

    sbit IR = cmdport^7;

    void IRSENSOR();

    void delay(unsigned int msec) // Function to provide time delay in msec.

    {

    int i,j ;

    for(i=0;i

  • 7/22/2019 I SEM Lab - 8051, AVR

    121/138

    void lcddata(unsigned char item) //Function to send data to LCD

    {

    dataport = item;

    rs= 1;

    rw=0;

    e=1;

    delay(1);

    e=0;

    }

    void lcddata_string(unsigned char *str) // Function to send string to LCD

    {

    int i=0;

    while(str[i]!='\0')

    {

    lcddata(str[i]);

    i++;

    delay(1);

    }

    }

    void main()

    {

    lcdcmd(0x38); // for using 8-bit 2 row mode of LCD

    delay(100);

    lcdcmd(0x0E); // turn display ON for cursor blinking

    delay(10);

  • 7/22/2019 I SEM Lab - 8051, AVR

    122/138

    lcdcmd(0x01); //clear screen

    delay(10);

    lcdcmd(0x06); //display ON

    delay(10);

    lcdcmd(0x80); // bring cursor to position 6 of line 1

    delay(100);

    IRSENSOR();

    }

    void IRSENSOR ()

    {

    if(IR==0)

    {

    lcddata_string("no obstacle");

    }

    else

    {

    lcddata_string("obstacle ");

    }

    }

  • 7/22/2019 I SEM Lab - 8051, AVR

    123/138

    CIRCUIT DIAGRAM:

    Fig 10.3 IR Sensor Using 8051 Microcontroller

    SAMPLE OUTPUT:

  • 7/22/2019 I SEM Lab - 8051, AVR

    124/138

    Fig 10.4 Output of IR Sensor when no obstacle present

    Fig 10.5 Output of IR Sensor when obstacle present

    RESULT:

    Therefore, obstacle detection by interfacing LCD and IR sensor using 8051 microcontroller.

    has been executed successfully.

  • 7/22/2019 I SEM Lab - 8051, AVR

    125/138

    EX NO:11

    DATE:

    INTERFACING LED WITH AVR

    AIM

    To write a program for interfacing LED with AVR microcontroller to Blink by using AVRstudio.

    COMPONENTS REQUIRED

    Microcontroller IC Atmega 16

    Max 232 IC & 9pin RS232 Female connector

    Bread board and connecting wires

    Resistors 1K ohm,

    Crystal oscillator 8 MHz

    Capacitor 1F, 10 F, 33pF

    LED 3mm - 8

    THEORY

    AVR MICROCONTROLLER

    The AVR is a modified Harvard architecture 8-bit RISC single chip. The AVR was one of the

    first microcontroller families to use on-chip flash memory for program storage, as opposed

    to one-time programmable ROM, EPROM, or EEPROM used by other microcontrollers at thetime. Among the first of the AVR line was the AT90S8515, which in a 40-pin DIP package has

    the same pin out as an 8051 microcontroller, including the external multiplexed address and data

    bus. The polarity of the RESET line was opposite (8051's having an active-high RESET, whilethe AVR has an active-low RESET) but other than that, the pin out was identical.

  • 7/22/2019 I SEM Lab - 8051, AVR

    126/138

    PIN DIAGRAM

    FIG 11.1 Pin Diagram of ATMEGA16S

    PIN DESCRIPTION

    ATmega16 Microcontroller Pin Description

    Pin 40Vcc

    Vcc provides supply voltage to the chip.

    The voltage source is +5V.

    Pin 20Ground

    Pins 19,18 : XTAL1 and XTAL

    Pin 9Reset

    It is an input pin and is active high (normally low.

    The high pulse must be high at least 2 machine cycles.

    FIG 11.2 Crystal Oscillator

  • 7/22/2019 I SEM Lab - 8051, AVR

    127/138

    CIRCUIT DIAGRAM

    FIG 11.3 Interfacing LEDS with ATMEGA16

    CIRCUIT CONNECTION

    Give the Vcc (5V) supply to the 40th

    pin and ground to the 20th

    pin of microcontroller.

    Give the reset pin connections (reset switch, 10uF and 10k resistor) to the 9th

    pin of the

    controller as per the circuit diagram. Give the oscillator connections (8 MHz crystal oscillator and two 33pF capacitors) as per the

    circuit diagram.

    Connect the pins PB0 - PB7 to data line

    PROGRAM

    #include int main(void)

    {

    int a=0xFF, i=0;

    DDRB=0xFF;while(1)

    {

    for(int j=0; j

  • 7/22/2019 I SEM Lab - 8051, AVR

    128/138

    PROCEDURE

    Write the seven segment coding for display in AVR Studio software and generate a hexcode.

    Give the circuit connections as given above.

    Interface the AVR microcontroller and LED.

    Switch on the power supply and view the LED blinking.

    ISIS EMULATION

    FIG 11.4 Simulation Result of LED Interfacing

    RESULT

    Thus the program is written to interface LED with AVR microcontroller is verified and executed.

  • 7/22/2019 I SEM Lab - 8051, AVR

    129/138

    EX NO: 12

    DATE:

    PROGRAMMING WITH ANALOG COMPARATOR OF AVR

    AIM

    To implement Analog Comparator of AVR.

    DESCRIPTION

    The Analog Comparator compares the input values on the positive pin AIN0 and negative pin

    AIN1. When the voltage on the positive pin AIN0 is higher than the voltage on the negative pin

    AIN1, the Analog Comparator Output, ACO, is set. The comparators output can be set to trigger

    the Timer/Counter1 Input Capture function. In addition, the comparator can trigger a separateinterrupt, exclusive to the Analog Comparator. The user can select Interrupt triggering on

    comparator output rise, fall or toggle.

    FIG 12.1 Block Diagram of Analog Comparator

  • 7/22/2019 I SEM Lab - 8051, AVR

    130/138

    SPECIAL FUNCTION IO REGISTER

    FIG 12.2 SFIOR

    Bit 3 ACME: Analog Comparator Multiplexer Enable

    When this bit is written logic one and the ADC is switched off (ADEN in ADCSRA is zero), the

    ADC multiplexer selects the negative input to the Analog Comparator. When this bit is writtenlogic zero, AIN1 is applied to the negative input of the Analog Comparator.

    ANALOG COMPARATOR CONTROL AND STATUS REGISTER

    FIG12.3 ACSR

    Bit 7 ACD: Analog Comparator Disable

    When this bit is written logic one, the power to the Analog Comparator is switched off. This bit

    can be set at any time to turn off the Analog Comparator. This will reduce power consumption inactive and Idle mode. When changing the ACD bit, the Analog Comparator Interrupt must be

    disabled by clearing the ACIE bit in ACSR. Otherwise an interrupt can occur when the bit ischanged.

    Bit 6ACBG: Analog Comparator Bandgap SelectWhen this bit is set, a fixed bandgap reference voltage replaces the positive input to the Analog

    Comparator. When this bit is cleared, AIN0 is applied to the positive input of the Analog

    Comparator.

    Bit 5ACO: Analog Comparator Output

    The output of the Analog Comparator is synchronized and then directly connected to ACO. Thesynchronization introduces a delay of 1 - 2 clock cycles.

    Bit 4 ACI: Analog Comparator Interrupt Flag

    This bit is set by hardware when a comparator output event triggers the interrupt mode definedby ACIS1 and ACIS0. The Analog Comparator Interrupt routine is executed if the ACIE bit is

    set and the I-bit in SREG is set. ACI is cleared by hardware when executing the corresponding

    interrupt handling vector. Alternatively, ACI is cleared by writing a logic one to the flag.

  • 7/22/2019 I SEM Lab - 8051, AVR

    131/138

    Bit 3ACIE: Analog Comparator Interrupt EnableWhen the ACIE bit is written logic one and the I-bit in the Status Register is set, the Analog

    Comparator Interrupt is activated. When written logic zero, the interrupt is disabled.

    Bit 2

    ACIC: Analog Comparator Input Capture EnableWhen written logic one, this bit enables the Input Capture function in Timer/Counter1 to be

    triggered by the Analog Comparator. The comparator output is in this case directly connected tothe Input Capture front-end logic, making the comparator utilize the noise canceller and edge

    select features of the Timer/Counter1 Input Capture interrupt. When written logic zero, no

    connection between the Analog Comparator and the Input Capture function exists. To make thecomparator trigger the Timer/Counter1 Input Capture interrupt, the TICIE1 bit in the Timer

    Interrupt Mask Register (TIMSK) must be set.

    Bits 1, 0ACIS1, ACIS0: Analog Comparator Interrupt Mode SelectThese bits determine which comparator events that trigger the Analog Comparator interrupt.

    TABLE 12.1 ACIS0/ACIS1 SETTING

    ANALOG COMPARATOR MULTIPLEXED INPUT

    It is possible to select any of the ADC7..0 pins to replace the negative input to the AnalogComparator. The ADC multiplexer is used to select this input, and consequently, the ADC must

    be switched off to utilize this feature. If the Analog Comparator Multiplexer Enable bit (ACME

    in SFIOR) is set and the ADC is switched off (ADEN in ADCSRA is zero), MUX2..0 inADMUX select the input pin to replace the negative input to the Analog Comparator. If ACME

    is cleared or ADEN is set, AIN1 is applied to the negative input to the Analog Comparator.

  • 7/22/2019 I SEM Lab - 8051, AVR

    132/138

    Table 12.2 Analog Comparator Multiplexed Input

    PROGRAM

    #include // Most basic include files

    #include // Add the necessary ones

    #include // here

    // Define here the global static variables

    int My_global;

    // Interrupt handler example for INT0

    //SIGNAL(SIG_INTERRUPT0) {

    }

    // It is recommended to use this coding style to

    // follow better the mixed C-assembly code in the

    // Program Memory window//

    void my_function(void) { // Put the open brace '{' here

    asm("nop"); // Inline assembly example

    }

    // ***********************************************************// Main program

    // ***********************************************************

  • 7/22/2019 I SEM Lab - 8051, AVR

    133/138

    int main(void) {

    DDRD=0X00; // ANI0,1 AS I/PDDRB=0X01; //PB0 AS O/P

    ACSR=0X00;

    while(1) { // REFLECT ACO BIT STATUS IN PB0

    if(ACSR&(1

  • 7/22/2019 I SEM Lab - 8051, AVR

    134/138

    EX NO: 13

    DATE:

    INTERFACING SEVEN SEGMENT DISPLAY WITH AVR

    AIM

    To write a program for interfacing seven segment display with AVR microcontroller to display

    the numerical value and non numerical characters by using AVR studio.

    COMPONENTS REQUIRED

    Microcontroller IC Atmega 16

    Max 232 IC & 9pin RS232 Female connector

    Bread board and connecting wires Resistors 1K ohm,

    Crystal oscillator 8 MHz

    Capacitor 1F, 10 F, 33pF

    Seven segment display 4

    THEORY

    SEVEN SEGMENT DISPLAY

    A seven-segment display (SSD), or seven-segment indicator, is a form of electronic display

    device for displaying decimal numerals that is an alternative to the more complex dot-

    matrix displays. Seven-segment displays are widely used in digital clocks, electronic meters, and

    other electronic devices for displaying numerical information.

    A seven segment display, as its name indicates, is composed of seven elements. Individually onor off, they can be combined to produce simplified representations of thearabic numerals. Often

    the seven segments are arranged in an oblique (slanted) arrangement, which aids readability. In

    most applications, the seven segments are of nearly uniform shape and size (usuallyelongated hexagons, though trapezoids and rectangles can also be used), though in the case

    of adding machines, the vertical segments are longer and more oddly shaped at the ends in an

    effort to further enhance readability.

  • 7/22/2019 I SEM Lab - 8051, AVR

    135/138

    FIG13.1 Indi

    CIRCUIT DIAGRAM

    FIG 13.2 Interfa

    vidual Segment for Seven Segment Display

    ing Seven-Segment Display with ATMEGA1

    6S

  • 7/22/2019 I SEM Lab - 8051, AVR

    136/138

    CIRCUIT CONNECTION

    Give the Vcc (5V) supply to the 40th

    pin and ground to the 20th

    pin of microcontroller.

    Give the reset pin connections (reset switch, 10uF and 10k resistor) to the 9th

    pin of the

    controller as per the circuit diagram.

    Give the oscillator connections (8 MHz crystal oscillator and two 33pF capacitors) as per thecircuit diagram.

    Connect the pins PB0 - PB7 to data line and PD0PD3 to control line of the seven segmentdisplay.

    PROGRAM

    #include // Most basic include files#include // Add the necessary ones

    #include // here

    #include

    int DATA[4]={0x60,0xda,0xf2,0x66};

    unsigned char i,j=0x01;

    SIGNAL(SIG_INTERRUPT0) {

    }

    // Main program

    int main(void) {

    DDRB=0XFF;DDRD=0XFF;

    while(1) {

    // Infinite loop;for(i=0;i

  • 7/22/2019 I SEM Lab - 8051, AVR

    137/138

    PROCEDURE

    Write the seven segment coding for display in AVR Studio software and generate a hexcode.

    Give the circuit connections as given above.

    Interface the AVR microcontroller and seven segment displays.

    Switch on the power supply and view the seven segment display.

    ISIS EMULATION

    FIG 13.3 Simulating Seven-Segment Display in ISIS

    RESULTThus the program is written to interface seven segment displays with AVR microcontroller is

    verified and executed.

  • 7/22/2019 I SEM Lab - 8051, AVR

    138/138