2个回答
展开全部
#include<reg51.h>
#include<intrins.h>
#define NOP _nop_()
#define KEY_PRESS
#include "WIN24C02.H"
unsigned char code table[]={0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09,0x11,0xC1,0x63,0x85,0x61,0x71};
///////////////////////////////
#define uchar unsigned char
uchar times=0;
uchar dis_buf[4]={0};
uchar key_flag=0;
uchar key_cnt=0;
uchar exint_flag=0;
uchar buzzer_cnt=0;
uchar int_flag=0;
unsigned int int_cnt=0;
///////////////////////////////////
sbit ir_Induction=P1^0;
sbit ming=P3^7;
sbit ir_s=P3^2;
sbit iic_sda=P0^5;
sbit iic_clk=P0^4;
unsigned int ir_num=0;
sfr isp_wdt = 0xE1;
sfr isp_data = 0xE2;
sfr isp_addrh = 0xE3;
sfr isp_addrl = 0xE4;
sfr isp_cmd = 0xE5;
sfr isp_trig = 0xE6;
sfr isp_contr = 0xE7;
/*****************************************/
/***************STC89CXX_EEPROM扇区擦除程序*************************/
void STC89CXX_EEPROM_Erase(unsigned char t_addr)
{
isp_addrh = t_addr; //可以不要 扇区里任意一个字节的地址都是扇区地址,无需求首地址,单片机会自己处理
isp_addrl = 0;
// EA = 0; //关中断
isp_contr = 0x81;
isp_cmd = 3; //扇区擦除,要某字节为空,必须擦除1个扇区
isp_trig = 0x46; //先送0x46再送 0xb9 ISP/IAP 触发寄存器,每次都需要如此
isp_trig = 0xb9; //送完0xb9后 ISP/IAP 立即被触发启动
// EA = 1;
// 开中断
}
/***********************************************/
void STC89CXX_EEPROM_Char_Write(unsigned int Write_addr ,unsigned int Write_data)
{
isp_data = (Write_data>>8)&0xff;
isp_addrh = Write_addr/256; //送地址高位
isp_addrl = Write_addr%256; //地址低位
// EA = 0; /* 关中断 */
isp_contr = 0x81;
isp_cmd = 2; //送扇区命令
isp_trig = 0x46;
isp_trig = 0xb9;
isp_data = Write_data&0xff;
isp_addrl = (Write_addr+1)%256;
isp_contr = 0x81;
isp_cmd = 2; //送扇区命令
isp_trig = 0x46;
isp_trig = 0xb9;
// EA = 1;
/* 开中断 */
}
/**************************************************************/
unsigned int STC89CXX_EEPROM_Char_Read(unsigned int Read_addr)
{
unsigned int temp=0;
isp_addrh = Read_addr/256; //送地址高字节
isp_addrl = Read_addr%256; //送地址低字节(从0开始到num-1为止)
// EA = 0; /* 关中断 */
isp_contr = 0x81; /*20M,是0x80 orl 1 */
isp_cmd = 1; // 1表示字节读
isp_trig = 0x46; //先送0x46再送 0xb9 ISP/IAP 触发寄存器,每次都需要如此
isp_trig = 0xb9; //送完0xb9后 ISP/IAP 立即被触发启动
temp= (isp_data<<8);
isp_addrl = (Read_addr+1)%256;
isp_contr = 0x81; /*20M,是0x80 orl 1 */
isp_cmd = 1; // 1表示字节读
isp_trig = 0x46; //先送0x46再送 0xb9 ISP/IAP 触发寄存器,每次都需要如此
isp_trig = 0xb9;
temp|=isp_data;
// EA = 1; //空指令
/* 开中断 */
return(temp);
}
static void iic_start()
{
iic_sda=1;
iic_clk=1;
NOP;
iic_sda=0;
iic_clk=0;
}
static void iic_stop()
{
iic_clk=0;
iic_sda=0;
iic_clk=1;
NOP;
iic_sda=1;
}
static void iic_write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
iic_clk=0;
iic_sda=dat&0x80;
NOP;
iic_clk=1;
NOP;
dat<<=1;
}
iic_clk=0;
}
static uchar recive_ack()
{
uchar i=0;
iic_clk=0;
NOP;
//iic_sda=1;
NOP;
iic_clk=1;
NOP;
NOP;
while(iic_sda);
iic_clk=0;
// if(i==200)
// return 0 ;
//else
return 1;
}
static void send_ack()
{
uchar i=0;
iic_sda=1;
NOP;
iic_clk=0;
NOP;
iic_sda=0;
NOP;
iic_clk=1;
NOP;
iic_clk=0;
iic_sda=1;
}
void iic_write(uchar addr,uchar dat)
{
iic_start();
iic_write_byte(0xa0);
recive_ack();
iic_write_byte(addr);
recive_ack();
iic_write_byte(dat);
recive_ack();
iic_stop();
}
static uchar iic_read_byte()
{
uchar i;
uchar dat=0;
for(i=0;i<8;i++)
{
// iic_sda=0;
dat<<=1;
iic_clk=0;
NOP;
iic_clk=1;
NOP;
dat|=iic_sda;
}
iic_clk=0;
return dat;
}
uchar iic_read(uchar addr)
{
uchar dat;
iic_start();
iic_write_byte(0xa0);
recive_ack();
iic_write_byte(addr);
recive_ack();
iic_start();
iic_write_byte(0xa1);
recive_ack();
dat=iic_read_byte();
iic_stop();
return dat;
}
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
{
for(y=100;y>0;y--);
}
}
void iic_writes(uchar addr,void *buf,uchar num)
{
uchar i;
uchar *pt=(uchar *)buf;
for(i=0;i<num;i++)
{
iic_write(addr+i,pt[i]);
delay(10);
}
}
void iic_reads(uchar addr,void *buf,uchar num)
{
uchar i;
uchar *pt=(uchar *)buf;
for(i=0;i<num;i++)
{
pt[i]=iic_read(addr+i);
//delay(3);
}
}
sbit KEY1=P3^6;
unsigned char i_com=0x88;
//unsigned char flag=0;
void timer0_init()
{
TMOD=0x11;
TH0=0xfd;
TL0=0x80;
ET0=1;
TR0=1;
TH1=0xfd;
TL1=0x80;
ET1=1;
TR1=1;
}
void ch_value( )
{
dis_buf[0]=(ir_num/1000)%10;
dis_buf[1]=(ir_num/100)%10;
dis_buf[2]=(ir_num/10)%10;
dis_buf[3]=(ir_num)%10;
}
void timer0_isq()interrupt 1
{
TH0=0xf8;
TL0=0x84;
P2=0xff;
P0= (8>>(times));
P2=table[dis_buf[times]];
times=++times%4;
}
void display()
{
P2=0xff;
P0= (i_com>>(0));
P2=table[dis_buf[0]];
delay(3);
P2=0xff;
P0= (i_com>>(1));
P2=table[dis_buf[1]];
delay(3);
P2=0xff;
P0= (i_com>>(2));
P2=table[dis_buf[2]];
delay(3);
P2=0xff;
P0= (i_com>>(3));
P2=table[dis_buf[3]];
delay(3);
}
void int_0() interrupt 0
{
// EA=0;
//display();
exint_flag=1;
int_flag=1;
EX0=0;
ir_num++;
#ifdef KEY_PRESS
// IE0=0;
#endif
// EA=1;
}
void timer1_isq()interrupt 3
{
TH1=0xfa;
TL1=0x84;
if(key_flag&&(++key_cnt==10))
{
if(KEY1)
{
key_flag=0;
}
key_cnt=0;
}
if(int_flag&&(++int_cnt==20))
{
#ifdef KEY_PRESS
if(ir_s)
#else
if(!ir_s)
#endif
{
int_flag=0;
}
int_cnt=0;
}
if(exint_flag)
{
ming=0;
}
if(exint_flag&&(++buzzer_cnt==40))
{
ming=1;
buzzer_cnt=0;
exint_flag=0;
}
}
void main()
{
delay(500);
timer0_init();
WIN24C02_init(); //24c02初始化子程序
EX0=1;
IT0=1;
IE0=0;
EA=1;
PT0=1;
// ir_num=STC89CXX_EEPROM_Char_Read(0x2000);
iic_reads(0x10,&ir_num,2);
//ir_num=WIN24C02_read(0x00)|(WIN24C02_read(0x01)<<8);
ch_value();
while(1)
{
if(ir_s==1)
{
ir_s=0;
}
ch_value();
//display();
if(!EX0)
{
if(ir_num>=9999)
ir_num=0;
iic_writes(0x10,&ir_num,2);
while(int_flag&&KEY1);//display();
//STC89CXX_EEPROM_Erase(0x20);
//STC89CXX_EEPROM_Char_Write(0x2000,ir_num);//存数据到EEPROM
// WIN24C02_write(0x00,ir_num&0xff);
// WIN24C02_write(0x01,(ir_num>>8)&0xff);
//IE0=0;
ch_value();
#ifdef KEY_PRESS
IE0=0;
#else
IE0=0;
#endif
EX0=1;
}
if(!KEY1)
{
// display();
EX0=0;
ir_num=0;
//STC89CXX_EEPROM_Erase(0x20);
//STC89CXX_EEPROM_Char_Write(0x2000,ir_num);//存数据到EEPROM
iic_writes(0x10,&ir_num,2);
// WIN24C02_write(0x00,ir_num&0xff);
// WIN24C02_write(0x01,(ir_num>>8)&0xff);
ch_value();
// EA=1;
while(!KEY1);
IE0=0;
EX0=1;
}
}
}
#include<intrins.h>
#define NOP _nop_()
#define KEY_PRESS
#include "WIN24C02.H"
unsigned char code table[]={0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09,0x11,0xC1,0x63,0x85,0x61,0x71};
///////////////////////////////
#define uchar unsigned char
uchar times=0;
uchar dis_buf[4]={0};
uchar key_flag=0;
uchar key_cnt=0;
uchar exint_flag=0;
uchar buzzer_cnt=0;
uchar int_flag=0;
unsigned int int_cnt=0;
///////////////////////////////////
sbit ir_Induction=P1^0;
sbit ming=P3^7;
sbit ir_s=P3^2;
sbit iic_sda=P0^5;
sbit iic_clk=P0^4;
unsigned int ir_num=0;
sfr isp_wdt = 0xE1;
sfr isp_data = 0xE2;
sfr isp_addrh = 0xE3;
sfr isp_addrl = 0xE4;
sfr isp_cmd = 0xE5;
sfr isp_trig = 0xE6;
sfr isp_contr = 0xE7;
/*****************************************/
/***************STC89CXX_EEPROM扇区擦除程序*************************/
void STC89CXX_EEPROM_Erase(unsigned char t_addr)
{
isp_addrh = t_addr; //可以不要 扇区里任意一个字节的地址都是扇区地址,无需求首地址,单片机会自己处理
isp_addrl = 0;
// EA = 0; //关中断
isp_contr = 0x81;
isp_cmd = 3; //扇区擦除,要某字节为空,必须擦除1个扇区
isp_trig = 0x46; //先送0x46再送 0xb9 ISP/IAP 触发寄存器,每次都需要如此
isp_trig = 0xb9; //送完0xb9后 ISP/IAP 立即被触发启动
// EA = 1;
// 开中断
}
/***********************************************/
void STC89CXX_EEPROM_Char_Write(unsigned int Write_addr ,unsigned int Write_data)
{
isp_data = (Write_data>>8)&0xff;
isp_addrh = Write_addr/256; //送地址高位
isp_addrl = Write_addr%256; //地址低位
// EA = 0; /* 关中断 */
isp_contr = 0x81;
isp_cmd = 2; //送扇区命令
isp_trig = 0x46;
isp_trig = 0xb9;
isp_data = Write_data&0xff;
isp_addrl = (Write_addr+1)%256;
isp_contr = 0x81;
isp_cmd = 2; //送扇区命令
isp_trig = 0x46;
isp_trig = 0xb9;
// EA = 1;
/* 开中断 */
}
/**************************************************************/
unsigned int STC89CXX_EEPROM_Char_Read(unsigned int Read_addr)
{
unsigned int temp=0;
isp_addrh = Read_addr/256; //送地址高字节
isp_addrl = Read_addr%256; //送地址低字节(从0开始到num-1为止)
// EA = 0; /* 关中断 */
isp_contr = 0x81; /*20M,是0x80 orl 1 */
isp_cmd = 1; // 1表示字节读
isp_trig = 0x46; //先送0x46再送 0xb9 ISP/IAP 触发寄存器,每次都需要如此
isp_trig = 0xb9; //送完0xb9后 ISP/IAP 立即被触发启动
temp= (isp_data<<8);
isp_addrl = (Read_addr+1)%256;
isp_contr = 0x81; /*20M,是0x80 orl 1 */
isp_cmd = 1; // 1表示字节读
isp_trig = 0x46; //先送0x46再送 0xb9 ISP/IAP 触发寄存器,每次都需要如此
isp_trig = 0xb9;
temp|=isp_data;
// EA = 1; //空指令
/* 开中断 */
return(temp);
}
static void iic_start()
{
iic_sda=1;
iic_clk=1;
NOP;
iic_sda=0;
iic_clk=0;
}
static void iic_stop()
{
iic_clk=0;
iic_sda=0;
iic_clk=1;
NOP;
iic_sda=1;
}
static void iic_write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
iic_clk=0;
iic_sda=dat&0x80;
NOP;
iic_clk=1;
NOP;
dat<<=1;
}
iic_clk=0;
}
static uchar recive_ack()
{
uchar i=0;
iic_clk=0;
NOP;
//iic_sda=1;
NOP;
iic_clk=1;
NOP;
NOP;
while(iic_sda);
iic_clk=0;
// if(i==200)
// return 0 ;
//else
return 1;
}
static void send_ack()
{
uchar i=0;
iic_sda=1;
NOP;
iic_clk=0;
NOP;
iic_sda=0;
NOP;
iic_clk=1;
NOP;
iic_clk=0;
iic_sda=1;
}
void iic_write(uchar addr,uchar dat)
{
iic_start();
iic_write_byte(0xa0);
recive_ack();
iic_write_byte(addr);
recive_ack();
iic_write_byte(dat);
recive_ack();
iic_stop();
}
static uchar iic_read_byte()
{
uchar i;
uchar dat=0;
for(i=0;i<8;i++)
{
// iic_sda=0;
dat<<=1;
iic_clk=0;
NOP;
iic_clk=1;
NOP;
dat|=iic_sda;
}
iic_clk=0;
return dat;
}
uchar iic_read(uchar addr)
{
uchar dat;
iic_start();
iic_write_byte(0xa0);
recive_ack();
iic_write_byte(addr);
recive_ack();
iic_start();
iic_write_byte(0xa1);
recive_ack();
dat=iic_read_byte();
iic_stop();
return dat;
}
void delay(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
{
for(y=100;y>0;y--);
}
}
void iic_writes(uchar addr,void *buf,uchar num)
{
uchar i;
uchar *pt=(uchar *)buf;
for(i=0;i<num;i++)
{
iic_write(addr+i,pt[i]);
delay(10);
}
}
void iic_reads(uchar addr,void *buf,uchar num)
{
uchar i;
uchar *pt=(uchar *)buf;
for(i=0;i<num;i++)
{
pt[i]=iic_read(addr+i);
//delay(3);
}
}
sbit KEY1=P3^6;
unsigned char i_com=0x88;
//unsigned char flag=0;
void timer0_init()
{
TMOD=0x11;
TH0=0xfd;
TL0=0x80;
ET0=1;
TR0=1;
TH1=0xfd;
TL1=0x80;
ET1=1;
TR1=1;
}
void ch_value( )
{
dis_buf[0]=(ir_num/1000)%10;
dis_buf[1]=(ir_num/100)%10;
dis_buf[2]=(ir_num/10)%10;
dis_buf[3]=(ir_num)%10;
}
void timer0_isq()interrupt 1
{
TH0=0xf8;
TL0=0x84;
P2=0xff;
P0= (8>>(times));
P2=table[dis_buf[times]];
times=++times%4;
}
void display()
{
P2=0xff;
P0= (i_com>>(0));
P2=table[dis_buf[0]];
delay(3);
P2=0xff;
P0= (i_com>>(1));
P2=table[dis_buf[1]];
delay(3);
P2=0xff;
P0= (i_com>>(2));
P2=table[dis_buf[2]];
delay(3);
P2=0xff;
P0= (i_com>>(3));
P2=table[dis_buf[3]];
delay(3);
}
void int_0() interrupt 0
{
// EA=0;
//display();
exint_flag=1;
int_flag=1;
EX0=0;
ir_num++;
#ifdef KEY_PRESS
// IE0=0;
#endif
// EA=1;
}
void timer1_isq()interrupt 3
{
TH1=0xfa;
TL1=0x84;
if(key_flag&&(++key_cnt==10))
{
if(KEY1)
{
key_flag=0;
}
key_cnt=0;
}
if(int_flag&&(++int_cnt==20))
{
#ifdef KEY_PRESS
if(ir_s)
#else
if(!ir_s)
#endif
{
int_flag=0;
}
int_cnt=0;
}
if(exint_flag)
{
ming=0;
}
if(exint_flag&&(++buzzer_cnt==40))
{
ming=1;
buzzer_cnt=0;
exint_flag=0;
}
}
void main()
{
delay(500);
timer0_init();
WIN24C02_init(); //24c02初始化子程序
EX0=1;
IT0=1;
IE0=0;
EA=1;
PT0=1;
// ir_num=STC89CXX_EEPROM_Char_Read(0x2000);
iic_reads(0x10,&ir_num,2);
//ir_num=WIN24C02_read(0x00)|(WIN24C02_read(0x01)<<8);
ch_value();
while(1)
{
if(ir_s==1)
{
ir_s=0;
}
ch_value();
//display();
if(!EX0)
{
if(ir_num>=9999)
ir_num=0;
iic_writes(0x10,&ir_num,2);
while(int_flag&&KEY1);//display();
//STC89CXX_EEPROM_Erase(0x20);
//STC89CXX_EEPROM_Char_Write(0x2000,ir_num);//存数据到EEPROM
// WIN24C02_write(0x00,ir_num&0xff);
// WIN24C02_write(0x01,(ir_num>>8)&0xff);
//IE0=0;
ch_value();
#ifdef KEY_PRESS
IE0=0;
#else
IE0=0;
#endif
EX0=1;
}
if(!KEY1)
{
// display();
EX0=0;
ir_num=0;
//STC89CXX_EEPROM_Erase(0x20);
//STC89CXX_EEPROM_Char_Write(0x2000,ir_num);//存数据到EEPROM
iic_writes(0x10,&ir_num,2);
// WIN24C02_write(0x00,ir_num&0xff);
// WIN24C02_write(0x01,(ir_num>>8)&0xff);
ch_value();
// EA=1;
while(!KEY1);
IE0=0;
EX0=1;
}
}
}
展开全部
C51 COMPILER V9.00 发送 11/26/2013 17:27:02 PAGE 1
C51 COMPILER V9.00, COMPILATION OF MODULE 发送OBJECT MODULE PLACED IN 发送.OBJCOMPILER INVOKED BY: e:\Keil\C51\BIN\C51.EXE 发送.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #include <reg52.h> 2 3 #define uint unsigned int 4 #define uchar unsigned char 5 6 sbit P2_6=P2^6; //方波产生位 7 sbit signalBit=P2^5; //发送信号标志位 8 9 uchar mazhi0[]={0x33,0x22,0x00,0xff}; 10 uchar mazhi1[]={0x33,0x22,0x01,0xfe}; 11 uchar mazhi2[]={0x33,0x22,0x02,0xfd}; 12 uchar mazhi3[]={0x33,0x22,0x03,0xfc}; 13 uchar mazhi4[]={0x33,0x22,0x04,0xfb}; 14 uchar mazhi5[]={0x33,0x22,0x05,0xfa}; 15 uchar mazhi6[]={0x33,0x22,0x06,0xf9}; 16 uchar mazhi7[]={0x33,0x22,0x07,0xf8}; 17 uchar mazhi8[]={0x33,0x22,0x08,0xf7}; 18 uchar mazhi9[]={0x33,0x22,0x09,0xf6}; 19 uchar mazhia[]={0x33,0x22,0x0a,0xf5}; 20 uchar mazhib[]={0x33,0x22,0x0b,0xf4}; 21 uchar mazhic[]={0x33,0x22,0x0c,0xf3}; 22 uchar mazhid[]={0x33,0x22,0x0d,0xf2}; 23 uchar mazhie[]={0x33,0x22,0x0e,0xf1}; 24 uchar mazhif[]={0x33,0x22,0x0f,0xf0}; 25 26 void delay(uint tt) 27 { 28 1 while(tt--); 29 1 } 30 31 void SendIRdata(uchar mData[4]) //红外发射函数 32 { 33 1 uchar i,j; 34 1 uchar test[4]; 35 1 for(i=0;i<4;i++) 36 1 test[i]=mData[i]; 37 1 TH1=0XDF; //发送9ms的高电平 38 1 TL1=0XE3; 39 1 TR1=1; //启动T1工作 40 1 signalBit=1; //发送信号 41 1 while(!TF1); 42 1 TH1=0xef; //发送4.5ms的低电平 43 1 TL1=0xf0; 44 1 signalBit=0; 45 1 while(!TF1); 46 1 /********* 发送 用户码 控制码 控制反码 ***********/ 47 1 for(j=0;j<4;j++) //发送十六位地址的前八位 48 1 { 49 2 for(i=0;i<8;i++) //先发送0.565ms的38KHZ红外波(即编码中0.565ms的高电平) 50 2 { 51 3 TH1=0XFE; 52 3 TL1=0X70; 53 3 signalBit=1; 54 3 while(!TF1); //停止发送红外信号(即编码中的高电平) 55 3 if(test[j]&0x01) //判断二进制数个位为1还是0 C51 COMPILER V9.00 发送 11/26/2013 17:27:02 PAGE 2
56 3 { 57 4 TH1=0XFA; //1为宽的低电平,持续时间1.685ms 58 4 TL1=0X30; 59 4 } 60 3 else 61 3 { 62 4 TH1=0XFE; //0为窄的低电平,持续时间0.56ms 63 4 TL1=0x50; 64 4 } 65 3 signalBit=0; 66 3 while(!TF1); 67 3 test[j]=test[j]>>1; 68 3 } 69 2 } 70 1 TR1=0; //发送停止位 71 1 signalBit=1; 72 1 delay(12); 73 1 signalBit=0; 74 1 } 75 76 void lchu0() 77 { 78 1 SendIRdata(mazhi0); 79 1 } 80 81 void lchu1() 82 { 83 1 SendIRdata(mazhi1); 84 1 } 85 86 void lchu2() 87 { 88 1 SendIRdata(mazhi2); 89 1 } 90 91 void lchu3() 92 { 93 1 SendIRdata(mazhi3); 94 1 } 95 96 void lchu4() 97 { 98 1 SendIRdata(mazhi4); 99 1 } 100 101 void lchu5() 102 { 103 1 SendIRdata(mazhi5); 104 1 } 105 106 void lchu6() 107 { 108 1 SendIRdata(mazhi6); 109 1 } 110 111 void lchu7() 112 { 113 1 SendIRdata(mazhi7); 114 1 } 115 116 void lchu8() 117 {C51 COMPILER V9.00 发送 11/26/2013 17:27:02 PAGE 3
118 1 SendIRdata(mazhi8); 119 1 } 120 121 void lchu9() 122 { 123 1 SendIRdata(mazhi9); 124 1 } 125 126 void lchua() 127 { 128 1 SendIRdata(mazhia); 129 1 } 130 131 void lchub() 132 { 133 1 SendIRdata(mazhib); 134 1 } 135 136 void lchuc() 137 { 138 1 SendIRdata(mazhic); 139 1 } 140 141 void lchud() 142 { 143 1 SendIRdata(mazhid); 144 1 } 145 146 void lchue() 147 { 148 1 SendIRdata(mazhie); 149 1 } 150 151 void lchuf() 152 { 153 1 SendIRdata(mazhif); 154 1 } 155 156 void isr_T1(void) interrupt 3 157 { 158 1 TH0=0xf4; //设定时值0为38K 也就是每隔13us中断一次,周期为26us 159 1 TL0=0xf4; 160 1 } 161 162 163 void timeint(void) interrupt 1 164 { 165 1 P2_6=~P2_6; //产生38k的载波信号 166 1 } 167 168 void keyscan() 169 { 170 1 uchar key_l,key_h; 171 1 uchar key; 172 1 P1=0xf0; 173 1 key_l=P1; 174 1 key_l=key_l&0xf0; 175 1 if(key_l!=0xf0) 176 1 { 177 2 delay(100); 178 2 if(key_l!=0xf0) 179 2 {C51 COMPILER V9.00 发送 11/26/2013 17:27:02 PAGE 4
180 3 key_l=P1&0xf0; 181 3 key_l=key_l|0x0f; 182 3 P1=key_l; 183 3 key_h=P1; 184 3 key_h=key_h&0x0f; 185 3 key_l=key_l&0xf0; 186 3 key=key_l+key_h; 187 3 } 188 2 switch(key) //按下相应的键显示相对应的码值 189 2 { 190 3 case 0x7e:lchu0();break;//0 191 3 case 0x7d:lchu1();break;//1 192 3 case 0x7b:lchu2();break;//2 193 3 case 0x77:lchu3();break;//3 194 3 case 0xbe:lchu4();break;//4 195 3 case 0xbd:lchu5();break;//5 196 3 case 0xbb:lchu6();break;//6 197 3 case 0xb7:lchu7();break;//7 198 3 case 0xde:lchu8();break;//8 199 3 case 0xdd:lchu9();break;//9 200 3 case 0xdb:lchua();break;//a 201 3 case 0xd7:lchub();break;//b 202 3 case 0xee:lchuc();break;//c 203 3 case 0xed:lchud();break;//d 204 3 case 0xeb:lchue();break;//e 205 3 case 0xe7:lchuf();break;//f 206 3 } 207 2 } 208 1 } 209 210 void main(void) 211 { 212 1 signalBit=0; 213 1 P2_6=0; 214 1 TMOD=0x12; //设定定时器1和0工作方式为1和2 215 1 ET0=1; //定时器0中断允许 216 1 ET1=1; 217 1 TH0=0xf4; 218 1 TL0=0xf4; 219 1 TR0=0; //先关闭定时器 0 220 1 TR1=0; 221 1 EA=1; 222 1 while(1) 223 1 { 224 2 TR0=1; 225 2 keyscan(); 226 2 TR0=0; 227 2 } 228 1 }
MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 487 ---- CONSTANT SIZE = ---- ---- XDATA SIZE = ---- ---- PDATA SIZE = ---- ---- DATA SIZE = 64 8 IDATA SIZE = ---- ---- BIT SIZE = ---- ----END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
C51 COMPILER V9.00, COMPILATION OF MODULE 发送OBJECT MODULE PLACED IN 发送.OBJCOMPILER INVOKED BY: e:\Keil\C51\BIN\C51.EXE 发送.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #include <reg52.h> 2 3 #define uint unsigned int 4 #define uchar unsigned char 5 6 sbit P2_6=P2^6; //方波产生位 7 sbit signalBit=P2^5; //发送信号标志位 8 9 uchar mazhi0[]={0x33,0x22,0x00,0xff}; 10 uchar mazhi1[]={0x33,0x22,0x01,0xfe}; 11 uchar mazhi2[]={0x33,0x22,0x02,0xfd}; 12 uchar mazhi3[]={0x33,0x22,0x03,0xfc}; 13 uchar mazhi4[]={0x33,0x22,0x04,0xfb}; 14 uchar mazhi5[]={0x33,0x22,0x05,0xfa}; 15 uchar mazhi6[]={0x33,0x22,0x06,0xf9}; 16 uchar mazhi7[]={0x33,0x22,0x07,0xf8}; 17 uchar mazhi8[]={0x33,0x22,0x08,0xf7}; 18 uchar mazhi9[]={0x33,0x22,0x09,0xf6}; 19 uchar mazhia[]={0x33,0x22,0x0a,0xf5}; 20 uchar mazhib[]={0x33,0x22,0x0b,0xf4}; 21 uchar mazhic[]={0x33,0x22,0x0c,0xf3}; 22 uchar mazhid[]={0x33,0x22,0x0d,0xf2}; 23 uchar mazhie[]={0x33,0x22,0x0e,0xf1}; 24 uchar mazhif[]={0x33,0x22,0x0f,0xf0}; 25 26 void delay(uint tt) 27 { 28 1 while(tt--); 29 1 } 30 31 void SendIRdata(uchar mData[4]) //红外发射函数 32 { 33 1 uchar i,j; 34 1 uchar test[4]; 35 1 for(i=0;i<4;i++) 36 1 test[i]=mData[i]; 37 1 TH1=0XDF; //发送9ms的高电平 38 1 TL1=0XE3; 39 1 TR1=1; //启动T1工作 40 1 signalBit=1; //发送信号 41 1 while(!TF1); 42 1 TH1=0xef; //发送4.5ms的低电平 43 1 TL1=0xf0; 44 1 signalBit=0; 45 1 while(!TF1); 46 1 /********* 发送 用户码 控制码 控制反码 ***********/ 47 1 for(j=0;j<4;j++) //发送十六位地址的前八位 48 1 { 49 2 for(i=0;i<8;i++) //先发送0.565ms的38KHZ红外波(即编码中0.565ms的高电平) 50 2 { 51 3 TH1=0XFE; 52 3 TL1=0X70; 53 3 signalBit=1; 54 3 while(!TF1); //停止发送红外信号(即编码中的高电平) 55 3 if(test[j]&0x01) //判断二进制数个位为1还是0 C51 COMPILER V9.00 发送 11/26/2013 17:27:02 PAGE 2
56 3 { 57 4 TH1=0XFA; //1为宽的低电平,持续时间1.685ms 58 4 TL1=0X30; 59 4 } 60 3 else 61 3 { 62 4 TH1=0XFE; //0为窄的低电平,持续时间0.56ms 63 4 TL1=0x50; 64 4 } 65 3 signalBit=0; 66 3 while(!TF1); 67 3 test[j]=test[j]>>1; 68 3 } 69 2 } 70 1 TR1=0; //发送停止位 71 1 signalBit=1; 72 1 delay(12); 73 1 signalBit=0; 74 1 } 75 76 void lchu0() 77 { 78 1 SendIRdata(mazhi0); 79 1 } 80 81 void lchu1() 82 { 83 1 SendIRdata(mazhi1); 84 1 } 85 86 void lchu2() 87 { 88 1 SendIRdata(mazhi2); 89 1 } 90 91 void lchu3() 92 { 93 1 SendIRdata(mazhi3); 94 1 } 95 96 void lchu4() 97 { 98 1 SendIRdata(mazhi4); 99 1 } 100 101 void lchu5() 102 { 103 1 SendIRdata(mazhi5); 104 1 } 105 106 void lchu6() 107 { 108 1 SendIRdata(mazhi6); 109 1 } 110 111 void lchu7() 112 { 113 1 SendIRdata(mazhi7); 114 1 } 115 116 void lchu8() 117 {C51 COMPILER V9.00 发送 11/26/2013 17:27:02 PAGE 3
118 1 SendIRdata(mazhi8); 119 1 } 120 121 void lchu9() 122 { 123 1 SendIRdata(mazhi9); 124 1 } 125 126 void lchua() 127 { 128 1 SendIRdata(mazhia); 129 1 } 130 131 void lchub() 132 { 133 1 SendIRdata(mazhib); 134 1 } 135 136 void lchuc() 137 { 138 1 SendIRdata(mazhic); 139 1 } 140 141 void lchud() 142 { 143 1 SendIRdata(mazhid); 144 1 } 145 146 void lchue() 147 { 148 1 SendIRdata(mazhie); 149 1 } 150 151 void lchuf() 152 { 153 1 SendIRdata(mazhif); 154 1 } 155 156 void isr_T1(void) interrupt 3 157 { 158 1 TH0=0xf4; //设定时值0为38K 也就是每隔13us中断一次,周期为26us 159 1 TL0=0xf4; 160 1 } 161 162 163 void timeint(void) interrupt 1 164 { 165 1 P2_6=~P2_6; //产生38k的载波信号 166 1 } 167 168 void keyscan() 169 { 170 1 uchar key_l,key_h; 171 1 uchar key; 172 1 P1=0xf0; 173 1 key_l=P1; 174 1 key_l=key_l&0xf0; 175 1 if(key_l!=0xf0) 176 1 { 177 2 delay(100); 178 2 if(key_l!=0xf0) 179 2 {C51 COMPILER V9.00 发送 11/26/2013 17:27:02 PAGE 4
180 3 key_l=P1&0xf0; 181 3 key_l=key_l|0x0f; 182 3 P1=key_l; 183 3 key_h=P1; 184 3 key_h=key_h&0x0f; 185 3 key_l=key_l&0xf0; 186 3 key=key_l+key_h; 187 3 } 188 2 switch(key) //按下相应的键显示相对应的码值 189 2 { 190 3 case 0x7e:lchu0();break;//0 191 3 case 0x7d:lchu1();break;//1 192 3 case 0x7b:lchu2();break;//2 193 3 case 0x77:lchu3();break;//3 194 3 case 0xbe:lchu4();break;//4 195 3 case 0xbd:lchu5();break;//5 196 3 case 0xbb:lchu6();break;//6 197 3 case 0xb7:lchu7();break;//7 198 3 case 0xde:lchu8();break;//8 199 3 case 0xdd:lchu9();break;//9 200 3 case 0xdb:lchua();break;//a 201 3 case 0xd7:lchub();break;//b 202 3 case 0xee:lchuc();break;//c 203 3 case 0xed:lchud();break;//d 204 3 case 0xeb:lchue();break;//e 205 3 case 0xe7:lchuf();break;//f 206 3 } 207 2 } 208 1 } 209 210 void main(void) 211 { 212 1 signalBit=0; 213 1 P2_6=0; 214 1 TMOD=0x12; //设定定时器1和0工作方式为1和2 215 1 ET0=1; //定时器0中断允许 216 1 ET1=1; 217 1 TH0=0xf4; 218 1 TL0=0xf4; 219 1 TR0=0; //先关闭定时器 0 220 1 TR1=0; 221 1 EA=1; 222 1 while(1) 223 1 { 224 2 TR0=1; 225 2 keyscan(); 226 2 TR0=0; 227 2 } 228 1 }
MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 487 ---- CONSTANT SIZE = ---- ---- XDATA SIZE = ---- ---- PDATA SIZE = ---- ---- DATA SIZE = 64 8 IDATA SIZE = ---- ---- BIT SIZE = ---- ----END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询