FMA Unit 3 Imp (Question & Answers
FMA Unit 3 Imp (Question & Answers
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.
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;
}
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
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
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.
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
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;
}