8051 : 8 bit Timer0 overflow using Interrupt
Page 1 of 1
8051 : 8 bit Timer0 overflow using Interrupt
This example program shows how to configure timer/counter 1 as an
8-bit timer. An interrupt service routine (ISR) is invoked each
time the timer overflows (goes from 0xFF to 0x00). Inside the
ISR, a variable called overflow_count is incremented by one.
To test this program...
1. Start the debugger.
2. Set a breakpoint on the 'overflow_count++' line in the ISR.
3. Run the program.
Each time the interrupt routine is invoked, the debugger will stop
running the program. Position the cursor over 'overflow_count' to
see its current value.
#include <reg52.h> /* 8051 specific header file */
/*------------------------------------------------
Timer 1 Interrupt Service Routine.
Set a breakpoint on 'overflow_count++' and run the
program in the debugger. You will see this line
executes every 100 clock cycles (or 10,000 Hz).
So, overflow_count is actually a 1/10,000 sec
timer.
------------------------------------------------*/
static unsigned long overflow_count = 0;
void timer1_ISR (void) interrupt 3
{
overflow_count++; /* Increment the overflow count */
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
/*--------------------------------------
Set Timer1 for 8-bit timer with reload
(mode 2). The timer counts to 255,
overflows, is reloaded with 156, and
generates an interrupt.
Set the Timer1 Run control bit.
--------------------------------------*/
TMOD = (TMOD & 0x0F) | 0x20; /* Set Mode (8-bit timer with reload) */
TH1 = 256 - 100; /* Reload TL1 to count 100 clocks */
TL1 = TH1;
ET1 = 1; /* Enable Timer 1 Interrupts */
TR1 = 1; /* Start Timer 1 Running */
EA = 1; /* Global Interrupt Enable */
/*--------------------------------------
Do Nothing. Actually, the timer 1
interrupt will occur every 100 clocks.
Since the oscillator runs at 12 MHz,
the interrupt will happen every 10 KHz.
--------------------------------------*/
while (1)
{
}
}
8-bit timer. An interrupt service routine (ISR) is invoked each
time the timer overflows (goes from 0xFF to 0x00). Inside the
ISR, a variable called overflow_count is incremented by one.
To test this program...
1. Start the debugger.
2. Set a breakpoint on the 'overflow_count++' line in the ISR.
3. Run the program.
Each time the interrupt routine is invoked, the debugger will stop
running the program. Position the cursor over 'overflow_count' to
see its current value.
#include <reg52.h> /* 8051 specific header file */
/*------------------------------------------------
Timer 1 Interrupt Service Routine.
Set a breakpoint on 'overflow_count++' and run the
program in the debugger. You will see this line
executes every 100 clock cycles (or 10,000 Hz).
So, overflow_count is actually a 1/10,000 sec
timer.
------------------------------------------------*/
static unsigned long overflow_count = 0;
void timer1_ISR (void) interrupt 3
{
overflow_count++; /* Increment the overflow count */
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
/*--------------------------------------
Set Timer1 for 8-bit timer with reload
(mode 2). The timer counts to 255,
overflows, is reloaded with 156, and
generates an interrupt.
Set the Timer1 Run control bit.
--------------------------------------*/
TMOD = (TMOD & 0x0F) | 0x20; /* Set Mode (8-bit timer with reload) */
TH1 = 256 - 100; /* Reload TL1 to count 100 clocks */
TL1 = TH1;
ET1 = 1; /* Enable Timer 1 Interrupts */
TR1 = 1; /* Start Timer 1 Running */
EA = 1; /* Global Interrupt Enable */
/*--------------------------------------
Do Nothing. Actually, the timer 1
interrupt will occur every 100 clocks.
Since the oscillator runs at 12 MHz,
the interrupt will happen every 10 KHz.
--------------------------------------*/
while (1)
{
}
}
Similar topics
» 8051: 16 bit timer0 overflow using Interrupt service routine.
» 8051: External interrupt on IO pins
» interrupt-driven button code in CCS C
» 8051 read write port
» interfacing 8051 with JHD12864 and samsung ks0108b glcd 128x64
» 8051: External interrupt on IO pins
» interrupt-driven button code in CCS C
» 8051 read write port
» interfacing 8051 with JHD12864 and samsung ks0108b glcd 128x64
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum