INTERFACING Hardware Interfacing 1. Waveform Generation using Dual DAC 2. Stepper Motor interface. 3. LCD Interface 4.ADC interface 5.LED Interface 6.Serial port Interface 1. Dual Dac Interface to generate a. b. c. d. Square waveform Triangular Waveform Ramp waveform Sine waveform The DAC is a device widely used to convert digital pulses to analog signals. There are two methods of creating a DAC. They are binary weighted and R/2R ladder. The Binary Weighted DAC, which contains one resistor or current source for each bit of the DAC connected to a summing point. These precise voltages or currents sum to the correct output value. This is one of the fastest conversion methods but suffers from poor accuracy because of the high precision required for each individual voltage or current. Such highprecision resistors and current-sources are expensive, so this type of converter is usually limited to 8-bit resolution or less. The R-2R ladderDAC, which is a binary weighted DAC that uses a repeating cascaded structure of resistor values R and 2R. This improves the precision due to the relative ease of producing equal valued matched resistors (or current sources). However, wide converters perform slowly due to increasingly large RC-constants for each added R-2R link. The first criterion for judging a DAC is its resolution, which is a function of the number of binary inputs. The common ones are 8, 10, and 12 bits. The number of data bit inputs decides the resolution of the DAC since the number of analog output levels is equal to 2n, where n is the number of data bit inputs. DAC0808: The digital inputs are converter to current Iout, and by connecting a resistor to the Iout pin, we can convert the result to voltage. The total current Iout is a function of the binary numbers at the D0-D7 inputs of the DAC0808 and the reference current Iref , and is as follows: MICROCONTROLLERS LAB-18ECL47 2019-20 Usually reference current is 2mA. Ideally we connect the output pin to a resistor, convert this current to voltage, and monitor the output on the scope. But this can cause inaccuracy; hence an op-amp is used to convert the output current to voltage. The 8051 connection to DAC0808 is as shown in the figure 1 below. Figure 1: 8051 connection to DAC0808 Dual DAC P0.0 to P0.7 8 0 P1.0 5 to 1 P1.7 DAC B10800 B8 Xout CRO DAC B10800 Yout B8 Figure 2: Circuit Diagram for waveform generation Dept. of Electronics &Telecommunication Engineering, B.I.T 2 MICROCONTROLLERS LAB-18ECL47 2019-20 1a.Write an 8051 ALP to generate a square waveform of 5v amplitude on Port0./Port 1 using DAC (Digital-to-Analog converter). Algorithm 1. 2. 3. Let initial, amplitude of the square wave be 0V. Output the values 00h (0ff) and ffh (on) Values through P0/P1. Every time amplitude output the value thro P0/P1 note the waveform on CRO. Program #define BACK: delay 0x0E2EE /*500usec delay routine available in monitor*/ ORG 0 MOV A,#00H /*Clear Accumulator*/ MOV P0,A /*Move 00h to P0 & P1*/ MOV P1,A LCALL DELAY /*Call delay routine*/ MOV A,#0FFH /*Move FFh into Accumulator*/ MOV P0,A /*Move FFh to P0 & P1*/ MOV P1,A LCALL DELAY SJMP BACK DELAY: MOV R1,#0FFH HERE: DJNZ R1,HERE RET END /* Generate random delay*/ Dept. of Electronics &Telecommunication Engineering, B.I.T 3 MICROCONTROLLERS LAB-18ECL47 2019-20 Flow chart: STAR T Configure P1 a O/P port Load P1 with FFH CALL DELAY Load P1 with 00H CALL DELAY Flowchart for Square wave generation using DAC 1b. Write an 8051 ALP to generate Triangular waveform of 5v amplitude on Port0./Port 1 using DAC. Algorithm Output the initial value 00 through P0. Increment it in steps of 1 until a count value of FFh (5V) is reached. Every time repeat step 1. 3. Decrement it in steps of 1 until a zero value is reached and repeat step 1. 1. 2. Dept. of Electronics &Telecommunication Engineering, B.I.T 4 MICROCONTROLLERS LAB-18ECL47 2019-20 Program UP: ORG 0 CLR A /*Clear Accumulator*/ MOV P0,A /*Move 00h to P0 & P1*/ MOV P1,A INC A /*Increment Accumulator*/ CJNE A,#0FFH,UP /*Compare Accumulator with FFh */ DOWN: MOV P0,A /*Move 00h to P0 & P1*/ MOV P1,A DEC A /*Decrement Accumulator*/ CJNE A,#00,DOWN /*Compare Accumulator with 00h */ SJMP UP END Flow Chart Dept. of Electronics &Telecommunication Engineering, B.I.T 5 MICROCONTROLLERS LAB-18ECL47 2019-20 Flowchart for Triangular wave using DACof 5v amplitude on 1c. Write an 8051 ALP to generate Ramp (up generation or down) waveform Port0./Port 1 using DAC. //UP RAMP Algorithm Output the initial value 00 through P0. Increment it in steps of 1 until a count value of FFh (5V) is reached. Every time repeat step 1. 3. Repeat step 1 & 2 continuously. 1. 2. Program ORG 0 CLR A /*Clear Accumulator*/ UP: MOV P0,A /*Move 00h to P0 & P1*/ MOV P1,A INC A /*Increment Accumulator*/ CJNE A,#0FFH,UP /*Compare Accumulator with FFh */ SJMP UP END //DOWN RAMP Algorithm Output the initial value FF through P0. Decrement it in steps of 1 until a count repeat step 1. 3. Repeat step 1 & 2 continuously. 1. 2. value of 00h (0V) is reached. Every time Program ORG 0 UP1: MOV A,#0FFH UP: MOV P0,A MOV P1,A DEC A CJNE A,#00H,UP /*Move FFh into Accumulator*/ UP: /*Move FFh to P0 & P1*/ /*Decrement Accumulator*/ /*Compare Accumulator with 00h */ SJMP UP1 END Dept. of Electronics &Telecommunication Engineering, B.I.T 6 MICROCONTROLLERS LAB-18ECL47 2019-20 Start Initialize Accumulator Send accumulator content to DAC accumulatot Point DPTR to message to Increment/Decrement accumulator If ACC≤ FF/00 Stop Flowchart for Ramp wave generation using DAC Dept. of Electronics &Telecommunication Engineering, B.I.T 7 MICROCONTROLLERS LAB-18ECL47 2019-20 1d. Write an 8051 ALP to generate Sine waveform of 5v amplitude (10V p-p)on Port0./Port 1 using DAC. Algorithm Compute different step values (θ = 20o,15o…) of sine using the equation V= 5V +5Vsinθ. . Output the values thro P0. 1. More the steps smoother will be sine wave. 2. E.g.: θ = 1o V= 5V +5Vsinθ = 5V The value sent to DAC is 25.6X5V= 128. Program ORG 0000H CLR A UP : /* Clear Accumulator*/ MOV DPTR,#SINE /*Move the address of look up table*/ MOV R0,#36 /* Output different values */ LABEL: MOVC A,@A+DPTR /* Move the value from look up table into Accumulator*/ MOV P0,A/* Move accumulator value to P0 &P1*/ MOV P1,A CLR A INC DPTR /* increment DPTR DJNZ R0,LABEL /* Jump to label till R0=00h*/ SJMP UP /* infinite loop*/ ORG 050H SINE:DB128,151,172,192,210,226,239,248,254,255,254,248,239,226,210,192,172,150,128, 106,84,64,46,30,17,8,2,0,2,8,17,30,46,64,84,106 END Dept. of Electronics &Telecommunication Engineering, B.I.T 8 MICROCONTROLLERS LAB-18ECL47 2019-20 Flowchart: STAR T INITIALISE VARIABLE USE DPTR AS A POINTER TO POINT TO THE LOOK UP TABLE DATA ACCESSING USING INDEXED ADREESSING MODE VALUE IS SENT TO THE PORT 1 AND INCREEMENT DPTR DECREEMENT COUNTER = 0?? NO YES END Flowchart for Sine wave generation using DAC Dept. of Electronics &Telecommunication Engineering, B.I.T 9 MICROCONTROLLERS LAB-18ECL47 2019-20 2. Stepper motor Interfacing -8051 A stepper motor is a device that translates electrical pulses into mechanical movement in steps of fixed step angle.So, The stepper motor rotates in steps in response to the applied signals. It is used in disk drives, dot matrix printers, plotters and robotics and process control industries.It is mainly used for position control. Stepper motors have a permanent magnet called rotor (also called the shaft) surrounded by a stator . There are also steppers called variable reluctance stepper motors that do not have a PM rotor. The most common stepper motors have four stator windings that are paired with a center-tap. This type of stepper motor is commonly referred to as a. four-phase or unipolar stepper motor. The center tap allows a change of current direction in each of two coils when a winding is grounded, thereby resulting in a polarity change of the stator. Even a small stepper motor require a current of 400mA for its rotation.But the ports of the microcontroller can not source this much amount of current .If you try to connect the motor directly to the microcontroller ,the motor try to draw large current from the controller and the microcontroller will be damaged .So, we need to use a driver circuit ,to increase the port current of the microcontroller. This can be achieved by using an array of power transistors.But now a days we have driver circuits available readily in the form of ICs.ULN2003 is one such driver IC which is a High-Voltage High-Current Darlington transistor array and can give a current of 500mA.This current is sufficient to drive the stepper motor. This IC is also provided with Dept. of Electronics &Telecommunication Engineering, B.I.T 10 MICROCONTROLLERS LAB-18ECL47 2019-20 diodes that can avoid the damage of motor due to back emf and large eddy currents. So, this ULN2003 is used as a driver to interface the stepper motor to the microcontroller. The significant thing in a stepper motor is the step angle. It is the minimum angle through which the motor rotates in response to each excitation pulse. In a four phase motor if there are 200 steps in one complete rotation then then the step angle is 360/200 = 1.8 0 .So to rotate the stepper motor we have to apply the excitation pulse. For this we send a hexa decimal code through the ports of the microcontroller. The hex code mainly depends on the construction of the steppe motor. So, all the stepper motors do not have the same Hex code for their rotation. We have to refer the operation manual supplied by the manufacturer. 3. Write an 8051 ALP to run the stepper motor in clockwise / Anti clock wise directions. Stepper motor unlike DC motor rotates in steps. step Stepper motor has 4 coils which forms the stator and a central rotor. Rotation depends on excitation of stator coils. coil A coil B coil C coil D 1 0 0 0 1 2 1 0 0 0 3 0 1 0 0 4 0 0 1 0 Anyone of these values forms the initial value. To get 360o revolution 200 steps are required. Step angle= 360o /200 = 1.8o. (difference between 2 teeth). Algorithm 1. 2. 3. 4. Configure P0 as output. Apply the initial excitation of 11 to motor coils through P0. For clockwise motion -Rotate right once the excitation and repeat step 2. For anticlockwise motion -Rotate left once the excitation and repeat step 2. Dept. of Electronics &Telecommunication Engineering, B.I.T 11 MICROCONTROLLERS LAB-18ECL47 2019-20 Program ORG 0H SJMP 30H ORG 30H MOV MOV P0,#00H A,#88H MOV ACALL ACALL RL SJMP P0,A DELAY DELAY A LOOP DELAY: MOV R1,#0FFH DECR: MOV R2,#0FFH DECR1: DJNZ R2,DECR1 LOOP: /*P0 as output*/ /*DATA to activate one winding at a time*/ /*Move it to P0*/ /*Delay*/ /*Rotate the bit pattern*/ DJNZ R1,DECR RET END Dept. of Electronics &Telecommunication Engineering, B.I.T 12 MICROCONTROLLERS LAB-18ECL47 2019-20 Start Initialize Ports Set count for stepping sequence Out put data for a sequence Wait for a delay time Decrement counter No Is count =0 Yes Stop Flowchart for Stepper Motor Interface Dept. of Electronics &Telecommunication Engineering, B.I.T 13 MICROCONTROLLERS LAB-18ECL47 2019-20 4. Interfacing LCD Module -8051 Microcontroller: LCD modules are based on liquid crystaltechnology. The seven segment displays suffer from dissipation of power and also they are not suitable for display of graphs etc.. Hence the Liquid Crystal modules have become very popular now a days. They consume less power and they are highly suitable to display graphics ,numbers and characters etc.. General LCD modules are available in different version like 1x16 (one line -16 characters) or 2x16 (two lines -16 characters)or 2 x 20(two lines 20 characters) etc..The pin diagram of the LCD module is shown below.It is a 14-pin module in which 8 pins are for data ,one pin is Vcc, one pin is Gnd ,one pin is for contrast(Vee) ,one pin is for RS,one pin is for EN and another pin is used for R/W. The description of the pins is given below. The pins 7 -1 4 denotes the data pins which are used to transfer Data .These pins are used to send information to the LCD or to read the contents of the LCD’s internal registers. RS is the register select pin..The LCD has two internal registers.One is command code register and the other is the data register.The command code register is used to send commands like clear display,cursor at home etc..The data register allows the user to send the data to be displayed on the LCD. When RS = 1 ,the data register is selected and when RS = 0 the command code register is selected. RS = 0 is also used to check the busy flagbit to find whether the LCD is ready to receive information. The busy flag is D7 and can be read when R/W = 1 and RS=0. For ex: if R/W= 1 ,RS=0 AND D7 = 1 means the LCD is busy due to internal operations and will not accept any new data. Suppose D7=0 means the LCD is ready to receive the data. R/W pin allows the user to read the information from LCD or write information to the LCD. When R/W = 0 writing is enabled and R/W=1 read is enabled. Dept. of Electronics &Telecommunication Engineering, B.I.T 14 MICROCONTROLLERS LAB-18ECL47 2019-20 EN pin is used by the LCD to latch information on its data pins.When data is given to data pins,a high to low pulse must be applied to this pin so that the LCD can Latch in the data present at the data pins. Vcc ,Vss and Vee pins denote supply , ground and contrast pins respectively. Vee pin is meant for adjusting the contrast of the LCD display and the contrast can be adjusted by varying the voltage at this pin. This is done by connecting one end of a POT to the Vcc (5V), other end to the Ground and connecting the center terminal (wiper) of of the POT to the Vee pin. The contrast pin is applied a negative voltage so that by adjusting this voltage the contrast of the Display can be controlled. Interfacing circuit: The interfacing of LCD module to 8051 microcontroller is shon below. The 8-data pins are connected to Port1 and control pins are connected to Port 2 pins.The busy flag must be checked before the data is send to the LCD.With the help of a 10K potentiometer the contrast of the LCD display is adjusted. Dept. of Electronics &Telecommunication Engineering, B.I.T 15 MICROCONTROLLERS LAB-18ECL47 2019-20 3.Write an 8051 ALP to initialize and display String “MICROCONTROLLER” on LCD*/ Algorithm 1. LCD initialization. The steps for initializing the LCD display. Send 38H to the 8 bit data line for initialization Send 0CH Display ON & No cursor & Blink off. Send 80H for displaying RAM address. Send 01H for clearing the display and return the cursor. 2. Sending data to the LCD. The steps for sending data to the LCD module. Make R/W low. Make RS=0 if data byte is a command and make RS=1 if the data byte is a data to be displayed. Place data byte on the data register. Pulse E from high to low. Repeat above steps for sending another data. Program #define #define #define RS P3.7 /*lcd register select (0)- instruction reg & (1) data reg*/ RW P3.6 /*read (1) or write (0)*/ E P3.5 /*lcd enable*/ ORG 0H SJMP 30 ORG 30H ACALL INIT /*Routine initialize the LCD*/ MOV DPTR,#MSG /*Point to message to be displayed*/ DISP: CLR A /*clear acc*/ MOVC A,@A+DPTR /*Get the character to be displayed*/ JZ HERE /*if it is last char terminate the routine*/ ACALL DAT /*Routine to write into LCD display RAM*/ INC DPTR /*increment the pointer*/ SJMP DISP /* display again*/ HERE: SJMP $ Dept. of Electronics &Telecommunication Engineering, B.I.T 16 MICROCONTROLLERS LAB-18ECL47 INIT: 2019-20 /*LCD initialization Routine*/ MOV A,#38H /* func set command word, selects 2 data lines, 2 disp lines, font 5x7*/ ACALL CMD /*routine to write into command reg*/ MOV A,#01H /*clear LCD display*/ ACALL CMD MOV A,#0CH /*Command, selects the Display ON & No cursor & Blink off*/ ACALL CMD MOV A,#80H /*Sets the display RAM address*/ ACALL CMD /*i.e. selects the first line*/ RET CMD: CLR RS CLR RW MOV P2,A SETB E CLR E ACALL DELAY RET /* Select Instruction Register, by making RS low*/ /*Select LCD Write operation, by clearing RW*/ /*move Command DATA into P2 , i.e. LCD Data BUS*/ /*Enable the LCD by making high */ /*Disable the LCD*/ DAT: SETB RS CLR RW MOV P2,A SETB E CLR E ACALL DELAY RET DELAY: MOV R1,#0FFH DJNZ R1,$ DJNZ R2, DELAY RET ORG 100H MSG: DB END /* Select DATA register of LCD*/ /*Select Read operation*/ /*Move the DATA to P2*/ /*Enable LCD*/ /*Disable LCD*/ /*0usec delay, when clock 11.0592MHz*/ "MICROCONTROLLER",00H Dept. of Electronics &Telecommunication Engineering, B.I.T 17 MICROCONTROLLERS LAB-18ECL47 2019-20 Start Initialize LCD 38H 2line ‘5x7’ 01H clear LCD 80H 1st line Send to display Send to Command register Point DPTR to message Move character to accumulator Check if A = 0 Stop Send character to data register Increment DPTR Flowchart for LCD Interface Dept. of Electronics &Telecommunication Engineering, B.I.T 18 MICROCONTROLLERS LAB-18ECL47 2019-20 Interfacing toggle switch The circuit diagram for interfacing push button switch to 8051 is as shown in the diagram. Push button S1 connected to the INT0 pin is depressed the LED D1 connected to P0.0 goes ON and remains ON until push button switch S1 is depressed and this cycle can be repeated. Push button switch S2 is connected to the INT1 pin is depressed the LED D2 connected to P0.0 goes ON and is turned off after a delay time when a push button switch S2 is still depressed. 6.Write an ALP using 8051 to Interface a simple toggle switch to generate an interrupt Which switches on an LED (i) continuously as long as switch is on and (ii) only once for a small time when the switch is turned on. ORG 0000H LJMP MAIN ORG 0013H SETB P0.0 CLR P0.0 RETI Dept. of Electronics &Telecommunication Engineering, B.I.T 19 MICROCONTROLLERS LAB-18ECL47 2019-20 ORG 0003H SETB P0.7 ACALL DELAY CLR P0.7 ACALL DELAY RETI ORG 30H MAIN: MOV IE, #85H HERE: SJMP HERE DELAY: MOV R7,#0C8H MOV TMOD,#01H; //16-bit timer0 selected L2: MOV TH0,#0DBH; // loading high byte in TH MOV TL0,#0FFH; // loaded low byte in TL SETB TR0 // running the timer L1: JNB TF0, L1; //checking the timer flag register if it is not equal to 1 CLR TR0 ; // if tf0=1 stop the timer CLR TF0 ; DJNZ R7,L2 // clear the timer flag bit for next calculation RET END Start Initialize Ports Enable interrupts If INTO =0 If INTO =1 Turn on LED 2 Turn on LED 1 Delay Stop Stop Dept. of Electronics &Telecommunication Engineering, B.I.T 20 MICROCONTROLLERS LAB-18ECL47 2019-20 Analog-to-digital converter (ADC) interfacing: ADCs (analog-to-digital converters) are among the most widely used devices for data acquisition. A physical quantity, like temperature, pressure, humidity, and velocity, etc., is Converted to electrical (voltage, current) signals using a device called a transducer, or sensor We need an analog-to-digital converter to translate the analog signals to digital numbers, so microcontroller can read them. ADC0808/0809 chip: ADC808 has 8 analog inputs. It allows us to monitor up to 8 different transducers using only single chip. The chip has 8-bit data output just like the ADC804. The 8 analog input channels are multiplexed and selected according to the values given to the three address pins, A, B, and C. that is; if CBA=000, CH0 is selected; CBA=011, CH3 is selected and so on. The pin details of ADC0808 are as shown in the figure 11 below. (Explanation can be done as is with ADC0804). Figure 11: Pin out of ADC0808 Steps to Program ADC0808/0809 1. Select an analog channel by providing bits to A, B, and C addresses. 2. Activate the ALE pin. It needs an L-to-H pulse to latch in the address. 3. Activate SC (start conversion) by an H-to-L pulse to initiate conversion. 4. Monitor EOC (end of conversion) to see whether conversion is finished. 5. Activate OE (output enable) to read data out of the ADC chip. An H-to-L pulse to the OE pin will bring digital data out of the chip. ADC will convert this analog voltage into equivalent digital value. This digital signal is applied to the Port 1 of the microcontroller .The data is processed and displayed on the LCD module. Dept. of Electronics &Telecommunication Engineering, B.I.T 21 MICROCONTROLLERS LAB-18ECL47 2019-20 Note : This method is useful to measure small currents only. Because with large currents the small resistor will dissipate large heat. So, to measure large voltages high wattage wire wound resistors or Torroid must be used. 4. Write an 8051 ALP to interface ADC-0804 and convert an analog input connected to it and display the digital value on LCD. #define #define #define lcdstr lcddat hexasc sbit eoc = P2^4 sbit start_conv = P1^5 sbit read_adc = P1^6 adc_data 0x0e39e 0x0e2b4 0x0e3b0 ; End of conversion ;start of converstion ; Read ADC data 0x50 ORG 0000H SJMP 30h ORG 30h L1: MOV DPTR,#msg1 LCALL lcdstr CALL CONVERT CALL DISPLAY SJMP L1 /*Display the above string on LCD*/ CONVERT: MOV P0,#0ffh MOV P1,#00h MOV R3,#07H MOV A,R3 MOV P1,A /*P0 as input port*/ /*P1 as output port*/ /* SELECT CHANNEL 7 */ /*check for the conversion*/ SETB /*start conversion */ start_conv CLR start_conv NOP JB eoc,$ SETB nop MOV MOV CLR RET display: MOV SWAP read_adc /*clear conversion */ /*read ADC*/ A,P0 adc_data,A read_adc A A,#adc_data /*Get the scanned value into A reg*/ /*Swap to get the highernibble into lower nibble*/ Dept. of Electronics &Telecommunication Engineering, B.I.T 22 MICROCONTROLLERS LAB-18ECL47 2019-20 LCALL hexasc /*convert higher nibble to ASCII*/ LCALL lcddat /*Display it on LCD*/ MOV A,adc_data LCALL hexasc /*Convert to ASCII*/ LCALL lcddat RET msg1: db 'Digital value=',00h END Start Initialize ports Send SOC to ADC If EOC = =1 Send read signals to ADC Read the data from ADC Convert Hex to ASCII Display on LCD Flowchart for interfacing ADC Stop Dept. of Electronics &Telecommunication Engineering, B.I.T 23 MICROCONTROLLERS LAB-18ECL47 2019-20 Interfacing the 8051 Microcontroller serial port to PC: As the RS-232 standard is developed earlier to TTL devices ,a USART such as 8251 is not directly compatible with these signal levels .Because of this ,voltage transistors called line drivers and line receivers are used to interface TTL logic with RS-232 signals . The line driver MC 1488 is used to convert RS-232 to TTL. The microcontroller is connected to the PC using the DB9 connector. The TxD and Rx D pins are connected to the TI in and RI in pins of the MAX 232 IC and the TI out and RI in pins of the MAX IC are connected to the RxD and TxD pins of the DB9 connector as shown in the interface diagram. Basics of Serial communication Data transfer between two electronic devices (Ex Between a computer and microcontroller or a peripheral device) is generally done in two ways (i).Serial data Transfer and (ii).Parallel data Transfer Serial communication uses only one or two data lines to transfer data and is generally used for long distance communication. In serial communication the data is sent as one bit at a time in a timed sequence on a single wire. Serial Communication takes place in two methods, Asynchronous data Transfer and Synchronous data Transfer. Dept. of Electronics &Telecommunication Engineering, B.I.T 24 MICROCONTROLLERS LAB-18ECL47 2019-20 Serial Data Transfer Communication through RS232 A personal computer has a serial port known as communication port or COM Port used to connect a modem for example or any other device, there could be more then one COM Port in a PC. Serial ports are controlled by a special chip called UART (Universal Asynchronous Receiver Transmitter). RS 232 standard describes a communication method where information is sent bit by bit on a physical channel. The RS stands for Recommended Standard.The information must be broken up in data words. The length of a data word is variable. It is one of the popularly known interface standard for serial communication between DTE & DCE. This RS-232-C is the commonly used standard when data are transmitted as voltage .This standard was first developed by Electronic industries association(EIA). For the RS-232C, a 25 pin D type connector is used . DB-25P male and DB-25S female. RS232 standard was first introduced in 1960’s by Telecommunications Industry Association(TIA). 5. Write a C program to (i) transmit and (ii) to receive a set of characters serially by interfacing 8051 to a terminal. //Sending Data to PC Hyper terminal #include<reg51.h> void main() { char A[]="Serial Communication"; unsigned int i=0; P3=0x03; //Initializing 8051 UART ports as input port TMOD=0x20; // Timer1 Mode2 8 bit auto reload TH1=0xFD; // 9600 bps SCON=0x50; // 8 Data bit, 1 start bit, bit 1 stop TR1=1; // Timer1 ON while(1) { while(A[i]!='\0'){ SBUF=A[i]; // Placing character one by one in the serial buffer register while(TI==0); // It automatically poles up to 1 when character is transmitted TI=0; i++; } i=0; Dept. of Electronics &Telecommunication Engineering, B.I.T 25 MICROCONTROLLERS LAB-18ECL47 2019-20 } } //Receiving the data from PC hyper terminal #include<reg51.h> sbit rs=P3^5; //Register select (RS) sbit en=P3^6; //Enable (EN) pin sbit rw=P3^7; //Read write (RW) pin //Function for sending values to the command register of LCD void lcdcmd(unsigned char value) { P1=value; rs = 0; rw = 0; en = 1; delay(50); en=0; delay(50); } //Function for sending values to the data register of LCD void display(unsigned char value) { P1=value; rs = 1; rw = 0; en = 1; delay(500); en=0; delay(50); } //function to initialize the registers and pins of LCD void lcdint(void) { P1=0x00; //Port 1 is used as output port P3=0x03; //Port 3 higher bits from 2 to 7 are used as output and lower two //bits are used to activate void delay(unsigned int time) //Time delay function { unsigned int i,j; for(i=0;i< time;i++) for(j=0;j<5;j++); } lcdcmd(0x38); lcdcmd(0x01); Dept. of Electronics &Telecommunication Engineering, B.I.T 26 MICROCONTROLLERS LAB-18ECL47 2019-20 lcdcmd(0x80); } void main() { char dat; lcdint(); TMOD=0x20; // Timer1 Mode2 8 bit auto reload TH1=0xFD; // 9600 bps SCON=0x50; // 8 Data bit, 1 start bit, 1 stop bit TR1=1; // Timer1 ON while(1) { while(SBUF!=0x0D)//Checking if enter key is pressed { while(RI==0); dat=SBUF; RI=0; display(dat); delay(50); } } } In PC go to Start->Programs->Accessories->Communications->HyperTerminal open new connection, name it whatever you want then click OK. Now a window appears select COM1 from connect using drop down menu click OK Now set COM1 properties bits per second as you specified in the program for example 9600, Data bits=8, Parity=none, Stop bits=1, Flow control=none then click OK 1 START Start timer 1 Flow chart main Is for TI Flag program high r main program Load T SBUF to A register Clear R1 Flag START Program TMOD Register for timer- 1 in mode-2 operation Load initial court in high order count register of timer-1 Load port-0 content in A register Delay Clear TI Flag Load content of register in port -2 Program SCON Register for mode-1 serial communication Return 1 Flow chart for main program Dept. of Electronics &Telecommunication Engineering, B.I.T Flow chart for interrupt service subroutine 27 MICROCONTROLLERS LAB-18ECL47 2019-20 8051-Viva Questions 1. What is meant by micro controller? Microcontroller is a small chip that has in-build Micro processor, memory, ports, timers and converter. Micro controllers are designed for specific use. For instance, micro controller in TV remote is mainly designed for controlling TV. 2. List out the features of 8051 micro controller? 40 Pin IC. 128 bytes of RAM. 4K ROM. 2 Timers (Timer 0 and Timer 1). 32 Input/ Output pins. 1 serial port. 6 Interrupts (Including Reset). 3. What are the types of interrupts in 8051? External interrupt 0 (IE0) has highest priority among interrupts. Timer interrupt 0 (TF0) External interrupt 1 (IE1) Timer interrupt 1 (TF1) has lowest priority among other interrupts. Serial port Interrupt Reset 4. What is an interrupt? Hardware or software can communicate to micro controller through interrupts. Interrupts are external signal that controls the micro processor. Interrupt signals are generated by sources like software programs or hardware controls. 5. What is an Interrupt service routine? When micro controller is under sudden interrupt, it will call ISR (Interrupt service routine) that will store the address of current memory address and takes the control to new interrupt memory address. After the interrupt, the control will transfer back to its previous address. 6.what are the different addressing modes in 8051? Direct addressing Register addressing Register indirect addressing Implicit addressing Immediate addressing Index addressing 7. Difference between JUMP and CALL instruction? JUMP: Takes the control to newly specified address. CALL: Takes the control to newly specified address but control will resume back to its current address once its call is finished. Dept. of Electronics &Telecommunication Engineering, B.I.T 28 MICROCONTROLLERS LAB-18ECL47 2019-20 8. Draw the pin diagram of 8051 micro controller? 1) What is the difference between microprocessor and microcontroller? The microprocessor are general purpose computer with CPU, memory addressing circuit, interrupt handling circuits and its models vary in data size from 4 to 32 bits. The microcontrollers are special purpose digital controller with above features of microprocessor plus it has timer parallel and serial I/O and internal RAM and ROM. 2) Describe the internal Ram of 8051? The internal RAM of 8051 is generally 128 bytes. It is classified as 4 register banks of 8 registers each. 16 byte addressable at bit level & 8 bytes of general purpose data memory. 3) How many timers are available in 8051? There are two 16 bit timer/counter option. They are T0 and T1. 4) What are the different control registers in 8051? The following control register are present in 8051: TCON, TMOD, SCON, PCON, IP, & IE. 5) How many interrupt sources are made available in 8051? There are two external & three internal interrupt sources. Dept. of Electronics &Telecommunication Engineering, B.I.T 29 MICROCONTROLLERS LAB-18ECL47 2019-20 6) Which register of PSW are used to select register bank? The register R50 and R51 are used to select register bank. 7) Which flag is used in arithmetic instruction? The overflow flag (OV) is used in arithmetic instruction. 8) Which flag is used for BCD arithmetic? The auxiliary carry flag is used BCD arithmetic. 9) The carry flag is used for which instruction? The carry flag is used for arithmetic, jump, rotate and Boolean instruction. 10) What is the position of the port during power on or power reset? The port P0 to P3 will be high during power ON condition. 11) List the different register priority wise? The register are listed as per the following ranking based on priority to response an interfacing. 1) IE0 2) TF0 3) IE1 4) TF1 5) SERIAL (T1 OR R1) 12) Name the features of 8051? The following are the 3 main features of 8051. 128 byte RAM, $k ON-CHIP ROM and 48 bit I/O ports. 13) What is the major difference between 8051 and 8052 microcontroller? The 8052 has everything that the 8051 has. It has an extra timer and the on-chip ROM is 8k instead. The RAM in 8052 is 256 byte instead of 128 bytes. 14) List different 8-bit register of 8051? A, B, R0 TO R7, SP. 15) List 16 bit register of 8051? DPTR, PC. 16) Why is assembly language caused as low level language? The assembly language is called as low level language as it directly deals with the internal structure of CPU. 17) What is a compiler? Dept. of Electronics &Telecommunication Engineering, B.I.T 30 MICROCONTROLLERS LAB-18ECL47 2019-20 A compiler is a program that translates high level language into machine code. 18) Which directive is used for ASCII string? The ‘DB’ directive is used for ASCII string. 19) What is the advantage in using the ‘EQU’ directive to define a constant value? If the value are to be changed later, it can be easily done once in one place instead of every occurrence. 20) What is the flag register in 8051 called as? The flag register 8051 is called as program status word i.e., PSW. 21) What is the size of flag register in 8051? The size of flag register in 8051 is 8 bit. 22) Which bits of the PSW registers are users definable? D1 and D5 i.e., PSW.1 and PSW.5 are user definable. 23) When is the carry flag (CF) set? The carry flag is set whenever there is carry out from the d7 bit. This flag is affected after. 24) Is it possible to have immediate data as destination? No, it is not possible to have immediate data as destination. 25) What should be the number in the register RP? It must be always the ram address. 26) Which register are used for indirect addressing? Only register R0 and R1 may be used for indirect addressing. 27) All external data moves must involve which register? All external data moves must involve the accumulator. 28) How many bytes can register RP address? Rp can address 256 bytes. 29) How many bytes can DPTR address? DPTR can address 64k bytes. Dept. of Electronics &Telecommunication Engineering, B.I.T 31 MICROCONTROLLERS LAB-18ECL47 2019-20 30) What happens to the PC before it is added to A to form the final address of the code byte? The PC is incremented by 1 to point to the next instruction. 31) What precaution one has to take to handle the subtract operation? The carry flag is set to 0 if is not to be included as part of the subtraction operation. 32) Which register bank is used if we alter RS0 and RS1 of the PSW by the following 2 instruction? SETB psw.3 SETB psw.4 The register bank 3 is used. 33) Name 16 bit register in 8051? PC and DPTR. 34) What is the result of the following code and where it is kept? MOV A, #15H MOV R2, #13H ADD A, R2 The result is 28H and is stored in accumulator. 35) In the ADD instruction, when CY is raised? The CY is raised or set whenever there is a carry out from the D7 bit. 36) In the ADD instruction, when is AC flag set? If there is a carry from D3 to D4 during an ADD or SUB operation, this bit is set, otherwise it is cleared. 37) Which flag is used to perform BCD arithmetic? The AC flag is used to perform the BCD arithmetic. 38) In the 8051, which register bank conflict with the stack? Bank1. 39) In ‘JZ = NEXT’ which register content is checked to see if it zero? The accumulator. Dept. of Electronics &Telecommunication Engineering, B.I.T 32 MICROCONTROLLERS LAB-18ECL47 2019-20 40) What do the mnemonic LCALL and ACALL stand for? LCALL is termed as Long Call. ACALL is termed as Absolute Call. 41) Show the instruction to enable EX0 and Timer0 interrupts? MOV IE, # 1000011b 42) Which pin of the 8051 is assigned to the external hardware interrupt INT? Port pin P3.3 which is pin 13 on the 40 pin DIP package is assigned as external hardware interrupt. 43) What address in the interrupt vector table is assigned t the INT1 and Timer1 interrupts? 0013 for INT1 and 001b for timer1. 44) What address in the interrupt vector table is assigned to timer 0? 0000Bh is assigned as address to timer 0. 45) Which bit of IE belong to the Timer interrupt? Show how both are enabled? Bits D1 and D3 belong to the timer interrupts. They are enabled by the instruction: MOV IE # 10001010b 46) What address in the interrupt vector table is assigned to INT0 and INT1? What at=re their pin number? The address are 0003h and 0013h. pins are 12(p3.2) and 13(p3.3). 47) which bit of ie register belong to the external hardware interrupt? Show how both are enabled. Bits d0 and d2 belong to the external hardware interrupt and are enabled by the instruction MOV IE,#10000101B. 48) Assume that IE bit for external hardware interrupt EX1 is enabled and is active low explain how these interrupt works when it is activated? Upon application of a low pulse(min 4 machine cycle wide), to pin p3.3, the 8051. is interrupted in whatever it is doing and jump to ROM location 00BH to execute the ISR. 49) Explain the role that each of the two bits TCON.0 and TCON.2 play in the execution of external interrupts 0? Dept. of Electronics &Telecommunication Engineering, B.I.T 33 MICROCONTROLLERS LAB-18ECL47 2019-20 TCON.0 is set to high to make INT0 an edge triggered. If INT 0 is edge triggered(i.e TCON.0 is set ) whenever high to low pulse is applied to the INT0 is latched and kept by the TCON.0 bit by making TCON.2 high. While the ISR for INT0 is being serviced,TCON.2 stays high. No matter how many times a high to low pulse is applied to pin INT0. Upon the execution of the last instruction of the ISR, which is RETI the TCON.2 bit is cleared indicating that the INT0 pin can respond to another interrupt. 50) What address in the interrupt vector table is assign to the serial interrupt? 23H is the address assigned for serial interrupt. 51) How are interrupts better than Polling? In polling the processor needs to continuously monitor the i/o device whether it is ready or not. This continuous monitoring of the device may lead to undue waste of time. Whereas the device may interrupt the processor as and when required ,in the mean time the processor my carry on with some other piece of work. 52) Explain the working of the enable pin of the LCD The data written on to the data pins is taken in only when a high to low pulse is given on this pin or to understand the data written comes into action only when high to low pulse is given. 53) What is the role of SBUF register during serial communication? Any data that needs to be transmitted or received is passed through the SBUF register. the data being transmitted is moved to SBUF and the data received through serial communication is collected in SBUF. 54) A stepper with a step angle of 5 degrees has how many steps per revolution? a) 144 b) 72 c) 48 d) 24 Answer is 72. 55) What is the total number of steps needed to rotate one complete rotation? The total number of steps are 180 steps * 20 = 360 56) What is the purpose of LM336 Zener diode around pot setting Vref/2? The purpose of LM336 Zener diode around pot setting Vref/2 is to overcome any fluctuation in power supply. 57) LM34 provides __10mv_ __for each degree of _ Fahernheit,_(Fahernheit, Celsius) temperature. Dept. of Electronics &Telecommunication Engineering, B.I.T 34 MICROCONTROLLERS LAB-18ECL47 2019-20 58) LM35 provides _10mv__for each degree of _ Celsius __(Fahernheit, Celsius) temperature. 59) A stepper with a step angle of 7.5 degrees has how many steps per revolution? a) 144 b) 72 c) 48 d) 24 Answer is 48 60) A stepper with a step angle of 15 degrees has how many steps per revolution? a) 144 b) 72 c) 48 d) 24 Answer is 24 61) A stepper with a step angle of 2.5 degrees has how many steps per revolution? a) 144 b) 72 c) 48 d) 24 Answer is 144 62) What will be the order of 4 step sequence of stepper motor if we start with 0110? a) 1,2,4 b) 4,1,2 c) 1,4,2 d) 2,1,4 Answer is 4,1,2. 63) What will be the order of 4 step sequence of stepper motor if we start with 0011? a) 1,2,4 b) 4,1,2 c) 1,4,2 d) 2,1,4 Answer is 1,2,4. 64) What will be the order of 4 step sequence of stepper motor if we start with 1100? a) 1,2,4 b) 4,1,2 c) 1,4,2 d) 2,1,4 Answer is 2,1,4. Dept. of Electronics &Telecommunication Engineering, B.I.T 35 MICROCONTROLLERS LAB-18ECL47 2019-20 65) What is the direction (out, in) for each of following pins of ADC 808/809. A) A,B,C B) SC C)EOC. The direction for A,B,C is in, for SC is also in and for EOC is out . 66) In ADC804, the INTR signal is a) Input Signal b) Output signal It is output signal 67) Which pin of ADC804 indicates end of conversion? a) INTR b) CS c) RD d) Vcc. The pin of ADC804 which indicates end of conversion is INTR. 68) Which pin of ADC804 indicates start of conversion? a) INTR b) CS c) RD d) Vcc. The pin of ADC804 which indicates start of conversion is RD 69) Both the ADC 804 and ADC 808 are ___8____ bit converter 70) For an ADC804 chips, find the step size of Vref values a) Vref/2 = 1.28 Volt b) Vref/2 = 1 Volt The step size for 1.28 Volt is Vref = 2.56/ 256 = 10 The step size for 1Volt is Vref = 2/ 256 = 7.81 71) Is self clocking present in ADC 808/809? No 72) On what factor speed of conversion depends? Speed of conversion depends on frequency of clock connected to the clock pin. 73) What is the relation between digital output voltage and analog input voltage and step size? Dout = Vin/ step-size. 74) DAC 808 is an __8___ bit D to A Converter 75) To get the full scale output what should be the inputs for DAC? All inputs are high. 76) Assume that R=5k and Iref= 2mA, calculate Vout for 10011001? Dept. of Electronics &Telecommunication Engineering, B.I.T 36 MICROCONTROLLERS LAB-18ECL47 2019-20 Iout = 2 mA * 153/ 255 = 1.195 mA. Vout = 1.195 * 5 = 5.975 77) Assume that R=5k and Iref= 2mA, calculate Vout for 11001000? Iout = 2 mA * 200/ 256 = 1.562 mA Vout = 1.562 * 5 = 7.8125. 78) What will be the value of column of keypad matrix if no key is pressed? The value of column of keypad matrix if no key is pressed is all 1’s 79) To detect the key press which of following is grounded? A) All rows B) One row at a time C) both (A) and (B) Answer is A 80) To identify the key press which of following is grounded? A) All rows B) One row at a time C) both (A) and (B) Answer is B 1. 2. 3. 4. 5. 6. 7. 8. Intel 8051 follows which architecture: Harvard or Newman? What is the difference between Harvard and Newman architecture? 8051 was developed using which technology nmos or pmos? Why 8051 is called 8 bit microcontroller? What is the width of data bus? What is the width of address bus? How many memory locations address bus can access (also tell in kb)? The memory locations that the address bus can access will be of RAM or ROM or both? 9. How much on-chip RAM is available? 10. On-chip RAM is also called _____ memory? 11. How much on-chip ROM is available? 12. On-chip ROM is also called _____ memory? 13. How many byte is of the bidirectional input/output port? 14. What is UART? 15. The original 8051 core runs at how many clock cycles per machine cycle? 16. With 12 MHz clock frequency how many instructions(of 1 machine cycle and 2 machine cycle) it can execute per second? 17. What are the four distinct types of memory in 8051? 18. Internal RAM is located from address 0x00 to ___? 19. Tell the addresses which can be accessed directly? 20. Tell the addresses which are bit addressable? 21. Tell the addresses which can be accesssed only by indirect addressing mode? 22. Tell the addresses where the special function registers are present and also which addressing mode is used to access them? Dept. of Electronics &Telecommunication Engineering, B.I.T 37 MICROCONTROLLERS LAB-18ECL47 2019-20 23. Are SFR bit addressable or byte addressable? 24. What type of memory is Program Memory? Read only/Read-write/write only? 25. *Does the program memory can be only used for storing code only, if not then how that data is accessed? 26. What is the address of external data memory from which it starts? 27. How the external data memory is accessed? 28. The internal RAM memory of the 8051 is: 128 bytes 29. This program code will be executed continuously: STAT: MOV A, #01H JNZ STAT 30. The 8051 has ___2_____ 16-bit counter/timers 31. The address space of the 8051 is divided into four distinct areas: internal data, external data, internal code, and external code. True 32. Data transfer from I/O to external data memory can only be done with the MOVX command. True 33. The 8051 can handle ____5____ interrupt sources. 34. The special function registers are maintained in the next 128 locations after the general-purpose data storage and stack. True 35. This statement will set the address of the bit to 1 (8051 Micro-controller): SETB 01H 36. MOV A, @ R1 will: copy the contents of memory whose address is in R1 to the accumulator 37. A label is used to name a single line of code. True 38. The following program will receive data from port 1, determine whether bit 2 is high, and then send the number FFH to port 3: True READ: MOV A,P1 ANL A,#2H CJNE A,#02H,READ MOV P3,#FFH 39. Device pins XTAL1 and XTAL2 for the 8051 are used for connections to an external oscillator or crystal. True 40. When the 8051 is reset and the line is HIGH, the program counter points to the first program instruction in the: internal code memory 41. An alternate function of port pin P3.4 in the 8051 is: Timer 0 Both registers TL0 and TL1 are needed to start Timer 0. False 42. The I/O ports that are used as address and data for external memory are: ports 0 and 2 43. The last 96 locations in the internal data memory are reserved for general-purpose data storage and stack. False 44. Microcontrollers often have: CPUs, RAM, ROM 45. The 8051 has 4 parallel I/O ports. 46. The total external data memory that can be interfaced to the 8051 is: 64K 47. Which instruction will load the value 35H into the high byte of timer 0? MOV TH0, #35H 48. Bit-addressable memory locations are: 20H through 2FH 49. The 8-bit address bus allows access to an address range of: 00 to FFH 50. The contents of the accumulator after this operation MOV A,#0BH Dept. of Electronics &Telecommunication Engineering, B.I.T 38 MICROCONTROLLERS LAB-18ECL47 2019-20 ANL A,#2CH will be 00001000 51. The start-conversion on the ADC0804 is done by using the: 52. Which of the following instructions will move the contents of register 3 to the accumulator? MOV A, R3 53. Which of the following statements will add the accumulator and register 3? ADD A, R3 54. Data transfer from I/O to external data memory can only be done with the MOV command. False 55. Which of the following commands will move the number 27H into the accumulator? MOV A, #27H 56. This program code will read data from port 0 and write it to port 2, and it will stop looping when bit 3 of port 2 is set: True STAT: MOV A, PO MOV P2,A JNB P2.3, STAT 57. Which of the following commands will move the value at port 3 to register 2? MOV R2, P3 58. The number of data registers is: 32 59. When the 8051 is reset and the EA line is LOW, the program counter points to the first program instruction in the: external code memory 60. The designs of a centigrade thermometer and a PWM speed-control circuit can be implemented by the 8051. True 61. What is the difference between the 8031 and the 8051? The 8031 is ROM-less. 62. The I/O port that does not have a dual-purpose role is: port 1 63. To interface external EPROM memory for applications, it is necessary to demultiplex the address/data lines of the 8051. 64. The following command will copy the accumulator to the location whose address is 23H: MOV 23H,A True 65. The special function registers can be referred to by their hex addresses or by their register names. True 66. The contents of the accumulator after this operation : MOV A,#2BH ORL A,00H will be: 2B H 67. The following program will cause the 8051 to be stuck in a loop: False LOOP: MOV A, #00H JNZ LOP 68. Which of the following commands will copy the contents of RAM whose address is in register 0 to port 1? MOV P1, @ R0 69. The statement CALL READ passes control to the line labeled READ. True 70. Which of the following commands will copy the contents of location 4H to the accumulator? MOV A, 04H 71. The microcontroller is useful in systems that have nonvariable programs for dedicated applications. True 72. The total amount of external code memory that can be interfaced to the 8051 is: 64K Dept. of Electronics &Telecommunication Engineering, B.I.T 39 MICROCONTROLLERS LAB-18ECL47 2019-20 73. The ADC0804 has 8-bit resolution 74. A HIGH on which pin resets the 8051 microcontroller? RST 75. An alternate function of port pin P3.1 in the 8051 is: serial port output 76. Which of the following instructions will move the contents of the accumulator to register 6? MOV R6, A 77. The following command will rotate the 8 bits of the accumulator one position to the left: RL A: True 78. An alternate function of port pin P3.0 (RXD) in the 8051 is: serial port input ---------------------xxx--------------------- REFERENCE: Muhammed Ali Mazidi, Janice Gillies Pie Mazidi, “The 8051 Microcontroller and Embedded Systems”— Pearson EducationAsia. Dept. of Electronics &Telecommunication Engineering, B.I.T 40