3rd Internal

download 3rd Internal

of 38

Transcript of 3rd Internal

  • 8/3/2019 3rd Internal

    1/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    MICROCONTROLLER ASSIGNMENT 3

    Question 1:- Explain the registers and pins of an LCD panel and write and 8051 C

    program to display the message HELLO on the LCD panel.

    LCD pin descriptions:

    Pin Symbol I/O Description

    1 Vss - Ground

    2 Vcc - +5V power supply

    3 VEE - power supply to control

    4 RS I RS=0 to select code register

    RS=1 to select data register5 R/W I R/W=0 for write R/W for read

    6 E I/O Enable

    7 DB0 I/O The 8-bit data bus

    8 DB1 I/O The 8-bit data bus

    9 DB2 I/O The 8-bit data bus

    10 DB3 I/O The 8-bit data bus

    11 DB4 I/O The 8-bit data bus

    12 DB5 I/O The 8-bit data bus

    13 DB6 I/O The 8-bit data bus

    14 DB7 I/O The 8-bit data bus

    VCC , VSS and VEE :-

    While VCC and VSS provide +5V and ground, respectively, VEE is used for controlling LCD

    contrast.

    RS, register select:-

    If RS=0, the instruction command code register is selected, allowing the user to send a

    command such as clear display, return home etc.

    If RS=1 the data register is selected, allowing the user to send data to be displayed on

    the LCD.

  • 8/3/2019 3rd Internal

    2/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    R/W, read/write: R/W input allows the user to write the information to the LCD or read

    the information from it.

    E, enable:-

    The enable pin is used by the LCD to latch information presented to its data pins. When

    data is supplied to data pins, a high-to-low pulse must be applied to this pin in order for

    the LCD to latch in the data present at the data pins. This pulse must be a minimum of

    450ns wide.

    D0-D7:-The 8-bit data pins, D0-D7, are used to send information to the LCD or read the

    contents of the LCDs internal registers. To display the letters and numbers we send

    ASCII codes and also there are instruction command codes we send to these pins while

    making RS=1.

    We also use RS=0 to check the busy flag bit to see if the LCD is ready to receive

    information. The busy flag is D7 and can be read when R/W=1, RS=0.

    Registers of LCD panel :-There are main two registers inside the LCD

    1.Data register: This register is selected when RS=1. Here LCD stores the messageto be displayed i.e. allowing the user to send data to be displayed on the LCD.

    2.Code register: This register is selected when RS=0. Allowing the user to send acommand to LCD. The following table lists the command codes.

    Code (hex) Command to LCD instruction register

    1 Clear display screen

    2 Return home

    4 Shift cursor to left

    5 Shift display right

    6 Shift cursor to right

    7 Shift display left

  • 8/3/2019 3rd Internal

    3/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    8 Display off, cursor off

    A Display off, cursor on

    C Display on, cursor off

    E Display on, cursor blinking

    F Display on, cursor blinking10 Shift cursor position to left

    14 Shift cursor position to right

    18 Shift entire display to the left

    1C Shift entire display to the right

    80 Force cursor to beginning of 1st

    line

    C0 Force cursor to beginning of 2nd

    line

    38 2 lines and 5*7 matrix

    C-Program:

    #include

    sfr ldata = 0x90; //P1=LCD data pins

    sbit rs = P2^0;

    sbit rw = P2^1;

    sbit en =P2^2;

  • 8/3/2019 3rd Internal

    4/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    void lcdcmd(unsigned char value)

    {

    ldata = value; //put the value on the pins

    rs = 0; //to select code register

    rw =0;en = 1; //strobe the enable pin

    MSDelay (1);

    en=0;

    return;

    }

    void lcddata (unsigned char value)

    {

    ldata = value; //put the value on the pins

    rs = 1; //to select the data registerrw =0;

    en = 1; //strobe the enable pin

    MSDelay (1);

    en=0;

    return;

    }

    void MSDelay (unsigned int itime){

    Unsigned int i, j;

    for (i=0; i

  • 8/3/2019 3rd Internal

    5/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    MSDelay (250);

    lcdcmd (0x01); // Clear display screen

    MSDelay (250);

    lcdcmd (0x06); // Shift cursor to right after inserting each //characterMSDelay (250);

    lcdcmd (0x86); //line 1, position 6

    MSDelay (250);

    for (k=0; k

  • 8/3/2019 3rd Internal

    6/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Q2. Draw the 8051 connection to DAC0808 at port P1 and write

    8051 ALP to generate a sine wave

  • 8/3/2019 3rd Internal

    7/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    ALP:

    ORG 40

    AGAIN: MOV DPTR, #TABLE ;Initialize DPTR to start of table

    MOV R2, #COUNT ;Initialize count

    BACK: CLR A

    MOVC A,@A+DPTR

    Angle(Degrees) Sin (angle) Voltage Voltage sent to

    Magnitude DAC5V + (Voltage Mag. *

    (5V*Sin(angle)) 25.6)

    0 0 5 128

    30 0.5 7.5 192

    60 0.87 9.33 238

    90 1 10 255

    120 0.87 9.33 238

    150 0.5 7.5 192

    180 0 5 128

    210 -0.5 2.5 64

    240 -0.87 0.67 17

    270 -1 0 0

    300 -0.87 0.67 17

    330 -0.5 205 64

    360 0 5 128

  • 8/3/2019 3rd Internal

    8/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    MOV P1,A ;Move values from LUT to P1

    INC DPTR

    DJNZ R2,BACK ;Loop the values

    SJMP AGAIN ;Looping sine wave

    ORG 300H ;Initializing LUTTABLE: DB 128,192,238,255,192,128,64,17,0,17,64,128

    END

    Question 3:-Show a simple keyboard interfacing with Port 1 and Port 2 of 8051 and

    explain its operation.

    Keyboards are organized in a matrix of rows & columns. The cup Accesses both rows &

    columns through ports; therefore, with two 8 bit Ports, an 8 8 matrix of keys can be

    connected to a microprocessor. When a key is pressed, a row & a column make a

    contact; otherwise, there is no connection between rows & columns. In IBM pc

    keyboards, a single microcontroller takes care of hardware & software interfacing of

    the keyboard. In such system, it is the function of programs stored in the EPROM of the

    microcontroller to scan the keys continuously, identify which one has been activated, &

    present it to the motherboard.

    Fig shows 44 matrix connected to two ports. The rows are connected to anoutput port & the columns are connected to an input port. If no key has been pressed,

    reading the input port will yield 1s for all columns since they are

    connected to high (Vcc). If all the rows are grounded & a key is pressed, one of the

    columns will have 0 since the key pressed provides the path to ground. It is the function

    of the microcontroller to scan the keyboard continuously to detect & identify the key

    pressed.

    Operation:-

    To detect the pressed key, the microcontroller grounds all the rows by providing 0 to

    the output latch, then it reads the columns. If the data read from the columns is D3-

    D0=1111, no key has been pressed and the process continues until a key press is

  • 8/3/2019 3rd Internal

    9/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    detected. However, if one of the column bits has a zero, this means that a key press has

    occurred. After a key press is detected, the microcontroller will go through the process

    of identifying the key. Starting with the top row, the microcontroller grounds it by

    providing a low to row D0 only; then it reads the columns. If the data read is all 1s, no

    key in that row is activated and the process is moved to the next row. It grounds the

    next row, reads the columns, and checks for any zero. This process continues until the

    row is identified. After identification of the row in which the key has been pressed, the

    next task is to find out which column the pressed key belongs to. This should be easy

    since the microcontroller knows at any time which row and column are being accessed.

  • 8/3/2019 3rd Internal

    10/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    4. Show a scheme of interfacing ADC0809 to 8051 controller. Write

    an ALP to obtain the output from such an interface. Discuss

    practical application.

    Ans.

  • 8/3/2019 3rd Internal

    11/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    The ADC0809 chip has an 8 bit data output. The 8 analog input channels are

    multiplexed and selected according to the table(i) shown using 3 address pins A,B and

    C.

    Selected Analog

    ChannelC B A

    IN0 0 0 0

    IN1 0 0 1

    IN2 0 1 0

    IN3 0 1 1

    IN4 1 0 0

    IN5 1 0 1IN6 1 1 0

    IN7 1 1 1

    IN0

    IN7

    D0

    D7

    EOCOE

    Vref(+)Vref(-)

    gnd Clk Vcc

    SC ALE C B A

    (LSB)

    ADC0809

    Table 1

    Fig. 1

  • 8/3/2019 3rd Internal

    12/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Table 2

    Steps for programming the ADC0809:

    The following are the steps to get data from an ADC0809

    1. Select analog channel by providing bits to A, B and C address according totable(ii).

    2. Activate the ALE (address latch enable) pin. It needs an L-H pulse to latch inthe address.

    3. Activate SC (start conversion) by an L-H pulse to initiate conversion.4. Monitor EOC (end of conversion) to see whether conversion is finished. H-L

    output indicates that the data is converted and is ready to be picked up. If

    we do not use EOC, we can read the converted digital data after a brief

    time delay. The delay size depends on the speed of the external clock we

    connect to CLK pin.

    5. Activate OE (output enable) to read data out of the ADC chip. An L-H pulseto the OE pin will bring digital data out of the chip.

    Vref (V) Vin (V) Step Size (mV)

    Not connected 0 to 5 5 / 256 = 19.534.0 0 to 4 4 / 256 = 15.62

    3.0 0 to 3 3 / 256 = 11.71

    2.56 0 to 2.56 2.56 / 256 = 10

    2.0 0 to 2.0 2 / 256 = 7.81

    1.0 0 to 1.0 1 / 256 = 3.90

  • 8/3/2019 3rd Internal

    13/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    In ADC0809 there is no self clock and the clock must be prepared from an

    external source to CLK pin. The speed of conversion depends on the frequency

    of the clock connected to the CLK pin, it cannot be faster than 100

    microseconds.

    Programming ADC0809 in Assembly

    ALE BIT P2.4

    OE BIT P2.5

    SC BIT P2.6

    EOC BIT P2.7

    ADDR_A BIT 92.0

    ADDR_B BIT P2.1

    ADDR_C BIT P2.2

    MYDATA EQU P1

    ORG 0H

    MOV MYDATA, #0FFH ;make P1 as input

    SETB EOC ;make EOC as input

    CLOCK

    WR(SC)

    RD(OE)

    ALE

    ADDR

    EOC(INTR)

    D0-D7

    LATCH

    ADDRESSLATCH

    DATA

    Selecting a Channel and Read Timing for ADC0809

  • 8/3/2019 3rd Internal

    14/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    CLR ALE ;clear ALE

    CLR SC ;clear WR

    CLR OE ;clear RD

    BACK: CLR ADDR_C ;C=0

    CLR ADDR_B ;B=0SETB ADDR_A ;A=1 (select channel 1)

    ACALL DELAY ;make sure address is stable

    SETB ALE ;latch address

    ACALL DELAY ;delay for fast DS89C4x0 Chip

    SETB SE ;start conversion

    ACALL DELAY

    CLR ALE

    CLR SC

    HERE: JB EOC, HERE ;wait until doneHERE1:JNB EOC, HERE1 ;wait until done

    SETB OE ;enable RD

    ACALL DELAY ;wait

    MOV A, MYDATA ;read data

    CLR OE ;clear RD for next time

    ACALL CONVERSION ;hex to ASCII

    ACALL DATA_DISPLAY ;display the data

    SJMP BACK

  • 8/3/2019 3rd Internal

    15/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Question 5:- Describe the 8051 connection to stepper motor. A switch is

    connected to pin p2.7. Write a c program to monitor the status of SW and

    perform the following

    If SW=0, the stepper motor moves clockwise.

    If SW=1, the stepper motor moves counter-clockwise.

    The following steps show the 8051 connection to the stepper motor and its

    programming:

    1. Use an ohmmeter to measure the resistance of the leads. This should identify

    which COM leads are connected to which winding leads.

    2. The common wire(s) are connected to the positive side of the motors power

    supply. In many motors, +5V is sufficient.

    3. The four leads of the stator winding are controlled by four bits of the 8051

    port (P1.0 P1.3). However, since the 8051 lacks the sufficient current to drivethe stepper motor windings, we must use a driver such as the ULN2003 to

    energize the stator. Instead of the ULN2003, we could have used transistors as

    drivers, as shown in the figure below. However, notice that if transistors are used

    as drivers, we must also use diodes to take care of inductive current generated

    when the coil is turned off. One reason that using the ULN2003 is preferable to

    the use of transistors as drivers is that the ULN2003 has an internal diode to take

    care of the back EMF.

  • 8/3/2019 3rd Internal

    16/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Fig. 8051 Connection to Stepper

    // A C program to monitor P2.7 (SW) and rotate the stepper motor// clockwise if SW = 0; anticlockwise if SW = 1

    #include

    sbit SW=P2^7;

    void main() {

    SW=1;

    while(1) {

    if(SW==0)

    {

  • 8/3/2019 3rd Internal

    17/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    P1=0x66;

    MSDelay(100);

    P1=0xCC;

    MSDelay(100);

    P1=0x99;

    MSDelay(100);

    P1=0x33;

    MSDelay(100);

    }

    else

    {

    P1=0x66;

    MSDelay(100);

    P1=0x33;

    MSDelay(100);

    P1=0x99;

    MSDelay(100);

    P1=0xCC;

    MSDelay(100);

    }

  • 8/3/2019 3rd Internal

    18/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    }

    }

    void MSDelay(unsigned int value)

    {

    unsigned intx,y;

    for(x=0;x

  • 8/3/2019 3rd Internal

    19/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    doing other things. The ability to control the speed of the dc motor using PWM is

    one reason that DC motors are preferred over AC motors. AC motor speed is

    dictated by the AC frequency of the voltage applied to the motor and frequency is

    generally fixed. Therefore we cannot control the speed of AC motor when the

    load is increased. We can also change the DC motors direction and torque.

    Question 7:- Explain the registers and pins of an LCD panel and write an 8051 ALP

    display message MSRIT on the LCD panel.

    The Optrex LCD Panel has 14 pins. The function of each pin is given in the table

    below.

    Pin Symbol I/O Description

    1 VSS - Ground

    2 VCC - +5V power supply

    3 VEE - Power supply to control contrast

    1/4 POWER 25% DC

    1/2 POWER 50% DC

    3/4 POWER 75% DC

    FULL POWER 100% DC

    Pulse width Modulation Comparision

  • 8/3/2019 3rd Internal

    20/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    4 RS I RS = 0 to control command register

    RS = 1 to select data register

    5 R/W I R/W = 0 for write, R/W = 1 for read

    6 E I/O Enable

    7 DB0 I/O The 8-bit data bus

    8 DB1 I/O The 8-bit data bus

    9 DB2 I/O The 8-bit data bus

    10 DB3 I/O The 8-bit data bus

    11 DB4 I/O The 8-bit data bus

    12 DB5 I/O The 8-bit data bus

    13 DB6 I/O The 8-bit data bus

    14 DB7 I/O The 8-bit data bus

    VCC, VSS and VEE :-

    VCC and VSS provide +5V and ground respectively. VEE is used for controlling LCD

    contrast.

    RS, register select:-

    The RS pin is used to select between different registers of the LCD. If RS = 0, the

    instruction command code register is selected, allowing the user to send a

  • 8/3/2019 3rd Internal

    21/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    command such as clear display, cursor at home, etc. to the LCD. If RS = 1 the data

    register is selected, allowing the user to send data to be displayed on the LCD.

    R/W, Read/Write:-

    R/W input allows the user to write information to the LCD or read information

    from it. R/W=1 when reading; R/W=0 when writing.

    E, Enable:-

    The enable pin is used by the LCD to latch information presented to its data pins.

    When data is supplied to data pins, a high-to-low pulse must be applied to this pin

    in order for the LCD to latch in the data present at the data pins. This pulse must

    be a minimum of 450ns wide.

    D0-D7:-

    The 8-bit data pins, D0-D7, are used to send information to the LCD or read the

    contents of the LCDs internal registers.

    To display letters and numbers, we send ASCII codes for the letters A-Z, a-z, and

    numbers 0-9 to these pins while making RS=1.

    There also instruction command codes that can be sent to the LCD to clear the

    display or force the cursor to home position or blink the cursor. The table below

    lists the instruction command codes.

  • 8/3/2019 3rd Internal

    22/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    RS = 0 is used to check the busy flag bit to find out if the LCD is ready to receive

    information. The busy flag is D7 and can be read when R/W = 1 and RS = 0. When

    D7 = 1, the LCD is busy taking care of internal operations and will not accept anynew information. When D7 = 0, the LCD is ready to receive new information.

    Code

    (Hex)

    Command to LCD instruction register

    1 Clear display screen

    2 Return home

    4 Decrement cursor (shift cursor to left)

    6 Increment cursor (shift cursor to right)

    5 Shift display right

    7 Shift display left

    8 Display off, cursor off

    A Display off, cursor on

    C Display on, cursor off

    E Display on, cursor blinking

    F Display on, cursor blinking

    10 Shift cursor position to left

    14 Shift cursor position to right

    18 Shift the entire display to the left

  • 8/3/2019 3rd Internal

    23/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    1C Shift the entire display to the right

    80 Force cursor to beginning of 1st

    line

    C0 Force cursor to beginning of 2nd

    line

    38 2 lines and 5X7 matrix

    The LCD has data and command (instruction) registers. The data register is used

    to store the data to be displayed on the LCD. The command (instruction) register

    is used to store the instructions to the LCD.

    Program to display MSRIT on the LCD Panel

    ORG 0000H

    MOV DPTR, #MYCOM

    C1: CLR A

    MOVC A,@A+DPTR

    ACALL COMNWRT ; call command subroutine

    ACALL DELAY ; give LCD some time

    JZ SEND_DAT

    INC DPTR

    SJMP C1

    SEND_DAT: MOV DPTR, #MYDATA

  • 8/3/2019 3rd Internal

    24/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    D1: CLR A

    MOVC A,@A+DPTR

    ACALL DATAWRT ; call command subroutine

    ACALL DELAY ; give LCD some time

    INC DPTR

    JZ AGAIN

    SJMP D1

    AGAIN: SJMP AGAIN ; stay here

    COMNWRT: MOV P1, A ; SEND COMND to P1

    CLR P2.0 ; RS=0 for command

    CLR P2.1 ; R/W=0 for write

    SETB P2.2 ; E=1 for high pulse

    ACALL DELAY ; give LCD some time

    CLR P2.2 ; E=0 for H-to-L

    RET

    DATAWRT: MOV P1, A ; SEND DATA to P1

    SETB P2.0 ; RS=1 for data

    CLR P2.1 ; R/W=0 for write

    SETB P2.2 ; E=1 for high pulse

  • 8/3/2019 3rd Internal

    25/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    ACALL DELAY ; give LCD some time

    CLR P2.2 ; E=0 for H-to-L pulse

    RET

    DELAY: MOV R3, #250 ; long delay for fast CPUs

    HERE2: MOV R4, #255

    HERE: DJNZ R4, HERE

    DJNZ R3, HERE2

    RET

    ORG 0300H ; lookup table

    MYCOM: DB 38H,0EH,01H,06H,84H,00H ; commands and null

    MYDATA: DB MSRIT, 00H ; data and null

    END

    8.DRAW 8051 CONNECTION T0 DAC0808 AT PORT P1 ANDWRITE A C PROGRAM TO GENERATE SINE WAVE.

    Ans.

  • 8/3/2019 3rd Internal

    26/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    In DAC 808 the digital inputs are converted to current (Lout) and by connecting a

    resistor to the Lout pin we convert the result to voltage. Lout is a function of digital

    inputs and reference current as below.

    The Lrefcurrent is generally 2mA.The maximum output current, (if all inputs to

    DAC are high) is 1.99mA (from the above figure).

    Generating Sine Wave:

    Full scale output of DAC is achieved when all data inputs of DAC are high.

    Therefore, to achieve full-scale output we use the following equation.

    Vout=5V*(1 + sin).

  • 8/3/2019 3rd Internal

    27/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Vout for DAC for various angles is calculated as below at increment of 30 degrees.

    Angle @

    (degrees)

    sin@ Vout(voltage

    mag)

    5V*(1 + sin)

    Values sent to

    DAC(decimal)

    (Voltage mag * 25.6)

    0 0 5 128

    30 0.5 7.5 192

    60 0.866 9.33 238

    90 1.0 10 255

    120 0.866 9.33 238

    150 0.5 7.5 192

    180 0 5 128

    210 -0.5 2.5 64240 -0.866 0.669 17

    270 -1.0 0 0

    300 -0.866 0.699 17

    330 -0.5 2.5 64

    360 0 5 128

    C program to generate sine wave:

    #include

    sfr DACDATA =P1;

    void main()

    {

    Unsigned char table[12]={128,192,238,255,238,192,128,64,17,0,17,64};

    Unsigned char x;

    while(1)

    {

    for(x=0;x

  • 8/3/2019 3rd Internal

    28/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Question9:- How can a microcontroller be used to control automatically the speed

    of a dc motor. Explain the concept clearly.

    The speed of motor depends on three factors:

    a)load

    b) Current

    c) Voltage

    For a fixed load we can maintain a steady speed using a method called pulse

    width modulation.

    By changing (modulating) the width if the pulse applied to the DC motor we can

    increase or decrease the amount power provided to the motor, thereby

    increasing o decreasing the motor speed. Although the voltage has a fixed

    amplitude, it has a variable duty cycle. That means the wider the pulse, the higher

    the speed.PWM is so widely used in DC motor control that some microcontrollers

    come with PWM circuitry embedded in the chip. In such microcontrollers all we

    have to do is to load the proper registers with values of high and low portions of

    the desired pulse, and rest is taken care by the microcontroller. This allows the

    microcontroller to do other things. For microcontroller without PWM circuitry, wemust create the various duty cycle pulses using software which prevents the

    microcontroller from doing other things.

    Pulse width modulation comparison:

    .25 power 25% DC

    .50 power 50% DC

    .75 power 75% DC

    Full power 100% DC

  • 8/3/2019 3rd Internal

    29/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    EX: C program to control speed of DC motor based switches connected to p2.6

    and p2.7 as per table below

    SW2(p2.7) SW(p2.6) Comments

    0 0 DC motor moves slowly(25% duty cycle)

    0 1 DC motor moves moderately(50% duty cycle)

    1 0 DC motor moves fast(75% duty cycle)

    1 1 DC motor moves very fast(100% duty cycle)

    #include

    sbit mtr=P1^0;

    void msdelay(unsigned int value);

    void main()

    {

    Unsigned char x;

    P2=0xFF;

    z=P2;

    z=z&0x03;

    mtr =0;

    while (1)

    {

    switch (z)

  • 8/3/2019 3rd Internal

    30/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    {

    case(0):

    {

    mtr=1;

    msdelay(25);

    mtr=0;

    msdelay(75);

    break;

    }

    case(1):

    {

    mtr=1;

    msdelay(50);

    mtr=0;

    msdelay(50);

    break;

    }

    case(2):

    {

    mtr=1;

  • 8/3/2019 3rd Internal

    31/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    msdelay(75);

    mtr=0;

    msdelay(25);

    break;

    }

    default:

    mtr=1;

    }

    }

    }

    Q10. Show a scheme of interfacing an 8-bit ADC to 8051

    controller. Write the code required in C to obtain the output

    from such an interface. Discuss practical application.

    Ans.

  • 8/3/2019 3rd Internal

    32/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Code:

    #include

    sbit RD = P2^5;

    sbit WR= P2^6;

    sbit INTR = P2^7;

    sfr MYDATA = P1;void main()

    {

    unsigned char value;

    MYDATA = 0xFF; /* make P1 as input */

    INTR = 1; /* make INTR input */

  • 8/3/2019 3rd Internal

    33/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    RD = 1; /* set RD high */

    WR = 1; /* set WR high */

    while(1)

    {

    WR = 0; /*send WR pulse */WR = 1;

    while(INTR==1); /* wait for EOC*/

    RD = 0; /*send RD pulse

    value = MYDATA; // read value

    ConvertAndDisplay(value); //function to convert and display the

    output

    RD = 1;

    }

    }

    Practical Applications:

    Analog-to-digital converters are among the most widely used devices for data

    acquisition. Digital computers use binary (discrete) values, but in the physical

    world everything is analog (continuous). Temperature, pressure (wind or liquid),

    humidity, and velocity are a few examples of physical quantities that we deal with

    every day. A physical quantity is converted to electrical (voltage, current) signalsusing a device called a transducer. Transducers are also referred to as sensors.

    Sensors for temperature, velocity, pressure, light, and many other natural

    quantities produce an output that is voltage (or current). Therefore, we need an

    analog-to-digital converter to translate the analog signals to digital numbers so

    that the microcontroller can read and process them. An ADC has n-bit resolution

    where n can be 8, 10, 12, 16 or even 24 bits. The higher-resolution ADC provides a

    smaller step size, where step size is the smallest change that can be discerned by

    an ADC.

  • 8/3/2019 3rd Internal

    34/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Question 11:- Describe the 8051 connection to stepper motor. A switch is

    connected to pin p2.7. Write a c program to monitor the status of SW and

    perform the following

    If SW=0, the stepper motor moves clockwise.

    If SW=1, the stepper motor moves counter-clockwise.

    The following steps show the 8051 connection to the stepper motor and its

    programming:

    1. Use an ohmmeter to measure the resistance of the leads. This should identify

    which COM leads are connected to which winding leads.

    2. The common wire(s) are connected to the positive side of themotors power

    supply. In many motors, +5V is sufficient.

    3. The four leads of the stator winding are controlled by four bits of the 8051

    port (P1.0 P1.3). However, since the 8051 lacks the sufficient current to drivethe stepper motor windings, we must use a driver such as the ULN2003 to

    energize the stator. Instead of the ULN2003, we could have used transistors as

    drivers, as shown in the figure below. However, notice that if transistors are used

    as drivers, we must also use diodes to take care of inductive current generated

    when the coil is turned off. One reason that using the ULN2003 is preferable to

    the use of transistors as drivers is that the ULN2003 has an internal diode to take

    care of the back EMF.

  • 8/3/2019 3rd Internal

    35/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Fig. 8051 Connection to Stepper

    ORG 0H ; starting address

    MAIN: SETB P2.7 ; make an input

    MOV A, #66H ; starting phase value

    MOV P1, A ; send value to port

    TURN: JNB P2.7, CW ; check switch results

    RR A ; rotate right

    ACALL DELAY ; call delay

    MOV P1, A ; write value to port

    SJMP TURN ; repeat

    CW: RL A ; rotate left

    ACALL DELAY ; call delay

    MOV P1, A ; write value to port

    SJMP TURN ; repeat

    DELAY: MOV R2, #100

    H1: MOV R3, #255

    H2: DJNZ R3, H2

    DJNZ R2, H1

    RET

    END

  • 8/3/2019 3rd Internal

    36/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Q12. Show a simple keyboard interface with port P1 and P2 of

    8051 and explain its operation.

    Ans: Keyboards are organized in a matrix of rows & columns. The cup Accesses

    both rows & columns through ports; therefore, with two 8 bit Ports, an 8 8

    matrix of keys can be connected to a microprocessor. When a key is pressed, a

    row & a column make a contact; otherwise, there is no connection between rows

    & columns. In IBM pc keyboards, a single microcontroller takes care of hardware

    & software interfacing of the keyboard. In such system, it is the function of

    programs stored in the EPROM of the microcontroller to scan the keys

    continuously, identify which one has been activated, & present it to the

    motherboard.

    Fig shows 44 matrix connected to two ports. The rows are connected to

    an output port & the columns are connected to an input port. If no key has been

    pressed, reading the input port will yield 1s for all columns since they are

    connected to high (Vcc). If all the rows are grounded & a key is pressed, one of

    the columns will have 0 since the key pressed provides the path to ground. It isthe function of the microcontroller to scan the keyboard continuously to detect &

    identify the key pressed.

  • 8/3/2019 3rd Internal

    37/38

    5th

    Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

    Operation:

    To detect the pressed key, the microcontroller grounds all the rows by providing 0

    to the output latch, then it reads the columns. If the data read from the columns

    is D3-D0=1111, no key has been pressed and the process continues until a key

    press is detected. However, if one of the column bits has a zero, this means that a

    key press has occurred. After a key press is detected, the microcontroller will go

    through the process of identifying the key. Starting with the top row, the

    microcontroller grounds it by providing a low to row D0 only; then it reads the

    columns. If the data read is all 1s, no key in that row is activated and the process

    is moved to the next row. It grounds the next row, reads the columns, and checks

    for any zero. This process continues until the row is identified. After identification

  • 8/3/2019 3rd Internal

    38/38

    of the row in which the key has been pressed, the next task is to find out which

    column the pressed key belongs to. This should be easy since the microcontroller

    knows at any time which row and column are being accessed.