已解决
#include "reg51.h" //STC15W204S @12MHz
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
sfr P4 = 0xC0; //1111,1111 端口4
sfr P5 = 0xC8; //xxxx,1111 端口5
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 IE2 = 0xaf; //中断使能寄存器2
sfr AUXR = 0x8e; //辅助寄存器
sfr T2H = 0xD6; //定时器2高8位
sfr T2L = 0xD7; //定时器2低8位
sbit IR_OUT = P2^5; //红外经输出脚,按下时输出高电平
//sbit P32 = P3^2; //P3.2INT0按下时检测唤醒
sbit P24 = P2^4; //按键
sbit P23 = P2^3; //按键
sbit P22 = P2^2; //按键
sbit P21 = P2^1; //按键
sbit P20 = P2^0; //按键
sbit P37 = P3^7; //按键
sbit P36 = P3^6; //按键
sbit P35 = P3^5; //按键
sbit P34 = P3^4; //按键
sbit P33 = P3^3; //按键
sbit P31 = P3^1; //按键
sbit P30 = P3^0; //按键
bit FLAG; //1:上升沿中断 0:下降沿中断
bit ir_flag;
bit key_flag;
unsigned char ir_sys[]={0x7F,0x80};//16位用户码
unsigned char ir_data;
unsigned char ir_code;
unsigned int count;
unsigned int set_count;
void Delayms(uint ms) [url=]//1mS@12.000MHz[/url]
{ unsigned char i, j;
while(ms--){
i = 12;
j = 169;
do
{
while (--j);
} while (--i);}
}
void key_scan()
{
if(!P24){key_flag = 1; ir_data = 0x12;} else
if(!P23){key_flag = 1; ir_data = 0x07;} else
if(!P22){key_flag = 1; ir_data = 0x13;} else
if(!P21){key_flag = 1; ir_data = 0x33;} else
if(!P20){key_flag = 1; ir_data = 0x21;}
}
void ir_sendbyte() //红外发送一个字节数据
{
unsigned char i;
for(i=0;i<8;i++)
{
//-----------------------------------------
set_count = 42;
count = 0;
ir_flag = 1;
TR0 = 1;
while(count<set_count); //固定低电平
ir_flag = 0;
TR0 = 0;
//-----------------------------------------
if(ir_code & 0x01)
{
set_count = 129;
} else
{
set_count = 43;
}
count = 0;
ir_flag = 0;
TR0 = 1;
while(count<set_count);
TR0 = 0;
ir_code = ir_code >> 1;
}
ir_flag = 0;
}
void ir_send()
{
//-----------------------------------------
set_count = 692; //
count = 0;
ir_flag = 1;
TR0 = 1;
while(count<set_count); //固定低电平
TR0 = 0;
//-----------------------------------------
set_count = 346; //
count = 0;
ir_flag = 0;
TR0 = 1;
while(count<set_count); //固定低电平
TR0 = 0;
//-----------------------------------------
ir_code = ir_sys[1];
ir_sendbyte();
ir_code = ir_sys[0];
ir_sendbyte();
ir_code = ir_data;;
ir_sendbyte();
ir_code = ~ir_data;
ir_sendbyte();
//-----------------------------------------
set_count = 42;
count = 0;
ir_flag = 1;
TR0 = 1;
while(count<set_count); //固定低电平
ir_flag = 0;
TR0 = 0;
}
void Timer0Init(void) //13微秒@12MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x64; //设置定时初值
TH0 = 0xFF; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void main(void)
{
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x10;
P5M1 = 0x00;
IR_OUT = 0;
count = 0;
ir_flag = 0;
Timer0Init(); //初始化Timer
TR0 = 0;
ET0 = 1; //使能定时器0中断
IT0 = 1; //设置INT0的中断类型为仅下降沿,下降沿唤醒
EX0 = 1; //使能INT0中断
EA=1;
while(1)
{
PCON = 0x02; //MCU进入掉电模式
key_scan();
if(key_flag)
{
ir_send();
key_flag = 0;
Delayms(100); //防止长重复
}
}
}
void tm0_isr() interrupt 1 using 1 //13微秒@12MHz
{
TL0 = 0x64; //设置定时初值
TH0 = 0xFF; //设置定时初值
count++;
if(ir_flag)
{
IR_OUT =! IR_OUT;
} else IR_OUT=0;
}
void exint0() interrupt 0 //INT0中断入口
{
FLAG = INT0; //保存INT0口的状态, INT0=0(下降沿); INT0=1(上升沿)
}
|