timer0.c
来自「PIC单片机的定时器使用的demo程序.内附说明.」· C语言 代码 · 共 45 行
C
45 行
#include <pic.h>
/*
* Example code for using timer0 on a 17C756
* Just sets up a 1 second interrupt and increments a variable
*/
/*
* Calculate preload value for one second timer
*/
#define PERIOD 1000000 // period in uS - one second here
#define XTAL 4000000 // crystal frequency - 4MHz
#define ICLK (XTAL/4) // crystal is divided by four
#define SCALE 16 // prescale by 16 - check for overflow!
#define PRELOAD PERIOD*ICLK/SCALE/1000000
unsigned long seconds; // second count
/* service routine for timer 0 interrupt */
void interrupt
timer0_isr(void) @ 0x10
{
seconds++;
TMR0 += -PRELOAD; // re-load timer
// no need to clear T0IF - the hardware did it
}
main()
{
// initialize timer 0;
T0STA = 0b01000; // prescale by 16
T0CS = 1; // select internal clock
TMR0 = -PRELOAD; // preload timer
T0IE = 1; // enable timer interrupt
GLINTD = 0; // enable global interrupts
for(;;)
continue; // let interrupt do its job
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?