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

FMA Unit 3 Imp (Question & Answers

The document discusses various C data types that can be used for the 8051 microcontroller. It describes unsigned char, signed char, unsigned int, signed int, sbit, bit and sfr data types. It provides the value ranges and typical uses for each data type when programming the 8051 microcontroller.

Uploaded by

Omkar Chede
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 views10 pages

FMA Unit 3 Imp (Question & Answers

The document discusses various C data types that can be used for the 8051 microcontroller. It describes unsigned char, signed char, unsigned int, signed int, sbit, bit and sfr data types. It provides the value ranges and typical uses for each data type when programming the 8051 microcontroller.

Uploaded by

Omkar Chede
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/ 10

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

You might also like