16F877A有三个定时器,中断方式都调试出来了,想把它们整合到一块同时工作,可搞了半天,就是不干。最后整明白了,PIC16系列单片机的中断入口中有一个。PIC的功能是很强大,外围模块也多,速度也较快,但PIC的这个中断方案不好,应该改进。以下是三个定时中断同时工作的调试代码。
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3B31);
void init();
uint Timer0Num,Timer1Num,Timer2Num;//
void timer0();
void timer1();
void timer2();
void main()
{
init();
while(1)
{
if(Timer0Num>=20)
{
Timer0Num = 0;
RD0 = ~RD0;
}
if(Timer1Num >= 4)
{
Timer1Num = 0;
RD2 = !RD2;
}
if(Timer2Num >= 50)
{
Timer2Num = 0;
RD4 = !RD4;
}
}
}
void init(void)
{
TRISD = 0;
PORTD = 0;
OPTION = 0x07;//256分频
TMR0 = 56;
PIE1 |= 0x01 | 0x02;
TMR1H = (65536-50000)/256;
TMR1L = (65536-50000)%256;
T1CON = 0x21;//4分频
TMR2 = 0;
PR2 = 100;
T2CON = 0x1D;
INTCON = 0xE0;
}
void interrupt Ints()
{
if(T0IE&&T0IF) timer0();
if(TMR1IE&&TMR1IF) timer1();
if(TMR2IE&&TMR2IF) timer2();
}
void timer0()
{
T0IF = 0;
TMR0 = 56;
Timer0Num++;
}
void timer1()
{
TMR1IF = 0;
TMR1H = (65536-50000)/256;
TMR1L = (65536-50000)%256;
Timer1Num++;
}
void timer2()
{
TMR2IF = 0;
TMR2 = 0;
PR2 = 200;
Timer2Num++;
}
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3B31);
void init();
uint Timer0Num,Timer1Num,Timer2Num;//
void timer0();
void timer1();
void timer2();
void main()
{
init();
while(1)
{
if(Timer0Num>=20)
{
Timer0Num = 0;
RD0 = ~RD0;
}
if(Timer1Num >= 4)
{
Timer1Num = 0;
RD2 = !RD2;
}
if(Timer2Num >= 50)
{
Timer2Num = 0;
RD4 = !RD4;
}
}
}
void init(void)
{
TRISD = 0;
PORTD = 0;
OPTION = 0x07;//256分频
TMR0 = 56;
PIE1 |= 0x01 | 0x02;
TMR1H = (65536-50000)/256;
TMR1L = (65536-50000)%256;
T1CON = 0x21;//4分频
TMR2 = 0;
PR2 = 100;
T2CON = 0x1D;
INTCON = 0xE0;
}
void interrupt Ints()
{
if(T0IE&&T0IF) timer0();
if(TMR1IE&&TMR1IF) timer1();
if(TMR2IE&&TMR2IF) timer2();
}
void timer0()
{
T0IF = 0;
TMR0 = 56;
Timer0Num++;
}
void timer1()
{
TMR1IF = 0;
TMR1H = (65536-50000)/256;
TMR1L = (65536-50000)%256;
Timer1Num++;
}
void timer2()
{
TMR2IF = 0;
TMR2 = 0;
PR2 = 200;
Timer2Num++;
}