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相似。