stc15w红外遥控发射解码程序

#include
#include
#define u8 unsigned char
#define u16 unsigned int
#define FOSC 11059200L
#define F36_0KHz (65536-FOSC/2/36000) //1T模式
//#define F38_4KHz (65536-FOSC/2/12/38400) //12T模式

sbit K0=P0^0;
sbit K1=P0^1;
sbit K2=P0^2;
sbit K3=P0^3;

sbit IR_Sout =P3^7;
sbit T1CLKO = P3^4; //定时器1的时钟输出脚

sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;
sfr AUXR = 0x8e; //辅助特殊功能寄存器
sfr INT_CLKO = 0x8f; //唤醒和时钟输出功能寄存器
//u8 Date;
void IR_Send38khz( )//是发射38KHZ的程序 //P34脚
{
AUXR |= 0x40; //定时器1为1T模式// AUXR &= ~0x40; //定时器1为12T模式
TMOD = 0x00; //设置定时器为模式1(16位自动重装载)
TMOD &= ~0x40; //C/T1=0, 对内部时钟进行时钟输出// TMOD |= 0x40; //C/T1=1, 对T1引脚的外部时钟进行时钟输出
TL1 = F36_0KHz; //初始化计时值
TH1 = F36_0KHz >> 8;
TR1 = 1;
INT_CLKO = 0x02; //使能定时器1的时钟输出功能
}

void Delay10us(u16 M) //@11.0592MHz
{
u8 n; u16 m;
for (m=0;m{_nop_();n = 25;while (--n);}
}

void IR_Send_Startcode( )
{IR_Sout=1;Delay10us(900);IR_Sout=0;Delay10us(450);}

void IR_Send_Bit0( )
{IR_Sout=1;Delay10us(56);IR_Sout=0;Delay10us(56);}

void IR_Send_Bit1( )
{IR_Sout=1;Delay10us(56);IR_Sout=0;Delay10us(169);}

void IR_Send_8Bitdate(u8 num)//发射8位数据位
{
u8 i;
for(i=8;i>0;i--)
{
if(num&0x01) IR_Send_Bit1( );//发高
else IR_Send_Bit0( );//发低
num=num>>1;//接着下一位数据&0x01
}
}

/********
void IR_Send_32Bitdate(u8 Address_Date, u8 Date)//发射引导加32BIT(即一帧数据)
{
IR_Send_Startcode( );

IR_Send_8Bitdate(Address_Date);//address
IR_Send_8Bitdate(~Address_Date);//~address

IR_Send_8Bitdate(Date); //
IR_Send_8Bitdate(~Date);//~dat
}
******/
void IR_Send_32Bitdate(u8 Date)//发射引导加32BIT(即一帧数据)
{
IR_Send_Startcode( );

IR_Send_8Bitdate(0x02);//address
IR_Send_8Bitdate(0xfd);//~address

IR_Send_8Bitdate(Date); //
IR_Send_8Bitdate(~Date);//~dat
}

Keyscan( )//按键扫描 //可移植4x4按键程序代替
{
u8 value;
if (K0==0) {value=0x26;}
if (K1==0) {value=0xe2;}
if (K2==0) {value=0x7f;}
if (K3==0) {value=0x5e;}
return(value);
}

void main( )
{
extern u8 Date;
P0M0 = 0x00;P0M1 = 0x00;P1M0 = 0x00;P1M1 = 0x00;P2M0 = 0x00;
P2M1 = 0x00;P4M0 = 0x00;P4M1 = 0x00;P5M0 = 0x00;P5M1 = 0x00;
P3M0 =0x00;P3M1 =0x00;//输出
IR_Send38khz( );
while(1)
{


while(K0==0||K1==0||K2==0||K3==0)
{

Dat

e=Keyscan( );
IR_Send_32Bitdate(Date);
//P3M0 =0x00;P3M1 =0x80;//关闭IO口
}

}

}



///接收代码部分
#include
#include
#define u8 unsigned char
#define u16 unsigned int
#define FOSC 11059200L
sfr AUXR = 0x8e; //辅助特殊功能寄存器
sfr INT_CLKO = 0x8f; //唤醒和时钟输出功能寄存器
sbit LED=P0^0;
u8 IRdate[33];
u8 IRtime;
u8 IRcode[4];
bit IRrecve_ok;
bit Recve32bit_ok;
//bit startflag; u8 Num;

void Timer0_Init( ) //150微秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x85; //设置定时初值
TH0 = 0xF9; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时

ET0=1;
EA=1;
}

void EXInt0_Init( )
{
IT0 = 1; //设置INT0的中断类型 (1:仅下降沿 0:上升沿和下降沿)
EX0 = 1; //使能INT0中断
EA = 1;
}



void IRpros( )
{
u8 i,j,k=1,valve;

for(j=0;j<4;j++)
{

for(i=0;i<8;i++)
{valve=valve>>1;
if((IRdate[k]>10)&&(IRdate[k]<18)){valve=valve|0x80;}
//1.12MS~2.25MS 150usxIRdate
k++;}

IRcode[j]=valve;
}
Recve32bit_ok=1;
}

void main()
{
LED=0;
EXInt0_Init( );
Timer0_Init( );
while(1)
{if(IRrecve_ok) IRpros( );
if(Recve32bit_ok) P2=IRcode[3];}
}

void Time0() interrupt 1
{IRtime++;}

void Exint0() interrupt 0 //INT0中断入口
{
static bit startflag; static u8 Num;
if(startflag)
{
if ((IRtime>60)&&(IRtime<120)) Num=0; //90X150
IRdate[Num]=IRtime;
IRtime=0;
Num++;
if(33==Num){Num=0;IRrecve_ok=1;}
}
else
{startflag=1;IRtime=0;}
}




相关文档
最新文档