#include <reg52.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0X77,0X7c,0X39,0X5e,0X79,0X71,0x00};
sbit IR=P3^2;
sbit we1=P1^0;
sbit we2=P1^1;
sbit we3=P1^2;
sbit we4=P1^3;
uchar N[4]; //識別碼,數據碼存放
uchar X1,X2;
void XSQ(void);
void YS(uchar time);
/****************** 主函數 *******************/
void main(void)
{
X1=X2=0;
IT0 = 1;
EX0 = 1;
EA = 1;
while(1)
{
XSQ();
}
}
/**********************數碼管顯示函數************************/
void XSQ(void)
{
we1=0;
we2=0;
we3=0;
we4=0;
we1=0;P0=tab[X1];we2=1;YS(1);
we2=0;P0=tab[X2];we1=1;YS(1);
}
/********************** 外部中斷函數************************/
void exint0() interrupt 0
{
uint cnt;
uchar i;
EX0 = 0;
cnt = 0;
while(!IR)
{
cnt++;
} //記錄引導碼時間
if(cnt < 1000)
{EX0=1;return;} //9ms的計數值(12MHz:1000< cnt <1500)
cnt = 0;
while(IR)
{
if(cnt++>400)
{EX0=1;return;}
} //防卡死,超時保護(12MHz: > 300)
if(cnt<200)
{EX0=1;return;}
//(12MHz不分頻: <260)
for(i=0; i<32; i++) //讀取32位位碼
{
cnt = 0;
while(!IR);
while(IR)
if(cnt++ > 200)
{EX0=1;return;} //超時保護(12MHz:>=200)
N[i/8] >>= 1;
if(cnt>60) N[i/8] |= 0x80; //0和1的計數界線(12MHz:< 109)
}
if(N[0] == ~N[1] && N[2] == ~N[3]) //校驗識別碼,數據碼
{
X1 = N[2]/16;
X2 = N[2]%16;
}
EX0 = 1;
}
/***************************************************************
延時程序
1ms×time (晶振=12MHz)
***************************************************************/
void YS(uchar time)
{
uchar i,j;
for(i=0; i<time; i++)
for(j=0; j<247; j++)_nop_();
}
|