Unit 3 Notes-1
Unit 3 Notes-1
The hex a decimal file produced by the compiler are uploaded into the Rom of the
microcontroller to make it operation. The size of this file is of a concern for a micro controller
programmer because of the following points
1. It is easy and less time consuming to write program in C than assembly language
2. C is easier to modify and update.
3. Codes available in function libraries can be easily used.
4. Code is portable to other microcontroller with little or no modification.
1. Unsigned char
The character data type is the most common choice of users in many applications in MCS 8051.
The unsigned char is an 8-bit type that takes values in the range of 0 – 255 (00 – FFH). It is
one of the widely used data type for the 8051. In many situations like setting a counter value,
where there is no need for signed data, we should use unsigned char instead of the signed char.
The C compiler uses the sighed char as the default if we do not put unsigned in front of char.
2. Signed char
The signed char is an 8-bit data type that uses the most significant bit (D7 of D7 – D0) to
represent the – or + values. As a result, we have only 7 bits for the magnitude of the signed
number giving us values from -128 to 128. In situations where + and – are needed to represent
a given quantity such as temperature, the use of the signed char data type is a must.
3. Unsigned int
The unsigned int is a 16-bit data type that takes a value in the range of 0 to 65535 (0000 to
FFFFH). In the 8051, unsigned int is used to define 16-bit variable s such as memory locations.
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 2 bytes of RAM, we should avoid using this data type.
4. Signed int
Signed int is a 16-bit data type that uses the most significant bit (D15 of D15 – D0) to represent
the – or + values. As a result, we have only 15 bits for the magnitude of the number or values
from -32768 to 32767.
5. Sbit (single bit)
The sbit keyword is a widely used 8051 C data type designed specifically to access single bit
addressable register. It allows access to the single bits of the SFR register.
MCS 8051 has two timers/counters, namely Timer 1 and Timer 0. They can be used either as
a timer to generate a time delay or as a counter to count events happening outside the
microcontroller. In MCS-8051 both the timer/counter are 16 bits wide. As the architecture of
MCS-8051 is of 8 bits, each 16-bit timer is accessed using two separate register sets, one for
lower bytes and one for higher.
As stated, before both the timer registers of MCS-8051 are 16 bits registers, to accesses them
two separate register sets are used. The lower byte register is called TL0/TL1 (TL0 in case of
Timer 0 and TL1 in case of Timer 1) and the higher byte register is called TH0/TH1. These
registers can be accessed like any other register set such as register ‘A’, ‘B’, etc. For example,
MOV TL0, #4FH. This instruction moves the value 4FH into TL0, i.e. the lower byte of Timer
0. The instruction ‘MOV R5, TH0’ moves the content of TH0 register to R5 register of an
active register set.
TMOD register
To set up various operation mode of both the Timer 0 and 1, same register called TMOD (timer
mode register) is used. TMOD is an 8-bit special function register in which the lower 4 bits are
assigned for the Timer 0 and upper 4 bits are assigned for the Timer 1. In each case, the lower
2 bits (M0 and M1) are used to set the timer modes and upper 2 bits (C/T and GATE) for
specifying the operations. The architecture of the TMOD register is shown in the figure below.
C/T (clock/timer)
This bit of the TMOD register is used to decide whether the timer is used as a delay generator
or an event counter. If C/T = 0, timer is used as a time delay generation. The clock source for
the time delay is the crystal frequency of the 8051. It is to be noted that the frequency of the
timer is always 1/12th of the frequency of the crystal attached to MCS-8051.
GATE
This bit is used for starting and stopping the timers. Both the timer can be started or turned off
individually because of the two GATE bits of the TMOD register. The timer can be started and
turned off using either software control or hardware control or both. Using software means
timers are started by setting the TR bits, TR0 and TR1 (SETB TR1). While, for stopping the
TR bits is cleared (CLR TR1). For hardware, an external source is required to make the GATE
in the TMOD register equal to 1.
MO and M1
M0 and M1 bits are used to select the timer mode. There are three modes of operation Mode 0,
1 and 2 which are selected as per the Table given below.
Mode 1 Operation
This is a 16-bit timer which allows values from 0000H to FFFFH. These values are 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 the institution ‘SETB TR0/TR1’. After the timer is started, it starts to
count up. It counts up until it reaches the value FFFFH. When it rolls over from FFFFH to
0000H, a flag bit called TF (timer flag) is set. This timer flag can be monitored. When this
timer flag is raised, one option is to stop timer with instruction ‘CLR TR0/TR1’. After the timer
reaches its limit and roll over, in order to repeat the process, the registers TH and TL must be
reloaded with the original values, and TF must be reset to 0.
Mode 2 Operation
It is an 8 bit timer; therefore, it allows only values of 00 to FFH to be loaded into the timer’s
register TH. After, TH is loaded with the 8 bits value, the 8051 gives a copy of it to TL. Then
the timer must be started. This is done by the instruction ‘SETB TR0’ for Timer 0 and ‘SETB
TR1’ for Timer 1. This is just like mode 1. After the timer is started, it starts to count up by
incrementing the TL registers. It counts up until it reaches its limits FFH. When it rolls over
from FFH to 00H, it sets high the TF. If we are using Timer 0, TF0 gets high. If we are using
Timer 1, TF 1 is raised. When the TL register rolls from FFH to 00H 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 no need to load a value on the TH register. This makes mode 2
an auto-reload, in contrast with mode 1 in which the programme has to reload TH and TL.
Mode 0 Operation
Mode 0 is exactly like mode 1 except that it is a 13-bit timer instead of 16 bits. The 13-bit
counter can hold values between 0000 to 1FFFFH in TH – TL. Therefore, when the time
reaches its maximum of 1FFFH, it rolls over to 0000, and TF is raised.
Mode 3 Operation
Mode 3 is different for Timer0 and Timer1. When the Timer0 is working in mode 3, the TL0
will be used as an 8-bit timer/counter. It will be controlled by the standard Timer0 control bits,
T0 and INT0 inputs. The TH0 is used as an 8-bit timer but not the counter. This is controlled
by Timer1 Control bit TR1. When the TH0 overflows from FFH to 00H, then TF1 is set to 1.
When the Timer 1 is working in Mode 3, it simply holds the count but does not run.
When Timer 0 is in mode 3, the Timer 1 is configured in one of the mode 0, 1 and 2. In this
case, the Timer 1 cannot interrupt the microcontroller. When the TF 1 is used by TH 0 timer,
the Timer 1 is used as Baud Rate Generator.
COUNTER PROGRAMMING
When the timer/counter is used as a timer, the 8051’s crystal is used as the source of the
frequency. When it is used as a counter, however, it is a pulse outside the 8051 that increments
the TH, TL register. All other functions are similar as discussed for the timer mode.
Recall from the last section that the C/T bit in the TMOD register decides the source of the
clock for the timer. If C/T = 0, the timer gets pulses from the crystal. In contrast, when C/T =1,
the timer is used as a counter and gets its pulse from outside the 8051. Therefore, C/T = 1, the
counter counts up as pulses are fed from pin 14 and 15. These pins are called T0 (Timer 0
input) and T1 (Timer 1 output). Notice that these two pins belong to port 3. In the case of Timer
0, when C/T = 1, pin 3.4 provides the clock pulse and the counter counts up for each clock
pulse coming from that pin. Similarly, for Timer 1, when C/T = 1 each clock pulse coming in
from pin 3.5 makes the counter count up.
TCON register
In previous discussion, TR0 and TR1 flags are used to turn on or off the timer. These bits are
part of a register called TCON (timer control). This is an 8-bit register. The upper four bits are
set to store the TF and TR bit of both the Timer 0 and Timer 1. The lower bits are set aside for
controlling the interrupt bits which will be discussed later.
Solution:
#include<reg51.h>
void main ()
{
unsigned char a;
for (a =0; a<=255; a++)
P2=a;
}
Program 2: Write a program to toggle the bits of port P1 to generate a square wave using
software delay of 5 seconds.
Solution:
#include<reg51.h>
void main ()
{
int x;
while (1)
{
P1= 0*00;
for (x = 0; x <= 5000; x++)
P1 = 0*FF;
for (x = 0; x<=5000; x++)
}
}
Program 3: Write a program to toggle Port 1 pin 4 to generate a square wave using software
delay of 5 seconds.
Solution:
#include<reg51.h>
sbit mybit =p1^4;
void main ()
{
int x;
while (1)
{
mybit = 0;
for (x = 0; x <= 5000; x++)
mybit = 1;
for (x = 0; x<=5000; x++)
}
}
Program 4: Write a program to get bit P1.0 and send it to P2.7 after inverting it.
Solution:
#include<reg51.h>
sbit mybit1 = P1^0;
sbit mybit2 = P2^2;
void main ()
{
mybit2 = ~ mybit1
}
Program 5: Write a program to monitor P1.5. If it is high, send 55H to P0, otherwise send
AAH to P2.
Solution:
#include <reg51.h>
sbit mybit = P1^5;
void main ()
{
mybit = 1;
while (1)
{
if (mybit = =1)
P0 = 0*55;
else
P2 = 0*AA;
}
}
Program 6: Write an 8051 C program to toggle all the bits of port P1 continuously with
some delay in between. Use Timer 0, 16-bit mode to generate the delay.
Solution:
#include <reg5I.h>
void TODelay(void);
void main (void)
{
while(l)
{
Pl=Ox55:
TODelay ();
Pl=OxAA;
TODelay ();
}
}
void TODelay(void);
{
TMOD=OxO1;
TLO=OxOO;
THO=Ox35;
TRO=l;
while (TFO==O);
TRO=O;
TFO=O;
}
Program 7: Write an 8051 C program to toggle only bit P 1.5 continuously every 50 ms.
Use Timer 0, mode I (l6-bit) to create the delay. Assume XTAL = 11.0592MHz
Solution:
#include <reg5l.h>
void TOMIDelay(void);
sbit mybit=P1^5;
void main (void)
{
while(l)
{
mybit=~mybit;
TOMIDelay();
}
}
void TOMIDelay(void)
{
TMOD=OxOl;
TLO=OxFD;
THO=Ox4B;
TRO=l;
while (TFO==O);
TRO=O;
TFO=O;
}
Program 8: Write an 8051 C program to create a frequency of 2500 Hz on pin P2. 7. Use
Timer 1, Mode 2 to create the delay.
Solution:
#include <regS1.h>
void T1M2Delay(void);
sbit mybit=P2A7;
void main (void)
{
unsigned char X;
while (1)
{
mybit=-mybit;
TlM2Delay ();
}
}
void T1M2Delay(void)
{
TMOD=Ox20;
TH1=48;
TR1=1;
while (TF1==O);
TR1=O;
TF1=O;
}