8051: 16 bit timer0 overflow using Interrupt service routine.
Page 1 of 1
8051: 16 bit timer0 overflow using Interrupt service routine.
This example program shows how to configure timer/counter 0 as a
16-bit timer. An interrupt service routine (ISR) is invoked each
time the timer overflows (goes from 0xFFFF to 0x0000). 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.
/* Hardware specific header file*/
#include <reg52.h>
/*------------------------------------------------
Timer 0 Interrupt Service Routine.
Set a breakpoint on 'overflow_count++' and run the
program in the debugger. You will see this line
executes every 65536 clock cycles.
------------------------------------------------*/
static unsigned long overflow_count = 0;
void timer0_ISR (void) interrupt 1
{
overflow_count++; /* Increment the overflow count */
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
/*--------------------------------------
Set Timer0 for 16-bit timer mode. The
timer counts to 65535, overflows, and
generates an interrupt.
Set the Timer0 Run control bit.
--------------------------------------*/
TMOD = (TMOD & 0xF0) | 0x01; /* Set T/C0 Mode */
ET0 = 1; /* Enable Timer 0 Interrupts */
TR0 = 1; /* Start Timer 0 Running */
EA = 1; /* Global Interrupt Enable */
/*--------------------------------------
Do Nothing. Actually, the timer 0
interrupt will occur every 65536 clocks.
--------------------------------------*/
while (1)
{
}
}
16-bit timer. An interrupt service routine (ISR) is invoked each
time the timer overflows (goes from 0xFFFF to 0x0000). 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.
/* Hardware specific header file*/
#include <reg52.h>
/*------------------------------------------------
Timer 0 Interrupt Service Routine.
Set a breakpoint on 'overflow_count++' and run the
program in the debugger. You will see this line
executes every 65536 clock cycles.
------------------------------------------------*/
static unsigned long overflow_count = 0;
void timer0_ISR (void) interrupt 1
{
overflow_count++; /* Increment the overflow count */
}
/*------------------------------------------------
MAIN C function
------------------------------------------------*/
void main (void)
{
/*--------------------------------------
Set Timer0 for 16-bit timer mode. The
timer counts to 65535, overflows, and
generates an interrupt.
Set the Timer0 Run control bit.
--------------------------------------*/
TMOD = (TMOD & 0xF0) | 0x01; /* Set T/C0 Mode */
ET0 = 1; /* Enable Timer 0 Interrupts */
TR0 = 1; /* Start Timer 0 Running */
EA = 1; /* Global Interrupt Enable */
/*--------------------------------------
Do Nothing. Actually, the timer 0
interrupt will occur every 65536 clocks.
--------------------------------------*/
while (1)
{
}
}
Similar topics
» 8051 : 8 bit Timer0 overflow using Interrupt
» 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