0% found this document useful (0 votes)
11 views11 pages

LAB 12 8051 Timer programming in C

Uploaded by

tanveer1111110
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)
11 views11 pages

LAB 12 8051 Timer programming in C

Uploaded by

tanveer1111110
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/ 11

LAB 12

PROGRAMMING 8051
TIMERS IN C
IMPORTANT
 The general purpose registers of the 8051, such
as R0-R7, A, and B are under the control of C
compiler and are not accessed directly by C
statement.
 In the case of SFRs, the entire RAM space 80 –
FFH is accessible directly using 8051 C
statements
ACCESSING 8051 TIMERS REGISTERS
 In 8051 C we can access the timer registers THx,
TLx, and TMOD directly using the reg51.h
header file
ACCESSING 8051 TIMERS REGISTERS-EXAMPLES
The following program will toggle all bits of P1 continuously
with some delay. We are using timer 0, 16-bit mode to
generate the delay.

#include <reg51.h>
void Delay(void);
void Delay()
void main (void) {
{ TMOD = 0x01;
while (1) TL0 = 0x00;
{ TH0 = 0x35;
P1=0x55; TR0 = 1;
Delay(); while (TF0 = = 0);
P1=0xAA; TF0 = 0;
Delay(); TR0 = 0;
} }
}
ACCESSING 8051 TIMERS REGISTERS-EXAMPLES
The following program will toggle bit P1.5 continuously
every 50ms. Crystal Frequency is 11.0592MHz.

#include <reg51.h>
void Delay(void);
sbit mybit = P1^5;
void main (void) void Delay()
{
{
TMOD = 0x01;
while (1) TL0 = 0x00;
{ TH0 = 0x4C;
mybit = ~mybit; TR0 = 1;
Delay(); while (TF0 = = 0);
TF0 = 0;
} TR0 = 0;
}
}
ACCESSING 8051 TIMERS REGISTERS-EXAMPLES
Assume that a 1 – Hz external clock is being fed into
P3.5. The following program will use counter 1 in
mode 2 to count up and display the state of the TL1
count on P1

#include <reg51.h> do
//sbit T11 = P3^5; {
void main (void) TR1 = 1;
{ P1 = TL1;
//T11 = 1; }
while (TF1 ==0);
TMOD = 0x60;
TR1 = 0;
TH1 = 0; TF1 = 0;
while (1) }
{ }
8051 TIMERS PROGRAMMING-EXERCISES
Program Timer 0 in C to generate a square wave of
3 kHz. Assume that XTAL = 11.0592 MHz
8051 TIMERS PROGRAMMING-EXERCISES
Program Timer 1 in C to generate a square wave of
0.5 kHz. Assume that XTAL = 11.0592 MHz
8051 TIMERS PROGRAMMING-EXERCISES
A switch is connected to pin P1.0. write an 8051 C
program to monitor SW and create the following
frequencies on pin P1.4:
SW = 0 300 Hz
SW = 1 700 Hz
8051 TIMERS PROGRAMMING-EXERCISES
Program Timer 1 in C to be an event counter. Use
mode 1 and display the binary count on P1 and
P2 continuously.
8051 TIMERS PROGRAMMING-EXERCISES
Assume that a 1 Hz external clock is being fed into
pin T0 (P3.4). Write a C program for counter 0 in
mode 1 to display the seconds and minutes on P1
and P2 respectively.

You might also like