0% found this document useful (0 votes)
35 views32 pages

WINSEM2023-24 BECE320E ETH VL2023240504752 2024-03-18 Reference-Material-I

The document discusses various aspects of embedded C programming for 8051 microcontrollers including programming timers, timer registers, timer modes, clock sources, starting and stopping timers, and using timers to generate delays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views32 pages

WINSEM2023-24 BECE320E ETH VL2023240504752 2024-03-18 Reference-Material-I

The document discusses various aspects of embedded C programming for 8051 microcontrollers including programming timers, timer registers, timer modes, clock sources, starting and stopping timers, and using timers to generate delays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Embedded C Programming

Srihari Mandava
Department of Electrical Engineering,
SELECT

Dr. A. Nayeemulla Khan


Programming Timers
• The 8051 has two timers/counters (Timer 0 and Timer 1),
they can be used either as
 Timers to generate a time delay or as
 Event counters to count events happening outside the
microcontroller

• Both Timer 0 and Timer 1 are 16 bits wide


 Since 8051 has an 8-bit architecture, each 16-bits
timer is accessed as two separate registers of low byte
and high byte
Timer 0 & 1 Registers
• The 16-bit register of Timer 0 is accessed as low byte and
high byte.

• The low byte register is called TLO (Timer 0 low byte)


and the high byte register is referred to as THO (Timer 0
high byte).

• These registers can be accessed like any other register,


such as A, B, RO, RI, R2, etc.

• These registers can also be read like any other register.


Timer 0 & 1 Registers
TMOD (timer mode) register
• Both timers 0 and 1 use the same register, called TMOD
(timer mode), to set the various timer operation modes.

• TMOD is a 8-bit register


 The lower 4 bits are for Timer 0
 The upper 4 bits are for Timer 1
 In each case,
 The lower 2 bits are used to set the timer mod.
The upper 2 bits to specify the operation
M1, M0
• M0 and Ml select the timer mode.
• There arc three modes: 0, 1, and 2
• Mode 0 is a 13-bit timer, mode 1 is a 16-bit timer, and
mode 2 is an 8-bit timer.

C/T (clock/timer)
• This bit in the TMOD register is used to decide whether
the timer is used as a delay generator or an event counter.

• If C/T = 0, it is used as a timer for time delay generation,


The clock source for the time delay is the crystal
frequency of the 8051.
M1, M0
• M0 and Ml select the timer mode.
• There arc three modes: 0, 1, and 2
• Mode 0 is a 13-bit timer, mode I is a 16-bit timer, and
mode 2 is an 8-bit timer.

C/T (clock/timer)
• This bit in the TMOD register is used to decide whether
the timer is used as a delay generator or an event counter.

• If C/T = 0, it is used as a timer for time delay generation,


The clock source for the time delay is the crystal
frequency of the 8051.
• Indicate which mode and which timer are selected for
each of the following.
(a) MOV TMOD, #01H
(b) MOV TMOD, #20H
(c) MOV TMOD, #12H
Clock source for Timer

• Every timer needs a clock pulse to tick. What is the


source of the clock pulse for the 8051 timers?
• If C/T = 0, the crystal frequency attached to the 8051 is
the source of the clock for the timer.
• This means that the size of the crystal frequency attached
to the 8051 also decides the speed at which the 8051
timer ticks.
• The frequency for the timer is always 1/12th the
frequency of the crystal attached to the 8051
Find the timer's clock frequency and its period for various
805 I-based systems, with the following crystal frequencies.
(a) 12 MHz
(b) 16 MHz
(c) 11.0592 MHz

Note: 8051 timers use 1/12 of XTAL frequency, regardless


of machine clock time.

XTAL frequency of 11.0592 MHz is considered as XTAL =


11.0592 MHz allows the 8051 system to communicate with
the IBM PC with no errors.
Gate
• Timers of 8051 do starting and stopping by either
software or hardware control.
• In using software to start and stop the timer where
GATE=0
 The start and stop of the timer are controlled by way
of software by the TR(timer start) bits TR0 and TR1.
This is achieved by the instructions "SETB TRl" and
"CLR TRl" for Timer I, and "SETB TR0" and "CLR
TR0" for Timer 0.
These instructions start and stop the timers as long as
GATE =0 in the TMOD register.
• The hardware way of starting and stopping the timer by
an external source is achieved by making GATE=1 in the
TMOD register

Find the value for TMOD if we want to program Timer 0 in


mode 2, use 8051 XTAL for the clock source, and use
instructions to start and stop the timer.

TMOD= 0000 0010


Timer 0, mode 2,
C/T = 0 to use XTAL clock source, and gate = 0 to use
internal (software) start and stop method.
Mode 1
The following are the characteristics and operations of mode1:

1. It is a 16-bit timer; therefore, it allows value of 0000 to


FFFFH to be loaded into the timer’s register TL and TH
2. After TH and TL are loaded with a 16-bit initial value, the
timer must be started
 This is done by SETB TR0 for timer 0 and SETB
TR1for timer 1
3. After the timer is started, it starts to count up .
 It counts up until it reaches its limit of FFFFH
When it rolls over from FFFFH to 0000, it sets high a
flag bit called TF (timer flag) – Each timer has its own
timer flag: TF0 for timer 0, and TF1 for timer 1 .This timer
flag can be monitored
Mode 1
 When this timer flag is raised, one option would be to stop
the timer with the instructions CLR TR0 or CLR TR1, for
timer 0 and timer 1.
• After the timer reaches its limit and rolls over, in order to
repeat the process
TH and TL must be reloaded with the original
value, and
 TF must be reloaded to 0.
To generate a time delay

1. Load the TMOD value register indicating which timer


(timer 0 or timer 1) is to be used and which timer mode (0 or
1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) to see if it is raised
 Get out of the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
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 void TODelay (unsigned char x)
{
#include<reg51.h> unsigned char i;
void TODelay(unsigned char x); for(i=0;i<x;i++)
void main() {
{ TMOD=0x01;
while(1) TL0=0x00;
{ TH0=0x35;
P2=0x55; TR0=1;
TODelay(5); while(TF0==0);
TR0=0;
P2=0xAA; TF0=0;
TODelay(5); }
}
}

}
The delay length depends on three factors:
 The crystal frequency
 The number of clocks per machine cycle
 The C compiler.

• The original 8051 used 1/12 of the crystal oscillator frequency as


one machine cycle. In other words, each machine cycle is equal to
12 clocks periods of the crystal frequency connected to the X I - X2
pins .

• The time it takes for the 8051 to execute an instruction is one or


more machine cycles.

• AT89C51/52 uses 12, while the OS5000 uses 4 clocks, and the
OS89C4xO uses only one clock per machine cycle.
• The C compiler is a factor in the delay size since various 8051
C compilers generate different hex code sizes.

• There is a major difference between the AT89C51 and


DS89C4xO chips in term of the time it takes to execute a
single instruction.

• The OS89C4xO executes instructions 12 times faster than the


AT89C51 chip, they both still use Osc/12 clock for their
timers.
• Write an 8051 C program to toggle only bit P1.5 continuously
every 50 ms. Use Timer 0, mode 1 (16-bit) to create the delay.
Test the program on the (a) AT89C51 and (b) DS89C420.
void T0M1Delay(unsigned char
#include<reg51.h>
x)
void T0M1Delay(unsigned char);
{
sbit mybit=P2^5;
unsigned char i;
void main()
for(i=0;i<x;i++)
{
{
//P2=0;
while(1)
TMOD=0x001;
{
TL0=0xFD;
TH0=0x4B;
mybit=~mybit;
TR0=1;
while(TF0==0);
T0M1Delay(5);
TR0=0;
}
TF0=0;
}
}

}
To calculate the values to be loaded into the TL and TH registers,

Assume XTAL = 11.0592 MHz, use the following steps for


finding the TH, TL registers’ values

1.Divide the desired time delay by 1.085 us


2.Perform 65536 – n, where n is the decimal value we got
in Step1
3.Convert the result of Step2 to hex, where yyxx is the
initial hex value to be loaded into the timer’s register.
4. Set TL = xx and TH = yy
Assume that XTAL = 11.0592 MHz. What value do we need to
load the timer’s register if we want to have a time delay of 5 ms
(milliseconds)?

Since XTAL = 11.0592 MHz, the counter counts up every 1.085


us. This means that out of many 1.085 us intervals we must make
a 5 ms pulse. To get that, we divide one by the other. We need 5
ms / 1.085 us = 4608 clocks.

To Achieve that we need to load into TL and TH the value 65536


– 4608 = EE00H. Therefore, we have TH = EE and
TL = 00.
Mode 0

• Mode 0 is exactly like mode 1 except that it is a 13-bit timer


instead of 16-bit. The 13-bit counter can hold values between
0000 to 1FFFH in TH - TL.

• Therefore, when the timer reaches its maximum of IFFH, it


rolls over to 0000 and TF is raised.
Mode 2

• It is an 8-bit timer; therefore, it allows only values of 00 to


FFH to be loaded into the timer’s register T.
• After TH is loaded with the 8-bit 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 TR1for timer 1
• After the 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)
Mode 2

• When the TL register rolls from FFH to 0 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 an auto-reload, in contrast with mode
1 in which the programmer has to reload TH and TL.
To generate a time delay

1. Load the TMOD value register indicating which timer (timer 0


or timer 1) is to be used, and the timer mode (mode 2) is
selected
2. Load the TH registers with the initial count value
3. Start timer
4. Keep monitoring the timer flag (TF) to see whether it is raised
 Get out of the loop when TF goes high
5. Clear the TF flag
6. Go back to Step4, since mode 2 is auto-reload
Write an 8051 C program to toggle only pin P1.5 continuously
every 250 ms. Use Timer 0, mode 2 (8-bit auto-reload) to create t
he delay.
#include <reg51.h> void TOM2Delay(void)
void TOM2Delay(void); {
sbit mybit=P1^5; TMOD=0x02;
TH0=233;
void main (void)
TR0=1;
{ while (TF0==0) ;
unsigned char x, y; TR0=0;
while (1) TF0=0;
{ }
mybit=~mybit;
for(x=0;x<250;x++)
for(y=0;y<40;y++)
TOM2Delay();
}
}
Write an 8051 C program to create a frequency of 2500 Hz on
pin
P2.7. Use
#include Timer 1, mode 2 to create delay. void T1M2Delay(void)
<reg51.h> {
void T1M2Delay(void); TMOD=0x20;
sbit mybit=P2^7; TH1=0x48;
void main(void) TR1=1;
{ while (TF1==0);
unsigned char x; TR1=0;
while (1) TF1=0;
{ }
mybit=~mybit;
T1M2Delay();
} 1 / 2500 Hz = 400 us
400 us / 2 = 200 us
}
200 us / 1.085 us = 184
A switch is connected to pin P 1.2. Write an 8051 C program to
monitor SW and create the following frequencies on pin P1.7:
SW=0: 500 Hz
SW=l: 750Hz
Use Timer 0, mode 1 for both of them.
Timers as Counters

• Timers can also be used as counters counting events


happening outside the 8051
 When it is used as a counter, it is a pulse outside of the
8051 that increments the TH, TL registers.
 TMOD and TH, TL registers are the same as for the timer
discussed previously

• The C/T bit in the TMOD registers decides the source of the
clock for the timer
When C/T = 1, the timer is used as a counter and gets its
pulses from outside the 8051.
 The counter counts up as pulses are fed from pins 14 and
15, these pins are called T0 (timer 0 input) and T1 (timer 1
input)
A timer can be used as a counter if we provide pulses from
outside the chip instead of using the frequency of the crystal
oscillator as the clock source.

By feeding pulses to the T0(P3.4) and T1 (P3.5) pins, we turn


Timer 0 and Timer 1 into counter 0 and counter 1, respectively.
Assume that a 1-Hz external clock is being fed into pin T1
(P3.5).
Write a C program for counter 1 in mode 2 (8-bit auto reload) to
count up and display the state of the TL1 count on P1. Start the
count at 0H.

You might also like