You are on page 1of 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 (<<). These bit wise operators are widely used in software engineering for embedded systems and control.

AND A 0 0 1 1 B 0 1 0 1 A&B 0 0 0 1

OR A|B 0 1 1 1

EX-OR A^B 0 1 1 0

Inverter Y=~B 1 0

The following shows some examples: 1.) 0x35 & 0x0F = 0x05 2.) 0x04 | 0x68 = 0x6C 3.) 0X54 ^ 0x78 = 0x2C 4.) ~0x55 = 0xAA Eg. /* ANDing */ /*ORing*/ /*XORing*/ /* Inverting 55h*/

#include <reg51.h> void main(void) { P0= 0x35 & 0x0F; //ANDing

P1= 0x04 | 0x68; //ORing P2= 0x54 ^ 0x78; //XORing P3= ~0x55; P1=0x9A >> 3; P2=0x77 >> 4; P3=0x6 << 4; } There are two bit wise shift operators in C: (i)shift right(>>) and (ii) shift left(<<). Their format in C is as follows: data>>number of bits to be shifted right data<<number of bits to be shifted left Eg. 0x9A>>3=0x13 /*shifting right 3 times*/ x77>>4=0x07 x6<<4 = 0x60 /*shifting right 4 times*/ /* shifting left 4 times*/ //Inverting //shifting right 3 times //shifting right 4 times //shifting left 4 times

2. a. Explain different modes of operation of timer/counter with relevant block diagrams. Ans: The TMOD SFR: The TMOD SFR is used to control the mode of operation of both timers. Each bit of the SFR gives the microcontroller specific information concerning how to run a timer. The high four bits (bits 4 through 7) relate

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 following functions: Bit Name Explanation of Function When this bit is set the timer will only run when INT1 (P3.3) is high. 7 GATE1 When this bit is clear the timer will run regardless of the state of INT1. When this bit is set the timer will count events on T1 (P3.5). When 6 C/T1 this bit is clear the timer will be incremented every machine cycle. 5 T1M1 Timer mode bit (see below) 4 T1M0 Timer mode bit (see below) When this bit is set the timer will only run when INT0 (P3.2) is high. 3 GATE0 When this bit is clear the timer will run regardless of the state of INT0. When this bit is set the timer will count events on T0 (P3.4). When 2 C/T0 this bit is clear the timer will be incremented every machine Timer

1 1

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 each timer) 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 was kept around in the 8051 to maintain compatibility with its predecessor, the 8048. Generally the 13-bit timer mode is not used in new development. When the timer is in 13-bit mode, TLx will count from 0 to 31. When TLx is incremented from 31, it will "reset" to 0 and increment THx. Thus, effectively, only 13 bits of the two timer 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 to zero 8192 machine cycles later. Again, there is very little reason to use this mode and it is only mentioned so you wont be surprised if you ever end up analyzing archaic code which has been passed down through

the generations (a generation in a programming shop is often on 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. Tc=period of one machine cycle=1.08510-6s (fosc=11.0592MHz)

osc

12

13 bit counter
TH(8b) TL(5b) TF

T0 INTERRUPT
TR
0

CONTROL

Gate
INTO

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 16 bits are used. TLx is incremented from 0 to 255. When TLx is incremented from 255, it resets to 0 and causes THx to be incremented by 1. Since this is a full 16-bit timer, the timer may contain up to 65536 distinct values. If you set a 16-bit timer to 0, it will overflow back to 0 after 65,536 machine cycles.

Delay= (216-n) Tc , where n is count which is to be placed in TH & TL register. Tc=period of one machine cycle=1.08510-6s (fosc=11.0592MHz).

osc

12

16 bit counter
TH(8b) TL(8b) TF

T0 INTERRUPT
TR
0

CONTROL

Gate
INTO

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 the timer itself. Thus, TLx starts counting up. When TLx reaches 255 and is subsequently incremented, instead of resetting to 0 (as in the case of modes 0 and 1), it will be reset to the value stored in THx. For example, lets say TH0 holds the value FDh and TL0 holds the value FEh. If we were to watch the values of TH0 and TL0 for a few machine cycles this is what wed see:

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 known value and TLx is the SFR that is constantly incremented. If you use mode 0 or 1, youd have to check in code to see if the timer had overflowed and, if so, reset the timer to 200. This takes precious instructions of execution time to check the value and/or to reload it. When you use mode 2 the microcontroller takes care of this for you. Once youve configured a timer in mode 2 you dont have to worry about checking to see if the timer has overflowed nor do you have to worry about resetting the value--the microcontroller hardware will do it all for you. The auto-reload mode is very commonly used for establishing a baud rate which we will talk more about in the Serial Communications chapter. Delay= (28-n)Tc , where n is count which is to be placed in TH & TL register. Tc=period of one machine cycle=1.08510-6s (fosc=11.0592MHz).

osc

12

8 bit counter interrupt


T0 CONTROL TR0
TRISTATE BUFF

TL0 (8b)

TF

EN

gate
INTO TH0(8b)

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

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

osc

12

interrupt
T0 TR0 CONTROL

TL0(8b)

16 BIT COUNTER
TF

Gate
INTO

OSC

12
TR1 CONTROL

TH0

TF1

2. b. Explain the registers and pins of an LCD panel and write an 8051 C program to display message HELLO on the LCD panel. ANS: REGISTERS OF THE LCD PANEL:

a. Command code register: This register is selected when RS=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 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 entire display to left

Shift entire display to right 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 each pin is given in the following table, Pin descriptions of LCD. Pin Symbol 1 Vss 2 Vcc 3 Vee 4 RS I/O I Description GROUND +5V power supply Power supply to control contrast RS=0 to select command register RS=1 to select data register R/W=1 for write, R/W=1 for read Enable THE 8-bit data bus THE 8-bit data bus THE 8-bit data bus

1c 80

5 6 7 8 9

R/W E DB0 DB1 DB2

I I/O I/O I/O I/O

10 11 12 13 14

DB3 DB4 DB5 DB6 DB7

I/O I/O I/O I/O I/O

THE 8-bit data bus THE 8-bit data bus THE 8-bit data bus THE 8-bit data bus THE 8-bit data bus

Vcc, Vss and VEE : While Vcc and Vss provides +5V and ground, respectively, Vee is used for controlling LCD contrast. RS, register select: There are two very important register in LCD. This RS register is used to select them. If RS=0, the instruction command code register is selected. If RS=1, the data register is selected. R/W, read/write: This input allows the LCD to write the information into it or read the information. R/W=1 when it is reading and R/W=0 when it is writing. E, enable: The enable pin is used by the LCD to latch information presented to its data pins. 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 register. D7 acts as a busy flag and can be read when R/W=1, RS=0.

An 8051 C program to display HELLO on the LCD panel. #include <reg51.h> sfr ldata = 0x90; sbit rs = P2^0; sbit rw = P2^1; sbit en =P2^2;

//P1=LCD data pins

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; rs = 1; rw =0; en = 1; MSDelay (1); en=0; return; //put the value on the pins //to select the data register //strobe the enable pin

} void MSDelay (unsigned int itime) { unsigned int i, j; for (i=0; i<itime; i++) for (j=0; j<1275; j++); } void main () { unsigned char msg[] =HELLO; int k; lcdcmd (0x38); //2 lines and 5*7 matrix MSDelay (250); lcdcmd (0x0E); // Display on and cursor blinking MSDelay (250); lcdcmd (0x01); // Clear display screen MSDelay (250); lcdcmd (0x06); // Shift cursor to right after inserting each //character MSDelay (250); lcdcmd (0x86); //line 1, position 6 MSDelay (250); for (k=0; k<5; k++) { lcddata (msg[k]); MSDelay(250);

} }
3.)Explain the steps to program timers in mode 1 and write an 8051 program to generate 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 are taken: 1.) Load the TMOD value indicating which timer (Timer 1 or Timer 0) is to be used 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. It is calculated on the basis of the given formula:delay=(216-n)x (time period of clock cycle) where n combined value stored in THx and TLx. and clock cycle is calculated as Tc=1/fc and fc=fosc /12; fosc 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

START: mode)

MOV TMOD,#01H

;Timer 0, mode 1(16 bit ;TL0=F2H, the Low byte ;TH0=FFH, the High byte

HERE:MOV TL0,#0F2H MOV TH0,#0FFH CPL P1.5 ACALL DELAY SJMP HERE

;toggle P1.5

;load TH,TL again

;----------delay using Timer 0 DELAY: SETB TR0 AGAIN:JNB TF0,AGAIN it rolls CLR TF0 RET END ;start Timer 0 ; monitor Timer 0 flag till ;over ;clear Timer 0 flag ;return to instruction post call

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 a single pin of micro controller. There are two ways to transfer a byte of data serially.

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

Consider the following ALP.

MOV TMOD ,#20H //timer 1, mode 0 MOV TH1,#-3 MOV SCON,#50H SETB TR1 CLR RI JNB RI,$ MOV A,SBUF MOV P0,A MOV 60H,A END //start timer 1 //reception mode //wait for character to come in //move received data into A //move it to P0 //move it into location 60H //9600 baud

The above program demonstrates receiving data sent in serial form anf sending it out 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 sequence of data and spaces in between them. Ex : Consider the following C program.

#include<reg51.h> sbit P1b0 = P1^0; sbit regAMSB = ACC^7; void main(){ unsigned char conbyte = 0x44; unsigned char x; ACC = conbyte; for(x=0;x<8;x++){ P1b0 = regAMSB; ACC == ACC<<1; }}

Here the data 44H is serially sent out 1 bit at a time via P1.0. The MSB goes first in this case.

4b) Draw the 8051 connection to DAC0808 at port P1 and write an 8051 ALP to generate sine wave.

Angle(Degrees)

Sin (angle)

0 30 60 90 120 150 180 210 240

0 0.5 0.87 1 0.87 0.5 0 -0.5 -0.87

Voltage Magnitude 5V + (5V*Sin(angle)) 5 7.5 9.33 10 9.33 7.5 5 2.5 0.67

Voltage sent to DAC (Voltage Mag. * 25.6) 128 192 238 255 238 192 128 64 17

270 300 330 360


ALP : ORG 40

-1 -0.87 -0.5 0

0 0.67 205 5

0 17 64 128

AGAIN: MOV DPTR,#TABLE MOV R2,#COUNT BACK: CLR A MOVC A,@A+DPTR MOV P1,A INC DPTR DJNZ R2,BACK SJMP AGAIN ORG 300H TABLE DB 128,192,238,255,192 DB 128,64,17,0,17,64,128 END

; initializing DPTR to the starting of table ; initializing count ; ; ; moving values from LookUp table to Port 1

; looping the values ; looping the sine wave ; initializing the LUP

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

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= (216 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.

Delay= (216 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<reg51.h> sbit mybit =P1^7; sbit SW = P1^2; //initialize data //initializ switch

void T0M1Delay(unsigned char); void main(void){ SW = 1; while(1){ mybit = ~mybit; if (SW==0) T0M1Delay(0); else T0M1Delay(1); } } //change state //configure for input

void T0M1Delay(unsigned char c){ TMOD = 0x01; if(c==0){ TL0 =0x67; TH0 =0x FC; } else{ TL0 = 0x9A; TH0 = 0xFD; } TR0 = 1; while(TF0==0); TR0 = 0; TF0 = 0; } //wait till TF0 becomes 0 //reset TR0 and TF0 //n is FD9A for 750 Hz //n is FC67 for 500 Hz

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 to the output latch, then it reads the columns. If the data read from the columns is D3 - DO = 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. For example, if D3 - DO = 1101, this means that a key in the Dl column has been pressed. 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 DO only; then it reads the columns. If the data read is all Is, 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 task is achieved as the microcontroller knows 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 in data processing. As the speed of data transfer increases, the EM disturbances also increases, 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.

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 causing undesirable interference is data reception. One key advantage of serial over parallel communication is that the serial communication allows for data to be transferred in from a remote device or transferred out from the hard drive to a remote device. This two-way communication process makes it possible to connect work stations to larger terminals as well as a wide range of peripheral devices.

Serial communication can span longer distances than parallel communication. Although longer cables exist, the recommended maximum length 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 DATA ARE 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:

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 microcontrollers differentiate between two priority levels defined in the IP register. SFR register IPH which enables all the interrupts to be assigned 1 out of 4 priorities (excluding reset). Here is a list of priorities: 7 6 5 4 3 2 1 0 ps Pt1 Px1 Pt0 PX0 bch bbh Bah B9h B8h

Undefined Undefined Undefined Serial Interrupt Priority Timer 1 Interrupt Priority External 1 Interrupt Priority Timer 0 Interrupt Priority External 0 Interrupt Priority

IP register (Interrupt Priority Register)

Bits of this register determine the interrupt source priority. PT2 Timer T2 interrupt priority:

0 - Priority 0 1 - Priority 1 PS Serial port interrupts priority:

1. 0 - Priority 0 2. 1 - Priority 1 PT1 Timer T1 interrupts priority: 0 - Priority 0 1 - Priority 1 PX1 External interrupts INT1 priority: 1. 0 - Priority 0 2. 1 - Priority 1 PT0 Timer T0 interrupt priority: 0 - Priority 0 1 - Priority 1 PX0 External interrupt INT0 priority: 0 - Priority 0 1 - Priority 1 IPH Register (Interrupt Priority High)

PT2H Timer T2 interrupt priority PSH Serial port interrupt priority PT1H Timer T1interrupt priority PX1H External interrupt INT1 priority

PT0H Timer T0 interrupt priority PX0H External interrupt INT0 Priority Bits of this register can be combined with appropriate bits of the IP register. This is how a new priority list with 4 interrupt priority levels (5 including reset) is obtained. IP bit 0 0 1 1 IPH bit 0 1 0 1 Interrupts Priority 0 (lowest) Priority 1 (low) Priority 2 (high) 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 with 8051 micro controller.

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.4 Oe bit p2.5

Sc

bit p2.6

Eoc bit p2.7 Addr_a bit p2.0 Addr_b bit p2.1 Addr_c bit p2.2 Mydata equ p1 Org 0h Mov mydata ,#offh Setb soc ; to make p1 as input ; to make eoc as input

Cle ale; to clear ale Clr sc; clear wr Cle oe;clear rd Back: Clr Addr_c; c=0 Clr Addr_b; b=0 Setb addr_a; a=1 to select channel 1 Acall delay; make sure address is table Setb ale;latch address Acall delay; delay for fast ds89c4x0 chip Setb sc; start conversion Acall 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

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 <reg51.h> sbit WAVE =P0^1; void timer0() interrupt 1 { WAVE=~WAVE; } void serial0() interrupt 4 { if (TI==1) //toggle pin

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

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 <reg51.h> 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; while(1) { for(i=0; i<22; i++) message SerTx(message[i]); } } //send all characters of the //start timer

void SerTx(unsigned char x) { SBUF = x; while(T1==0); //place value in buffer //wait until transmitted

T1 = 0; }

10 b) 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 counterclockwise

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 drive the 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.

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 stepper motor // clockwise if SW = 0; anticlockwise if SW = 1 #include <reg.h> sbit SW=P2^7; void main() { SW=1; while(1) {

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); } } }

void MSDelay(unsigned int value) unsigned int x,y; for(x=0;x<1275;x++) for(y=0;y<value;y++); }

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 Reset External hardware interrupt 0 (INT0) Timer 0 interrupt (TF0) External hardware interrupt 1 (INT1) Timer 1 interrupt (TF1) Serial COM interrupt (RI and TI)

ROM Location (Hex) 0000 0003 9

Pin

Flag clearing Auto Auto

P3.2

000B 0013 P3.3

Auto Auto

001B 0023

Auto Programmer clears it

In the above table, the RESET interrupt is activated once the reset pin is held high for at least 2 machine cycles before it goes low. Also it is noted that a limited number of bytes are set aside for each interrupt (8 bytes except RESET which has 3 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 bypass the Interrupt Vector Table if needed.

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

ORG 0 LJMP START vector table

; the wake-up ROM reset location ; bypass/redirect away from the interrupt

ORG 0030H START: .... .... END ; normal program executed on wake-up

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

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

EA
7 0

ET2
5

ES
4

ET1
3

EX1
2

ET0
1

EX0

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 all interrupts or set to 1 to permit individual interrupts to be enabled by their interrupt 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 INT0 interrupt or cleared to 0 to disable it. The Interrupt Priority (IP) register is used by the programmer to determine if any interrupt is to have a high or low priority.


7 0

PT2
5

PS
4

PT1
3

PX1
2

PT0
1

PX0

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 a low priority to it. Interrupts with a high priority can interrupt another interrupt with 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 have the 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. But since the interrupts are polled according to the above ranking, they will have the following priority overriding the default priority assigned by 8051: Highest Priority External interrupt 1

(INT1)

Timer 1 interrupt External interrupt 0 Timer 0 interrupt Lowest Priority Serial communication

(TF1) (INT0) (TF0) (RI and TI)

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

12 b) How can a microcontroller be used to control automatically the speed of 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 pulse applied to the motor we can increase or decrease the amount of power provided to the motor and thereby increasing/decreasing the motor speed. Here the voltage has fixed amplitude but variable duty cycle i.e. wider the pulse, higher the speed.

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 high and low portions of the desired pulse, the rest is taken care of by the microcontroller. This allows the microcontroller to do other things. For microcontrollers without PWM circuitry, the various duty cycles must be created using software, which prevents the microcontroller from doing other things.

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

Calculations: Since 250ms > 71ms which is the maximum delay the 8051 can generate in one go, 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 <reg51.h> void T1M1Delay(void); void main() { unsigned char x; P0 = 0x55; (55H) P2 = 0x55; while(1) { P2 = ~P2; P0 = ~P0; for(x=0; x<10; x++) // toggle all bits of P0 and P2 // let P0 and P2 initially contain 01010101

T1M1Delay(); 10 times } }

// generate 25ms delay and call it

void T1M1Delay(void) { TMOD = 0x10; TL1 = 0xFE; TH1 = 0xA5; TR1 = 1; while(TF1==0); TR1 = 0; TF1 = 0; } // Timer 1, mode 1 (16-bit) // load TL1 // load TH1 // turn on Timer 1 // wait till TF1 rolls over // turn off Timer 1 // clear Timer flag TF1

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 ALP display message MSRIT on the LCD panel.

14 a)

TIMER The internal system clock of the 8051 is applied to the timer It counts machine cycles It is used to generate time delays in programs, to generate specific baud rates for use in serial communication etc. C/ T = 0

COUNTER External signal is applied to its clock input It counts external events It is used in counting events like number of times a switch turns on/off or the number of pulses sent by a sensor etc. C/ T = 1

Timer modes: The TMOD register of the 8051 has two bits for each timer i.e. M1 and M0. These two bits decide the mode of operation of the respective timer which is detailed below:

M1 0

M0 0

Mode 0

Mode Description 13-bit timer mode

i.e. 8-bit timer/counter THx with TLx as 5-bit prescaler 0 1 1 16-bit timer mode 16-bit timer/counters THx and TLx are cascaded; there is no prescaler 8-bit auto reload 8-bit auto reload timer/counter' THx holds a value that is to be reloaded into TLx each time it overflows Split timer mode

1
14 b )

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

Pin 1 2 3 4

Symbol VSS VCC VEE RS

I/O I

Description Ground +5V power supply Power supply to control contrast RS = 0 to control command register RS = 1 to select data register

5 6 7 8 9 10 11 12 13 14

R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7

I I/O I/O I/O I/O I/O I/O I/O I/O I/O

R/W = 0 for write, R/W = 1 for read Enable The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus 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 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.

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 any new information. When D7 = 0, the LCD is ready to receive new information.

Code (Hex) 1 2 4 6 5 7 8 A C E F 10 14 18 1C 80 C0 38

Command to LCD instruction register

Clear display screen Return home Decrement cursor (shift cursor to left) Increment cursor (shift cursor to right) Shift display right Shift display left Display off, cursor off Display off, cursor on Display on, cursor off Display on, cursor blinking Display on, cursor blinking Shift cursor position to left Shift cursor position to right Shift the entire display to the left Shift the entire display to the right Force cursor to beginning of 1st line Force cursor to beginning of 2nd line 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 ACALL DELAY JZ SEND_DAT INC DPTR SJMP C1 SEND_DAT: MOV DPTR,#MYDATA

;;

; call command subroutine ; give LCD some time

D1: CLR A MOVC A,@A+DPTR ACALL DATAWRT ACALL DELAY INC DPTR JZ AGAIN SJMP D1 ; call command subroutine ; give LCD some time

AGAIN: SJMP AGAIN COMNWRT: MOV P1, A CLR P2.0 CLR P2.1 SETB P2.2 ACALL DELAY CLR P2.2 RET DATAWRT: MOV P1, A SETB P2.0 CLR P2.1 SETB P2.2 ACALL DELAY CLR P2.2 RET DELAY: MOV R3, #250 HERE2: HERE: MOV R4, #255 DJNZ R4, HERE

; stay here ; send command to LCD ;SEND COMND to P1 ; RS=0 for command ; R/W=0 for write ; E=1 for high pulse ; give LCD some time ; E=0 for H-to-L

; SEND DATA to P1 ; RS=1 for data ; R/W=0 for write ; E=1 for high pulse ; give LCD some time ; E=0 for H-to-L pulse

; long delay for fast CPUs

DJNZ R3, HERE2 RET ORG 0300H MYCOM: DB 38H,0EH,01H,06H,84H,00H ; lookup table ; commands and null ; data and null

MYDATA: DB MSRIT, 00H END

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

#include <reg51.h> sbit SW = P2^0; sbit destnBit = P1^0; // The switch SW // The destination bit P1.0

sbit regA_LSB = ACC^0; // Use LSB and MSB of the accumulator to send data sbit regA_MSB = ACC^7; void main() { unsigned char sdata = 0x44; unsigned char x; ACC = sdata; if(SW==0) for(x=0; x<8; x++) { destnBit = regA_LSB; ACC >>= 1; } // send LSB first // data to be sent // serially

else for(x=0; x<8; x++) {

destnBit = regA_MSB; ACC <<= 1; }

// send MSB first

16 a) write a 8051 c program to convet a given hex data fdh into its equivalent decimal data and display the result digit on p1,p0,p2?
Ans:
#include<reg51.h> Void main(void)

{
Unsigned char x, binbyte,d1,d2,d3; Binbyte=fdh; X= binbyte/10; D1 = binbyte%10; D2 = x%10; D2=x/10; msb P0=d1; P1=d2; // hex data //divide by 10 // find remainder i.e lsb // middle digit

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 (Iout), and by connecting a resistor to the Iout pin, we convert the result to voltage. The total current provided by the Iout pin is a function of the binary numbers at the DO - D7 inputs of the DAC0808 and the reference current (Iref), and is as follows:

where DO is the LSB, D7 is the MSB for the inputs, and Iref is the input current that must be applied to pin 14. The Iref current is generally set to 2.0 mA. Figure 13-18 shows the generation of current reference (setting Iref = 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 fluctuation associated. C program to generate sine wave #include <REG51xD2.H> #include "lcd.h"

void delay() { TR1 = 1; while(TF1 == 0); TR1 = 0; TF1 = 0; return; } void main() { unsigned char i;

/* delay routine */

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<24;i++) { P0 = table[i]; P1 = table[i]; TL1 = 0xf0; TH1 = 0xff; /*--------------------------------------------

To find count n in mode 1 use:

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 VARIOUS OPERATION MODES. TH0 D15 D14 D13 D12 D11 D10 D9 D8

TLO

D7

D6

D5

D4

D3

D2

D1

D0

TIMER 0 REGISTERS

TH1 TL1

D15 D7

D14 D6

D13 D5

D12 D4

D11 D3

D10 D2

D9 D1

D8 D0

TIMER 1 REGISTERS

TMOD BIT PATTERNS;


MSB GATE TIMER1 C/T M1 M0 GATE C/T TIMER0 M1 LSB M0

M1 0 0 1 1

M0 0 1 0 1

MODE 0 1 2 3

OPERATING MODES 13 BIT TIMER MODE, 5 BIT PRESCALAR 16 BIT TIMER MODE, NO PRESCALAR 8 BIT AUTO RELOAD, THX HOLDS VALUE TO BE RELOADED TO TLX 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 M1 SELECT THE TIMER MODE. WITH REFERENCE TO TABULAR COLUMN SHOWN THERE ARE THREE MODES OF OPERATION. MODE 0 IS A 13 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.

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 Clr tr0 Setb tf0 Clr tf0 Setb tcon.4 Clr tcon.4 Setb tcon.5 Clr tcon.5

Timer 0

Setb tr1 Clr tr1 Setb tf1 Clr tf1

Setb tcon6 Clr tcon.6 Setb tcon.7 Clr tcon.7

Timer 1
The bit patterns of tcon register is shown below Tf1 Tr1 Tf0 Tr0 Ie1 It1 Ie0 It0

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 when interrupt 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 when external interrupt is detected. Cleared by hardware when interrupt 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 registers by 8051. The timer is then started by the instructions setb tro for timer 0 and setb tr1 for timer1.

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 an auto reload process which makes tl register loaded by th register

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

XTAL OSC
SETS

MOD 12

AND GATE

TL

TF
OVERFLOW FLAG

RELOAD

AS FF->0

C/TBAR TR

TH

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.

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 <REG51xD2.H> #include "lcd.h" unsigned char pedestal,count1,count,rx;

void speed() { while( P1_1); while(!P1_1); count=0x00; do { while( P1_0); while(!P1_0); /* wait for P1_1 to become 0 */ /* wait for P1_1 to become 1 */

count++ ;

/* increment after every pulse at P1_0 */

}while( P1_1);

/* When P1_1 is high for 1 sec, the number of

pulses on P1_0 will give the count i.e., speed, in rotations per second. */ return; }

void display() { if (count > 0x09) {

/* convert the count to ASCII for LCD display */

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 */

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 15 110 150

rps 57 45 32 */

P0=pedestal; InitLcd(); WriteString("SPEED = ");

/* send pedestal thru P0 */

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 (3600 rpm = 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. DAC 4. Motor drive section 5. Timing and reference voltage generator 6. Feedback section DAC: This is where uC is active. The digital input to this 8-bit DAC is given from port P0. Depending on the digital value, the output of DAC varies and 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 program control, 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_1 as 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.

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 a byte 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

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

SM0 0 0 1 1

SM1 0 1 0 1

Serial Mode 0 Serial Mode 1, 8-bit, 1 stopbit, 1 start bit Serial Mode 2 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 on the 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.

RB8 RB8 is the D2. In serial mode 1, this bit gets a copy of the stop bit when an 8-bit data 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-bit character, 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, it gets rid of the start and stop bits and places the byte in the SBUF register. Then it rises the RI flag bit to indicate that the byte has been received and should be picked 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

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

Delay = (216 N)Tc 50 ms = (216 N)Tc considering XTAL frequency to be 11.0592 MHz, Tc=1.085 micro seconds N works out to be 4BFDH

ORG 0000H SJMP START ORG 0040H START: MOV TMOD,#10H MOV P1,#0FFH L1: MOV A,P1 CPL A ACALL DELAY delay MOV P2,A ; timer 1, mode 0 ; make port 1 as input ; move data of port 1 to A ; complementing the data ; call delay to establish required

; move data in A to Port 2

SJMP L1 DELAY: MOV TL1,#0FDH MOV TH0,#4BH SETB TR1 JNB TF1,$ CLR TR1 CLR TF1 RET END

; repeat the procedure ; N=4BFDH ; ; start Timer 1 ; wait till overflow ; reset TR1 and TF1

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

#include <reg51.h> sbit RD = P2^5; sbitwr = P2^6; sbit INTR = P2^7; sfr MYDATA = P1; void main() { unsigned char value; MYDATA = 0xFF; INTR = 1; // make P1 as input // make INTR input

RD = 1; WR = 1; while(1) { WR = 0; WR = 1; while(INTR==1); RD = 0; value = MYDATA;

// set RD high // set WR high

//send WR pulse

// wait for EOC //send RD pulse // 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) signals using 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.

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

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

Upon reset the 8051 makes INT0 and INT1 low-level triggered interrupts. To make them 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 edgetriggered mode of the hardware interrupts. IT0 and IT1 are bits D0 and D2 of the

TCON register, respectively. They are also referred to as TCON.0 and TCON.2 since the 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 pins are low-level triggered. By making the TCON.0 and TCON.2 bits high with instructions such as "SETB TCON. 0" and "SETB TCON. 2", the external hardware interrupts 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 be interrupted and forced to jump to location 0013H in the vector table to service the 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 falling edge triggered external Interrupt.

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

22. a. Explain RS232 hand shaking signals and specify the purpose of MAX232 while interfacing. Ans: RS232 handshaking signals are used to ensure fast and reliable data transmission between two devices. Many of the pins of

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 COM port) is turned on, after going through a self-test, it sends out signal DTR to indicate that it is ready for communication. Otherwise it will not activate. This is an active low signal and it acts as an output pin from DTE (PC COM port) and an input to the modem. 2. DSR (data set ready). When DCR (modem) is turned on and has gone through the self test, it asserts DSR to indicate that it is ready to communicate. Thus it is an output from modem and input to the PC. This is an active low signal. If it is inactive it indicates that it cannot accept or 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 modem that it has a byte of data to transmit. RTS is an active low output from the DTE and an input to the modem. 4. CTS (clear to send). In response to RTS, when the modem has room for storing the data it is to receive, it sends out signal CTS to the DTE(PC) to indicate that it can receive the data now. This input signal to the DTE is used by DTE to start transmission. 5. DCD (data carrier detect). The modem asserts signal DCD to inform the DTE (PC) that a valid carrier has been

detected and that contact between it and the other modem is established. Therefore, DCD is an output from the modem and an input to the PC (DTE). 6. RI (ring indicator). An output from the modem (DCE) and an input to a PC (DTE) indicates that the telephone is ringing. It goes on and off in synchronization with the ringing 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 TxD and RxD pins. The MAX232 converts from RS232 voltage levels to TTL voltage levels, and vice versa. This chip uses same power source as that of 8051s source voltage. 22.b. Describe the 8051 connection to stepper motor. A switch is 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.

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 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 sufficient current to drive the stepper motor windings, we must use a driver such as the ULN2003 to energize 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 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 back EMF. A program to monitor the status of SW and perform the given operations. Label MAIN: Mnemonic ORG SETB MOV MOV JNB RR ACALL MOV SJMP RL ACALL MOV SJMP Operands OH P2.7 A,#66H P1,A P2.7,CW A DELAY P1,A TURN A DELAY P1,A TURN Comments ;starting address ;make an input ;starting phase value ;send value to port ;check switch result ;rotate right ;call delay ;write value to port ;repeat ;rotate left ;call delay ;write value to port ;repeat

TURN:

CW:

DELAY: H1: H2: MOV MOV DJNZ DJNZ RET END R2,#100 R3,#255 R3,H2 R2,H1

23.) Write a C program to send the message Good Morning serially at 9600 baud rate, 8 data bit, 1 stop bit. Ans-> #include <reg51.h> void main(void) { unsigned char z; unsigned char Messg[]=Good Morning; TMOD=0x20; TH1=0xFD; SCON=0x50; stop bit, TR1=1; //Use Timer 1, 8-bit auto reload //9600 baud rate //serial mode 1, 8 bit data, 1 //1 start bit //start timer

for(z=0;z<13;z++) //write message { SBUF=Messg[z]; while(TI==0); TI=0; //place value in buffer //wait for transmit

//clear transmit flag

} }

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


Importance of the Ti flag: Following steps illustrate that the 8051 goes through in transmitting 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 was transmitted and is ready to transfer the next character. 5. By monitoring the TI flag, we make sure that we are not overloading the SBUF register. 6. After SBUF is loaded with a new byte, the TI flag bit must be 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.

Importance of the RI flag bit: In receiving bits via its RxD pin, the 8051 goes through the following steps. 1. It receives the start bit indicating that the next bit is the first bit of the character byte it is about to receive. 2. The 8-bit character is received one bit at time. When the last bit is received, a byte is formed and placed in SBUF. 3. The stop bit is received. When receiving the stop stop bit the RI=1, indicating that an entire character has been received. 4. By checking the RI flag bit when it is raised, we know that a character has been received and in the SBUF register. 5. After SBUF contents are copied, the RI flag bit must be forced to zero. 6. By checking the RI flag bit we know whether or not the 8051 has received a character byte. 24. b. Show a simple keyboard interface with Port 1 and Port 2 of 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 and columns through 2 8-bit ports. When key is pressed, a row

and a column make a contact: otherwise, there is no connection between rows and columns. In IBM keyboards, a single microcontroller takes care of hardware and software interfacing of the keyboard. In such systems, it is the function of programs stored in the EPROM of the microcontroller to scan the keys continuously, identify which one has to be activated, and present it to the motherboard. The following figure shows a 4*4 matrix connected to two ports. The rows are connected to the output port and the columns are connected to an input port. If no key has been pressed, reading of the input port yield 1s for all columns since they are all connected to high. If all the rows are grounded and 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 and identify the key pressed. 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 grounded 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, read 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.

You might also like