Lecture 12 8051 Timer Programing v2
Lecture 12 8051 Timer Programing v2
Email: [email protected]
8051 IO description
In 8051, I/O operations are done using four
ports and 40 pins. The following pin diagram
shows the details of the 40 pins. I/O operation−
port reserves 32 pins where each port has 8
pins. The other 8 pins are designated as Vcc,
GND, XTAL1, XTAL2, RST, EA (bar), ALE/PROG
(bar), and PSEN (bar).
The initial values of the timer register can be set by the user and can be used to generate
required counts.
https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/
Counters
Timers are also called counters because they are used to count external
events. Timers are mainly used for counting or measuring external events.
For example in the case of a visitor counter, the sensor placed for detecting
the presence of a person goes high when someone crosses the door. The
output of the sensor is connected to the Timer Clock Input Pin of the
microcontroller. The timer register inside the microcontroller increments
each time when a person crosses the door. The value can be later read by
the CPU.
https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/
Prescalar
Overflow
Overflow means the counter reached its maximum output and roll over to zero.
The microcontroller has an overflow flag to indicate the overflow of the counter and
generates overflow interrupts.
This gives an additional option to count the number of times the counter overflowed and
it extends the range of the counter. For example, if we need to count up to 512, we can
use the 8-bit timer and overflow flag. After two overflows, the count must be 512
(256+256).
https://openlabpro.com/guide/timer-and-
counter-with-pic18f4550/
Overflow
Overflow means the counter reached its maximum output and roll over to zero.
The microcontroller has an overflow flag to indicate the overflow of the counter and
generates overflow interrupts.
This gives an additional option to count the number of times the counter overflowed and
it extends the range of the counter. For example, if we need to count up to 512, we can
use the 8-bit timer and overflow flag. After two overflows, the count must be 512
(256+256).
https://microcontrollerslab.com/8051-timer-
generate-delay/
−
https://microcontrollerslab.com/8051-timer-
generate-delay/
Clock source:
Timer needs a clock source. If C/T = 0, the crystal frequency attached to the 8051 is the source
of the clock for the timer. The value of crystal frequency attached to the microcontroller
decides the speed at which the timer ticks. Now suppose that crystal frequency is 11.059MHz.
Timer’s clock frequency:
The frequency for the timer is always 1/12th of the frequency of the crystal attached to the
8051.
TF = 1/12 x 11.059MHz = 921583 Hz
2. Subtract the value of N from the maximum number of counts possible for 16 bit timer i.e.
2^16 = 65536.
M=65536-N
M=65536-9216
M= 56320
3. Convert this value to hexadecimal and write in TH and TL registers
MH=DC00H
TH=DCH
TL=00H
https://microcontrollerslab.com/8051-timer-
generate-delay/
#include<reg51.h>
sbit led=P2^0; // led at PORT 2 pin 0
void Delay(void); // Delay function declaration
DELAY code 8051 timer 0, mode 1 void main () // main function
{
led=0; //output PORT
while(1) // infinite loop −
{
led = 1; // LED ON
Delay();
led = 0; // LED OFF
Delay();
}
}
void Delay()
{
TMOD = 0x01; // Timer0 mode1
TH0=0xDC; //initial value for 10ms
TL0=0x00;
TR0 = 1; // timer0 start
while (TF0 == 0); // check overflow condition
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}
https://microcontrollerslab.com/8051-timer-generate-delay/
−
Additional Examples
http://what-when-how.com/8051-microcontroller/programming-
timers-0-and-1-in-8051-c/ −