Micro Controller 8051_89S52 Embedded C Programming

download Micro Controller 8051_89S52 Embedded C Programming

of 59

Transcript of Micro Controller 8051_89S52 Embedded C Programming

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    1/59

    0101010101010101010101010

    010101010101010101010101010101010101010101010

    10100101010101010101010101010101010101010101010101010101

    1 01 0 10 1 00 1 01 0 10 1 01 1 0 10 1 01 0 10 1 01 0 10 1

    0 10 10 10 10 10 01 01 0 1 01 01 01 01 01 01 01

    1 010 101 010 100 10 1 010 1010 101 010 1

    01 010 101 010 100 1 10 1010 101 010 1

    01 010 101 010 100 1 10 1010 101 010 1

    010 101 010 100 10 1 010 1010 101 010 1

    1 01 01 01 01 00 10 10 1 0 10 10 10 10 10 10 10 1

    0 1 01 0 10 0 10 10 1 01 0 10 1 0 0 1 01 0 10 1 01 0 10 1 01 0 10

    1001010101010101010101010101010101010101010101010101

    0101010101010101010101010101010101010101

    0101010101010101

    0101010101010101010101010101010101010

    100101010101010101010101010101010101010101010101010

    10101001010101010101 01010101010101010101

    0 10 10 10 10 01 01 01 0 1 01 01 01 01 01 01 01 01

    010 101 010 100 101 1 010 1010 101 010

    1 010 101 010 100 1 010 1010 101 010 1

    01 010 101 010 100 1 10 1010 101 010 1

    1 010 101 010 100 1 010 1010 101 010 1

    0 10 10 10 10 10 01 01 1 01 01 01 01 01 01 01 0

    0 10 10 10 10 01 01 01 01 0 1 01 01 01 01 01 01 01 01

    010100101010101010101010 0101010101010101010101010

    01010101010101010101010101010101010101010101010

    01010101010101010101010101010101

    0101010101010101010101010101010

    010101010101010101010101010101010101010101010101

    10101001010101010101010 1010101010101010101010

    0 10 10 10 10 01 01 01 01 1 01 01 01 01 01 01 01 01

    0 10 10 10 10 10 01 01 0 10 10 10 10 10 10 10

    01 010 1010 101 001 0 101 010 101 010 1

    01 010 1010 101 00 0 101 010 101 010 1

    01 010 1010 101 00 0 101 010 101 010 1

    1 01 01 01 01 01 00 10 0 10 10 10 10 10 10 10

    1 01 01 01 01 00 10 10 10 0 10 10 10 10 10 10 10 1

    101010010101010101010 10101010101010101010

    10010101010101010101010101010101010101010101010101

    01010101010101010101010101010101010

    010101010101010

    10101010101010101010101010101010101010101

    101001010101010101010101010101010101010101010101010101

    1 01 0 10 1 00 1 01 0 10 1 01 0 0 1 01 0 10 1 01 0 10 1 01 0 1

    0 10 10 10 10 10 01 01 0 1 01 01 01 01 01 01 01 0

    1 010 1010 101 001 10 101 010 101 010

    01 010 1010 101 00 0 101 010 101 010 1

    01 010 1010 101 00 0 101 010 101 010 1

    01 010 1010 101 001 10 101 010 101 010

    0 10 10 10 10 10 01 01 0 0 10 10 10 10 10 10 10 1

    1 01 0 10 1 00 1 01 0 10 1 01 1 01 0 10 1 01 0 10 1 01 0 10

    1010010101010101010101010101010101010101010101010101010

    0101010101010101010101010101010101010101010

    1010101010101010101010101

    ResearchDesign Lab

    www.researchdesignlab.com

    Email: [email protected] I www.researchdesignlab.com

    An ISO 9001- 2008 Certified Company

    Microcontroller 8051/89S52Embedded C Programming

    Getting Stated with Keil Software

    Introduction to Embedded C

    Interfacing Examples

    Mini Projects on GSM, Xbee & Bluetooth

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    2/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COM

    Index

    Embedded Systems 8051/89S52 Microcontroller-Introduction 1

    Keil-Introduction 2

    Creating a new project 2

    Creating a new source file 5

    Creating HEX for the Part 9

    Testing Program in Debugger 12

    Running the Keil Debugger (Simulation) 14

    LED Blinking using 8051 Microcontroller and Keil AT89S52 17

    LCD Keypad interfacing using 8051 Microcontroller and Keil AT89S52 19

    LCD Keypad and Relays interfacing using 8051 Microcontroller and Keil AT89S52 23

    Serial Communication 28

    Simple Serial interfacing using 8051 Microcontroller and Keil AT89S52 30

    Bluetooth and Relays interfacing using 8051 Microcontroller and Keil AT89S52 34

    Keypad and XBee interfacing using 8051 Microcontroller and Keil AT89S52 37

    LEDs and XBee interfacing using 8051 Microcontroller and Keil AT89S52 41

    GSM Modem and LCD interfacing using 8051 Microcontroller and Keil AT89S52 44

    Analog to digital conversion in 8051 Microcontroller and Keil-AT89S52 52

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    3/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 P1 WWW.RESEARCHDESIGNLAB.COMI

    Embedded Systems8051/89S52 Microcontroller

    Introduction

    The 8051 microcontroller is one of the most

    popular general purpose microcontrollers in

    use today. The 8051 is an 8-bit microcontroller

    which means that most available operations

    are limited to 8 bits. 8051 chips are used in a

    wide variety of control systems, telecom

    applications, robotics as well as in the

    automotive industry.

    There are 4 8-bit ports: P0, P1, P2 and P3.

    PORT P1 (Pins 1 to 8): The port P1 is a general

    purpose input/output port which can be used

    for a variety of interfacing tasks. The other

    ports P0, P2 and P3 have dual roles or

    additional functions associated with them

    based upon the context of their usage. The

    port 1 output buffers can sink/source four TTL

    inputs. When 1s are written to portn1 pins are

    pulled high by the internal pull-ups and can be

    used as inputs.

    Fig A.Pin description of 8051

    PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO port, but Port P3 has additional functions such

    as, serial transmit and receive pins, 2 external interrupt pins, 2 external counter inputs, read and

    write pins for memory access.

    PORT P2 (pins 21 to 28): PORT P2 can also be used as a general purpose 8 bit port when no external

    memory is present, but if external memory access is required then PORT P2 will act as an address bus

    in conjunction with PORT P0 to access external memory. PORT P2 acts as A8-A15, as can be seen fromfig 1.1

    PORT P0 (pins 32 to 39)P0 can be used as a general purpose 8 bit port when no external memory is

    present, but if external memory access is required then PORT P0 acts as a multiplexed address and

    data bus that can be used to access external memory in conjunction with PORT P2. P0 acts as AD0-

    AD7, as can be seen from fig 1.1

    Overview:

    Ports:

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    4/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP2I

    Keil provides a broad range of development tools like ANSI C compiler, macro assembler, debuggers

    and simulators, linkers, IDE, library managers, real-time clock operating systems and evaluation

    boards for 8051.

    Install Keil by following the instructions sets provided in your software.

    1) Open Keil.

    2) Select New Vision Project from the Project Menu.

    Keil

    Creating a new project

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    5/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP3I

    3) Name the project Led1.

    4) Click on the Save Button.

    5) The device window will be displayed. Select the manufacturer of the IC.

    Here let us use ATMEL AT89S52

    6) Double Click on Atmel.

    Tips

    IR ObstacleSensor

    Quick Overview

    Based on a simple basic

    Idea, this IR obstacle

    sensor, is easy to build,

    easy to calibrate and

    still, it provides a

    detection range of 10-

    30 cm. This sensor can

    be used for most indoor

    applications where noimportant ambient light

    is present. It is the same

    principle in ALL Infra-

    Red proximity sensors.

    The basic idea is to send

    infra red light through

    IR-LEDs, which is then

    reflected by any object

    in front of the sensor.

    Code & Schematic

    For more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    6/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP4I

    7) Scroll down and select AT89S52

    8) Click OK9) Choose No.

    Tips

    CarbonMonoxideSensor

    Quick Overview

    M Q - 7 g a s s e n s o r

    composed by micro

    AL2O3 ceramic tube, Tin

    Dioxide (SnO2) sensitive

    layer , measur ingelectrode and heater

    are fixed into a crust

    made by plastic and

    stainless steel net. The

    h e a t e r p r o v i d e s

    n e c e s s a r y w o r k

    conditions for work of

    sensitive components.

    Code & Schematic

    For more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    7/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP5I

    Creating a

    new source file

    1)Click File Menu and

    select New.

    2) A new window will

    open up in the Keil IDE.

    3) Let us write a simple

    code that would toggle

    the status of Port 1 witha small delay.

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    8/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP6I

    4) Click on File menu

    and select Save as

    5) Name the file

    Led1_blinking.c

    6) Click the Save Button

    7) In the Project

    Workspace window,

    click on the +

    symbol in front of Target1.

    Tips

    Quick Overview

    This is a simple-to-use liquefied petroleum gas (LPG) sensor,

    suitable for sensing LPG (composed of mostly propane and

    butane) concentrations in the air. Used in gas leakage

    detecting equipments for detecting of LPG, iso-butane,

    propane, LNG combustible gases. If output goes above a

    preset range the output is low else high in idle condition.

    Code & Schematic

    For more detailswww.researchdesignlab.com Gas Sensor

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    9/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP7I

    8) Below that Source Group 1 would appear, right click on it.

    9) Click on Add Files

    to Group Source

    Group 1

    10) Select

    Led1_blinking.c11) Click Add button

    12) Click Close button.

    Tips

    Digital SoilMoistureSensor

    Quick Overview

    This sensor can be used

    to test the moisture of

    soil, when the soil is

    having water shortage,

    the module output is at

    high level, else the

    output is at low level.

    By using this sensor one

    can automatically water

    the flower plant, or any

    other plants requiring

    automatic watering

    technique. Module

    triple output mode,

    digital output is simple,

    analog output more

    accurate, serial output

    with exact readings.

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    10/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP8I

    13) Click Close button.

    Expand the Source

    Group 1 in the tree

    menu to ensure that

    the file was added to

    the project.

    Tips

    Code & Schematic

    For more detailswww.researchdesignlab.com

    DigitalHeart BeatSensor

    Quick Overview

    This heart beat sensor is designed to give

    digital output of heart beat when a finger is

    placed on it. When the heartbeat detector is

    working, the top-most LED flashes with each

    heart beat. This digital output can be

    connected to micro controller directly to

    measure the Beats Per Minute (BPM) rate. It

    works on the principle of light modulation by

    blood flow through finger at each pulse.

    Module dual output mode, digital output is

    simple, serial output with exact readings.

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    11/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP9I

    Creating HEX for the

    Part

    1) Right click on Target

    1 in Tree menu

    2) Select Options for

    Target Target 1

    3) Select Target Tab

    4) Change Xtal (Mhz) from 33.0 to 11.0592

    Tips

    Analog

    LightIntensitySensor

    Quick Overview

    Light dependent resistor

    (LDR), suitable for use in

    projects which require a

    device or circuit to be

    automatically switched

    on or off in darkness or

    light. As the amount of

    light falling on this LDR

    increases, its resistance

    decreases. The light

    detector itself is just

    5mm in diameter.Analog

    output more accurate.

    Code & Schematic

    For more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    12/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 P10 WWW.RESEARCHDESIGNLAB.COMI

    5) Select Output Tab

    6) Click on Create

    Hex File check box

    7) Click OK Button

    8. Click on Project

    Menu and select

    Rebuild all Target

    Files

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    13/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP11I

    9. In the Build Window it should report 0 Errors (s), 0 Warnings

    10. You are now ready to Program your Part

    Tips

    Code & Schematic

    For more detailswww.researchdesignlab.com

    DigitalVibration

    Sensor

    Quick Overview

    This basic piezo sensor can be used in anti-theft

    devices, electronic locks, mechanical equipment

    vibration detection, sound gesture application and

    detection range bull's-eye counts vibration testing

    occasions. These vibration levels could be given to

    any controller/processor and necessary decisions

    could be taken through it. Module triple output

    mode, digital output is simple, analog output more

    accurate, serial output with exact readings.

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    14/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP12I

    Testing Program in Debugger

    1) Click on the File Menu and select Save

    2)Click on Project Menu and select Rebuild all Target Files

    Tips

    DigitalLightIntensitySensor

    Quick Overview

    Light dependent resistor

    (LDR), suitable for use inprojects which require a

    device or circuit to be

    automatically switched

    on or off in darkness or

    light. As the amount of

    light falling on this LDR

    increases, its resistance

    decreases. The light

    detector itself is just

    5mm in d iameter .

    Module triple output

    mode, digital output issimple, analog output

    more accurate, serial

    output with exact

    readings.

    Code & Schematic

    For more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    15/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP13I

    3) In the Build Window it should report 0 Errors (s), 0 Warnings

    4) Click on Debug Menu and Select Start/Stop Debug Session.

    5) If you are using a free version of Keil the dialog appears. Click OK.

    Tips

    SerialUltrasonicDistanceMeasure

    Quick Overview

    Ultrasonic DistanceSensor comes with an

    ASCII serial O/P and

    prov ided op t imum

    ranging & detection of

    long to short distance

    ranges. Owing to their

    stable, precise, non-

    contact and accurate

    distance measurements

    from about 2 cm to 4

    m e t e r s . C o m p a c t l y

    designed, easy usable,

    high ranging and easily

    interfaced upon micro

    controllers RX and TX

    pin.

    Code & Schematic

    For more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    16/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP14I

    Running the Keil Debugger (Simulation)

    1) The Keil Debugger should be now running.

    2) Click on Peripherals. Select I/O Ports, Select Port 1.

    Tips

    UltrasonicRangingSensor

    Quick Overview

    U l t r a s o n i c s e n s o r

    provides stable and

    a c c u r a te d i s t a n c e

    measurements from

    2cm to 450cm. It has an

    focus of less than 15

    degrees and an accuracy

    of about 2mm.

    Code & SchematicFor more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    17/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP15I

    3) A new window

    should port will pop

    up. This represents

    the Port and Pins

    4) To execute your code Click Run. The Parallel Port 1 Box status

    should change as a continuous loop.

    Tips

    UltrasonicObstacle

    Sensor

    Quick Overview

    Used to detect the move

    of human or object.

    Suitable for indoor and

    outdoor burglar proof

    application, vehicle

    a p p l i c a t i o n , A T M

    survillence camera etc.

    Code & Schematic

    For more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    18/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP16I

    5) To exit out, first Click on Debug Menu and Select Stop Running

    6) And then Click on

    Debug Menu and

    Select Start/Stop

    Debug Session

    Tips

    GyroSensor

    Quick Overview

    The L3G4200DTR is a

    low-power, three-axis

    angular rate sensor, able

    t o p r o v i d e

    u n p r e c e d e n t e d

    stablility of zero rate

    level and sensitivity over

    temperature and time.It

    inc ludes a sensing

    element and an IC

    interface capable of

    providing the measured

    angular rate to the

    external world through a

    d i g i t a l i n t e r f a c e

    (I2C/SPI).

    Code & SchematicFor more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    19/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP17I

    Learn embedded C programming in 8051

    Circuit and Working:

    Fig.1 shows the circuit of simple

    8051 Microcontroller interfaced

    with LEDs.

    Here are 3 simple programs for

    controlling LEDs through simple

    Embedded C programming in

    Microcontroller.

    Program 1 shows how to control

    the entire port by toggling 8

    LEDs.

    Program 2 shows how to controlsingle I/O pin of the controller

    one by one just like a Decimal

    counter output.

    Testing:

    1) Write the program as shown below and generate the hex

    file by the instructions provided in chapter 1.

    2) Burn the code with the help of burner to the controller.

    3) Power ON your microcontroller and check the result as per

    your program.

    Components/modules required :

    1) 8051 project board (assembled/non assembled kit).

    2) 5V DC source.

    3) 8 LEDs.

    4) Resistors (1Kx8).5) IC AT89S52.

    6) 8051 IC burner.

    7) Connectors and cables.

    Fig 1. Circuit Diagram for LCD and 1x4 keypad interfacing

    LED Blinking using 8051 Microcontroller and Keil AT89S52

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    20/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP18I

    Program 1:

    #include

    #define LEDPORT P2

    void delay();void main()

    {

    P2=0X00;

    while(1)

    {

    LEDPORT =0XFF;

    delay();

    LEDPORT =0X00;

    delay();

    }}

    void delay()

    {

    unsigned int x=60000;

    while (x--) ;

    }

    // special function register

    //declarations for the

    // intended 8051 derivative

    //Defining Port 2 as the

    //'LEDPORT'

    // Function prototype declaration//Main Code

    //Set Port 2 all bits to 0

    //infinite loop

    //Set LEDPORT all bits to 1

    // Wait for a small delay

    //Set LEDPORT all bits to 0

    // Wait for small delay

    // Delay Routine

    // larger the value of x

    //the more is the delay.

    // executes this statement

    // until x decrements to 0

    Program 2:#include

    void delay();

    sbit LED0=P2^0;

    sbit LED1=P2^1;

    sbit LED2=P2^2;

    sbit LED3=P2^3;

    sbit LED4=P2^4;

    sbit LED5=P2^5;sbit LED6=P2^6;

    sbit LED7=P2^7;

    void main()//Main Code

    {

    P1=0x00;

    while(1)

    {

    LED0=1;

    delay();

    LED1=1;

    delay();

    //special function register declarations

    //for the intended 8051 derivative

    // Function prototype declaration

    //Define Port Pin P2.0 as LED0

    //Define Port Pin P2.1 as LED1

    //Define Port Pin P2.2 as LED2

    //Define Port Pin P2.3 as LED3

    //Define Port Pin P2.4 as LED4

    //Define Port Pin P2.5 as LED5//Define Port Pin P2.6 as LED6

    //Define Port Pin P2.7 as LED7

    //Set Port 2 all bits to 0

    // Continuous loop

    //Turn ON LED0

    //Wait for a small delay

    //Turn ON LED1

    //Wait for a small delay

    Tips

    CurrentSensor 20A

    Quick Overview

    The ACS712 provides

    economical and precise

    solutions for AC or DC

    current sensing in

    industrial, commercial,

    and communications

    systems. The devicepackage allows for easy

    implementation by the

    c u s t o m e r . T yp i c a l

    applications include

    motor control, load

    d e t e c t i o n a n d

    m a n a g e m e n t ,

    switchmode power

    s u p p l i e s , a n d

    o v e r c u r r e n t f a u l t

    protection. The device

    is not intended fora u t o m o t i v e

    applications.

    Code & Schematic

    For more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    21/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP19I

    LED2=1;

    delay();

    LED3=1;

    delay();

    LED4=1;

    delay();

    LED5=1;

    delay();

    LED6=1;

    delay();

    LED7=1;

    delay();

    P1=0x00;

    delay();

    }

    }void delay()

    {

    unsigned int x=60000

    while (x--);

    }

    //Turn ON LED2

    //Wait for a small delay

    //Turn ON LED3

    //Wait for a small delay

    //Turn ON LED4

    //Wait for a small delay

    //Turn ON LED5

    //Wait for a small delay

    //Turn ON LED6

    //Wait for a small delay

    //Turn ON LED7

    //Wait for a small delay

    //Turn OFF all LED's

    //Wait for a small delay

    // Delay Routine

    // larger the value of x the

    //more is the delay.

    // executes this statement

    //until x decrements to 0

    LCD Keypad interfacing using 8051

    Microcontroller and Keil AT89S52

    Circuit and Working:

    Components/modules required:

    Fig.2 shows the circuit of simple 8051

    Microcontroller interfaced with LCD and

    1x4 Keypad.

    Here is a simple program for interfacing

    LCD and keypad through simple

    Embedded C programming in

    Microcontroller.

    Program 3 demonstrates how to display in

    a LCD when an event occurs like a key is

    being pressed.

    1) 8051 project board

    (assembled/non assembled kit).

    2) 5V DC source.

    3) LCD interfacing Module

    4) 4 Keys keypad

    5) IC AT89S52.

    6) 8051 IC burner.

    7) Connectors and cables.

    Fig 2. Circuit Diagram for LCD and 1x4 keypad interfacing

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    22/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP20I

    Program 3:

    #include

    #define LCD_PORT P2

    sbit rs=P3^5;

    sbit en=P3^7;sbit D7=P2^7;

    sbit rw=P3^6;

    sbit key1=P0^3;

    sbit key2=P0^2;

    sbit key3=P0^1;

    sbit key4=P0^0;

    void busy();

    void CMD_WRT(unsigned char);

    void LCD_WRT(unsigned char *);

    void DATA_WRT(unsigned char);

    void DELAY();

    void main()

    {

    unsigned char CMD[]={0x38,0x0f,0x01,0x06,0x80};

    unsigned char TEMP,i;

    for(i=0;i

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    23/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP21I

    if (key1 ==0)

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Key 1 is pressed");

    while(key1==0);

    }

    else if (key2 ==0)

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Key 2 is pressed");

    while(key2==0);

    }

    else if (key3 ==0)

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Key 3 is pressed");

    while(key3==0);

    }

    else if (key4 ==0)

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Key 4 is pressed");

    while(key4==0);

    }

    else

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("No key Pressed");

    DELAY();DELAY();

    DELAY();

    }

    }

    }

    void busy()

    {

    D7=1;

    rs=0;rw=1;

    //Check whether switch 1 is being pressed

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Wait until the switch has been released

    //Check whether switch 2 is being pressed

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Wait until the switch has been released

    //Check whether switch 3 is being pressed{

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Wait until the switch has been released

    //Check whether switch 4 is being pressed

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Wait until the switch has been released

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    // A small delay for relaxation

    //This Function checks whether the LCD is ready to receive next byte

    //Keep D7 pin to High

    //Keep RS to Low to select command register//RW=1 for read

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    24/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP22I

    while(D7!=0)

    {

    en=0;

    en=1;

    }

    }

    void CMD_WRT(unsigned char val)

    {

    busy();

    LCD_PORT=val;

    rs=0;

    rw=0;

    en=1;

    en=0;}

    void LCD_WRT(unsigned char *string)

    {

    while(*string)

    DATA_WRT(*string++);

    }

    void DATA_WRT(unsigned char ch)

    {

    busy();

    LCD_PORT = ch;

    rs=1;

    rw=0;

    en=1;

    en=0;

    }

    void DELAY()

    {

    unsigned int X=60000,Y=60000;

    while(X--);

    while(Y--);

    }

    //Monitor D7 pin until it gets low

    //Provide a latch pulse from low to high to EN

    //This Function is used to write commands into the LCD

    //Execute busy function to know whether the LCD is

    //ready to receive any data/command

    //Put the variable val into LCD_PORT which is

    //connected to LCD data pins

    //Keep RS to Low to select command register

    // RW=0 for write

    //Provide a latch pulse from High to Low to EN

    //This Function is used to write Strings into the LCD

    //increment from the beginning of the string until a

    //null character is detected (end of the string)

    //separates a single byte from the string

    //This Function is used to write a byte of data into the LCD

    //Execute busy function to know whether the LCD is

    //ready to receive any data/command

    //Put the variable ch into LCD_PORT which is

    //connected to LCD data pins

    //Keep RS to High to select Data register

    // RW=0 for write

    //Provide a latch pulse from High to Low to EN

    //function for delay routine

    // larger the value of X and Y the more is the

    //delay.

    //executes this statement until X decrements to 0

    //executes this statement until Y decrements to 0

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    25/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP23I

    LCD Keypad and Relays interfacing using 8051Microcontroller and Keil AT89S52

    Circuit and Working:

    Fig.3.1 and Fig.3.2 shows the circuit of simple 8051

    Microcontroller interfaced with LCD, 1x4 Keypad and 4 Relays.

    Here is a simple Embedded C program for interfacing 4 Relaysto a 8051 Microcontroller which could be controlled by a key

    press event through a 1x4 keypad, the result or state of the

    relays being displayed on the LCD interfaced along with this.

    Program 4 enables a user to toggle the state of relays by

    pressing a key consequently the result gets displayed on the

    LCD interfaced.

    Fig 3.2.Circuit Diagram for Relay LCDand 1x4 keypad interfacing- Part 2.

    Fig 3.1.Circuit Diagram forRelay LCD and 1x4keypad interfacing- Part 1.

    Components/modules required:

    1) 8051 project board

    (assembled/non assembled kit).

    2) 12V and 5V DC source.3) LCD interfacing Module

    4) 4 Keys keypad

    5) 4 Relay Interfacing Board 12V

    6) IC AT89S52.

    7) 8051 IC burner.

    8) Connectors and cables

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    26/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP24I

    Program 4:

    #include

    #define LCD_PORT P2

    sbit rs=P3^5;

    sbit en=P3^7;

    sbit D7=P2^7;

    sbit rw=P3^6;

    sbit key1=P0^3;

    sbit key2=P0^2;sbit key3=P0^1;

    sbit key4=P0^0;

    sbit Relay1=P1^3;

    sbit Relay2=P1^2;

    sbit Relay3=P1^1;

    sbit Relay4=P1^0;

    void busy();

    void CMD_WRT(unsigned char);

    void LCD_WRT(unsigned char *);

    void DATA_WRT(unsigned char);

    void DELAY();

    void main()

    {

    unsigned char CMD[]={0x38,0x0f,0x01,0x06,0x80};

    unsigned char TEMP,i;

    P1=0X00;

    for(i=0;i

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    27/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP25I

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("RDL");

    DELAY();

    DELAY();

    DELAY();

    DELAY();

    while(1)

    {

    if (key1 ==0 && Relay1==0)

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Relay 1 is ON");

    Relay1=1;

    while(key1==0);

    }else if (key1 ==0 && Relay1==1)

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Relay 1 is OFF");

    Relay1=0;

    while(key1==0);

    }

    else if (key2 ==0 && Relay2==0)

    {

    CMD_WRT(0X01);CMD_WRT(0X80);

    LCD_WRT("Relay 2 is ON");

    Relay2=1;

    while(key2==0);

    }

    else if (key2 ==0 && Relay2==1)

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Relay 2 is OFF");Relay2=0;

    while(key2==0);

    }

    else if (key3 ==0 && Relay3==0)

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Relay 3 is ON");

    Relay3=1;

    while(key3==0);

    }

    //This command clears the LCD screen

    //This moves the cursor to the beginning of the 1st line

    //Writes the text 'RDL' in the LCD

    //These delay's will hold the above text 'RDL' for some time

    //Continuous loop

    //Check whether switch 1 is being pressed

    //and Relay1 is OFF

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Turn ON Relay1

    //Wait until the switch has been released

    //Check whether switch 1 is being pressed

    //and Relay1 is ON

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Turn OFF Relay1

    //Wait until the switch has been released

    //Check whether switch 2 is being pressed

    //and Relay2 is OFF

    //Clears the LCD screen//Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Turn ON Relay2

    //Wait until the switch has been released

    //Check whether switch 2 is being pressed

    //and Relay2 is ON

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD//Turn OFF Relay2

    //Wait until the switch has been released

    //Check whether switch 3 is being pressed

    //and Relay3 is OFF

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Turn ON Relay3

    //Wait until the switch has been released

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    28/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP26I

    else if (key3 ==0 && Relay3==1)

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Relay 3 is OFF");

    Relay3=0;

    while(key3==0);}

    else if (key4 ==0 && Relay4==0)

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Relay 4 is ON");

    Relay4=1;

    while(key4==0);

    }

    else if (key4 ==0 && Relay4==1){

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("Relay 4 is OFF");

    Relay4=0;

    while(key4==0);

    else

    {

    CMD_WRT(0X01);

    CMD_WRT(0X80);LCD_WRT("Press any key..");

    DELAY();

    }

    }

    }

    void busy(){

    D7=1;

    rs=0;

    rw=1;

    while(D7!=0)

    {

    en=0;

    en=1;

    }

    }

    //Check whether switch 3 is being pressed

    //and Relay3 is ON

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Turn OFF Relay3

    //Wait until the switch has been released

    //Check whether switch 4 is being pressed

    //and Relay4 is OFF

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Turn ON Relay4

    //Wait until the switch has been released

    //Check whether switch 4 is being pressed//and Relay4 is ON

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line

    //Writes the text within quotes in the LCD

    //Turn OFF Relay4

    //Wait until the switch has been released

    }

    //Clears the LCD screen

    //Moves the cursor to the beginning of the 1st line//Writes the text within quotes in the LCD

    // A small delay for relaxation

    //This Function checks whether the LCD is ready to receive

    //next byte

    //Keep D7 pin to High

    //Keep RS to Low to select command register

    // RW=1 for read

    //Monitor D7 pin until it gets low

    //Provide a latch pulse from low to high to EN

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    29/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP27I

    void CMD_WRT(unsigned char val)

    {

    busy();

    LCD_PORT=val;

    rs=0;

    rw=0;

    en=1;

    en=0;

    }

    void LCD_WRT(unsigned char *string){

    while(*string)

    DATA_WRT(*string++);

    }

    void DATA_WRT(unsigned char ch)

    {

    busy();

    LCD_PORT = ch;

    rs=1;

    rw=0;

    en=1;

    en=0;

    }

    void DELAY()

    {

    unsigned int X=60000;

    while(X--);

    }

    //This Function is used to write commands into the LCD

    //Execute busy function to know whether the LCD is

    //ready to receive any data/command

    //Put the variable val into LCD_PORT which is

    //connected to LCD data pins

    //Keep RS to Low to select command register

    // RW=0 for write

    //Provide a latch pulse from High to Low to EN

    //This Function is used to write Strings into the LCD

    // increment from the beginning of the string until

    //a null character is detected (end of the string)

    //separates a single byte from the string

    //This Function is used to write a byte of data into the LCD

    //Execute busy function to know whether the LCD

    //is ready to receive any data/command//Put the variable ch into LCD_PORT which is

    //connected to LCD data pins

    //Keep RS to High to select Data register

    // RW=0 for write

    //Provide a latch pulse from High to Low to EN

    //Function for delay routine

    // larger the value of X the more is the delay.

    //executes this statement until X decrements to 0

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    30/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP28I

    Asynchronous serial data communication is widely used for character-oriented transmissions. Each

    character is placed in between start and stop bits, this is called framing. Block-oriented data

    transfers use the synchronous method. The start bit is always one bit, but the stop bit can be one or

    two bits. The start bit is always a 0 (low) and the stop bit(s) is 1 (high).

    We need a line driver (voltage converter) to convert the R232s signals to TTL voltage levels that willbe acceptable to 8051s TxD and RxD pins.

    The baud rate of 8051 system should match the baud rate of the PCs COM port.

    Serial Communication

    Fig 4.1Rs232 to TTLConversion

    Fig 4.2Serial Transmissionof Character A

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    31/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP29I

    SM0, SM1

    They determine the framing of

    data by specifying the number

    of bits per character, and the

    start and stop bits.

    REN (receive enable)It is a bit-addressable register

    When it is high, it allows 8051 to

    receive data on RxD pin If low,and should be picked up before data is framed with start andthe receiver is disable.it is lost. stop bits.

    4. TR1 is set to 1 to start timer 1TI (transmit interrupt)Programming the 8051 to 5. TI is cleared by (TI=0;) TIWhen 8051 finishes the transfertransfer character bytes instructionof 8-bit Character It raises TIserially. 6. The character byte to beflag to indicate that it is ready1. TMOD register is loaded with transferred serially is written

    to transfer another byte TI bit th e va lu e TM OD =0 X2 0, into SBUF registeris raised at the beginning of theindicating the use of timer 1 in 7. The TI flag bit is monitoredstop bitmode 2 (8-bit auto-reload) to with the use of instruction

    set baud rate. while (TI==0); to see if theRI (receive interrupt)2. The TH1 is loaded with one of c h a r a c t e r h a s b e e nWhen 8051 receives data

    the values to set baud rate for transferred completely.serially via RxD, it gets rid of theserial data transfer 8. To transfer the next byte, gostart and stop bits and places

    3. The SCON register is loaded to step 5the byte in SBUF register Itwith the value 50H, indicatingraises the RI flag bit to indicateserial mode 1, where an 8-bitthat a byte has been received

    Programming the 8051 to receive character bytes serially

    1. TMOD register is loaded with the value TMOD=0X20, indicating the use of timer 1 in mode2

    (8-bit auto-reload) to set baud rate

    2. TH1 is loaded to set baud rate

    3. The SCON register is loaded with the value SCON=0X50,indicating serial mode 1, where an 8-

    bit data is framed with start and stop bits

    4. TR1 is set to 1 to start timer 1

    5. RI is cleared by RI=0; RI instruction6. The RI flag bit is monitored with the use of instruction while(RI==0); to see if an entire

    character has been received yet.

    7. When RI is raised, SBUF has the byte, its contents are moved into a safe place

    8. To receive the next character, go to step 5

    Fig 4.3: Serial Mode Selector

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    32/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP30I

    Circuit and Working:

    Fig.5 shows the circuit of simple 8051 Microcontroller interfaced with LEDs.

    Here is a simple Embedded C program for interfacing 8 LEDs to a 8051 Microcontroller

    which could be turned ON or OFF by sending few serial commands.

    Program 5 enables a user to turn ON/OFF a series of LEDs by sending serial data. The

    program is designed in such a way that a serial command A1 will turn ON the first LED

    and A0 will turn of the same LED. Similarly B1 will turn ON the second LED and B0 will

    turn of the same LED. This will continue for the remaining 6 LEDs. i.e. H1 and H0 would

    turn ON and OFF last LED (8th LED) respectively. You can enter the inputs in any serial

    window monitor software like Hyperterminal, Putty etc. Also you could design a GUI in

    software like Matlab, .NET etc. which could be used to control these LEDs.

    Components/modules required :

    1) 8051 project board with RS232 interface (assembled/non assembled kit).

    2) 5V DC source.

    3) 8 LEDs.4) Resistors (1Kx8).

    5) IC AT89S52.

    6) 8051 IC burner.

    7) Connectors and cables.

    Fig. 5 Circuit Diagram for Serial and LED interfacing

    Simple Serial interfacing using 8051 Microcontroller and Keil AT89S52

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    33/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP31I

    #include

    void delay();

    sbit LED0=P2^0;

    sbit LED1=P2^1;

    sbit LED2=P2^2;

    sbit LED3=P2^3;

    sbit LED4=P2^4;

    sbit LED5=P2^5;

    sbit LED6=P2^6;

    sbit LED7=P2^7;

    unsigned char byte1,byte2;

    void main()

    {

    TMOD=0X20;

    SCON=0X50;

    TH1=0XFD;

    TR1=1;

    delay();

    TI=0;

    SBUF='S';

    while (TI==0);

    TI=0;

    delay();

    P2=0x00;

    while(1)

    {

    RI=0;

    while(RI==0);

    byte1=SBUF;

    RI=0;while(RI==0);

    byte2=SBUF;

    RI=0;

    delay();

    delay();

    if(byte1=='A')

    {

    if(byte2=='1')

    {

    //special function register declarations

    //for the intended 8051 derivative

    // Function prototype declaration

    //Define Port Pin P2.0 as LED0

    //Define Port Pin P2.1 as LED1

    //Define Port Pin P2.2 as LED2

    //Define Port Pin P2.3 as LED3

    //Define Port Pin P2.4 as LED4

    //Define Port Pin P2.5 as LED5

    //Define Port Pin P2.6 as LED6

    //Define Port Pin P2.7 as LED7

    // Variable declarations

    // MAIN CODE

    //Serial Initialization//use Timer 1, mode 2

    //indicating serial mode 1, where an 8-bit

    //data is framed with start and stop bits

    //9600 baud rate

    //Start timer

    //Wait for a delay for serial initialization to finish

    // Transmit 'S' to check whether the setup is ready

    //Forcibly change the Transmit Interrupt Flag of 8051 to 0

    //Move 'S' to serial buffer memory

    //Wait until TI flag is set by hardware when an entire byte

    //has been transmitted// Forcibly clear TI flag

    //A small delay for relaxation

    //Set Port 2 all bits to 0

    // continuous loop

    //Forcibly clear the Receive Interrupt Flag of 8051 to 0

    //Wait until RI flag is set by hardware when an entire byte

    //has been received

    //Move the received byte of data into variable 'byte1'

    //Forcibly clear RI flag//Wait until RI flag is set by hardware when an entire byte

    //has been received

    //Move the received byte of data into variable 'byte2'

    //Forcibly clear RI flag

    //Check whether the 1st byte of

    //data is 'A'

    //Check whether the 2nd byte of

    //data is '1'

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    34/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP32I

    LED0=1;

    delay();

    }

    else if(byte2=='0')

    {

    LED0=0;

    delay();

    }

    }

    else if(byte1=='B')

    {

    if(byte2=='1')

    {

    LED1=1;

    delay();

    }

    else if(byte2=='0'){

    LED1=0;

    delay();

    }

    }

    else if(byte1=='C')

    {

    if(byte2=='1')

    {

    LED2=1;

    delay();}

    else if(byte2=='0')

    {

    LED2=0;

    delay();

    }

    }

    else if(byte1=='D')

    {

    if(byte2=='1'){

    LED3=1;

    }

    //Turn ON LED0

    //Wait for a small delay

    //Check whether the 2nd byte of

    //data is '0'

    //Turn OFF LED0

    //Wait for a small delay

    //Check whether the 1st byte of

    //data is 'B'

    //Check whether the 2nd byte of

    //data is '1'

    //Turn ON LED1

    //Wait for a small delay

    //Check whether the 2nd byte of//data is '0'

    //Turn OFF LED1

    //Wait for a small delay

    //Check whether the 1st byte of

    //data is 'C'

    //Check whether the 2nd byte of

    //data is '1'

    //Turn ON LED2

    //Wait for a small delay

    //Check whether the 2nd byte of

    //data is '0'

    //Turn OFF LED2

    //Wait for a small delay

    //Check whether the 1st byte of

    //data is 'D'

    //Check whether the 2nd byte of//data is '1'

    //Turn ON LED3

    Tips

    SOIC to DIPAdapter 8-Pin

    Quick Overview

    Adapter for standard 8

    SOIC SMD Parts to

    convert to standard 8 Pin

    DIP s ize.This SOIC

    breakout board is a PCB

    which will interface anSOIC package to 0.1"

    (2.54mm) headers which

    c a n b e u s e d o n

    b r e a d b o a r d s f o r

    p r o t o t y p i n g y o u r

    projects.Simply solder-

    on your 8-pin SOIC form-

    factor IC, along with

    some 0.1-inch-pitch

    headers, and you will

    h a v e a u s a b l e ,

    breadboard-friendlyunit.

    Code & Schematic

    For more detailswww.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    35/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP33I

    delay();

    }

    else if(byte2=='0')

    {

    LED3=0;

    delay();

    }}

    else if(byte1=='E')

    {

    if(byte2=='1')

    {

    LED4=1;

    delay();

    }

    else if(byte2=='0')

    {

    LED4=0;

    delay();

    }

    }

    else if(byte1=='F')

    {

    if(byte2=='1')

    {

    LED5=1;

    delay();

    }else if(byte2=='0')

    {

    LED5=0;

    delay();

    }

    }

    else if(byte1=='G')

    {

    if(byte2=='1')

    {

    LED6=1;

    delay();

    }

    else if(byte2=='0')

    {

    LED6=0;

    delay();

    }

    }

    else if(byte1=='H')

    {

    //Wait for a small delay

    //Check whether the 2nd byte of

    //data is '0'

    //Turn OFF LED3

    //Wait for a small delay

    //Check whether the 1st byte of

    //data is 'E'

    //Check whether the 2nd byte of

    //data is '1'

    //Turn ON LED4

    //Wait for a small delay

    //Check whether the 2nd byte of

    //data is '0'

    //Turn OFF LED4

    //Wait for a small delay

    //Check whether the 1st byte of

    //data is 'F'

    //Check whether the 2nd byte of

    //data is '1

    //Turn ON LED5

    //Wait for a small delay

    //Check whether the 2nd byte of

    //data is '0'

    //Turn OFF LED5

    //Wait for a small delay

    //Check whether the 1st byte of

    //data is 'G'

    //Check whether the 2nd byte of

    //data is '1'

    //Turn ON LED6

    //Wait for a small delay

    //Check whether the 2nd byte of

    //data is '0'

    //Turn OFF LED6

    //Wait for a small delay

    //Check whether the 1st byte of

    //data is 'H'

    Tips

    CurrentSensor 05A

    Code & Schematic

    For more detailswww.researchdesignlab.com

    Quick Overview

    The ACS712 provides

    economical and precise

    solutions for AC or DC

    current sensing in

    industrial, commercial,

    and communications

    systems. The device

    package allows foreasyimplementation by the

    c u s t o m e r . T yp i c a l

    app l icat ions inc lude

    motor control, load

    d e t e c t i o n a n d

    m a n a g e m e n t ,

    switchmode power

    s u p p l i e s , a n d

    o v e r c u r r e n t f a u l t

    protection. The device

    is not intended for

    a u t o m o t i v eapplications.

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    36/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP34I

    if(byte2=='1')

    {

    LED7=1;

    delay();

    }

    else if(byte2=='0')

    {LED7=0;

    delay();

    }

    }

    else

    {

    P2=0x00;

    delay();

    }}

    }

    void delay()

    {

    unsigned int x=60000;

    while (x--);

    }

    //Check whether the 2nd byte of

    //data is '1'

    //Turn ON LED7

    //Wait for a small delay

    //Check whether the 2nd byte of

    //data is '0'//Turn OFF LED7

    //Wait for a small delay

    //Set Port 2 all bits to 0 if any

    //other variable has been received

    //Wait for a small delay

    // Delay Routine

    // larger the value of x

    //the more is the delay.

    // executes this statement

    //until x decrements to 0

    Bluetooth and Relays interfacing

    using 8051 Microcontroller and

    Keil AT89S52

    Circuit and Working:

    Fig.6 shows the circuit of simple

    8051 Microcontroller interfaced

    with Bluetooth and 4 relays.

    Program 6 demonstrates how to

    receive data through Bluetooth.

    Components/modules required :

    1) 8051 project board

    (assembled/non assembled kit).

    2) 5V and 12V DC source.

    3) Bluetooth Module.

    4) 12V 4 Relay board.5) IC AT89S52.

    6) 8051 IC burner.

    7) Connectors and cables.

    Fig .6 Circuit Diagram for Bluetooth and 4 Relay interfacing

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    37/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP35I

    Program 6:

    #include

    void delay();

    sbit Relay1=P2^3;

    sbit Relay2=P2^2;

    sbit Relay3=P2^1;

    sbit Relay4=P2^0;

    unsigned char byte1,byte2;

    void main()

    {

    TMOD=0X20;

    SCON=0X50;

    TH1=0XFD;

    TR1=1;

    delay();

    TI=0;

    SBUF='S';

    while (TI==0);

    TI=0;

    delay();

    P2=0x00;

    while(1)

    {

    RI=0;

    while(RI==0);

    byte1=SBUF; '

    RI=0;

    while(RI==0);

    byte2=SBUF;

    RI=0;

    if(byte1=='1')

    {

    if(byte2=='N')

    {

    Relay1=1;

    }

    //special function register declarations

    //for the intended 8051 derivative

    //Function prototype declaration

    // Relay Connections

    //Relay 1 is connected to Port 2 pin 3

    //Relay 2 is connected to Port 2 pin 2

    //Relay 3 is connected to Port 2 pin 1

    //Relay 4 is connected to Port 2 pin 0

    // Variable declarations

    // MAIN CODE

    //Serial Initialization

    //use Timer 1, mode 2

    //indicating serial mode 1,where an 8-bit data

    //is framed with start and stop bits//9600 baud rate

    //Start timer

    //Wait for some time for serial initialization to finish

    // Transmit 'S' to check whether the setup is ready

    //Forcibly change the Transmit

    //Interrupt Flag of 8051 to 0

    //Move 'S' to serial buffer memory

    //Wait until TI flag is set by hardware

    //when an entire byte has been transmitted

    // Forcibly clear TI flag

    //A small delay for relaxation

    //Set Port 2 all bits to 0

    // continuous loop

    //Forcibly clear the Receive

    //Interrupt Flag of 8051 to 0

    //Wait until RI flag is set by hardware

    //when an entire byte has been received

    //Move the received byte of data into variable 'byte1

    //Forcibly clear RI flag

    //Wait until RI flag is set by hardware

    //when an entire byte has been received

    //Move the received byte of data into variable 'byte2'

    //Forcibly clear RI flag

    //Check whether the 1st byte of data is '1'

    //Check whether the 2nd byte of data is 'N'

    //Turn ON Relay1

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    38/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP36I

    else if(byte2=='F')

    {

    Relay1=0;

    }

    }

    else if(byte1=='2')

    {if(byte2=='N')

    {

    Relay2=1;

    }

    else if(byte2=='F')

    {

    Relay2=0;

    }

    }

    else if(byte1=='3'){

    if(byte2=='N')

    {

    Relay3=1;

    }

    else if(byte2=='F')

    {

    Relay3=0;

    }

    }

    else if(byte1=='4')

    if(byte2=='N')

    {

    Relay4=1;

    }

    else if(byte2=='F')

    {

    Relay4=0;

    }

    }

    else if(byte1=='X')

    {

    if(byte2=='N')

    {

    P2=0xFF;

    }

    else if(byte2=='F')

    {

    P2=0x00;

    }

    }

    //Check whether the 2nd byte of data is 'F'

    //Turn OFF Relay1

    //Check whether the 1st byte of data is '2'

    //Check whether the 2nd byte of data is 'N'

    //Turn ON Relay2

    //Check whether the 2nd byte of data is 'F'

    //Turn OFF Relay2

    //Check whether the 1st byte of data is '3'

    //Check whether the 2nd byte of data is 'N'

    //Turn ON Relay3

    //Check whether the 2nd byte of data is 'F'

    //Turn OFF Relay3

    //Check whether the 1st byte of data is '4'{

    //Check whether the 2nd byte of data is 'N'

    //Turn ON Relay4

    //Check whether the 2nd byte of data is 'F'

    //Turn OFF Relay4

    //Check whether the 1st byte of data is 'X'

    //Check whether the 2nd byte of data is 'N'

    //Turn ON all the Relays

    //Check whether the 2nd byte of data is 'F'

    //Turn OFF all the Relays

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    39/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP37I

    else

    {

    P2=0x00;

    }

    }

    }

    void delay()

    {

    unsigned int x=60000;

    while (x--);

    }

    //Clear Port 2 all bits to 0 if any other variable has been received

    //Function for delay routine//Delay Routine

    // larger the value of x the more is the delay.

    // executes this statement until x decrements to 0

    Fig.7 shows the circuit of simple 8051 Microcontroller interfaced with XBee and 1x4 Keypad.Program 7 demonstrates how to send data wirelessly when a key is being pressed.

    Components/modules required :

    1) 8051 project board (assembled/non assembled kit).

    2) 5V DC source.

    3) XBee(S2)

    4) XBee power supply board

    5) 1X4 keys keypad.

    6) IC AT89S52.

    7) 8051 IC burner.

    8) Connectors and cables.

    Fig 7. Circuit Diagram for LCD and 1x4 keypad interfacing

    Keypad and XBee interfacing using 8051 Microcontroller and Keil AT89S52

    Circuit and Working:

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    40/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP38I

    Program 7:

    #include

    sbit key1=P0^3;

    sbit key2=P0^2;

    sbit key3=P0^1;sbit key4=P0^0;

    void DELAY();

    void main()

    {

    unsigned char flag1=0,flag2=0,flag3=0,flag4=0; //Variable declarations

    TMOD=0X20;

    SCON=0X50;

    TH1=0XFD;TR1=1;

    DELAY();

    TI=0;

    SBUF='S';

    while (TI==0);

    TI=0;

    DELAY();

    P1=0X00;

    DELAY();while(1)

    {

    if (key1 ==0 && flag1==0)

    {

    SBUF='A';

    while (TI==0);

    TI=0;

    flag1=1;

    while(key1==0);DELAY();

    }

    else if (key1 ==0 && flag1==1)

    {

    SBUF='B';

    while (TI==0);

    TI=0;

    flag1=0;

    while(key1==0);

    DELAY();}

    //special function register declarations

    //for the intended 8051 derivative

    // Keypad connections

    //Switch 1 is connected to Port 0 pin 3

    //Switch 2 is connected to Port 0 pin 2

    //Switch 3 is connected to Port 0 pin 1//Switch 4 is connected to Port 0 pin 0

    //Call Function declarations for delay

    //Serial Initialization

    //use Timer 1, mode 2

    //indicating serial mode 1, where an 8-bit data is

    //framed with start and stop bits

    //9600 baud rate//Start timer

    //Wait for a delay for serial initialization to finish

    // Transmit 'S' to check whether the setup is ready

    //Forcibly clear the Transmit Interrupt Flag of 8051 to 0

    //Move 'S' to serial buffer memory

    //Wait until TI flag is set by hardware when an

    //entire byte has been transmitted

    //Forcibly clear TI flag

    //A small delay for relaxation

    //Set Port 1 all bits to 0

    //Continuous loop

    //Check whether switch 1 is being pressed

    //flag1 is Low

    //Move 'A' to serial buffer memory

    //Wait until TI flag is set by hardware when an

    //entire byte has been transmitted

    // Forcibly clear TI flag

    //Set flag1

    //Wait until the switch has been released//A small delay for relaxation

    //Check whether switch 1 is being pressed

    //and flag1 is Low

    //Move 'B' to serial buffer memory

    //Wait until TI flag is set by hardware when an

    //entire byte has been transmitted

    // Forcibly clear TI flag

    //Clear flag1

    //Wait until the switch has been released

    //A small delay for relaxation

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    41/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP39I

    else if (key2 ==0 && flag2==0)

    {

    SBUF='C';

    while (TI==0);

    TI=0;flag2=1;

    while(key2==0);

    DELAY();

    }

    else if (key2 ==0 && flag2==1)

    {

    SBUF='D';

    while (TI==0);

    TI=0;

    flag2=0;

    while(key2==0);

    DELAY();

    }

    else if (key3 ==0 && flag3==0)

    {

    SBUF='E';

    while (TI==0);

    TI=0;

    flag3=1;

    while(key3==0);

    DELAY();

    }

    else if (key3 ==0 && flag3==1)

    {

    SBUF='F';

    while (TI==0);

    TI=0;

    flag3=0;

    while(key3==0);

    DELAY();

    }

    else if (key4 ==0 && flag4==0)

    {

    SBUF='G';

    while (TI==0);

    //Check whether switch 2 is being pressed

    //and flag2 is Low

    //Move 'C' to serial buffer memory

    //Wait until TI flag is set by hardware

    //when an entire byte has been transmitted

    //Forcibly clear TI flag//Set flag2

    //Wait until the switch has been released

    //A small delay for relaxation

    //Check whether switch 2 is being pressed

    //and flag2 is High

    //Move 'D' to serial buffer memory

    //Wait until TI flag is set by hardware

    //when an entire byte has been transmitted

    //Forcibly clear TI flag

    //Clear flag2

    //Wait until the switch has been released

    //A small delay for relaxation

    //Check whether switch 3 is being pressed

    //and flag3 is Low

    //Move 'E' to serial buffer memory

    //Wait until TI flag is set by hardware

    //when an entire byte has been transmitted//Forcibly clear TI flag

    //Set flag3

    //Wait until the switch has been released

    //A small delay for relaxation

    //Check whether switch 3 is being pressed

    //and flag3 is High

    //Move 'F' to serial buffer memory

    //Wait until TI flag is set by hardware

    //when an entire byte has been transmitted// Forcibly clear TI flag

    //Clear flag3

    //Wait until the switch has been released

    //A small delay for relaxation

    //Check whether switch 4 is being pressed

    //and flag4 is Low

    //Move 'G' to serial buffer memory

    //Wait until TI flag is set by hardware//when an entire byte has been transmitted

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    42/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP40I

    TI=0;flag4=1;while(key4==0);DELAY();}else if (key4 ==0 &&flag4==1)

    {SBUF='H';while (TI==0);

    TI=0;flag4=0;while(key4==0);DELAY();}}}

    void DELAY(){unsigned int X=60000;while(X--);}

    // Forcibly clear TI flag//Set flag4//Wait until the switch has been released//A small delay for relaxation

    //Check whether switch 4 is being pressed

    //and flag4 is High

    //Move 'H' to serial buffer memory//Wait until TI flag is set by hardware//when an entire byte has been transmitted// Forcibly clear TI flag//Clear flag4//Wait until the switch has been released//A small delay for relaxation

    //Function for delay routine

    // larger the value of X the more is the delay.// executes this statement until / X decrements to 0

    3 Axis Accelerometer

    Quick Overview

    3-axis accelerometer to now have an on-board 3.3V regulator -

    making it a perfect choice for interfacing with a 5V microcontroller

    such as the . This breakout comes with 3 analog outputs for X, Y and Z

    axis breakout board. The ADXL335 is the latest and greatest from

    Analog Devices, known for their exceptional quality MEMS devices.

    The VCC takes up to 5V in and regulates it to 3.3V with an output pin.

    The analog outputs are ratiometric: that means that 0g measurement

    output is always at half of the 3.3V output (1.65V), -3g is at 0v and 3g

    is at 3.3V with full scaling in between. Fully assembled and tested.

    The XYZ filter capacitors are 0.1uF for a 50 Hz bandwidth

    Tips

    Code & Schematic

    For more details

    www.researchdesignlab.com

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    43/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP41I

    LEDs and XBee interfacing using 8051 Microcontroller and Keil AT89S52

    Circuit and Working:

    Fig.8 shows the circuit of simple 8051 Microcontroller interfaced with XBee and 8 LEDs.

    Program 8 demonstrates how to receive serial data wirelessly and toggle the state of a LED.

    Components/modules required :

    1) 8051 project board (assembled/non assembled kit).

    2) 5V DC source.

    3) XBee(S2)

    4) XBee power supply board

    5) 8 LEDs.

    6) Resistors (1Kx8).

    7) IC AT89S52.

    8) 8051 IC burner.

    9) Connectors and cables.

    Fig. 8 Circuit Diagram for XBee and LED interfacing

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    44/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP42I

    Program 8:

    #include

    void delay();

    sbit LED0=P2^0;

    sbit LED1=P2^1;sbit LED2=P2^2;

    sbit LED3=P2^3;

    sbit LED4=P2^4;

    sbit LED5=P2^5;

    sbit LED6=P2^6;

    sbit LED7=P2^7;

    unsigned char byte1,byte2;

    void main()

    {

    TMOD=0X20;

    SCON=0X50;

    TH1=0XFD;

    TR1=1;

    delay();

    TI=0;

    SBUF='S';

    while (TI==0);

    TI=0;

    delay();

    P2=0x00;

    while(1)

    {

    RI=0;

    while(RI==0);

    byte1=SBUF;

    RI=0;

    if(byte1=='A')

    {

    LED0=1;

    LED4=1;

    }

    else if(byte1=='B')

    //special function register declarations

    //for the intended 8051 derivative

    // Function prototype declaration

    //Define Port Pin P2.0 as LED0

    //Define Port Pin P2.1 as LED1//Define Port Pin P2.2 as LED2

    //Define Port Pin P2.3 as LED3

    //Define Port Pin P2.4 as LED4

    //Define Port Pin P2.5 as LED5

    //Define Port Pin P2.6 as LED6

    //Define Port Pin P2.7 as LED7

    // Variable declarations

    // MAIN CODE

    //Serial Initialization

    //use Timer 1, mode 2

    //indicating serial mode 1, where an 8-bit

    //data is framed with start and stop bits

    //9600 baud rate

    //Start timer

    //Wait for some time for serial initialization to finish

    // Transmit 'S' to check whether the setup is ready

    //Forcibly change the Transmit Interrupt//Flag of 8051 to 0

    //Move 'S' to serial buffer memory

    //Wait until TI flag is set by hardware

    //when an entire byte has been transmitted

    // Forcibly clear TI flag

    //A small delay for relaxation

    //Set Port 2 all bits to 0

    // continuous loop

    //Forcibly clear the Receive Interrupt

    //Flag of 8051 to 0

    //Wait until RI flag is set by hardware

    //when an entire byte has been received

    //Move the received byte of data into variable 'byte1'

    //Forcibly clear RI flag

    //Check whether the received byte of data is 'A'

    //Turn on LED0

    //Turn on LED4

    //Check whether the received byte of data is 'B'

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    45/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP43I

    {

    LED0=0;

    LED4=0;

    }

    else if(byte1=='C')

    {

    LED1=1;

    LED5=1;

    }

    else if(byte1=='D')

    {

    LED1=0;

    LED5=0;

    }

    else if(byte1=='E')

    {

    LED2=1;LED6=1;

    }

    else if(byte1=='F')

    {

    LED2=0;

    LED6=0;

    }

    else if(byte1=='G')

    {

    LED3=1;

    LED7=1;}

    else if(byte1=='H')

    LED3=0;

    LED7=0;

    }

    else

    {

    P2=0x00;

    delay();

    }

    }

    }

    void delay()

    {

    unsigned int x=60000;

    while (x--);

    }

    //Turn off LED0

    //Turn off LED4

    //Check whether the received byte of data is 'C'

    //Turn on LED1

    //Turn on LED5

    //Check whether the received byte of data is 'D'

    //Turn off LED1

    //Turn off LED5

    //Check whether the received byte of data is 'E'

    //Turn on LED2//Turn on LED6

    //Check whether the received byte of data is 'F'

    //Turn off LED2

    //Turn off LED6

    //Check whether the received byte of data is 'G'

    //Turn on LED3

    //Turn on LED7

    //Check whether the received byte of data is 'H'

    {

    //Turn off LED3

    //Turn off LED7

    //Set Port 2 all bits to 0

    //if any other variable has been received//Wait for a small delay

    // Delay Routine

    // larger the value of x the more is the delay.

    // executes this statement until x decrements to 0

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    46/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP44I

    GSM Modem and LCD interfacing using 8051 Microcontroller and Keil AT89S52

    Circuit and Working:

    Fig.9 shows the circuit of simple 8051 Microcontroller interfaced with GSM Modem and LCD. The

    Modem sends an SMS every time you turn on your microcontroller. Following this the modem will

    be waiting for any message to be received, once a message has been received, the message will

    be displayed on the LCD.

    Program 9 demonstrates how to initialize GSM modem through AT commands via a serialinterface and send/receive a SMS through it.

    Please note: GSM modem needs to be turned on at least 10 seconds before you turn on the micro

    controller (GSM takes a few seconds to turn on)

    Fig.9Circuit Diagramfor GSM Modem

    and 16X2 LCD

    Components/modules required :

    1)8051 project board (assembled/non assembled kit).

    2)5V and 12V,1A DC source.

    3)GSM Module.

    4)16X2 LCD interfacing module.

    5)IC AT89S52.

    6)8051 IC burner.7)Connectors and cables.

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    47/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP45I

    Program 9:

    #include"reg52.h"

    #define CR 0X0D

    #define LF 0X0A

    #define EOM 0X1A

    #define LCD_PORT P2

    sbit rs=P3^5;

    sbit rw=P3^6;

    sbit en=P3^7;

    sbit D7=P2^7;

    void busy();

    void CMD_WRT(unsigned char);

    void LCD_WRT(unsigned char *);

    void DATA_WRT(unsigned char);

    void TRANSMIT(unsigned char *);

    void transmit_byte(unsigned char );

    void READ_SMS(void);

    void SEND_CMD(unsigned char *BASE_ADD,unsigned char COUNT);

    void SEND_CRLF(unsigned char);

    unsigned char recv_byte (void);

    void RX_REPLY();

    void ENTER(void);

    void DELAY();

    unsigned char count12=0,message[50];

    unsigned char byte1,k,temp;

    unsigned char Test_Text[]="GSM Testing";

    unsigned char code CMD_1[]="AT";

    unsigned char code CMD_3[]="AT+CMGF=1";

    unsigned char code CMD_4[]="AT+CMGD=1";

    unsigned char code CMD_9[]="AT+CMGS=\"7411001407\"";

    //Special function register declarations

    //for the intended 8051 derivative

    //Define CR as 13

    //Define LF as 10

    //Define EOM as CNTRL+Z

    // LCD connections//Define Port 2 as LCD Data pins

    //Register Select is connected to Port 3 pin 5

    //Read/Write is connected to Port 3 pin 6

    //Enable is connected to Port 3 pin 7

    //Data Pin D7 is connected to Port 2 pin 7

    // Call function declarations

    //This Function checks whether the LCD is ready to

    //receive next byte

    //This Function is used to write commands

    //into the LCD//This Function is used to write Strings

    //into the LCD

    //This Function is used to write a byte of

    //data into the LCD

    //This Function is used to write Strings into

    //the serial Port

    //This Function is used to write a byte of

    //data into the serial Port

    //This function separates the text message from

    //the SMS received

    //This function sends GSM commands via the serial interface

    //This function writes a 8 bit hex value into

    //the serial interface

    //This function receives a byte of data

    //through the serial interface

    //This function waits for a character 'K'(of OK) which

    //the GSM modem replies for its commands

    //This function is used to hit enter into the GSM

    //modem whenever required

    //Call Function declarations for delay

    // Global variable declaration and initialization

    // GSM commands declaration

    //GSM Attention command

    //GSM text initialization

    //command

    //Delete previous SMS

    //command

    //Edit the 10//digit destination number here

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    48/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP46I

    void main()

    {

    unsigned char CMD[]={0x38,0x0f,0x01,0x06,0x80};

    unsigned char I,TEMP,count=0;

    for(i=0;i

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    49/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP47I

    SEND_CMD(CMD_3,9);

    DELAY();

    ENTER();

    RX_REPLY();

    DELAY();

    SEND_CMD(CMD_4,9);

    DELAY();

    ENTER();

    RX_REPLY();

    DELAY();

    SEND_CMD(CMD_9,20);

    DELAY();

    ENTER();DELAY();

    TRANSMIT(Test_Text);

    DELAY();

    transmit_byte(EOM);

    RX_REPLY();

    DELAY();

    CMD_WRT(0X01);

    CMD_WRT(0X80);

    LCD_WRT("TEXT SENT!!");DELAY();

    while(1)

    {

    DELAY();

    CMD_WRT(0XC0);

    LCD_WRT("WAITING..");

    DELAY();

    READ_SMS();

    DELAY();

    }

    }

    void busy()

    {

    D7=1;

    rs=0;

    rw=1;while(D7!=0)

    //Enter GSM text initialization command

    //A small delay for relaxation

    //Hit enter

    //Wait for GSM modem to respond

    //A small delay for relaxation

    //Enter Delete previous SMS command to free up

    //space for new SMS

    //A small delay for relaxation

    //Hit enter

    //Wait for GSM modem to respond

    //A small delay for relaxation

    //Enter the command for sending the SMS to a

    //destination number

    //A small delay for relaxation

    //Hit enter//A small delay for relaxation

    //Enter the text within the previously declared

    //variable Test_Text

    //A small delay for relaxation

    //Enter ASCII equivalent of CNTRL+Z

    //Wait for GSM modem to respond

    //A small delay for relaxation

    //This command clears the LCD screen

    //This moves the cursor to the beginning of the 1st line

    //Writes the text 'TEXT SENT!!' in the LCD//A small delay for relaxation

    //Continuous loop

    //A small delay for relaxation

    //This moves the cursor to the beginning of the 2nd line

    //Writes the text 'WAITING..' from the current pointer

    //of LCD

    //A small delay for relaxation

    //Call 'READ_SMS()' function which waits until a SMS has been

    //received and separates the text message from it

    //A small delay for relaxation

    //This Function checks whether the LCD is ready to receive next byte

    //Keep D7 pin to High

    //Keep RS to Low to select command register

    // RW=1 for read//Monitor D7 pin until it gets low

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    50/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP48I

    SEND_CRLF(LF);

    }

    void SEND_CRLF(unsigned char CRLF)

    {

    SBUF=CRLF;

    while(TI==0);

    TI=0;

    }

    void SEND_CMD(unsigned char *BASE_ADD,unsigned char COUNT){

    unsigned char I;

    for(I=0;I

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    51/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP49I

    {

    en=0;

    en=1;

    }

    }

    void CMD_WRT(unsigned char val)

    {

    busy();

    LCD_PORT=val;

    rs=0;

    rw=0;

    en=1;

    en=0;

    }

    void LCD_WRT(unsigned char *string)

    {

    while(*string)

    DATA_WRT(*string++);

    }

    void DATA_WRT(unsigned char ch)

    {

    busy();

    LCD_PORT = ch;

    rs=1;

    rw=0;

    en=1;

    en=0;

    }

    void DELAY(){

    unsigned int X=60000,Y=60000;

    while(X--);

    while(Y--);

    }

    void ENTER(void)

    {

    SEND_CRLF(CR);

    //Provide a latch pulse from low to high to EN

    //This Function is used to write commands into the LCD

    //Execute busy function to know whether the LCD is

    //ready to receive any data/command

    //Put the variable val into LCD_PORT which is

    //connected to LCD data pins

    //Keep RS to Low to select command register

    // RW=0 for write

    //Provide a latch pulse from High to Low to EN

    //This Function is used to write Strings into the LCD

    // increment from the beginning of the string until a

    //null character is detected (end of the string)

    // separates a single byte from the string

    //This Function is used to write a byte of data into the LCD

    //Execute busy function to know whether the LCD//is ready to receive any data/command

    //Put the variable val into LCD_PORT which is

    //connected to LCD data pins

    //Keep RS to High to select Data register

    // RW=0 for write

    //Provide a latch pulse from High to Low to EN

    //Function for delay routine

    // larger the value of X and Y the more is

    //the delay.

    // executes this statement until X decrements to 0;

    // executes this statement until Y decrements to 0;

    //Function used for sending ENTER command to the GSM Modem

    //Send the value CR(13) to the GSM modem via the

    //serial interface

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    52/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP50I

    // Function used to transmit a string of data into the serial interface

    //execute the next statement till the end of the string

    //Pass the 8 bit data located at the address location

    // of the pointer to the function 'transmit_byte'

    //Function used for sending an 8 bit data present on the variable// 'byte' to the serial interface

    //Move the contents of the variable 'byte' to serial buffer memory

    //Wait until TI flag is set by hardware when an entire byte has

    //been transmitted

    //Forcibly clear TI flag

    //This function waits until a SMS has been received and separates the text//message from it and displays it on the LCD

    //GSM read SMS command

    //Local Variables declaration and initialization

    //Assign any value to byte other than '+'

    //Breaks this loop only when '+' has been received, till then

    //the variable 'byte' keeps waiting for '+' to be received

    //Keep checking for the data received in the

    //call function 'recv_byte'//A small delay for relaxation

    //A small delay for relaxation

    //Enter the command used for reading a SMS

    //Hit enter

    //The string that comes next includes quotes(") along with other

    //information like time, date, number etc. of the received SMS

    //Wait for at least 8 such quotes(")

    //Breaks this loop only when '"' has been received, till then

    //the variable 'byte' keeps waiting for '"' to be received

    //Keep checking for the data received in the call

    //function 'recv_byte'

    //Assign any value to byte other than '"'

    //Assign any value to byte other than '13'

    //Breaks this loop only when '13' has been received, till then

    //the variable 'byte' keeps waiting for '13' to be received

    //Keep checking for the data recieved in the//call function 'recv_byte'

    void TRANSMIT(unsigned char *string)

    {

    while(*string)

    transmit_byte(*string++);

    }

    void transmit_byte(unsigned char byte)

    {

    SBUF=byte;

    while(!TI);

    TI=0;

    }

    void READ_SMS(void)

    {

    unsigned char code CMD_5[]="AT+CMGR=1";

    unsigned char byte,i=0, flag=0;

    byte=0;

    while(byte!='+')

    {

    byte=recv_byte();

    }DELAY();

    DELAY();

    SEND_CMD(CMD_5,9);

    ENTER();

    for(i=0;i

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    53/59

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    54/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP52I

    Analog to digital conversion in 8051/89c52 Microcontroller and Keil - ADC0804

    Circuit and Working:Fig 10. shows the circuit of simple 8051 Microcontroller interfaced with LCD Display and analog

    to digital converter IC ADC0804. After the connections are done properly, you will be able to

    view the digital value on the LCD display.Program 10 demonstrates how to read the analog values through IC ADC0804. The output of this

    IC will be an 8 bit value, this 8 bit value would be connected to a microcontroller. The

    microcontroller is programmed in such a way to read the port pin values and display its

    equivalent decimal value on the LCD display.

    Components/modules required:

    1) 8051 project board (assembled/non assembled kit).

    2) 5V DC source.

    3) LCD interfacing Module.

    4) IC ADC0804 with the circuit

    connections as shown in Fig 10.2

    5) IC AT89S52.

    6) 8051 IC burner.

    7) Connectors and cables.

    Fig10.1: Circuit Diagram for LCD and ADC interfacing-Part 1.

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    55/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP53I

    Fig 10.2: Circuit Diagram for LCD and ADC interfacing-Part 2.

    Programme 10#include

    #include

    #define adc_port P1

    #define LCD_PORT P2

    sbit rs=P3^5;

    sbit rw=P3^6;

    sbit en=P3^7;

    sbit D7=P2^7;

    void busy();

    void CMD_WRT(unsigned char);

    void LCD_WRT(unsigned char *);

    void DATA_WRT(unsigned char);

    void CONVERT_DISPLAY(unsigned char);

    void DELAY();

    //Define Port 1 as ADC port Data pins

    // LCD connections

    //Define Port 2 as LCD Data pins

    //Register Select is connected to Port 3 pin 5

    //Read/Write is connected to Port 3 pin 6

    //Enable is connected to Port 3 pin 7

    //Data Pin D7 is connected to Port 2 pin 7

    // Call function declarations

    //This Function checks whether the LCD is ready to receive

    //next byte

    //This Function is used to write commands into the LCD

    //This Function is used to write Strings into the LCD

    //This Function is used to write a byte of data into the LCD

    //This Function is used to convert Hex data to Decimal

    //equivalent and write into the LCD

    //Call Function declarations for delay

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    56/59

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    57/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP55I

    }

    void busy()

    {D7=1;

    rs=0;

    rw=1;

    while(D7!=0)

    {

    en=0;

    en=1;

    }

    }

    void CMD_WRT(unsigned char val)

    {

    busy();

    LCD_PORT=val;

    rs=0;

    rw=0;

    en=1;

    en=0;

    }

    void LCD_WRT(unsigned char *string)

    {

    while(*string)

    DATA_WRT(*string++);

    }

    void DATA_WRT(unsigned char ch)

    {

    busy();

    LCD_PORT = ch;

    rs=1;

    rw=0;

    en=1;

    en=0;

    }

    //This Function checks whether the LCD is ready to receive next byte

    //Keep D7 pin to High

    //Keep RS to Low to select command register

    // RW=1 for read

    //Monitor D7 pin until it gets low

    //Provide a latch pulse from low to high to EN

    //This Function is used to write commands into the LCD

    //Execute busy function to know whether the LCD is ready to receive any data/command

    //Put the variable val into LCD_PORT which is connected to LCD data pins

    //Keep RS to Low to select command register

    // RW=0 for write

    //Provide a latch pulse from High to Low to EN

    //This Function is used to write Strings into the LCD

    //increment from the beginning of the string until a

    //null character is detected (end of the string)

    // separates a single byte from the string

    //This Function is used to write a byte of data into the LCD

    //Execute busy function to know whether the LCD is ready to receive any

    //data/command

    //Put the variable val into LCD_PORT which is connected to LCD data pins

    //Keep RS to High to select Data register

    // RW=0 for write

    //Provide a latch pulse from High to Low to EN

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    58/59

    RESEARCH DESIGN LABS | VOLUME 1, ISSUE 1 WWW.RESEARCHDESIGNLAB.COMP56I

    //This Function is used to convert Hex data to Decimal equivalent

    //and write into the LCD

    //Local variable declarations

    //Local variable declarations//Local variable declarations

    //Move the Hex value 'd' to a variable 'temp'

    //Get the last number

    //Get the 2nd last number

    //Get the first number

    //Concatenate all the value into a single variable

    //or'ing with 0x30 will give

    //the ASCII equivalent of the decimal value

    //Write the 8 bit data into the LCD

    //Make WR low

    //nop delay

    //Make WR high

    //Wait for INTR to go low

    //Make RD low

    //Read ADC port

    //Make RD high

    void CONVERT_DISPLAY(unsigned char d)

    {

    unsigned char dig1,dig2,dig3,dig[3];

    unsigned char x;unsigned char temp;

    temp=d;

    temp=temp/10;

    dig1=d%10;

    dig2=temp%10;

    dig3=temp/10;

    dig[0]=dig3;

    dig[1]=dig2;dig[2]=dig1;

    CMD_WRT(0XCA);

    for(x=0;x

  • 8/10/2019 Micro Controller 8051_89S52 Embedded C Programming

    59/59

    www rese rchdesignl b comwww.researchdesignlab.com

    10101010101010101010101010

    1010101010101010101010101010101010101010101010

    10100101010101010101010101010101010101010101010101010101

    0 1 01 0 10 0 10 1 01 0 10 10 1 1 0 10 1 01 0 10 10 1 01 0 10

    0 10 10 10 10 10 01 01 01 1 01 01 01 01 01 01 01

    1 01 01 01 01 01 00 10 1 01 01 01 01 01 01 01

    01 010 10 10 10 10 01 10 10 10 10 10 10 10

    01 010 10 10 10 10 01 10 10 10 10 10 10 10

    1 01 01 01 01 01 00 10 1 01 01 01 01 01 01 01

    1 01 01 01 01 00 10 10 1 0 10 10 10 10 10 10 10 1

    0 1 01 0 10 0 10 1 01 0 10 10 1 0 1 01 0 10 1 01 0 10 1 01 0 10

    1010010101010101010101010101010101010101010101010101010

    10101010101010101010101010101010101010101 1

    1 1 101010101010101 1

    010101010101010101010101010101010101

    001010101010101010101010101010101010101010101010101

    1 0 1 01 0 0 10 1 0 10 1 0 10 1 0 10 1 1 1 0 1 01 0 1 01 0 1 01 0 1 01 0 1 01

    0 10 10 10 10 01 01 01 01 0 10 10 10 10 10 10 10 10

    0 10 10 10 10 10 01 01 0 10 10 10 10 10 10 10

    1 01 01 01 01 01 00 1 0 10 10 10 10 10 10 10

    01 010 10 10 10 10 0 10 10 10 10 10 10 10

    1 01 01 01 01 01 00 10 0 10 10 10 10 10 10 1

    0 10 10 10 10 10 01 01 0 0 10 10 10 10 10 10 10

    1 0 10 1 01 0 01 0 10 1 01 0 1 0 10 1 01 0 10 1 01 0 10 1 0

    0101001010101010101010101 1010101010101010101010101

    010101010101010101010101010101010101010101010101

    1 1010101010101010101010101010101

    010101010101010101010101010101

    01010101010101010101010101010101010101010101010

    101010010101010101010101 1 1 1 1 1010101010101010101010101

    0 1 01 0 10 1 00 1 01 01 0 1 0 1 01 01 0 10 1 01 0 10 1 01

    0 10 10 10 10 10 01 01 1 01 01 01 01 01 01 01

    0 10 10 10 10 10 10 01 1 01 01 01 01 01 01 0

    01 01 01 01 01 01 00 01 01 01 01 01 01 01

    01 01 01 01 01 01 00 1 01 01 01 01 01 01 01

    1 01 01 01 01 01 00 10 1 0 10 10 10 10 10 10 1

    1 01 01 01 01 00 10 10 10 0 10 10 10 10 10 10 10 10

    1 0 1 01 0 0 10 1 0 10 1 0 10 1 0 1 0 1 0 10 1 0 10 1 0 10 1 0 10 1 0 10 1

    10010101010101010101010101010101010101010101010101

    1010101010101010101010101010101010101

    010101010101010

    10101010101010101010101010101010101010101

    010010101010101010101010101010101010101010101010101010

    1 0 1 01 0 1 00 1 0 10 1 0 10 1 0 1 1 0 1 01 0 1 01 0 1 01 0 1 01 0 1 0

    1 01 01 01 01 00 10 10 1 1 01 01 01 01 01 01 01 01

    1 01 01 01 01 01 00 1 1 01 01 01 01 01 01 0

    01 01 01 01 01 01 00 01 01 01 01 01 01 01

    01 01 01 01 01 01 00 01 01 01 01 01 01 01

    0 10 10 10 10 10 10 01 1 01 01 01 01 01 01 0

    0 10 10 10 10 10 01 01 0 10 10 10 10 10 10 10 1

    0 1 01 0 10 1 00 1 01 01 0 10 1 0 1 01 01 0 10 1 01 0 10 1 01

    1010100101010101010101010101010101010101010101010101010101

    101010101010101010101010101010101010101010101

    1 1010101010101010101010101 1

    ResearchDesign Lab

    DO IT YOURSELF

    FUN

    IF YOUTHINK THAT

    PROJECTS ARE

    Sensors, Modules, Robotics & DIY Kits

    Soil-Moisture Sensor Serial 8 ChannelAC 230V SSR and Dimmer

    Power Line Modem

    10 min. Voice Playbackand Recorder Kit

    Data Logger Shield

    Serial 3 ChannelAC 230V SSR and Dimmer

    Visit our website for Live demo and documents of DIY Kits project

    Digital Dimmer

    3 Axis Accelorometer Bread Board Power Supply Gas Sensor Gyro Sensor Carbon Monoxide Sensor IR Obstacle Sensor

    PIC Project Board

    LiFi TX(Visible Light Communication)

    Real Time Clock

    Xbee USBAdapter with FT232RL

    Xbee AdapterDevelopment Board

    4 Channel IsolatedRelay Board