以下是源代码 现在公开开源
如果有任何疑问,可以联系我
#include<pic.h>
__config(0x3b32);
#define uchar unsigned char
#define uint unsigned int
#define s1 rc4
#define s2 rc5
#define nop() asm("nop")
void io_reset();
void int_reset();
void delay(uint x);
const uchar table[]={0xd7,0x11,0xcd,0x5d,0x1b,0x5e,0xde,0x15,0xdf,0x5f};
void dsp0(uchar num1,uchar num2,uchar num3);
uint cunt,cunt_temp,mpy,dev;
uchar flg_t1,ge,shi,bai,cunt_time,cunt_h;//显示代码
void main()
{
io_reset();
int_reset();
cunt_time=0x0a;
cunt_time=0x0a;//2s延时寄存器赋值,除法赋值;
while(!s1);
delay(10);
while(!s1);
delay(10);
while(1)
{ if(s1==1)
{
flg_t1=0x02;//开cunt和cunt_h计数
pie1=0x05;//开ccp中断和定时器1中断
}
bai=cunt/100;
shi=cunt%100/10;
ge=cunt%10/1;
dsp0(bai,shi,ge);//显示程序0
if(s2==1)
{
while(1)
{
pie1=0;//关ccp1中断
rd7=0;//点红灯
cunt_temp=0;
flg_t1=0x01;//关cunt_h计数
cunt=0;
bai=0;
shi=0;
ge=0;
mpy=0;
dev=0;
pie1=0x01;//开tmr1中断
if(s1==1)
{
rd6=0;//点绿灯
while(1)
{
pie1=0x05;//开ccp1和tmr1中断
tmr1on=1;//定时器1使能
flg_t1=1;//软中断使能
while(!cunt_time)
{
pie1=0x00;
while(1)
{
mpy=cunt*30;
dev=mpy/cunt_h;
bai=dev/100;
shi=dev%100/10;
ge=dev%10/1;
dsp0(bai,shi,ge);//显示程序0
}
}
}
}
}
}
}
}
void io_reset()
{
trisc=0xff;
trisb=0;
trisd=0;
trise=0;
portb=0;
portd=0;
porte=0;
rd6=1;//关绿灯
rd7=1;//关红灯
}
void int_reset()
{
intcon=0xc0;//开总中断,开第二梯队中断
pie1=0x00;//关中断
ccp1con=0x05;//设置ccp模式,每一个上升沿+1
pir1=0;//清中断标志
t1con=0x34;//分频比1:8,内部时钟,不同步
tmr1l=0;//清计数器低位
tmr1h=0;//清计数器高位
}
void delay(uint x)
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void dsp0(uchar num1,uchar num2,uchar num3)
{
portb=table[num1];//显示第一个数码管
porte=0x04;
delay(2);
portb=table[num2];//显示第二个数码管
porte=0x01;
delay(2);
portb=table[num3];//显示第三个数码管
porte=0x02;
delay(2);
}
void interrupt time0()
{
if(ccp1if==1)
{
cunt++;
ccp1if=0;
if(flg_t1==2)
cunt_h++;
}
if(flg_t1==1)
{
if(tmr1if==1)
{
tmr1if=0;
cunt_time--;
}
}
ccp1if=0;
tmr1if=0;
}