micro controller assignment 2 (all 24 from shiva prasad)

Upload: shashank-m-chanmal

Post on 07-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    1/88

    1.) Explain with example, bit-wise logic operators for 8051 C.Ans->

    The various bit wise operators are- AND (&), OR (|), EX-OR (^), Inverter (~), Shift

    Right (>>), and Shift Left (

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    2/88

    P1= 0x04 | 0x68; //ORing

    P2= 0x54 ^ 0x78; //XORing

    P3= ~0x55; //Inverting

    P1=0x9A >> 3; //shifting right 3 times

    P2=0x77 >> 4; //shifting right 4 times

    P3=0x6 >) and (ii) shift left(number of bits to be shifted right

    data3=0x13 /*shifting right 3 times*/

    x77>>4=0x07 /*shifting right 4 times*/

    x6

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    3/88

    to Timer 1 whereas the low four bits (bits 0 through 3)perform the exact same functions, but for timer 0.

    The individual bits of TMOD have the followingfunctions:

    Bit Name Explanation of Function Timer

    7 GATE1

    When this bit is set the timer willonly run when INT1 (P3.3) is high.When this bit is clear the timer

    will run regardless of the state of INT1.

    1

    6 C/T1

    When this bit is set the timer willcount events on T1 (P3.5). Whenthis bit is clear the timer will beincremented every machinecycle.

    1

    5 T1M1 Timer mode bit (see below) 1 4 T1M0 Timer mode bit (see below) 1

    3 GATE0

    When this bit is set the timer willonly run when INT0 (P3.2) is high.When this bit is clear the timerwill run regardless of the state of

    INT0.

    0

    2 C/T0

    When this bit is set the timer willcount events on T0 (P3.4). Whenthis bit is clear the timer will beincremented every machine

    0

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    4/88

    cycle. 1 T0M1 Timer mode bit (see below) 0 0 T0M0 Timer mode bit (see below) 0

    As you can see in the above chart, four bits (two for eachtimer) are used to specify a mode of operation. The modes of operation are:

    TxM1 TxM0 Timer Mode Description of Mode 0 0 0 13-bit Timer. 0 1 1 16-bit Timer 1 0 2 8-bit auto-reload 1 1 3 Split timer mode

    13-bit Time Mode (mode 0):

    Timer mode "0" is a 13-bit timer. This is a relic that waskept around in the 8051 to maintain compatibility with

    its predecessor, the 8048. Generally the 13-bit timer mode isnot used in new development.

    When the timer is in 13-bit mode, TLx will count from 0to 31. When TLx is incremented from 31, it will "reset" to 0and increment THx. Thus, effectively, only 13 bits of the twotimer bytes are being used: bits 0-4 of TLx and bits 0-7 of THx.This also means, in essence, the timer can only contain 8192

    values. If you set a 13-bit timer to 0, it will overflow back tozero 8192 machine cycles later.

    Again, there is very little reason to use this mode and itis only mentioned so you wont be surprised if you ever end upanalyzing archaic code which has been passed down through

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    5/88

    the generations (a generation in a programming shop is oftenon the order of about 3 or 4 months).

    Delay= (213-n) Tc , where n is count which is to be

    placed in TH & TL register. T c=period of one machinecycle=1.08510 -6s (f osc=11.0592MHz)

    13 bit counter

    INTERRUPT

    Gate

    16-bit Time Mode (mode 1):Timer mode "1" is a 16-bit timer. This is a very commonly

    used mode. It functions just like 13-bit mode except that all 16bits are used.

    TLx is incremented from 0 to 255. When TLx isincremented from 255, it resets to 0 and causes THx to beincremented by 1. Since this is a full 16-bit timer, the timer maycontain up to 65536 distinct values. If you set a 16-bit timer to0, it will overflow back to 0 after 65,536 machine cycles.

    osc 12

    TH(8b) TL(5b)

    INTO

    T0

    TR

    TF

    CONTROL

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    6/88

    Delay= (216-n) Tc , where n is count which is to be placedin TH & TL register. Tc=period of one machine cycle=1.08510

    -6s(f osc=11.0592MHz).

    16 bit counter

    INTERRUPT

    Gate

    8-bit Time Mode (mode 2):Timer mode "2" is an 8-bit auto-reload mode. When a

    timer is in mode 2, THx holds the "reload value" and TLx is thetimer itself. Thus, TLx starts counting up. When TLx reaches 255and is subsequently incremented, instead of resetting to 0 (asin the case of modes 0 and 1), it will be reset to the valuestored in THx.

    For example, lets say TH0 holds the value FDh and TL0 holdsthe value FEh. If we were to watch the values of TH0 and TL0for a few machine cycles this is what wed see:

    osc 12

    TH(8b) TL(8b)

    INTO

    T0

    TR

    TF

    CONTROL

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    7/88

    Machine Cycle TH0 Value TL0 Value 1 FDh FEh 2 FDh FFh 3 FDh FDh 4 FDh FEh 5 FDh FFh 6 FDh FDh 7 FDh FEh

    As you can see, the value of TH0 never changed. In fact,when you use mode 2 you almost always set THx to a knownvalue and TLx is the SFR that is constantly incremented.

    If you use mode 0 or 1, youd have to check in code tosee if the timer had overflowed and, if so, reset the timer to200. This takes precious instructions of execution time to checkthe value and/or to reload it. When you use mode 2 the

    microcontroller takes care of this for you. Once youveconfigured a timer in mode 2 you dont have to worry aboutchecking to see if the timer has overflowed nor do you have toworry about resetting the value--the microcontroller hardwarewill do it all for you.The auto-reload mode is very commonly used for establishing abaud rate which we will talk more about in the Serial

    Communications chapter.Delay= (28-n)Tc , where n is count which is to be placedin TH & TL register.Tc=period of one machine cycle=1.08510

    -6s (f osc=11.0592MHz).

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    8/88

    8 bit counterinterrupt

    gate

    Split Timer Mode (mode 3):Timer mode "3" is a split-timer mode. When Timer 0 is placedin mode 3, it essentially becomes two separate 8-bit timers.That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timerscount from 0 to 255 and overflow back to 0. All the bits that arerelated to Timer 1 will now be tied to TH0.While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 andTL1) can be put into modes 0, 1 or 2 normally--however, youmay not start or stop the real timer 1 since the bits that do thatare now linked to TH0. The real timer 1, in this case, will beincremented every machine cycle no matter what.

    osc 12

    INTO

    T0

    TR0

    TFTL0 (8b)

    TRISTATE BUFF

    TH0 8b

    CONTROL

    EN

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    9/88

    The only real use I can see of using split timer mode is if youneed to have two separate timers and, additionally, a baud rategenerator. In such case you can use the real Timer 1 as a baud

    rate generator and use TH0/TL0 as two separate timers .

    16 BIT COUNTER interrupt

    Gate

    2. b. Explain the registers and pins of an LCD panel and write an8051 C program to display message HELLO on the LCD panel.

    ANS:

    REGISTERS OF THE LCD PANEL:

    osc 12

    TL0(8b)

    INTO

    T0

    TR0

    TF

    CONTROL

    OSC 12 TH0 TF1

    TR1CONTROL

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    10/88

    a. Command code register: This register is selected whenRS=1. Here LCD stores the message to be displayed i.e. allowing the user to send data to be displayed on

    the LCD.b. Data register : This register is selected when RS=0.

    Allowing the user to send a command to LCD. The following table lists the command codes.

    LCD command codes:

    Code Command to LCDinstruction register

    1 Clear display screen2 Return home4 Decrement cursor( shift

    cursor to left)6 Increment cursor(shift

    cursor to right)5 Shift display right7 Shift display left8 Display off, cursor off A Display off ,cursor onC Display on, cursor off E Display on, cursor blinkingF Display on, cursor blinking

    10 Shift cursor position to left14 Shift cursor position to right18 Shift entire display to left

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    11/88

    1c Shift entire display to right80 Force cursor to beginning if

    1st line

    C0 Force cursor to beginning if 2nd line

    38 2 lines and 5x7 matrix LCD pin descriptions:

    The LCD panel contains 14 pins. The function of eachpin is given in the following table,

    Pin descriptions of LCD.

    Pin Symbol I/O Description1 Vss - GROUND2 Vcc - +5V power supply3 Vee - Power supply to

    control contrast4 RS I RS=0 to selectcommand registerRS=1 to select dataregister

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

    6 E I/O Enable7 DB0 I/O THE 8-bit data bus8 DB1 I/O THE 8-bit data bus9 DB2 I/O THE 8-bit data bus

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    12/88

    10 DB3 I/O THE 8-bit data bus11 DB4 I/O THE 8-bit data bus12 DB5 I/O THE 8-bit data bus13 DB6 I/O THE 8-bit data bus14 DB7 I/O THE 8-bit data bus

    Vcc, Vss and VEE : While Vcc and Vss provides +5Vand ground, respectively, Vee is used for controllingLCD contrast.

    RS, register select: There are two very importantregister in LCD. This RS register is used to select them.If RS=0, the instruction command code register isselected. If RS=1, the data register is selected.

    R/W, read/write: This input allows the LCD to writethe information into it or read the information.R/W=1 when it is reading and R/W=0 when it iswriting.

    E, enable: The enable pin is used by the LCD to latchinformation presented to its data pins.

    D0-D7 : The 8-bit data pins, D0-D7, are used to sendinformation to the LCD or read the contents of theLCDs internal register. D7 acts as a busy flag and canbe read when R/W=1, RS=0.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    13/88

    An 8051 C program to display HELLO on the LCD panel.

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

    sbit rs = P2^0; sbit rw = P2^1; sbit en =P2^2;

    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 register

    rw =0; en = 1; //strobe the enable pin MSDelay (1); en=0; return;

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    14/88

    }

    void MSDelay (unsigned int itime)

    { unsigned int i, j;

    for (i=0; i

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    15/88

    } }

    3.)Explain the steps to program timers in mode 1 and write an 8051 program togenerate a square wave of 50% duty cycle on the pin P1.5.

    Ans To generate a time delay, using timers in mode 1, the following steps aretaken:

    1.) Load the TMOD value indicating which timer (Timer 1 or Timer 0) is to beused and which timer mode( 0 or 1) is selected.

    2.) Load registers TL and TH with the initial count values.The initial count values are decided by the count required in the program. Itis calculated on the basis of the given formula:-

    delay=(2 16-n)x (time period of clock cycle)where n combined value stored in THx and TLx. and clock cycle is

    calculated as T c=1/f c and f c=f osc /12; f osc is 11.0592 MHz for AT89C51ED2.3.) Start the timer.4.) Keep monitoring the timer flag (TF) with the JNB TFx, target

    instruction to see if it is raised. Get out of the loop when TF becomes high.5.) Stop the timer. To stop the timer we use the CLR TRx instruction.6.) Clear TF flag for the next round.7.) Go back to step 2 to load TH and TL again. Here TH and TL have to be

    reloaded each time due to mode 1, whereas in mode 2 auto reload occurs.

    Program to create square wave of 50% duty cycle on pin P1.5

    ORG 0

    SJMP START

    ORG 0030H

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    16/88

    START: MOV TMOD,#01H ;Timer 0, mode 1(16 bitmode)

    HERE:MOV TL0,#0F2H ;TL0=F2H, the Low byte

    MOV TH0,#0FFH ;TH0=FFH, the High byte

    CPL P1.5 ;toggle P1.5

    ACALL DELAY

    SJMP HERE ;load TH,TL again

    ;----------delay using Timer 0

    DELAY:

    SETB TR0 ;start Timer 0

    AGAIN:JNB TF0,AGAIN ; monitor Timer 0 flag tillit rolls ;over

    CLR TF0 ;clear Timer 0 flag

    RET ;return to instruction post call

    END

    4a) What is data serialization? Explain different types with examples.

    Serializing data is a way of sending a byte of data one bit at a time through asingle pin of micro controller. There are two ways to transfer a byte of dataserially.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    17/88

    Using the serial port : In using the serial port, programmers have a very limitedcontrol over the sequence of data transfer.

    Consider the following ALP.

    MOV TMOD ,#20H //timer 1, mode 0

    MOV TH1,#-3 //9600 baud

    MOV SCON,#50H

    SETB TR1 //start timer 1

    CLR RI //reception mode

    JNB RI,$ //wait for character to come in

    MOV A,SBUF //move received data into A

    MOV P0,A //move it to P0

    MOV 60H,A //move it into location 60H

    END

    The above program demonstrates receiving data sent in serial form anf sending itout to port 0 in parallel form. Data is also stored in RAM 60H.

    The second method is to transfer data one bit at a time and control the sequenceof data and spaces in between them.

    Ex : Consider the following C program.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    18/88

    #include

    sbit P1b0 = P1^0;

    sbit regAMSB = ACC^7;

    void main(){

    unsigned char conbyte = 0x44;

    unsigned char x;

    ACC = conbyte;

    for(x=0;x

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    19/88

    Angle(Degrees) Sin (angle) Voltage

    Magnitude 5V +(5V*Sin(angle))

    Voltage sent to

    DAC (Voltage Mag. *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

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    20/88

    270 -1 0 0

    300 -0.87 0.67 17

    330 -0.5 205 64

    360 0 5 128 ALP :

    ORG 40

    AGAIN: MOV DPTR,#TABLE ; initializing DPTR to the starting of table

    MOV R2,#COUNT ; initializing count

    BACK: CLR A ;

    MOVC A,@A+DPTR ;

    MOV P1,A ; moving values from LookUp table to Port 1

    INC DPTR

    DJNZ R2,BACK ; looping the values

    SJMP AGAIN ; looping the sine wave

    ORG 300H ; initializing the LUP

    TABLE DB 128,192,238,255,192

    DB 128,64,17,0,17,64,128

    END

    5) What is the time it will take a timer of 8051 in mode l to overflow if initially setto OABCH with 6 MHz of crystal frequency.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    21/88

    Considering Up Counter

    Crystal frequency = 6MHz

    Clock frequency = (Crystal frequency)/ 12

    = 6MHz / 12

    = 0.5 MHz

    Tc(clock period)= 1/(clock frequency)

    Tc = 1 /(0.5MHz) = 2 micro seconds

    Delay= (2 16 N)Tc

    = (216 - 0ABCH) (2 micro seconds)

    = (216 2748) (2 micro seconds)

    = 125.576ms

    6a) A switch is connected to the pin P1.2. Write an 8051 C program to monitor the

    switch and create the following frequencies on pin P1.7.i) When SW = 0; 500Hz ii) When SW = 1; 750Hz

    Use timer 0, mode 1 for both of them.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    22/88

    Delay= (2 16 N)Tc

    When, When a frequency of 500 Hz is to be created, take delay as (1/500*2)sec.

    N=FC67

    Similarly, When freqeuncy is to be 750 Hz,

    N=FD9A

    #include

    sbit mybit =P1^7; //initialize data

    sbit SW = P1^2; //initializ switch

    void T0M1Delay(unsigned char);

    void main(void){

    SW = 1; //configure for input

    while(1){

    mybit = ~mybit; //change state

    if (SW==0)

    T0M1Delay(0);

    else

    T0M1Delay(1);

    }

    }

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    23/88

    void T0M1Delay(unsigned char c){

    TMOD = 0x01;

    if(c==0){

    TL0 =0x67; //n is FC67 for 500 Hz

    TH0 =0x FC;

    }

    else{

    TL0 = 0x9A; //n is FD9A for 750 Hz

    TH0 = 0xFD;

    }

    TR0 = 1;

    while(TF0==0); //wait till TF0 becomes 0

    TR0 = 0; //reset TR0 and TF0

    TF0 = 0;

    }

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    24/88

    6b) Show a simple keyboard interface with Port 1 and Port 2 of 8051 and explain

    its operation.

    Working

    To detect a pressed key, the microcontroller grounds all rows by providing 0 tothe output latch, then it reads the columns. If the data read from the columns isD3 - DO = 1111, no key has been pressed and the process continues until a keypress is detected. However, if one of the column bits has a zero, this means that akey press has occurred. For example, if D3 - DO = 1101, this means that a key inthe Dl column has been pressed. After a key press is detected, the microcontrollerwill go through the process of identifying the key. Starting with the top row, themicrocontroller grounds it by providing a low to row DO only; then it reads thecolumns. If the data read is all Is, no key in that row is activated and the process ismoved to the next row. It grounds the next row, reads the columns, and checksfor any zero. This process continues until the row is identified. After identificationof the row in which the key has been pressed, the next task is to find out which

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    25/88

    column the pressed key belongs to. This task is achieved as the microcontrollerknows at any time which row and column are being accessed.

    7.List the advantages of serial communication over parallel communication??

    Ans:

    Serial communication may be faster than the parallel one,provided the bits leave the transmitting device at a much higher speed.

    The complications of using Parallel data transfer are that that many wires are required as the width of bus. Eg : A 32 bit PCI bus would have 32 wires and the controls are required for each.

    In serial communication, just two wires will do the task required indata processing.

    As the speed of data transfer increases, the EM disturbances alsoincreases, making parallel data transfer very difficult.

    Another problem with parallel communication. is that the bits may

    not reach the destination at the same time. The various reception time of the several bits makes the device wastes time having to wait for all the bits to arrive, which may represent a significant fall in performance.

    Parallel data tx. Is semi duplex or half duplex, whereas serial data tx. is full duplex.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    26/88

    In the serial communication, since it only uses two wires, the manufacturers usually make four wires available, two for the transmission and two for the reception of data. That makes it possible for the simultaneous transmission and reception of data.

    Such architectural difference alone makes the serial communication about twice as fast as the parallel communication.

    Parallel requires more power; each line needs power whereas serial communication requires less power.

    The software has to process each line, where as in serial communication software has to process one line at a time.

    Hardware requires extra lines which makes data interception and transmission a difficult task.

    In parallel communication the lines can cross over causingundesirable interference is data reception.

    One key advantage of serial over parallel communication is that the serial communication allows for data to be transferred infrom a remote device or transferred out from the hard drive to aremote device. This two-way communication process makes it possible to connect work stations to larger terminals as well asa wide range of peripheral devices.

    Serial communication can span longer distances than parallel

    communication. Although longer cables exist, the recommended maximumlength of a parallel cable is 2m for fast mode and 5m for normal mode .

    Parallel holds a decided speed advantage over serial if the crosstalk between lines can be overcome . SINCE MARGE BITS OF DATAARE TRANSFERRED AT A TIME THERE WILL BE CROSS TALK BETWEEN LINES WHICH IS INEVITABLE.

    8)a. what is the default priority assigned to

    various interrupts after reset? Is it possible to alter the priority and how?

    Ans:

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    27/88

    When several interrupts are enabled, it may happen that while one of them is in progress, another one is requested. In such situations, the microcontroller needs to know whether to proceed with the execution of current interrupt routine or to meet a new interrupt request. For this

    reason, there is a priority list on the basis of which the microcontroller knows what to do. The previous versions of the microcontrollersdifferentiate between two priority levels defined in the IP register. SFR register IPH which enables all the interrupts to be assigned 1 out of 4priorities (excluding reset). Here is a list of priorities:

    7 - - Undefined 6 - - Undefined 5 - - Undefined 4 ps bch Serial Interrupt

    Priority 3 Pt1 bbh Timer 1 Interrupt

    Priority 2 Px1 Bah External 1 Interrupt

    Priority 1 Pt0 B9h Timer 0 Interrupt

    Priority 0 PX0 B8h External 0 Interrupt

    Priority

    IP register (Interrupt Priority Register)

    Bits of this register determine the interrupt source priority.

    PT2 Timer T2 interrupt priority :

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    28/88

    0 - Priority 0 1 - Priority 1

    PS Serial port interrupts priority:

    1. 0 - Priority 02. 1 - Priority 1

    PT1 Timer T1 interrupts priority:

    0 - Priority 0 1 - Priority 1

    PX1 External interrupts INT1 priority:

    1. 0 - Priority 02. 1 - Priority 1

    PT0 Timer T0 interrupt priority:

    0 - Priority 0 1 - Priority 1

    PX0 External interrupt INT0 priority:

    0 - Priority 01 - Priority 1IPH Register (Interrupt Priority High)

    PT2H Timer T2 interrupt priority

    PSH Serial port interrupt priority

    PT1H Timer T1interrupt priority

    PX1H External interrupt INT1 priority

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    29/88

    PT0H Timer T0 interrupt priority

    PX0H External interrupt INT0 Priority

    Bits of this register can be combined with appropriate bits of the IPregister. This is how a new priority list with 4 interrupt priority levels(5 including reset) is obtained.

    IP bit IPH bit Interrupts

    0 0 Priority 0

    (lowest)

    0 1 Priority 1 (low)

    1 0 Priority 2 (high)

    1 1 Priority 3

    (highest)

    8 B) SHOW A SCHEME OF INTERFACING ADC TO 8051 MICRO CONTROLLER. WRITE AN ALP TO OBTAIN THE OUT PUT FROM SUCH AN INTERFACE. DISCUSS PRACTICAL

    APPLICATIONS?

    Ans:

    The schematic diagram shown below shows the interfacing of adc809 with8051 micro controller.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    30/88

    The adc809 has following features:

    o Has 8 analog input and 8 bit data output o Vref(+)=5v o Vref(*)= gnd

    Pin diagram of adc809 is shown below

    Alp to obtain output from such an interface

    Ale bit p2.4Oe bit p2.5

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    31/88

    Sc bit p2.6Eoc bit p2.7Addr_a bit p2.0Addr_b bit p2.1

    Addr_c bit p2.2 Mydata equ p1Org 0h Mov mydata ,#offh ; to make p1 as input Setb soc ; to make eoc as input Cle ale; to clear ale Clr sc; clear wr Cle oe;clear rd

    Back:Clr Addr_c; c=0Clr Addr_b; b=0 Setb addr_a; a=1 to select channel 1Acall delay; make sure address is table Setb ale;latch addressAcall delay; delay for fast ds89c4x0 chip Setb sc; start conversionAcall delay Clr ale Clr sc

    Here:

    Jb eoc ,here; wait until done

    Here1:

    Jnb eoc,here1; ait until done

    Setb oe; enable read

    Acall delay; wait

    Mov a, mydata; read data

    Clr oe;clear read for next time

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    32/88

    Acall conversion;hex to ascii

    Acall data_display;display the data

    sjmp back

    PRACTICAL APPLICATIONS:

    USED IN DATA ACQUSITION SYSTEMS. USED IN POWER TRANSFORMERS USED IN CHOPPERS.

    9) Write an 8051 C program using interrupts to do the following: 1. Receive data serially and send it to P0 2. Read port P1, transmit data serially, and give a copy to P2 3.Generate a square wave of 5 kHz frequency on P0.1

    Assume that XTAL = 11.0592 MHz. Set the baud rate at 4800.

    Ans:

    #include sbit WAVE =P0^1; void timer0() interrupt 1{ WAVE=~WAVE; //toggle pin} void serial0() interrupt 4{if (TI==1)

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    33/88

    { TI=0; //clear interrupt as soon as timer 1 is set }else

    {P0=SBUF; //put value on pinsRI=0; //clear interrupt }} void main(){unsigned char x;P1=0xFF; //make P1 an input TMOD=0x22; TH1=0xF6; //4800 baud rate SCON=0x50; TH0=0xA4; //5 kHz has T=200usIE=0x92; //enable interrupts TR1=1; //start timer 1 TR0=1; //start timer 0 while (1) { x=P1; //read value from pins SBUF=x; //put value in buffer P2=x; //write value to pins}}

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    34/88

    10 a) Write an 8051 C program to send the message The Earth is Beautiful,to the serial port continuously. Assume XTAL=11.0592MHz, 9600 baud rate,8-bit data and one stop bit.

    #include

    void SerTx(unsigned char);

    void main() {

    unsigned char message[] = The Earth is Beautiful;

    unsigned char i;

    TMOD = 0x20; //use Timer 1, 8bit auto reload

    TH1 = 0xFD; //96000 Baud rate

    SCON = 0x50;

    TR1 = 1; //start timer

    while(1) {

    for(i=0; i

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    35/88

    T1 = 0;

    }

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    36/88

    10 b) Describe the 8051 connection to stepper motor. A Switch is connectedto pin P2.7. Write a C program to monitor the status of SW and perform thefollowing:

    If SW =0, the stepper motor moves clockwise. If SW =1, the stepper motor moves counterclockwise

    The following steps show the 8051 connection to the stepper motor and itsprogramming:

    1. Use an ohmmeter to measure the resistance of the leads. This should identifywhich COM leads are connected to which winding leads.

    2. The common wire(s) are connected to the positive side of the motors powersupply. In many motors, +5V is sufficient.

    3. The four leads of the stator winding are controlled by four bits of the 8051port (P1.0 P1.3). However, since the 8051 lacks the sufficient current to drive

    the stepper motor windings, we must use a driver such as the ULN2003 toenergize the stator. Instead of the ULN2003, we could have used transistors asdrivers, as shown in the figure below. However, notice that if transistors are usedas drivers, we must also use diodes to take care of inductive current generatedwhen the coil is turned off. One reason that using the ULN2003 is preferable tothe use of transistors as drivers is that the ULN2003 has an internal diode to takecare of the back EMF.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    37/88

    Fig. 8051 Connection to Stepper Motor -- Use one power supply for the motor and the driver (ULN2003) and another for 8051.

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

    #include

    sbit SW=P2^7;

    void main() {

    SW=1;

    while(1) {

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    38/88

    if(SW==0) {

    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 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    39/88

    void MSDelay(unsigned int value) {

    unsigned int x,y;

    for(x=0;x

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    40/88

    11) Explain the Interrupt Vector Table for 8051 and explain how to redirect 8051 from the IVT at power-up.

    The 8051 has 6 interrupts 5 of which are available to the user.

    Interrupt ROM Location (Hex) Pin Flag clearing

    Reset 0000 9 Auto

    External hardware interrupt 0(INT0)

    0003 P3.2 Auto

    Timer 0 interrupt (TF0) 000B Auto

    External hardware interrupt 1(INT1)

    0013 P3.3 Auto

    Timer 1 interrupt (TF1) 001B Auto

    Serial COM interrupt (RI andTI)

    0023 Programmer clearsit

    In the above table, the RESET interrupt is activated once the reset pin is heldhigh for at least 2 machine cycles before it goes low. Also it is noted that a limitednumber of bytes are set aside for each interrupt (8 bytes except RESET which has3 bytes). For service routines that are longer than 8 bytes, an LJMP instruction is

    placed instead which jumps the longer ISR.

    The 3 bytes space in the service routine of the reset interrupt is used to bypassthe Interrupt Vector Table if needed.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    41/88

    When the 8051 is powered up, the ISR at address 0000H is executed. If thiscontains an LJMP instruction, the 8051 can be redirected away from the vectortable as shown:

    ORG 0 ; the wake-up ROM reset location

    LJMP START ; bypass/redirect away from the interruptvector table

    ORG 0030H

    START: .... ; normal program executed on wake-up

    ....

    END

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    42/88

    12 a) Explain IE and IP registers with their bit patterns and show howpriorities change with an example.

    12 a)

    The Interrupt Enable (IE) register is used to control all interrupt action with onemaster(global) register and control each individual interrupt with an associated IEregister enable bit.

    EA ET2 ES ET1 EX1 ET0 EX0

    7 6 5 4 3 2 10

    The function of each bit in the IE SFR is as follows:

    EA : Enable interrupts bit. It is cleared to 0 by program to disable allinterrupts or set to 1 to permit individual interrupts to be enabled by theirinterrupt bits.

    ET2 and IE.6 : ET2 is reserved for future use; IE.6 is not implemented. ES : Enable serial port interrupt. Set to 1 by program to enable serial port

    interrupt or cleared to 0 to disable it. ET1 : Enable timer 1 overflow interrupt. Set to 1 by program to enable timer 1

    overflow interrupt or cleared to 0 to disable it. EX1 : Enable external interrupt 1. Set to 1 by program to enable INT1

    interrupt or cleared to 0 to disable it ET0 : Enable timer 0 overflow interrupt. Set to 1 by program to enable timer 0

    overflow interrupt or cleared to 0 to disable it. EX0 : Enable external interrupt 0. Set to 1 by program to enable INT0interrupt or cleared to 0 to disable it.

    The Interrupt Priority (IP) register is used by the programmer to determine if anyinterrupt is to have a high or low priority.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    43/88

    PT2 PS PT1 PX1 PT0 PX07 6 5 4 3 2 1

    0

    The function of each bit in the IE SFR is as follows:

    Bits 7 and 6 are not implemented. PT2 is reserved for future use. PS : Priority of serial port interrupt. Set/cleared by the program. PT1 : Priority of timer 1 overflow interrupt. Set/cleared by the program. PX1 : Priority of external interrupt 1. Set/cleared by the program. PT0 : Priority of timer 0 overflow interrupt. Set/cleared by the program. PX0 : Priority of external interrupt 0. Set/cleared by the program.

    Bits set to 1 give the accompanying interrupt a high priority while a 0 assigns alow priority to it. Interrupts with a high priority can interrupt another interruptwith a low priority; the lower priority interrupt continues after the higher is

    finished. If 2 interrupts with same priority occur at the same time, then they havethe following ranking:

    IE0 > TF0 > IE1 > TF1 > Serial i.e. RI or TI

    An example to show how priorities change:

    Let the interrupt priority register be loaded with 00001100 (0CH) to give

    higher priority to the external interrupt 1 and Timer 1 overflow interrupt. Butsince the interrupts are polled according to the above ranking, they will have thefollowing priority overriding the default priority assigned by 8051:

    Highest Priority External interrupt 1 (INT1)

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    44/88

    Timer 1 interrupt (TF1)

    External interrupt 0 (INT0)

    Timer 0 interrupt (TF0)

    Lowest Priority Serial communication (RI and TI)

    Here any high priority interrupt can interrupt a lower priority interrupt while theISR of low priority interrupt is being executed.

    12 b) How can a microcontroller be used to control automatically the speedof a DC motor? Explain the concept clearly.

    The speed of the DC motor depends on 3 factors (a) the load, (b) voltage and(c) current. For a given fixed load, we can maintain a steady speed by using a

    method called Pulse Width Modulation. By modulating the width of the pulseapplied to the motor we can increase or decrease the amount of power providedto the motor and thereby increasing/decreasing the motor speed.

    Here the voltage has fixed amplitude but variable duty cycle i.e. wider thepulse, higher the speed.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    45/88

    Fig : Pulse Width Modulation comparison

    Some microcontrollers come with the PWM circuitry embedded in the chip. In

    such computers the proper registers have to be loaded with the values of highand low portions of the desired pulse, the rest is taken care of by themicrocontroller. This allows the microcontroller to do other things. Formicrocontrollers without PWM circuitry, the various duty cycles must be createdusing software, which prevents the microcontroller from doing other things.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    46/88

    13) Write a C program to toggle all bits of P 0 and P 2 continuously with 250msec delay. Use the inverting operator.

    Calculations:

    Since 250ms > 71ms which is the maximum delay the 8051 can generate in onego, we generate a delay of 25ms and call it 10 times to get 250ms delay

    25 10 -3 = (216 n) 1.085 10 -6

    n = A5FE H

    #include

    void T1M1Delay(void);

    void main() {

    unsigned char x;

    P0 = 0x55; // let P0 and P2 initially contain 01010101(55H)

    P2 = 0x55;

    while(1) {

    P2 = ~P2; // toggle all bits of P0 and P2P0 = ~P0;

    for(x=0; x

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    47/88

    T1M1Delay(); // generate 25ms delay and call it10 times

    }

    }

    void T1M1Delay(void) {

    TMOD = 0x10; // Timer 1, mode 1 (16-bit)

    TL1 = 0xFE; // load TL1

    TH1 = 0xA5; // load TH1TR1 = 1; // turn on Timer 1

    while(TF1==0); // wait till TF1 rolls over

    TR1 = 0; // turn off Timer 1

    TF1 = 0; // clear Timer flag TF1

    }

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    48/88

    14 a) Differentiate between a counter and timer. Explain the timer modes of operation in 8051.

    b) Explain the registers and pins of an LCD panel and write an 8051 ALPdisplay message MSRIT on the LCD panel.

    14 a)

    TIMER COUNTER

    The internal system clock of the8051 is applied to the timer

    External signal is applied to its clockinput

    It counts machine cycles It counts external events

    It is used to generate time delays inprograms, to generate specific baudrates for use in serialcommunication etc.

    It is used in counting events likenumber of times a switch turnson/off or the number of pulses sentby a sensor etc.

    C/ T = 0 C/ T = 1

    Timer modes:

    The TMOD register of the 8051 has two bits for each timer i.e. M 1 and M 0. Thesetwo bits decide the mode of operation of the respective timer which is detailed

    below:

    M1 M0 Mode Mode Description

    0 0 0 13 -bit timer mode

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    49/88

    i.e. 8 -bit timer/counter THx with TLx as 5 -bitprescaler

    0 1 1 16 -bit timer mode

    16 -bit timer/counters THx and TLx arecascaded; there is no prescaler

    1 0 2 8-bit auto reload 8-bit auto reload timer/counter' THx holds avalue that is to be reloaded into TLx eachtime it overflows

    1 1 3 Split timer mode

    14 b )

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

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    50/88

    Pin Symbol I/O Description

    1 VSS - Ground

    2 VCC - +5V power supply

    3 VEE - Power supply to control contrast

    4 RS I RS = 0 to 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 V EE -- VCCand V SS provide +5V and ground respectively. V EE is used for

    controlling LCD contrast.

    RS, register select -- The RS pin is used to select between different registers of theLCD. If RS = 0, the instruction command code register is selected, allowing theuser to send a command such as clear display, cursor at home, etc. to the LCD. If

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    51/88

    RS = 1 the data register is selected, allowing the user to send data to be displayedon the LCD.

    R/W, Read/Write -- R/W input allows the user to write information to the LCD orread 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 itsdata pins. When data is supplied to data pins, a high-to-low pulse must be appliedto this pin in order for the LCD to latch in the data present at the data pins. Thispulse must be a minimum of 450ns wide.

    D0-D7 -- The 8-bit data pins, D0-D7, are used to send information to the LCD orread 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 thedisplay or force the cursor to home position or blink the cursor. The table belowlists the instruction command codes.

    RS = 0 is used to check the busy flag bit to find out if the LCD is ready to receiveinformation. The busy flag is D7 and can be read when R/W = 1 and RS = 0. WhenD7 = 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.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    52/88

    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

    1C Shift the entire display to the right

    80 Force cursor to beginning of 1 st line

    C0 Force cursor to beginning of 2 nd line

    38 2 lines and 5X7 matrix

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    53/88

    The LCD has data and command (instruction) registers. The data register is usedto store the data to be displayed on the LCD. The command (instruction) registeris 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

    D1: CLR A

    MOVC A,@A+DPTR

    ACALL DATAWRT ; call command subroutine

    ACALL DELAY ; give LCD some time

    INC DPTR

    JZ AGAIN

    SJMP D1

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    54/88

    AGAIN: SJMP AGAIN ; stay here

    COMNWRT: ; send command to LCD

    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-LRET

    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

    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

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    55/88

    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/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    56/88

    15) A switch (SW) is connected to P2.0 port pin. Write a C program to sendout the value 44H serially one bit at a time via P1.0, depending upon theswitch condition: When SW=0; LSB should go out first, when SW = 1; MSBshould go out first.

    #include

    sbit SW = P2^0; // The switch SW

    sbit destnBit = P1^0; // The destination bit P1.0

    sbit regA_LSB = ACC^0; // Use LSB and MSB of theaccumulator to send data

    sbit regA_MSB = ACC^7; // serially

    void main() {

    unsigned char sdata = 0x44; // data to be sent

    unsigned char x;

    ACC = sdata;

    if(SW==0)

    for(x=0; x>= 1;

    }

    else

    for(x=0; x

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    57/88

    destnBit = regA_MSB; // send MSB first

    ACC

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    58/88

    P2=d3;

    }

    16 b) drawthe 8051 connections to dac0808 at port1 and write c program to generate sine wave.

    Ans: schematic shown below shows the interfacing of dac and 8051at port1.

    The digital-to-analog converter (DAC) is a device widely used to convert digital pulses to analog signals. In the MC1408 (DAC0808), the digital inputs are converted to current (I out ), and by connecting a resistor to the Iout pin, we convert the result to voltage.

    The total current provided by the I out pin is a function of the binary numbers at the DO - D7 inputs of the DAC0808 and the reference current (I re f), and is as follows:

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    59/88

    where DO is the LSB, D7 is the MSB for the inputs, and I re f is the input current that must be applied to pin 14. The I re f current is generally set to 2.0 mA. Figure 13-18 shows the generation of current reference (setting I re f = 2 mA) by using the

    Standard 5-V power supply and IK and 1.5K-ohm standard resistors. Some DACs also use the zener diode (LM336), which overcomes any fluctuationassociated.

    C program to generate sine wave

    #include

    #include "lcd.h"

    void delay() /* delay routine */

    {

    TR1 = 1;

    while(TF1 == 0);

    TR1 = 0;

    TF1 = 0;

    return;

    }

    void main()

    {

    unsigned char i;

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    60/88

    unsigned int table[24] = {102,129,154,175,191,201,205,201,191,175,154,129,

    102, 76, 51, 30, 14, 4, 0, 4, 14, 30, 51, 76 };

    /*

    Value = A(1+sin0) x 25.6

    A = peak-to-peak value of sine wave.

    0 = angle in degrees (take at steps of 15*)

    ---------------------------------------------------------- */

    InitLcd();

    WriteString("Sine");

    TMOD = 0x10; /* Timer 1 in mode 1 */

    while(1)

    {

    for(i=0;i

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    61/88

    n = [65536 (Period of sine wave / No. of entries) / 1.085 uS ] + 23

    = [65536 (1 mS / 24) /1.085 uS] + 23 = 65520 = FFF0H

    The extra addition of 23 is to account for software overhead.

    --------------------------------------------------*/

    delay();

    }

    }

    }

    17) EXPLAIN TMOD and TCON REGISTERS WITH THEIR BIT PATTERN.

    ANS:

    Tmod register:

    THIS IS A COMMON REGISTER FROR BOTH THE TIMERS, IT IS A 8 BIT

    REGISTER WITH LOWER 4 bits for timer 0 and upper 4 bits for timer 1, in each case lower 2 bits to specify the mode and upper 2 bits to specify the operation. TMOD IS USED TO SET VARIOUSOPERATION MODES.

    TH0 D15 D14 D13 D12 D11 D10 D9 D8

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    62/88

    TLO D7 D6 D5 D4 D3 D2 D1 D0

    TIMER 0 REGISTERS

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

    TIMER 1 REGISTERS

    TMOD BIT PATTERNS;

    MSB TIMER1 TIMER0 LSB

    M1 M0 MODE OPERATING MODES0 0 0 13 BIT TIMER MODE, 5 BIT PRESCALAR 0 1 1 16 BIT TIMER MODE, NO PRESCALAR 1 0 2 8 BIT AUTO RELOAD, THX HOLDS

    VALUE TO BE RELOADED TO TLX1 1 3 SPLIT TIMER MODE

    THE TABLE ABOVE SHOWS THE BIT PATTERN OF TMOD WITH ITS VARIOUS OTHER OPERATION MODES POSSIBLE. EACH BIT IS EXPLAINED BELOW

    M0 AMD M1SELECT THE TIMER MODE. WITH REFERENCE TO TABULAR COLUMN SHOWN THERE ARE THREE MODES OF OPERATION. MODE 0 IS A13 BIT TIMER, MODE 1 IS A 16 BIT TIMER, and MODE 2 IS AN 8 BIT TIMER. MODE 1 AND 2 ARE WIDELY USED THESE DAYS.

    C/T(CLOCK TIMER): This bit in tmod register is used to decide

    whether timer is used as delay generator or event counter.

    GATE C/T M1 M0 GATE C/T M1 M0

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    63/88

    If its equal to 0 it is used as a timer for delay generation , the clock source for the time delay is the cryatal frequency of the 8051 processor. If c/t is 1 then it is used as event controller.

    Gate: The start and stopping of timer is controlled by

    software or hard ware, the software way of stopping or starting the timer can be achieved as long as gate=0, whereas hardware way of starting or stopping timer is done by making gate=1.

    Tcon register:

    Tcon register is also an 8 bit register which is also a bit addressable register. Setting or clearing of bits in this register is used to start or stop the timers tr0 and tr1. The register also contains other bit positions . it is used to store tf and tr of both timer 0 and timer 1.

    Table below shows the bit addressable features of tcon register:

    Setb tr0 Setb tcon.4Clr tr0 Clr tcon.4 Setb tf0 Setb tcon.5Clr tf0 Clr tcon.5

    Timer 0

    Setb tr1 Setb tcon6Clr tr1 Clr tcon.6 Setb tf1 Setb tcon.7Clr tf1 Clr tcon.7

    Timer 1

    The bit patterns of tcon register is shown below

    Tf1 Tr1 Tf0 Tr0 Ie1 It1 Ie0 It0

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    64/88

    Tf1 tcon.7 : timer1 overflow flag set by hard ware when the timer/counter 1 overflows. Cleared by hardware as processor vector for isr Tr1 tcon.6 : timer1 run control bit. Set / cleared by software to turn timer / counter 1 off/on. Tf0 tcon.5 : timer0 overflow flag set by hard ware when the timer/counter 0overflows. Cleared by hardware as processor vector to the service routine. Tr0 tcon.4 : timer0 run control bit. Set / cleared by software to turn timer / counter 0 off/on.Ie1 tcon.3 : external interrupt 1 edge flag, set by hardware when

    external interrupt is detected. Cleared by hardware wheninterrupt is processed.It1 tcon.2 : interrupt 1 control bit , set / cleared by software to specify falling edge /low level triggered external interrupt.Ie0 tcon.1 : external interrupt 0 edge flag, set by hardware whenexternal interrupt is detected. Cleared by hardware wheninterrupt is processed.It0 tcon.0 : interrupt 0 control bit , set / cleared by software to

    specify falling edge /low level triggered external interrupt

    18. A) explain mode 2 programming of timers with a neat sketch and specify programming steps?

    Ans:

    mode 2 is an eight bit timer which allows loading of 8 bit numbers from 11h-ffh, the loading of numbers takes to only th registers.

    Once the registers th are loaded a copy of th to tl registersby 8051. The timer is then started by the instructions setb trofor timer 0 and setb tr1 for timer1.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    65/88

    Once the timer is started the tl register begins to increment thus up counting operation takes place.

    Once it reaches the maximum limit that is ffh it rolls over from ffh to 00h, and sets the timer flag high. Tf0 goes high is

    timer 0 is used and tf1 goes high if timer 1 is used. As the timer flag is set to 1 tl registers automatically gets

    reloaded with original value by th register. Mode 2 is anauto reload process which makes tl register loaded by thregister

    Auto reloading makes th register fixed with original timer value. And copy of it is sent to tl register by 8051.

    Sq. wave

    OVERFLOW FLAG SETS

    RELOAD AS FF->0

    C/TBAR

    TR

    STEPS TO PROGRAM IN MODE2:

    LOAD THE TMOD REGISTER INDICATING WHICH TIMER, AND SELECT THE TIMER MODE2

    TH REGISTER MUST BE LOADED WITH INITIAL COUNT VALUE. TIMER IS THEN STARTED. JNB TFX, TARGET INSTRUCTION IS USED TO MONITOR TIMER FLAG TO COME

    OUT OF LOOP AS SOON AS TF IS HIGH. TIMER FLAG IS CLEARED

    MODE 2 IS AUTO RELOAD SO MONITORING TIMER FLAG IS CONTINUED AND SUSEQUENT STEPS ARE REPEATED.

    XTAL OSC

    MOD12

    AND

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    66/88

    18 B). HOW CAN A MICROCONTROLLER BE USED TO CONTROL AUTOMATICALLY THE SPEED OF A DC MOTOR? EXPLAIN THE CONCEPT CLEARLY.

    ANS:

    C PROGRAM TO CONTROL THE DC MOTOR IS SHOWN BELOW.

    #include

    #include "lcd.h"

    unsigned char pedestal,count1,count,rx;

    void speed()

    {

    while( P1_1); /* wait for P1_1 to become 0 */

    while(!P1_1); /* wait for P1_1 to become 1 */

    count=0x00;

    do

    {

    while( P1_0);

    while(!P1_0);

    count++ ; /* increment after every pulse at P1_0 */

    }while( P1_1); /* When P1_1 is high for 1 sec, the number of

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    67/88

    pulses on P1_0 will give the count i.e., speed,

    in rotations per second. */

    return;

    }

    void display() /* convert the count to ASCII for LCD display */

    {

    if (count > 0x09)

    {

    count1 = count/10;

    }

    rx = (count - count1*10);GotoXY(0x08,0x0);

    WriteChar(count1 + 0x30);

    GotoXY(0x09,0x0);

    WriteChar(rx + 0x30);

    return;

    }

    void ISR(void) interrupt 0 /* Int Vector at 000BH, Reg Bank 1 */

    {

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    68/88

    speed();

    display();

    }

    void main()

    {

    IEN0 = 0x81; /* IEN0 (of 89C51ED2, SFR address 0xA8) is the

    equivalent of IE reg of 8051.

    EX0 =1: Enable External interrupt 0 (INT0).

    Also EA=1. */

    P1 = 0xff; /* make P1 as input */

    pedestal = 110; /* pedestal for 45 rps (= 2700 rpm)

    (15 < pedestal < 150)

    pedestal rps

    15 57

    110 45

    150 32 */

    P0=pedestal; /* send pedestal thru P0 */

    InitLcd();

    WriteString("SPEED = ");

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    69/88

    GotoXY(0x0c,0x0);

    WriteString("rps");

    while(1); /* wait for interrupt INT0* */

    }

    The PULSE WITH MODULATION(PWM) technique is used to vary the speed of the DC motor (12V, 4W motor). PULSE WITH IS USED TO CONTROL THE SPEED OF THE MOTOR, KEEPING FREQUENCY CONSTANT AS WE VARY THE PULSE WIDTH THE SPEED OF MOTOR ALSO VARIES PROPORTIONALLY. the speed is maximum (3600rpm = 60 rps). The ramp and pedestal technique is used to change the PW and thereby the speed.

    The different sections of the circuit are:

    1. Rectifier and Regulator 2. Ramp generator 3. DAC4. Motor drive section5. Timing and reference voltage generator 6. Feedback section

    DAC: This is where uC is active. The digital input to this 8-bit DAC is givenfrom port P0. Depending on the digital value, the output of DAC variesand this analog value is the

    pedestal that controls the PW.

    Motor drive section: The ramp and the pedestal voltage are given to the two inputs of a comparator, the output of which is a PWM. Under programcontrol, the PW can be changed and hence the speed of the motor.

    Timing and reference voltage generator: The 555 timer IC gives a square

    pulse with on time (high pulse) of 1 sec. This pulse is given to port pin P1_1as a time reference to measure the speed of the motor.

    Feedback section: Square pulses obtained at the output represent the speed. These pulses are connected to P1_0. When P1_1 is high for 1 sec, the number of pulses on P1_0 will give the count, i.e., speed, in rps.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    70/88

    19. Explain serial port of 8051. Explain the significance of SCON register in detail.

    SBUF is a 8-bit register used solely for serial communication in the 8051. For abyte of data to be transferred via TxD line, it must be placed in the SBUF register.Similarly, SBUF holds the byte of data when it is received by the 8051's RxD line.

    SCON (Serial Configuration) register The SCON register is an 8-bit register used to

    program the start bit, stop bit, and data bits of data framing among other things.

    SM0, SM1

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    71/88

    SM0 and SM1 are the D7 and D6 of the SCON register respectively. These two bitsdetermine the framing of data by specifying the number of bits per character andthe start and stop bits. Following combinations indicate the operations.

    SM0 SM1

    0 0 Serial Mode 0

    0 1 Serial Mode 1, 8 - bit, 1 stopbit,1 start bit

    1 0 Serial Mode 2

    1 1 Serial Mode 3

    Serial Mode 1 allows baud rate to be variable and is set by timer 1 of the 8051.

    SM2

    SM2 is the D5 bit. This bit enables the multiprocessing capability of the 8051. SM2

    is made 0 when 8051 is not used in a multiprocessor environment.

    REN

    REN is the D4 bit. When the REN bit is high, it allows the 8051 to receive data onthe RxD pin of the 8051. By setting the REN to 0, receiver is disabled.

    TB8

    Transfer Bit 8, is the D3. It is used fr serial modes 2 and 3.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    72/88

    RB8

    RB8 is the D2. In serial mode 1, this bit gets a copy of the stop bit when an 8-bitdata is received. RB8 is also used in serial modes 2 and 3.

    TI

    Transmit Interrupt is the D1. When the 8051 finishes the transfer of the 8-bitcharacter, it rises the TI flag to indicate that it is ready to transmit another byte.

    RI

    Receive Interrupt is the D0 bit. When the 8051 receives data serially via RxD, itgets rid of the start and stop bits and places the byte in the SBUF register. Then itrises the RI flag bit to indicate that the byte has been received and should bepicked up before it is lost. RI is raised halfway through the stop bit.

    SCON Register(Bit-Addressable)

    SM0 SM1 SM2 REN TB8 RB8 TI RI

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    73/88

    20a) Write an ALP to read input from port 1, complement it and to output via port2. This transfer is to be done once in 50msec. Use Timer 1 to generate the delay.

    Delay = (2 16 N)Tc

    50 ms = (2 16 N)Tc

    considering XTAL frequency to be 11.0592 MHz, T c=1.085 micro seconds

    N works out to be 4BFDH

    ORG 0000H

    SJMP START

    ORG 0040H

    START: MOV TMOD,#10H ; timer 1, mode 0

    MOV P1,#0FFH ; make port 1 as input

    L1: MOV A,P1 ; move data of port 1 to A

    CPL A ; complementing the dataACALL DELAY ; call delay to establish requireddelay

    MOV P2,A ; move data in A to Port 2

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    74/88

    SJMP L1 ; repeat the procedure

    DELAY: MOV TL1,#0FDH ; N=4BFDH

    MOV TH0,#4BH ;

    SETB TR1 ; start Timer 1

    JNB TF1,$ ; wait till overflow

    CLR TR1 ; reset TR1 and TF1

    CLR TF1

    RET

    END

    20b) Show a scheme of interfacing an 8-bit ADC to 8051 controller. Write thesoftware required in C to obtain the output from such an interface. Discusspractical application

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    75/88

    #include

    sbit RD = P2^5;

    sbitwr = 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 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    76/88

    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 dataacquisition. Digital computers use binary (discrete) values, but in the physicalworld everything is analog (continuous). Temperature, pressure (wind or liquid),

    humidity, and velocity are a few examples of physical quantities that we deal withevery 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 naturalquantities produce an output that is voltage (or current). Therefore, we need an

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    77/88

    analog-to-digital converter to translate the analog signals to digital numbers sothat the microcontroller can read and process them. An ADC has n-bit resolutionwhere n can be 8, 10, 12, 16 or even 24 bits. The higher-resolution ADC provides asmaller step size, where step size is the smallest change that can be discerned byan ADC.

    21) What are edge triggered interrupts? How to set INT0 as level triggeredinterrupt and INT1 as edge triggered interrupt, explain with the help of SFRrelated to it.

    Interrupts which are triggered by an external (rising or) falling edge are known asEdge-triggered interrupts.

    Upon reset the 8051 makes INT0 and INT1 low-level triggered interrupts. To makethem edge-triggered interrupts, we must program the bits of the TCON register.The TCON register holds, the IT0 and IT1 flag bits that determine level or edge-triggered mode of the hardware interrupts. IT0 and IT1 are bits D0 and D2 of the

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    78/88

    TCON register, respectively. They are also referred to as TCON.0 and TCON.2 sincethe TCON register is bit-addressable. Upon reset, TCON.0 (IT0) and TCON.2 (IT1)are both 0s, meaning that the external hardware interrupts of INT0 and INT1 pinsare low-level triggered. By making the TCON.0 and TCON.2 bits high withinstructions such as "SETB TCON. 0" and "SETB TCON. 2", the external hardwareinterrupts of INT0 and INT1 become edge-triggered. For example, the instruction"SETB CON. 2" makes INT1 what is called an edge-triggered interrupt, in which,when a high-to-low signal is applied to pin P3.3, in this case, the controller will beinterrupted and forced to jump to location 0013H in the vector table to servicethe ISR (assuming that the interrupt bit is enabled in the IE register).

    Setting INT0 as level triggered and INT1 as edge Triggered interrupt.

    CLR TCON.0 makes INT0 low -level triggered Interrupt.

    SETB TCON.2 makes INT1 f alling edge triggered external Interrupt.

    Minimum Pulse duration to detect edge triggered interrupts for XTAL frequency11.0592 MHz is 1.085 micro seconds.

    22. a. Explain RS232 hand shaking signals and specify thepurpose of MAX232 while interfacing .

    Ans:

    RS232 handshaking signals are used to ensure fast andreliable datatransmission between two devices. Many of the pins of

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    79/88

    the RS-232 connector are used for handshaking signals.Their descriptions are provided below,

    1. DTR (data terminal ready). When a terminal (or a PC COMport) is turned on, after going through a self-test, it sendsout signal DTR to indicate that it is ready forcommunication. Otherwise it will not activate. This is anactive low signal and it acts as an output pin from DTE (PCCOM port) and an input to the modem.

    2. DSR (data set ready). When DCR (modem) is turned onand has gone through the self test, it asserts DSR toindicate that it is ready to communicate. Thus it is anoutput from modem and input to the PC. This is an activelow signal. If it is inactive it indicates that it cannot acceptor send the data.

    3. RTS (request to send). When the DTE device (such as a PC)has byte to transmit, it asserts RTS to signal the modemthat it has a byte of data to transmit. RTS is an active lowoutput from the DTE and an input to the modem.

    4. CTS (clear to send). In response to RTS, when the modemhas room for storing the data it is to receive, it sends outsignal CTS to the DTE(PC) to indicate that it can receive

    the data now. This input signal to the DTE is used by DTEto start transmission.

    5. DCD (data carrier detect). The modem asserts signal DCDto inform the DTE (PC) that a valid carrier has been

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    80/88

    detected and that contact between it and the othermodem is established. Therefore, DCD is an output fromthe modem and an input to the PC (DTE).

    6. RI (ring indicator). An output from the modem (DCE) andan input to a PC (DTE) indicates that the telephone isringing. It goes on and off in synchronization with theringing sound.

    During interfacing we need a line drive (voltage converter)to convert RS232s signals to TTL voltage levels that will be

    acceptable to the 8051s Tx D and RxD pins. The MAX232converts from RS232 voltage levels to TTL voltage levels,and vice versa. This chip uses same power source as thatof 8051s source voltage.

    22.b. Describe the 8051 connection to stepper motor. A switchis connected to pin P2.7. Write an ALP 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 counterclockwise.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    81/88

    Ans:

    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 towhich winding leads.

    2. The common wire(s) are connected to the positive side of the motors power supply. In many motor s, +5v issufficient.

    3. The four leads of the stator winding are controlled by fourbits of the 8051 port (p1.0-p1.3). However, since the 8051

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    82/88

    lacks sufficient current to drive the stepper motorwindings, we must use a driver such as the ULN2003 toenergize the stator. Instead of the ULN2003, we could

    have transistors as drivers, as shown in figure. However,notice that if transistors are used as drivers, we must alsouse diodes to take care of inductive current generatedwhen the coil is turned off. One reason that using theULN2003 is preferable to the use of transistors as drivers isthat the ULN2003 has an internal diode to take care of back EMF.

    A program to monitor the status of SW and perform thegiven operations.

    Label Mnemonic Operands CommentsORG OH ;starting address

    MAIN: SETB P2.7 ;make an input

    MOV A,#66H ;starting phase valueMOV P1,A ;send value to port

    TURN:JNB P2.7,CW ;check switch resultRR A ;rotate rightACALL DELAY ;call delayMOV P1,A ;write value to portSJMP TURN ;repeat

    CW: RL A ;rotate leftACALL DELAY ;call delayMOV P1,A ;write value to portSJMP TURN ;repeat

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    83/88

    DELAY:MOV R2,#100

    H1: MOV R3,#255

    H2: DJNZ R3,H2DJNZ R2,H1RETEND

    23.) Write a C program to send the message Good Morning serially at 9600baud rate, 8 data bit, 1 stop bit.

    Ans->

    #include

    void main(void)

    {

    unsigned char z;

    unsigned char Messg[]=Good Morning;

    TMOD=0x20; //Use Timer 1, 8-bit auto reload

    TH1=0xFD; //9600 baud rate

    SCON=0x50; //serial mode 1, 8 bit data, 1stop bit, //1 start bit

    TR1=1; //start timer

    for(z=0;z

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    84/88

    }

    }

    24. a. Explain importance of TI and RI flags.

    Ans: Importance of the Ti flag:

    Following steps illustrate that the 8051 goes through intransmitting a

    character via TxD.1. The byte character to be transmitted is written into the

    SBUF register.2. The start bit is transferred one bit at a time.3. The 8-bit character is transferred one bit at a time.4. The stop bit is transferred. During this stop bit transfer

    TI=1. It will indicating that the last character wastransmitted and is ready to transfer the next character.

    5. By monitoring the TI flag, we make sure that we are notoverloading the SBUF register.

    6. After SBUF is loaded with a new byte, the TI flag bit mustbe forced to 0 by the CLR TI instruction in order for this

    new byte to be transferred.7. By checking the TI flag bit, we know whether or not the

    8051 is ready to transfer another byte.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    85/88

    Importance of the RI flag bit:In receiving bits via its RxD pin, the 8051 goes through thefollowing steps.

    1. It receives the start bit indicating that the next bit is thefirst bit of the character byte it is about to receive.

    2. The 8-bit character is received one bit at time. When thelast bit is received, a byte is formed and placed in SBUF.

    3. The stop bit is received. When receiving the stop stop bitthe RI=1, indicating that an entire character has beenreceived.

    4. By checking the RI flag bit when it is raised, we know that acharacter has been received and in the SBUF register.

    5. After SBUF contents are copied, the RI flag bit must beforced to zero.

    6. By checking the RI flag bit we know whether or not the8051 has received a character byte.

    24. b. Show a simple keyboard interface with Port 1 and Port 2of 8051 and

    explain its operation.

    Ans. In the lowest level, key boards are organized in a matrix of rows and columns. The CPU accesses both rows andcolumns through 2 8-bit ports. When key is pressed, a row

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    86/88

    and a column make a contact: otherwise, there is noconnection between rows and columns. In IBM keyboards,a single microcontroller takes care of hardware andsoftware interfacing of the keyboard. In such systems, it isthe function of programs stored in the EPROM of themicrocontroller to scan the keys continuously, identifywhich one has to be activated, and present it to themotherboard. The following figure shows a 4*4 matrix connected to twoports. The rows are connected to the output port and thecolumns are connected to an input port. If no key has beenpressed, reading of the input port yield 1s for all columnssince they are all connected to high. If all the rows aregrounded and a key is pressed, one of the columns willhave 0 since the key pressed provides the path to ground.

    IT is the function of the microcontroller to scan thekeyboard continuously to detect and identify the keypressed. If the data read from the columns is d3 d0=1111, no keyhas been pressed and the process continues until a keypress is detected. However, if one of the column bits has a

    zero, this means that a key press has occurred. After a keypress is detected, the microcontroller will go through theprocess of identifying the key. Starting with the top row,the microcontroller grounded it by providing a low to row

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    87/88

    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 tothe next row. It grounds the next row, read the columns,and checks for any zero. This process continues until therow is identified. After identification of the row in whichthe key has been pressed, the next task is to find outwhich column the pressed key belongs to.

  • 8/3/2019 Micro Controller Assignment 2 (All 24 From Shiva Prasad)

    88/88