|
单片机解2262无线电遥控码 成功后用串口输出
作者 rewtewer
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit p00=P0^0;
sbit p01=P0^1;
sbit p02=P0^2;
sbit p03=P0^3;
sbit p04=P0^4;
sbit p05=P0^5;
sbit p06=P0^6;
sbit p07=P0^7;
sbit p32=P3^2;
sbit p34=P3^4;
sbit p35=P3^5;
sbit p36=P3^6;
sbit p37=P3^7;
sbit p11=P1^1;//继电器输出;
sbit p12=P1^2;//蜂鸣器;
bit ts1;//秒时钟;
bit tms100;//100毫秒时钟;
bit uf=0;//正在解码中标志位;
bit b;//数据码接收正确并收完成标志;
bit bw1,bw2,bw3,bw4,bw10,bw11;
uchar a1;//接收位计数;
uchar *p;
uint setdata;
uint address1;//数据帧地址;
uint dataint1;//数据帧数据;
uint datatemp[4];
void delay(uint a)
{
while(a--);
}
void suma(uint a,uchar b)//数码管显示(动态扫描);
{ //a为要显示的数,B为要显示位的位数;
static uchar b1,b2;
uchar code sumadata[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
if(a>9)b2=1;
else b2=0;
if(a>99)b2=2;
if(a>999)b2=3;
if(b1>b2)b1=0;
if(b1>=b)b1=0;
P1|=0xf0;
if(b1==0)P2=sumadata[a%10];
if(b1==1)P2=sumadata[a%100/10];
if(b1==2)P2=sumadata[a%1000/100];
if(b1==3)P2=sumadata[a/1000];
if(b)P1&=~(0X80>>b1);
b1++;
}
void int0init()//外部中断初始化;
{
IT0=1;
EX0=1;
EA=1;
uf=0;
}
void timer0init()//定时器0初始化;
{
TMOD=0X09;
TR0=1;
}
void time1init()//定时器1初始化;
{
TMOD&=0X0F;
TMOD|=0X10;
TH1=0X4B;
TL1=0XFF;
TR1=1;
ET1=1;
EA=1;
}
/////////////////////////////////////////////////////////////////////
void int0() interrupt 0 //外部中断处理;
{
uint j;
p05=!p05;
j=TH0;
j=j<<8;
j|=TL0;
TR0=0;
TH0=0 ;
TL0=0;
TR0=1;
if(!uf)
{
if(!TF0)
{
if((j<14000)&&(j>10000))
{
uf=1,a1=0,p01=!p01;
datatemp[0]=0;
datatemp[1]=0;
datatemp[2]=0;
datatemp[3]=0;
}
}
else TF0=0,uf=0,a1=0;
}
else
{
if(a1<64)
{
if((j>200)&&(j<850))//判断窄脉冲;
{
p02=!p02;
datatemp[a1/16]|=0x01<<(a1%16);
a1++;
}
else
{
if(j>900&&j<2250) //判断宽脉冲;
{
p03=!p03;
a1++;
}
else
{
p07=!p07;
a1=0;
uf=0;
}
}
}
}
if(a1>=64)
{
if((datatemp[0]==~datatemp[1])&&(datatemp[2]==~datatemp[3]))
{
address1=datatemp[0];
dataint1=datatemp[2];
b=1;
}
p00=!p00;
uf=0;
a1=0;
//EA=0;
}
}
////////////////////////////////////////////////////////////////
void usartinit() //串行通信初始化;
{
/*RCAP2H=(65535-12000000/9600/16 )/256;
RCAP2L=(65535-12000000/9600/16 )&0xFF;
T2CON=0x34; */
RCAP2H=(65535-11059200/9600/16 )/256;
RCAP2L=(65535-11059200/9600/16 )&0xFF;
T2CON=0x34;
TR2=1;
SM0=0;
SM1=1;
REN=1;
SM2=1;
//ES=1;
// EA=1;
}
void time1() interrupt 3 //定时器1中断函数,用于系统时钟;
{
static uchar a;
TH1=0X4B;
TL1=0XFF;
if(a>=20)a=0;
if(a<10)ts1=1;
else ts1=0;
a++;
tms100=!tms100;
bw10=1;
}
void main(void)
{
bit x1,x2;
uchar f,fw1,fw2,fs=4;
IP=0X01;
IPH=0X01;//设定外部中断0为最高优先级别;
usartinit();
timer0init();
int0init();
time1init();
while(1)
{
if(b)
{
if(f<2)p=&address1;
else p=&dataint1;
if(f==0)SBUF=p[0],f++;
else
{
if(TI==1)
{
TI=0;
if(f==1)SBUF=p[1];
if(f==2)SBUF=p[0];
if(f==3)SBUF=p[1];
if(f>=4)
{
f=0,b=0;EA=1;
}
f++;
}
}
}
if(!b)EA=1,f=0;
delay(100);
if(bw2)
{
if(ts1)fs=4;
else fs=0;
suma(setdata,fs);
}
else fs=4,suma(dataint1,fs);
if(!p35&&!bw1&&!bw2)
{
delay(4000);
if(!p35)bw1=1,bw2=1;
}
if(!p35&&!bw1&&bw2)
{
delay(4000);
if(!p35)bw2=0,bw1=1;
}
if(p35)bw1=0;
if(bw2)
{
if(!p36&&!bw3)
{
delay(2000);
if(!p36)
{
if(setdata<9999)setdata++,bw3=1;
}
}
if(p36)bw3=0,fw1=0;
if(!p37&&!bw4)
{
delay(2000);
if(!p37)
{
if(setdata>0)setdata--,bw4=1;
}
}
if(p37)bw4=0,fw2=0;
if(bw3&&bw11)
{
if(fw1<40)fw1++;
else
{
if(setdata<9999)setdata++;
}
}
if(bw4&&bw11)
{
if(fw2<40)fw2++;
else
{
if(setdata>0)setdata--;
}
}
}
bw11=0;
if(dataint1>=setdata&&!x1&&x2)x1=1;
if(dataint1<setdata)x1=0,x2=1;
if(!p34)
{
delay(2000);
if(!p34)x1=0,x2=0;
}
if(x1)
{
if(ts1)p11=0,p12=1;
else p11=1,p12=0;
}
else p11=1,p12=1;
if(bw10)bw11=1;bw10=0;
if(TF0)uf=0;
}
}
|
|