PIC16F877单片机定时器1使用中断方式调试程序如下:
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3B31);
void init();
uint intnum;
void main()
{
init();
while(1)
{
/* if(intnum >= 20)
{
intnum = 0;
RD0 = !RD0;
}*/
}
}
void init()
{
TRISD = 0; //
PORTD = 0; //PORTD输出低电平
INTCON = 0xC0;//开总中断、定时器1中断
TMR1H = (65536-50000)/256;//预装初值50ms*4
TMR1L = (65536-50000)%256;
PIE1 = 0x01; //开定时器1中断
T1CON = 0x21; //1:4分频,使能定时器1开始计数
/* TRISD=0;
PORTD=0;
INTCON=0xc0;
PIE1=0x01;
TMR1L=(65536-50000)%256;
TMR1H=(65536-50000)/256;
T1CON=0x21;*/
}
void interrupt time1()
{
TMR1IF=0; //定时器1溢出位清0
TMR1L=(65536-50000)%256;
TMR1H=(65536-50000)/256;
//RD0=!RD0;//intnum++;
if( ++intnum >= 20)
{
intnum = 0;
RD0 = !RD0;
}
}
总结:跟51定时器1相似。