0% found this document useful (0 votes)
8 views45 pages

FMA All Unit Imp (Question & Answers - Merged

The document provides a detailed overview of C data types used in the 8051 microcontroller, including Unsigned Char, Signed Char, Unsigned Int, Signed Int, Sbit, Bit, and SFR, along with their respective ranges and uses. It also includes various C programming examples related to configuring ports, timers, and generating waveforms, as well as explanations of the TMOD and TCON registers. Additionally, it discusses timer modes and the interrupt structure of the 8051 microcontroller.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views45 pages

FMA All Unit Imp (Question & Answers - Merged

The document provides a detailed overview of C data types used in the 8051 microcontroller, including Unsigned Char, Signed Char, Unsigned Int, Signed Int, Sbit, Bit, and SFR, along with their respective ranges and uses. It also includes various C programming examples related to configuring ports, timers, and generating waveforms, as well as explanations of the TMOD and TCON registers. Additionally, it discusses timer modes and the interrupt structure of the 8051 microcontroller.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Q1. Write a short note on C Data types for 8051 microcontroller.

Ans 1.
1. Unsigned Char: The unsigned char is an 8-bit data type. Thus, it must be
use to store the value in the range of 0-255 (00H-FFH). Since the 8051 is
an 8-bit microcontroller, it is the one of the most widely used data type.
2. Signed Char: The signed char is an 8-bit data type that uses the most
significant bit (D7 of D7 – DO) to represent the – or + value. As a result,
only 7 bits are there for the magnitude of the signed number, giving us
values from -128 to +127.
3. Unsigned int: The unsigned int is a 16-bit data type that takes a value in
the range of 0 to 65535 (0000 – FFFFH). In the 8051, unsigned int is used
to define 16-bit variables such as memory addresses. It is also used to set
counter values of more than 256. Since the 8051 is an 8-bit
microcontroller and the int data type takes two bytes of RAM, the int data
type must not be used, unless it is required. Since registers and memory
accesses are in 8-bit chunks, the misuse of int variables will result in a
larger hex file.The compiler will not generate an error for this misuse, but
the overhead in hex file size is noticeable.
4. Signed int: Signed int is a 16-bit data type that uses the most significant
bit (D15 of D15 – DO) to represent the – or + value. As a result, only 15 bits
are remaining for the magnitude of the number, or values from -32,768 to
+32,767.
5. Sbit (single bit): The sbit keyword is a widely used 8051 C data type
designed specifically to access single-bit addressable registers. It allows
access to the single bits of the SFR registers. Among the SFRs that are
widely used and are also bit-addressable are ports PO -P3. We can use sbit
to access the individual bits of the ports
6. Bit: The bit data type allows access to single bits of bit-addressable
memory spaces 20 – 2FH. While the sbit data type is used for bit-
addressable SFRs, the bit data type is used for the bit-addressable section
of RAM space 20 -2FH.
7. sfr: To access the byte-size SFR registers, we use the sfr data type.

• Unsigned char (8-bit) : 0-255 (Decimal), 00-FF H(Hexadecimal)


• Signed char (8-bit) : -128 to +127 Decimal
• Unsigned int (16-bit) : 0 to 65535 (Decimal) , 0000 to FFFF
H(Hexadecimal)
• Signed int (16-bit) : -32768 to +32767 (Decimal)
• Sbit (1-bit) : Single bit from Special Function Registers
• Bit (1-bit) : Single bit from bit addressable RAM(20 – 2F H) only.
• SFR (8-bit) : 8-bit from RAM address 80- FFH ONLY.

C compiler uses signed data type as default.


Q2. Write a program in C to configure Port 1 as input port and Port pin P2.0
as input pin.
Solution 2.
#include<reg51.h>
sbit P2_0= P2^0;
void main(void)
{
P1= 0XFF;
P2_0= 1;
}
Q3. Draw the TCON and TMOD register and explain all the bits of TMOD
Register.
Ans3. TMOD is Timer mode control register, it is 8 bit register.

• The timers have hardware or software way of starting and stopping.


• The hardware way of starting and stopping the timer by an external source
is achieved by making GATE=1 in the TMOD register.
• And if we change to GATE=0 then we do no need an external hardware to
start and stop the timers, it gets start and stop by software way.
• The second bit is C/T bit and is used to decide whether a timer is used as a
time delay generator or an event counter.
• If this bit is 0 then it is used as a timer and if it is 1 then it is used as a
counter.
• In upper or lower 4 bits, the last bits third and fourth are known as M1 and
M0 respectively.
• These are used to select the timer mode.
• Timer Modes
M1 M0 Mode Operation
0 0 0 13-bit timer mode
Counts from 0000 to
1FFF in Hexadecimal
or from 1 to 8192 in
decimal
0 1 1 16-bit timer mode
Counts from 0000 to
FFFF in Hexadecimal
or from 1 to 65536 in
decimal
1 0 2 8-bit auto reload mode
Counts from 0000 to
FF in Hexadecimal or
from 1 to 256 in
decimal
1 1 3 Spilt timer mode

TCON is Timer Control Register. It is an 8 bit register.

Q4. Explain the function of bit TF0 in TCON register and write a program in
C language to start timer 0
Ans4. TF0 is timer 0 overflow flag. It is set by hardware when timer/counter 0
overflows. It is 5th bit of TCON register, TCON.5
#include <reg51.h>
void main (void)
{
TR0 = 1;
}
Q5. Write a program in C language to copy the contents of Port 2 to Port 1.
Solution 5.
#include<reg51.h>
void main(void)
{
unsigned char x;
P2 = 0XFF;
x = P2;
P1 = x;
}

Q6. Write a program in C language to generate a square wave form on pin 5


of port 1. The frequency of the waveform is 125 Hz. Use timer 1 in mode 1.
Assume crystal frequency = 11.0592 MHz.
Ans6.
#include<reg51.h>
sbit wave=P1^5;
void main(void)
{
while(1)
{
wave=0;
TMOD= 0X10;(for timer 1 mode1)
TL1 = 0X9A;
TH1=0XF1;
TR1 = 1;
while(TF1==0);
TR1=0;
TF1=0;
wave = ~ wave;
}
}

Calculation :
Frequency = 125 Hz
Time = (1/125) = 8*(10^-3)sec = 8 msec
Time period of full square wave = 8msec
Timer period of half wave = 4msec (8/2)
Clock pulse needed to be counted by timer to give the delay of 4msec is =
(4msec/1.085usec) = 3686.63=3686

Count to be loaded in timer 1 to get the delay is = 65536-3686=61850


Hexadecimal of 61850 is F19A
TH1=0XF1 TL1=0X9A

Q7. Write a program to generate square wave of 50Hz frequency with 50%
duty cycle on pin 2.3. Assume XTAL=11.0592 MHz & use timer 0 in
mode l.
Ans 7.
#include<reg51.h>
sbit wave=P2^3;
void main(void)
{
while(1)
{
Wave=0;
TMOD= 0X ;(for timer 0 mode1 find the value)
TL0 = ; (find the value doing calculation)
TH0= ; (find the value doing calculation
TR0 = 1;
While(TF0==0);
TR0=0;
TF0=0;
wave = ~ wave;
}
}

Calculation :
Frequency = 50 Hz
Time = (1/50) = sec ( find the value)
Time period of full square wave = msec ( find the value)
Timer period of half wave = msec ( find the value)
Clock pulse needed to be counted by timer to give the delay of msec ( find the
value) is = (find the value/1.085usec) = find the value

Count to be loaded in timer 0 to get the delay is = find the value


Hexadecimal value is find the value

Q8. Draw & explain TCON register.


Ans 8.

Explanation of bits :
1) TF1 (TCON.7 ) – Timer 1 overflow flag. It is set by hardware to 1 when
timer/counter counts form all 1s to 0s(overflow). For e.g : when timer 1 in
mode 1 rolls from FFFFH to 0000H, TF1 becomes equals to 1.
It serves as an interrupt for timer 1. Memory location 001BH in the
interrupt vector table belongs to Timer 1 interrupt.

2) TR1(TCON.6) – Timer 1 run control bit. It is used to start or stop the


timer/counter. TR1=1 start the timer/counter1, TR1=0 stop the
timer/counter 1.
3) TF0 (TCON.5) – Timer 0 overflow flag. It is set by hardware to 1 when
timer/counter 0 counts form all 1s to 0s(overflow). For e.g : when timer 0
in mode 2 rolls from FFH to 00H, TF0 becomes equals to 1.
It serves as an interrupt for timer 0. Memory location 000BH in the
interrupt vector table belongs to Timer 0 interrupt.
4) TR0(TCON.4) – Timer 0 run control bit. It is used to start or stop the
timer/counter 0. TR0 =1 start the timer/counter 0, TR0 = 0 stop the
timer/counter 0.
5) IE1(TCON.3) - External interrupt 1 Edge flag. This bit is used by 8051 to
keep track of the edge-triggered interrupt only. It is set by CPU when
external interrupt edge (H to L transition) is detected. In case of external
hardware interrupt 1, when high to low edge transition occurs, IE1
becomes equal to 1, indicating external hardware interrupt 1 is in use.
6) IT1 (TCON.2) - External interrupt 1 type control bit. It is used to set the
low-level or edge-triggered modes of the external hardware interrupts of
INT 1 pin. The programmer can make it high to make the external
hardware interrupt edge triggered(H to L edge). When it is 0, it makes the
interrupt as low-level triggered.
7) IE0(TCON.1) - External interrupt 0 Edge flag. This bit is used by 8051 to
keep track of the edge-triggered interrupt only. It is set by CPU when
external interrupt edge (H to L transition) is detected. In case of external
hardware interrupt 0, when high to low edge transition occurs, IE0
becomes equal to 1, indicating external hardware interrupt 0 is in use.
8) IT0 (TCON.0) - External interrupt 0 type control bit. It is used to set the
low-level or edge-triggered modes of the external hardware interrupt of
INT0 pin. The programmer can make it high to make the external
hardware interrupt edge triggered (H to L edge). When it is 0, it makes the
interrupt as low-level triggered.

Q9. Write C Program to toggle bit P1.5 of port P1, 5000 times.
Ans9.
#include<reg51.h>
sbit mybit = P1^5;
void main(void)
{
unsigned int z;
for (z=0; z<5000; z++)
{
mybit = ~ mybit;
}
}
Q10. Write a program in C to toggle all bits of Port 1

Ans 10.
#include<reg51.h>
void main(void)
{
P1=~P1;
}

Q11. Write a 8051 C program to toggle only bit P2.4 continuously without
disturbing any other bit of port 2.
Ans 11.

#include<reg51.h>
sbit P2_4 = P2^4;
void main(void)
{
while (1)
{

P2_4 =~ P2_4;

}
}
Q12. Write a program in C language to toggle only bit P1.5 of port P1 ,
50000 times.
Ans12. Refer answer 9.
Q13. Explain Timer mode 0 & mode 1 of 8051 microcontroller.

Answer 13.
• Timer Mode 1: It is a 16-bit timer; therefore, it allows values from 0000 to
FFFFH to be loaded into the timer’s registers TL and TH. After TH and TL
are loaded with a 16-bit initial value, the timer must be started by
instruction TR1/TR0 =1. After the timer is started. It starts count up until
it reaches its limit of FFFFH. When it rolls over from FFFF to 0000H, it sets
high a flag bit called TF (timer flag). This timer flag can be monitored. After
the timer reaches its limit and rolls over, in order to repeat the process the
registers TH and TL must be reloaded with the original value and TF must
be reset to 0.
• Timer Mode 0: Mode 0 is exactly same like mode 1 except that it is a 13-bit
timer instead of 16-bit. It can hold values between 0000 to 1FFFH in TH-
TL. When the timer reaches its maximum of 1FFFH, it rolls over to 0000,
and TF is raised.
• Timer Mode 2: It is an 8 bit timer that allows only values of 00 to FFH to be
loaded into the timer’s register TH. After TH is loaded with 8 bit value, the
8051 gives a copy of it to TL. Then the timer must be started. It is done by
the instruction “TR0=1” for timer 0 and “TR1=1” for timer1.
• After timer is started, it starts to count up by incrementing the TL register.
It counts up until it reaches its limit of FFH. When it rolls over from FFH
to 00. It sets high the TF (timer flag). If we are using timer 0, TF0 goes
high; if using TF1 then TF1 is raised.
• When Tl register rolls from FFH to 00 and TF is set to 1, TL is reloaded
automatically with the original value kept by the TH register.
• To repeat the process, we must simply clear TF and let it go without any
need by the programmer to reload the original value.
• This makes mode 2 auto reload, in contrast in mode 1 in which
programmer has to reload TH and TL

Q14. Write a program in C language to configure timer 0 of 8051


microcontroller in mode 2 use 8051 XTAL for clock source and start the
timer 0.
Ans 15.
#include<reg51.h>
void main(void)
{
TMOD = 0X02;
TR0=1;
}

Q15. Write a program in C language to toggle only bit P1.5 continuously


every 50ms use timer 0 in mode 1 (16 bit) to create a delay. Assume crystal
frequency = 12MHz.

Ans 16.

#include<reg51.h>
void delay(void);
sbit P1_5 = P1^5;
void main(void)
{
P1_5 =~ P1_5;
delay();
}
Void delay(void)
{
TMOD = 0X01;
TL0 = 0XB0;
TH0 = 0X3C;
TR0 = 1;
while(TF0==0);
TF0 = 0;
TR0 = 0;
}
Q1. Write a short note on interrupt structure of 8051.
Ans1. There are six types of interrupts in 8051:
Timer Interrupts:
1) Timer 0 (TF0)
2) Timer 1 (TF1)
External Hardware Interrupts:
3) INT 0
4) INT 1
Serial Communication Interrupts:
5) Receive and Transmit Interrupts (RI & TI)
6) Reset Interrupt
• In ROM, fixed amount of space, and a memory address is given to each interrupt, hence
when an interrupt occurs microcontroller jumps to that particular address of the
interrupts in the memory.
Following are the address allotted to the interrupts where interrupt program (ISR) is
saved and is called ad INTERRUPT VECTOR TABLE

INTERRUPT VECTOR TABLE

Interrupts ROM Location

Reset 0000

INT 0 0003

TF0 000B

INT 1 0013

TF1 001B

RI & TI 0023
• The timer and serial interrupts are internally produced by the microcontroller.
• The external interrupts are produced by additional interfacing devices or
switches that are externally connected with the microcontroller at pin no.
12(P3.2) and pin no. 13(P3.3)
• These external interrupts can be level triggered or edge triggered and are called
as hardware interrupts.
• SFRs involved with interrupts are as follows:
• Interrupt Enable Register (IE) : It is used to enable or disable any interrupt.
• Interrupt Priority Register (IP) : It is used to change the priority levels of the
interrupt i. e. which interrupt will be executed first.
• Timer Control Register (TCON) : Used to monitor Timer Interrupts and
External Hardware Interrupts.
Q2. Write a program in C language to enable hardware interrupts INT0 and INT1.
Ans 2.
#include < reg51.h>
void main(void)
{
IE=0X85;
}
Q3. Write down the steps to program ADC 0809.
Ans 3. Following are the steps to program ADC 0809
1) Select the analog channel by providing bits to A, B, and C address according to
following table:

Selected C B A
Analog
Channel
IN0 0 0 0
IN1 0 0 1
IN2 0 1 0
IN3 0 1 1
IN4 1 0 0
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1
2) Activate the ALE (Address Latch Enable) pin. It needs a L to H pulse to latch in the
address.
3) Activate SC (Start of Conversion) by an L to H pulse to initiate the conversion.
4) Monitor EOC (end of conversion) to see whether conversion is finished. H to L
output indicates that the data is converted and is ready to be picked up. If we do
not use EOC, we can read the converted digital data after a brief time delay.
5) Activate OE (output enable) to read data out of the ADC chip. A L to H pulse to the
OE pin will bring digital data out of the chip. OE is same as RD pin in other ADC
chip.

Q4. Draw the IE register and explain the functions of bits EA, ET0 and EX0.
Ans 4.
Interrupt Enable Register (IE)

• Upon reset all interrupts are disabled (masked). They must be enabled by
software by using IE register for the microcontroller to respond to them. This
register is responsible for enabling (unmasking) and disabling (masking) the
interrupts. It is a bit addressable register.
• EA (IE.7) : It is called as Enable all . If EA =1, all interrupts are enabled, if EA =0 ,
no interrupt will be responded , even if the associated bit in the IE register is
high.
• IE. 6 : Not implemented, reserved for future use.
• ET2(IE.5) : Enables or disables timer 2 overflow.
• ES (IE.4) : Enables or disables the serial port interrupt.
• ET1 (IE.3) : Enables or disables the timer 1 overflow interrupt.
• EX1 (IE.2) : Enables or disables the external interrupt 1.
• ET1 (IE.1) : Enables or disables the timer 0 overflow interrupt.
• EX0 (IE.0) : Enables or disables the external interrupt 0.
Q5. Write down the steps in executing on an interrupt.
Ans 5. Steps followed by 8051 while occurrence of an Interrupt
1) It finishes the instruction it is executing and saves the address of the next
instruction (PC) on the stack.
2) It also saves the current status of all the interrupts internally (i.e. not on the
stack).
3) It jumps to a fixed location in memory called the interrupt vector table that
holds the address of the interrupt service routine.
4) The microcontroller gets the address of the ISR from the interrupt vector
table and jumps to it. It starts to execute the interrupt service subroutine
until it reaches the last instruction of the subroutine which is RETI (return
from interrupt).
5) After executing RETI instruction, the microcontroller returns to the place
where it was interrupted. First, it gets the program counter (PC) address
from the stack by popping the top two bytes of the stack into the PC.
Then it starts to execute from that address.
Q6. Draw and explain interfacing diagram of ADC with 8051.
Ans 6.

• Since physical world data is analog(continuous) and digital


computer/microcontrollers use binary or digital value, hence ADC is needed to
convert analog signals to digital numbers so that microcontroller can read and
process them.
A physical quantity is converted to electrical (voltage, current) signals using a device
called as transducer or sensors. ADC is used to translate analog signals to digital signals.
ADC 0809 has 8 different analog inputs, and produce 8-bit data output.
8 different analog inputs of ADC can be selected by using A B C input pin.
Selected
Analog C B A
Channel
IN0 0 0 0
IN1 0 0 1
IN2 0 1 0
IN3 0 1 1
IN4 1 0 0
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1

Microcontroller sends a LOW to HIGH level signal to ALE pin (its active-high pin) of ADC
to enable the latch in the address.
By applying HIGH to LOW Level signal to SC (Start Conversion), ADC starts analog to
digital conversion.
EOC (end of conversion) pin is monitored to see whether the conversion is finished. H
to L output indicates that the data is converted and is ready to be picked up.
After this, microcontroller enables the output line by applying a LOW to HIGH signal to
OE pin of ADC0809.
Q7. Explain interrupt structure of 8051 microcontroller with neat diagram
Ans 7. Same as answer 1,
Q8. Explain Interrupt Priority Register of 8051 Microcontroller.
Ans 8. Interrupt Priority Register (IP)
• The priorities are assigned to the interrupts to decide which interrupt will be
executed if more than one interrupts are activated at the same time.
• The following table shows the interrupt priority

External Interrupt 0 (INT0) Highest Priority

Timer Interrupt 0 (TF0)

External Interrupt 1 (INT 1)

Timer Interrupt 1 (TF1)

Serial Communication (RI & TI) Lowest Priority

• The priority of interrupts can be changed by assigning higher priorities to any


other interrupts by clearing or setting the individual bit in the Interrupt priority
(IP) register.
• Interrupt Priority Register is a bit addressable register.

Priority bit = 1 assigns high priority. Priority bit = 0 assigns low priority.
-- IP.7 : Reserved
-- IP.6 : Reserved
PT2 IP.5 : Timer 2 interrupt priority bit(8052 only)
PS IP. 4 : Serial port interrupt priority bit
PT1 IP.3 : Timer 1 interrupt priority bit
PX1 IP.2 : External interrupt 1 priority bit
PT0 IP.1 : Timer 0 interrupt priority bit
PX0 IP.0 : External interrupt 0 priority bit

Q9. Write short note on Interrupt enable register.


Ans 9. Same as answer 4.
Q10. Write a program to turn off LED for connected to Port 0 when interrupt
0 occurs and turn it on when interrupt 1 occurs.
Solution:
# include<reg51.h>
void EXT_interrupt0 (void) interrupt 0
{
P0=0;
}
void EXT_interrupt1 (void) interrupt 2
{
P0=1;
}
void main ()
{
IE=0X85;
IP=0X05;
while(1);
}
Q11. Explain the SOC, EOC, ALE & OE pin of ADC 0809.
Ans 11.
1) ALE : It is Address Latch Enable pin. It needs a L to H pulse to latch in the
address.
2) SOC : It is start of conversion pin. A L to H pulse is used to activate SOC pin to
initiate the conversion.
3) EOC : It indicates end of conversion. Monitor EOC (end of conversion) to see
whether conversion is finished. H to L output indicates that the data is converted
and is ready to be picked up. If we do not use EOC, we can read the converted
digital data after a brief time delay.
4) OE : It is output enable pin. OE (output enable) pin is activated to read data out of
the ADC chip. A L to H pulse to the OE pin will bring digital data out of the chip.
OE is same as RD pin in other ADC chip.

Q12. Write a C program to toggle bits of port1 when an external hardware


interrupt 0 occurs.
#include<reg51.h>
void EXT_interrupt0 (void) interrupt 0
{
P1=~P1;
}
void main ()
{
IE=0X81;
IP=0X01;
while(1);
}

Q13.Write a program in C language to assign highest priority to INT1 interrupt.


Discuss the sequence in which the interrupts are served if INT1 is assigned the
highest priority.
Ans 13.
#include<reg51.h>
void main(void)
{
IP = 0X04;
}
If INT 1 is assigned highest priority , in following sequence interrupts will be served :
1) INT 1
2) INT 0
3) TF0
4) TF1
5) RI+TI
Q14. Explain the function of following pins of ADC 0809
1) ADD A, ADD B and ADD C
2) EOC
3) START OF CONVERSION
4) OUTPUT ENABLE
Ans 14.
1) ADD A, ADD B and ADD C : These are address pins. The 8 analog input
channels are multiplexed and selected with the use of these pins, according to
the following table:
Selected
Analog ADD C ADD B ADD A
Channel
IN0 0 0 0
IN1 0 0 1
IN2 0 1 0
IN3 0 1 1
IN4 1 0 0
IN5 1 0 1
IN6 1 1 0
IN7 1 1 1

2,3,4 points same as answer 11


Q15. Draw a flowchart to read ADC.
Ans 15. Draw the flowchart for steps mention in answer 3.
Q1. Write down the steps to be followed to receive a data serially using 8051
microcontroller?
Ans 1. The following steps are followed to receive a data serially using 8051
microcontroller :
1. TMOD register is loaded with value = 20 H, indicating the use of Timer 1
, mode 2 (8-bit auto reload) to set the baud rate.

2. TH1 is loaded with value to set the baud rate.


3. SCON register is loaded with value = 50 H, indicating serial mode 1, where 8-bit
data is framed with start and stop bits and receive enable is turned on.

5 0

0 1 0 1 0 0 0 0

SM0 SM1 SM2 REN TB8 RB8 TI RI

4. TR1 set to 1 to start timer 1.


5. RI is cleared by ‘CLR RI’ or ‘RI=0’ instruction
6) RI flag bit is monitored with the use of instruction ‘ JNB RI,XX’ (or in case of C
programming instruction ‘ while (RI==0)) to see if entire character has been
received completely.
7) when RI is raised , SBUF has the byte. Its contents are moved into a safe place.
8) To receive the next character , go to step 5.

Q2. Write down a short note on interfacing of a GSM module with 8051
microcontroller.
 GSM stands for Global System for Mobile Communication. It is a digital cellular
technology used for transmitting mobile voice and data services.
 The idea of GSM was developed at Bell Laboratories in 1970. It is a widely used
mobile communication system in the world. GSM is an open and digital cellular
technology used for transmitting mobile voice and data services operate at the
850MHz, 900MHz, 1800MHz, and 1900MHz frequency bands.
 GSM module is a device or chip that uses GSM network to establish communication
between electronic devices.
 GSM is a standard developed by standard developed by the European
Telecommunications Standards Institute (ETSI).
 To communicate with GSM modem, AT commands are required.
 Microcontroller sends these commands to the GSM modem, which is then
activated to perform the required operation.
 RXD pin of 8051 microcontroller is connected to TXD pin of GSM module and
TXD pin of 8051 microcontroller is connected to RXD pin of GSM module.

 AT commands with a GSM modem can be used to access following information and
services:
1. Information and configuration pertaining to MODEM and SIM card.
2. SMS services.
3. MMS services.
4. Fax services.
5. Data and Voice link over mobile network.
Q3. Write a program to transfer a character “P” serially at baud rate of 9600, Use
serial port in Mode 1. Crystal frequency is 11.0592 MHz

Q4. Draw the SCON register and explain use of individual bits of the register in
detail./Write a short note on SCON register.
Ans 4. 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 SM2 REN TB8 RB8 TI RI

SM0 and SM1 are D7 and D6 of 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. They take the following combinations
Modes of serial communication
SM0 SM1
0 0 Serial Mode 0
0 1 Serial Mode 1, 8-bit data, 1 stop bit , 1 start bit
1 0 Serial Mode 2
1 1 Serial Mode 3
In serial mode 1, for each character a total of 10 bits are transferred , where the
first bit is the start bit , followed by 8 bits of data, and finally 1 stop bit.
SM2
It is the 5th bit of the SCON register. It enables the multiprocessing capability. It will
be set to 0, since it is not used.
REN
It is D4 bit of SCON register called as receive enable. When REN =1, 8051 receives
data on the RxD pin. If REN =0, the receiver is disabled. REN must be set to 1 , in
case of both receive and transfer data.
TB8
TB8(Transfer bit 8) is bit D3 of SCON. It is used for serial mode 2 and 3. It is not
used in our applications, hence it is made as 0.
RB8
RB8(receive bit 8) is bit D2 of SCON. It is used for serial mode 2 and 3. It is not used
in our applications, hence it is made as 0.
TI
TI(Transmit Interrupt) is bit D1 of SCON register, When 8051 finishes the transfer
of the 8-bit character, it raises the TI flag to indicate that it is ready to transfer
another byte. The TI bit is raised at the beginning of the stop bit.
RI
RI(receive interrupt) is bit D0 of SCON register, When 8051 receives data serially
via RxD, it get rid of the start and stop bits and places the byte in the SBUF register.
Then it raises the RI flag bit to indicate that a byte has been received and should be
picked up before it is lost. RI is raised hallway through the stop bit.

Q5. Write the steps taken by CPU to transfer data serially.


Ans 5.
The following steps are followed to transfer data serially using 8051
microcontroller :
1. TMOD register is loaded with value = 20 H, indicating the use of Timer 1
, mode 2 (8-bit auto reload) to set the baud rate.
2. TH1 is loaded with value to set the baud rate.
3. SCON register is loaded with value = 50 H, indicating serial mode 1, where 8-bit
data is framed with start and stop bits .

5 0

0 1 0 1 0 0 0 0

SM0 SM1 SM2 REN TB8 RB8 TI RI

4. TR1 set to 1 to start timer 1.


5. TI is cleared by ‘CLR TI’ or ‘TI=0’ instruction
6. The character byte to be transferred serially is written into the SBUF register,
7. TI flag bit is monitored with the use of instruction ‘JNB TI,XX’ (or in case of C
programming instruction while (TI==0)) to see if character has been transferred
completely.
8. To receive the next character, go to step 5.
Q6. Program 8051 in C to receive bytes of data serially and put them in P1. Set the
baud rate at 4800, 8-bit data, and I stop bit
Q7. Write program to transfer letter ‘T’ serially 10 times at baud rate of 4800.
Use serial port in mode 1. XTAL=12MHz.

#include<reg51.h>
void main(void)
{
unsigned char z;
z=0;
TMOD = 0X20;
TH1=0XFA;
SCON = 0X50;
TR1=1;
while(z<10)
{
SBUF ='T';
while(TI==0);
TI=0;
z++;
}
}
Q8. Write short note AT commands required for GSM.

Ans 8. AT is the abbreviation for Attention. AT commands are instructions used to


control modems. ‘AT’ is prefix that informs modem about start of command line.
AT Command instructions are single-line instructions that need to be sent to the device
in sequential order while the user has to wait for the response (in most cases) before
sending the next command. Sending across the relevant AT Commands and receiving its
responses usually requires a host device such as a microcontroller that is connected to
the modem via a UART (Universal Asynchronous Receiver/Transmitter). A UART is a
two-wire communication protocol that allows two devices to communicate with each
other in a half-duplex fashion.
AT Commands can be broadly classified into two sets, Basic Commands, and Extended
Commands.
Basic AT Commands
These are cellular modem AT Commands that do not begin with the “+” symbol. Some of
the common AT commands under the basic set are D (Dial), A (Answer), H (Hook
control), and O (Return to online data state).
Extended AT Commands
Extended Commands are those that start with the “+” symbol. All AT Commands for GSM
fall under this category and a few of the common commands are +CMGS (Send SMS
message), +CMGL (List SMS messages), and +CMGR (Read SMS messages).
Further, the AT Commands are of 4 categories primarily - Test Commands, Read
Commands, Set & Execution Commands.

Classification of AT commands

AT commands with a GSM/GPRS MODEM or mobile phone can be used to access


following information and services:
1. Information and configuration pertaining to mobile device or MODEM and
SIM card.
2. SMS services.
3. MMS services.
4. Fax services.
5. Data and Voice link over mobile network.

Examples of AT commands

• AT – To check connection has been established between modem and host


system (computer or microcontroller)
• ATD – To dial phone number
• ATA – To answer incoming calls
• ATH – To disconnect a call
• ‘AT + CMGF =’ – It is used to select the operating mode of the GSM modem
It takes one parameter. The value of the parameter can either be 0 or 1. The
values 0 and 1 refer to SMS PDU mode and SMS text mode respectively.
‘AT + CMGF =1’ – This command is used to set SMS text mode.

• ‘AT+CMGW=’- Phone number” - This command is used to store message in


the SIM.
• ‘AT+CMGS=’-This command is used to send a SMS message to a phone
number.
• ‘AT+CMGR =’- This command is used to read a SMS message.
• ‘AT+CMGL=’ – This command is used to list SMS messages.
• ‘AT+IPR=’ – Fixed DTE rate. It is the rate at which modem communicates with
the GSM network.
• ‘AT+CMSS =’ – Send message from storage.
• ‘AT+CMGD =’ – Delete message.
• ‘ATM’ – Monitor Speaker Mode.

Q9. Explain Serial port structure of 8051 microcontroller.

Ans 9 . The serial port structure of 8051 consists of UART(Universal Asynchronous


Receiver Transmitter) module. 8051 has RXD (serial data receive pin) and TXD (serial
data transmit pin) on PORT3.0 and PORT3.1 respectively to receive and transmit data
serially. In serial communication, data is sent one bit at a time.
Serial data communication uses two methods : Asynchronous and Synchronous.
Synchronous method transfers a block of data (character) at a time.
Asynchronous method transfers a single byte at a time.
In the asynchronous method, each character is placed between start and stop bits, called
as framing. Since the data received or transmitted in serial communication is all 0s and
1s; it is difficult to make sense of data without any protocol. Hence the data is packed in a
way called as data framing. In data framing, each character is placed between start and
stop bit.
The start bit is always one bit but the stop bit can be one or two bits. The start bit is
represented by low(0) and the stop bit by high(1).
In an asynchronous serial communication frame, the first START bit goes first followed
by the data byte and at last STOP bit goes which forms a 10-bit frame.
Refer following example to transmit ASCII character ‘A’
Character ‘A’ is equivalent to : 0100 0001 which is framed between start bit and stop bit.
Framing ASCII ”A”
Following registers are used for serial communication in 8051:

SBUF register :

SBUF is an 8-bit register used for serial communication. 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 register:

Refer answer 4 for explanation

Timer registers

Serial Communication registers of 8051 are programmed to transfer and receive data
serially.

Q10. Explain how Baud Rate is set for serial communication in 8051
microcontroller.
Ans10. The rate of data transfer in serial data communication is stated in bits per
second(bps) and is also called as baud rate. Baud rate is programable and is done with
the help of timer 1 in mode 2, that is 8-bit auto reload. 8051 divides the crystal frequency
by 12 to get the machine cycle frequency. If XTAL = 11.0592 MHz, the machine cycle
frequency is 921.6 kHz (11.0592MHz/12). The 8051’s serial communication UART
circuitry divides the machine cycle frequency of 921.6 kHz by 32 once more before it is
used by Timer 1 to set the baud rate. Therefore, 921.6 kHz divided by 32 gives 28,800 Hz.

For e.g : To set the baud rate of 9600, following calculations need to be performed:

Assume XTAL = 11.0592 MHz,


The machine cycle frequency of 8051 = 11.0592MHz/12 = 921.6 kHz and
921.6 kHz/32 = 28,800 Hz.
28,800 Hz is the frequency provided by UART to Timer 1 to set baud rate.
Calculation to load the value in TH1 to set the baud rate:
1) 28,800/3 = 9600 , where -3=FD H is loaded into TH1

Q11. Write a program to transfer a character “A” serially at baud rate of 4800, Use
serial port in mode 1. Crystal frequency is 11.0592 MHz

Ans11. Refer Answer 3

Q12. Assuming crystal frequency of 11.0592 MHz, find the value to be loaded in
TH1 to set up the following baud rates 1) 9600 2) 2400 3) 1200

Ans12. Given XTAL = 11.0592 MHz,


The machine cycle frequency of 8051 = 11.0592MHz/12 = 921.6 kHz and
921.6 kHz/32 = 28,800 Hz.
28,800 Hz is the frequency provided by UART to Timer 1 to set baud rate.
Calculation to load the value in TH1 to set the baud rate:
2) 28,800/3 = 9600 , where -3=FD H is loaded into TH1
3) 28,800/12 = 2400 where -12=F4 H is loaded into TH1
4) 28,800/24=1200 where -24 = E8 H is loaded into TH1
Q1. With a neat block diagram explain AC voltage measurement using 8051
microcontroller.
Ans1.
( Diagram : 3 marks, explanation : 4 marks)

• The voltage is measured by using voltage sensor.


• The output of voltage sensor is a voltage wave which is an analog signal
whereas 8051 microcontroller requires digital data, so ADC(Analog to Digital
Converter) is required for measurement of voltage.
• Th voltage to be measured is applied to the analog input terminal of the A/D
converter.
• The digital output is connected to I/O(Input/output) port of the
microcontroller, In the above diagram it is connected to port 2.
• The microcontroller reads the data from the port and voltage in digital form is
stored inside the microcontroller.
• The digital value is calibrated with the help of a look-up table and is displayed in
decimal numbers.
• LCD is interfaced with 8051 to display the voltage.
• In LCD Vss is ground pin, Vdd is supply pin and Vee is used to adjust the
brightness of display.
• RS is register select pin and is used to select between command and data
registers.
• When RS pin is set to command, it gives commands to LCD like setting position
of cursor, clearing LCD. When RS is set to data, it is used to display data on LCD
screen.
• RW pin is read/write pin. When RW is set to write mode(usually ground), the
microcontroller writes data to the LCD. When it is set to read mode, the
microcontroller reads data from LCD.
• E is Enable pin, it is used to enable the LCD.

Q2. Draw an interfacing diagram of stepper motor with 8051. Assuming the
motor is controlled through most significant 4 bits of port 1. Write a program in
C language to run the stepper motor continuously in anticlockwise direction.
Assume suitable step sequence.
Ans 2.

• While a conventional motor shaft runs freely, the stepper motor shaft moves in
a fixed repeat-able increment. The stepper motor discussed here has a total of 6
leads: 4 leads representing the four stator windings and 2 commons for the
center-tapped leads. As the sequence of power is applied to each stator
winding, the rotor will rotate. 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.
• Below table shows a 2-phase, 4-step stepping sequence.


# include<reg51.h>
void delay(unsigned int);
void main(void)
{
while(1)
{
P1 = 0x60;
delay(10);
P1 = 0x30;
delay(10);
P1 = 0x90;
delay(10);
P1 = 0xC0;
delay(10);
}
}
void delay(unsigned int time)
{
unsigned int i, j;
for(i=0; i<time; i++)
for(j=0; j<1275; j++);
}
Q3. Explain the function of an electromechanical relay and draw an interfacing
diagram of relay with microcontroller 8051 with suitable driver circuit.

Diagram : 3 Marks, Explanation : 4 marks


An electromechanical relay is a type of relay which function using a magnetic field
produced by an electromagnetic coil when a control signal is applied to it.
It is called as electromechanical since it has moving contacts in the output circuit
which are operated by applying an electrical signal.
An electromechanical relay transfers signals between its contacts through a
mechanical movement.
It has three sections viz. input section, control section and output section

When an input control voltage is applied to the electromagnetic coil, it gets


magnetized and the armature is attracted by the magnetic field produced by the coil.
The movable mechanical contacts are attached to the armature, thus when the
armature moves towards the electromagnet, the contacts closes, making the output
circuit switched on.
When the control signal is removed, the armature comes back to its original position
by the force of spring, making output circuit off.

Relays are the switches which aim at closing and opening the circuits electronically as
well as electromechanically. It controls the opening and closing of the circuit contacts
of an electronic circuit. When the relay contact is open (NO), the relay isn’t energize
with the open contact. However, if it is closed (NC), the relay isn’t energize given the
closed contact
Q4. Draw and explain interfacing of LED in common anode and common
cathode configurations. Write a program in C language for blinking display
of a LED connected to port pin P1.0. Use a suitable delay.

Ans4. Explanation: 6 marks, program: 4 marks

LED is a PN Junction diode which emits light when activated. It is activated when its
positive terminal is connected to positive supply and negative terminal is connected to
ground. In the following diagram, LED is connected to Port 2.0 pin.
LEDs need approximately voltage drop of 1.7 V and 10 mA current to flow through
them in order to glow at maximum intensity.

Interfacing of LED in common cathode configurations

In this circuit, LEDs are connected to the port P0. Since, it is common cathode
configuration, anode terminal of LEDs are connected to port 0 and all cathode
terminals are connected together to ground. The controller is connected with external
crystal oscillator to pin 18 and 19 pins. Crystal pins are connected to the ground
through capacitors of 33pf. LED is connected to the AT89C51 microcontroller with the
help of a current limiting resistor.

DRAW INTERFACING DIAGRAM AND WRITE EXPLANATION OF COMMON ANODE


CONFIGURATION BY YOURSELF , IT WILL BE SAME AS COMMON CATHODE, JUST
LEDs TERMINALS WILL GET REVERSED.

Q 5. Explain LED interfacing with 8051 microcontroller.

Ans 5. LED is a PN Junction diode which emits light when activated. It is activated
when its positive terminal is connected to positive supply and negative terminal is
connected to ground. In the following diagram, LED cathode terminal is connected to
Port 2.0 pin and anode is connected to Vcc supply. A program is written to turn on and
off LED with some time delay.
Ans 5.
Q6. With the help of block diagram explain Key interfacing using 8051.

Ans 6. In the following diagram, Key is like a switch which is used to control the LED
on and off operation. Key is connected to Port 0.0 pin, LED cathode terminal is
connected to Port 2.0 pin and anode terminal is connected to Vcc supply through
resistance R3. When key is pressed, 0 gets input into Port 0.0 pin, and Port 2.0 pin
gives output as 0 which turns on the LED. A program is written to turn on the LED
when key is pressed.

#include<reg51.h>
sbit switch = P0^0;
sbit Led = P2^0;
void main (void)
{
switch = 1;
if (switch==0)
{
Led = 0;
}
else
{
Led = 1;
}
}

Q7. With the help of block diagram explain Power measurement using 8051.

Q8. With the help of block diagram explain current measurement


using 8051.
Ans :
( Diagram : 3 marks, explanation : 4 marks)

• Small currents can be measured using the microcontroller.


• For this a resistor of small value (0.1 ohm to 1 ohm) is connected to the load and
Vcc in series as shown in the diagram.
• The small voltage drop (mV) across the resistor is connected to the inverting
terminal of op-amp for signal conditioning.
• The signal conditioning circuit will make the voltage suitable to apply to one of
the inputs of the ADC0809.
• This 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.
• To calculate the current Ohm's law, V= IR, is used.
• If the sense resistor’s value is 1 ohm and when 1 ampere of current is flowing in
the resistor , then 1 Volt will be developed across the sense resistor.
• So, I = V / R By measuring the voltage using the microcontroller, the current in
the resistor can be found.
• In LCD Vss is ground pin, Vdd is supply pin and Vee is used to adjust the
brightness of display.
• RS is register select pin and is used to select between command and data
registers.
• When RS pin is set to command, it gives commands to LCD like setting position
of cursor, clearing LCD. When RS is set to data, it is used to display data on LCD
screen.
• RW pin is read/write pin. When RW is set to write mode(usually ground), the
microcontroller writes data to the LCD. When it is set to read mode, the
microcontroller reads data from LCD.
• E is Enable pin, it is used to enable the LCD.

Q9. Draw interfacing diagram of a stepper motor with 8051 and write program
to rotate stepper motor 360 Degree in anticlockwise direction with step angle of
1.8 degree.

Ans 9. Refer Answer 12, also above program has been solved in class.

Q10. Draw interfacing diagram of stepper motor with 8051 and Write a C
program to rotate stepper motor in forward, reverse direction.

Ans10.
Program for clockwise(forward rotation)
# include<reg51.h>
void delay(unsigned int);
void main(void)
{
while(1)
{
P1 = 0x66;
delay(10);
P1 = 0xCC;
delay(10);
P1 = 0x99;
delay(10);
P1 = 0x33;
delay(10);
}
}
void delay(unsigned int time)
{
unsigned int i, j;
for(i=0; i<time; i++)
for(j=0; j<1275; j++);
}
Program for anticlockwise rotation(reverse rotation)
# include<reg51.h>
void delay(unsigned int);
void main(void)
{
while(1)
{
P1 = 0x66;
delay(10);
P1 = 0x33;
delay(10);
P1 = 0x99;
delay(10);
P1 = 0xCC;
delay(10);
}
}
void delay(unsigned int time)
{
unsigned int i, j;
for(i=0; i<time; i++)
for(j=0; j<1275; j++);
}

Q11. Write a C language program for 8051 to rotate stepper


motor in clockwise and anticlockwise direction with step angle
1.8º.

Ans 11. Above program has been solved in class.

Q12. Write a program to rotate a motor 64° in the clockwise direction. The
motor has a step angle of 2°. Use the normal 4 step sequence.

Ans 12.
# include<reg51.h>
void delay(unsigned int);
void main(void)
{
unsigned char x = 0;
while(x<8)
{
P1 = 0x66;
delay(10);
P1 = 0xCC;
delay(10);
P1 = 0x99;
delay(10);
P1 = 0x33;
delay(10);
x++;
}
}
void delay(unsigned int time)
{
unsigned int i, j;
for(i=0; i<time; i++)
for(j=0; j<1275; j++);
}

Q13.What is step angle? Calculate the steps required to complete one revolution
for 1.2 degree step angle stepper motor.

Ans 13. The step angle is the minimum degree of rotation associated with a
single step.
• Steps per revolution is the total number of steps needed to rotate one
complete rotation or 360 degrees
• Steps required to complete one revolution for 1.2 degrees = 360/1.2 = 300

Q14. What are applications of relay?

Ans 14. Relays are used to protect the electrical system and to minimize the damage to
the equipment connected in the system due to over currents/voltages. The relay is
used for the purpose of protection of the equipment connected with it.
These are used to control the high voltage circuit with low voltage signal in
applications audio amplifiers and some types of modems.
These are used to control a high current circuit by a low current signal in the
applications like starter solenoid in automobile. These can detect and isolate the faults
that occurred in power transmission and distribution system. Typical application
areas of the relays include

Industrial Automation
Power relays are extensively used in industrial automation to control motors, pumps,
fans, and various machine

Home Appliances

In the realm of home appliances, power relays play a vital role in regulating heating
elements, motors, and compressors. They are responsible for the reliable on-off
switching of devices such as ovens, refrigerators, and washing machines.

HVAC Systems

Heating, ventilation, and air conditioning (HVAC) systems rely on power relays to
control the operation of heaters, fans, and air conditioning units.

Security and Alarms

Power relays are integrated into security systems to control access points, alarms, and
surveillance equipment.

Automotive Industry

In the automotive sector, power relays control various components such as starter
motors, headlights, windshield wipers, and power windows.

Energy Distribution: Power relays are used in energy distribution systems to control
circuit breakers and switchgear. They ensure the safe and efficient distribution of
electricity within power grids and substations.

Other applications are:

 Lighting control systems


 Telecommunication
 Industrial process controllers
 Traffic control
 Motor drives control
 Protection systems of electrical power system
 Computer interfaces

Q15. With the help of block diagram explain voltage and current measurement
using 8051.
Q16. Draw an interfacing diagram of stepper motor with 8051. Assuming the
motor is controlled through least significant 4 bits of port 2, assuming the step
angle of motor is 1.8 degrees, Write a program in C language to run the stepper
motor in anticlockwise direction through an angle of 7.2 degrees. Assume
suitable step sequence.

Q17. A switch and an LED is connected to 8051 as shown in diagram.


The LED is connected in a common cathode configuration. Write a program in C
language to switch on the LED when Switch is in position A and switch it OFF
when it is in position B.

Ans 17.
#include<reg51.h>
sbit switch = P2^0;
sbit Led =P1^3;
void main (void)
{
switch = 1;
if (switch==0)
{
Led = 0;
}
else
{
Led = 1;
}
}

You might also like