📄 发射.c
字号:
#include <stc89.h>
//13.37589
#define uchar unsigned char
#define uint unsigned int
sbit led=P3^7;
sbit out=P3^6;
#define port P2
//定义地址码
#define addr0 0x01
#define addr1 0x02
bit key_bit; //按键重复标记
bit hw;
bit over;
bit ss;
uchar time;
void delay();
void InitTimer0(void);
void InitUART(void);
void send(uchar c);
uchar key();
void send_star();
void send_char(uchar c);
void send_hw(uchar c);
void delay1s(void) //误差 -0.000000000003us
{
unsigned char a,b,c;
for(c=45;c>0;c--)
for(b=132;b>0;b--)
for(a=167;a>0;a--);
}
void delay() //1ms 误差 -0.651041666667us
{
unsigned char a,b;
for(b=102;b>0;b--)
for(a=3;a>0;a--);
}
void InitTimer0(void)
{
TMOD = 0x22;
TH0 = 0xe6;
TL0 = 0xe6;
EA = 1;
ET0 = 1;
TR0 = 1;
} //13us
void InitUART(void) //4800
{
// TMOD = 0x20;
SCON = 0x50; //采用串口工作方式1(8位异步收发,波特率可变);允许串行口接收数据
TH1 = 0xF3;
TL1 = TH1;
PCON = 0x00;
ES = 1;
TR1 = 1;
}
void send(uchar c)
{
SBUF = c;
while(!TI);//一直发送数据,直至发送完毕
TI = 0;
}
uchar key()
{
uchar a;
a=0;
port=0x0f;
a=port;
if(a==0x0f)
{
key_bit=1;
delay();
return 0;
}
if(!key_bit) return 0;
delay();
if(port==a)
{
port=0xf0;
a|=port;
a=~a;
}
else return 0;
if(a)
{
key_bit=0;
switch(a)
{
case 0x11: return 1;
case 0x12: return 2;
case 0x14: return 3;
case 0x18: return 4;
case 0x21: return 5;
case 0x22: return 6;
case 0x24: return 7;
case 0x28: return 8;
case 0x41: return 9;
case 0x42: return 10;
case 0x44: return 11;
case 0x48: return 12;
case 0x81: return 13;
case 0x82: return 14;
case 0x84: return 15;
case 0x88: return 16;
default: return 0;
}
}// if(a)
}
void main(void)
{
uchar c;
InitTimer0();
InitUART();
led=0;
send(0xaa); //为了测试串口是否正常工作
while(1)
{
c=key();
if(c)
{
send(c);
send_hw(c);
}
}
}
void send_star()
{
uchar i;
i=8;
/*9ms内发射38KHZ方波*/
while(i--)
{
time=173; //9ms
hw=1; //发射
while(!over);
}
/*4.5ms内发射高电平*/
i=4;
while(i--) //4.5ms
{
time=173;
hw=0;
while(!over);
}
}
void send_char(uchar c)
{
uchar i;
for(i=0;i<8;i++)
{
time=45;
hw=1; //0.565ms的38KHZ方波
while(!over);
if(c&0x80) time=126; //1.685ms的间隔
else time=45; //0.565ms的间隔
hw=0;
c=c<<1;
while(!over);
time=45;
hw=1;
}
}
void send_hw(uchar c)
{
key_bit=0;
send_star();
send_char(addr0);
send_char(addr1);
send_char(c);
send_char(~c);
}
void Timer0Interrupt(void) interrupt 1
{
if(time) time--,over=0;
else over=1,hw=0;
if(hw) out=~out;
else out=1;
}
void UARTInterrupt(void) interrupt 4
{
uchar c;
if(RI)
{
RI = 0; //接收中断标志位;RI=1,表示数据接收完毕,并申请中断,要求CPU从接收SBUF提走数据,
/* c=SBUF; SBUF是独立的接收发送缓冲器,可同时发送、接收数据,必须由软件清零
if(c==0xaa) ya
{
delay1s();
ISP_CONTR=0x60;
} 为了方便下载*/
}
else
TI = 0; //发送中断标志位;TI=1,表示一帧数据发送结束;TI必须由软件清零
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -