利用proteus进行红外仿真
MCU采用的是51单片机
供参考和学习使用
单片机源程序如下:
- //作用 红外发射与接收一体 万能遥控解码
- #include"reg52.h"
- #define uchar unsigned char
- #define uint unsigned int
- //定时器寄存器设置
- #define THO_int0 0xfa
- #define TLO_int0 0x8d
- #define TH1_int1 0xff
- #define TL1_int1 177
-
- sbit LED1=P2^1;
- sbit LED2=P2^2;
- sbit LED3=P2^3;
- sbit LED4=P2^0;
- sbit Beep=P3^7;
- sbit KEY1=P1^4;
- sbit KEY2=P1^5;
- sbit KEY3=P1^6;
- sbit KEY4=P1^7;
- sbit infrared = P3^3;
- sbit infr_send = P3^4;
- sbit tiaoshi = P3^6;
- //表格存于ROM*************************************
- uchar code dsp_tap[16]={0xa0,0xbb,0x62,0x2a,0x39,0x2c,0x24,0xba,0x20,0x28,0x30,0x25,0xe4,0x23,0x64,0x74};
- uchar code dsp_cs[4]={0x70,0xd0,0xb0,0xe0};
- //uchar code dat_tap[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
- //uchar code dat_tap2[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
- //变量定义存于RAM**********************************
- uchar SimpleKeyState = 0; //简单按键的状态
- uchar SimpleKeyPressDelay = 7; //简单按键按实的大概时间,单位:10毫秒
- uchar SimpleKeyCode = 0xFF; //简单按键键码
- uchar infr_key1_data[4] = {0};
- uint infr_send_cont = 0;
- uchar infrared_new_data[4] = {0};
- uchar infrared_old_data[4] = {0x80,0xff,0xc8,0x37};
- uchar infrared_byte = 0;
- uchar infrared_byte_cont = 0;
- uchar infrared_old_state = 0;
- uchar infrared_new_state = 0;
- uint infrared_cont = 0;
- uint infrared_cont_now = 0;
- uchar flg_infrared = 0;
- uchar flg_infrared_ok = 0;
- uchar seg_step = 0;
- uchar dsp_step;
- uchar cont_4ms;
- uint cont_1s;
- uchar cont_500ms;
- uchar cont_10ms;
- uchar sec;
- uchar beef_delay;
- uchar dis_play[4] = {0};
- uint dis_num = 0;
- uchar flg_display = 0;
- uchar flg_change = 0;
- uchar flg_send_start = 0;
- uchar aaa[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
- uchar flg_key = 0;
- void writ_send(uint tin)
- {
- uchar j;
- uint i;
- for(i=0;i<tin;i++)
- {
- for(j=0;j<4;j++);
- infr_send =~ infr_send;
- }
- infr_send = 1;
- }
- void delay(uint w)
- {
- uint i;
- for(i = 0;i<w;i++);
- }
- void infr_send_ctrol(void)
- {
- uchar cont;
- EA=0;
- infrared_byte_cont = 0;
- infrared_byte = 0;
- writ_send(660); //头码9ms
- delay(748); //4500us 高电平延时
- for(cont = 0;cont < 33; cont++)
- {
- writ_send(38); //560us低电平发射 42
- if(infrared_old_data[infrared_byte] & aaa[infrared_byte_cont])
- {
- delay(280); //1690us 高电平延时 1 269
- infrared_byte_cont++;
- }else
- {
- delay(93); //560us 高电平延时 0 89
- infrared_byte_cont++;
- }
- if(infrared_byte_cont == 8)
- {
- infrared_byte++;
- infrared_byte_cont = 0;
- }
- }
- EA=1;
- }
- /*============
- 简单按键处理子函数
- ==============*/
- void SimpleKeyAction(void)
- {
- if(SimpleKeyCode == 1)
- {
- LED1 =~ LED1;
- infr_send_ctrol();
- }
- else if(SimpleKeyCode == 2)
- {
-
- LED2 =~ LED2;
- // infr_send =~ infr_send;
- }
- else if(SimpleKeyCode == 3)
- {
- //LED3 =~ LED3;
- }else if(SimpleKeyCode == 4)
- {
- Beep = 0;
- LED4 =~ LED4;
- }
- }
- /*============
- 简单按键扫描子函数
- ==============*/
- void SimpleKeyScan(void)
- {
- P1 =P1|0xf0;
- if(SimpleKeyPressDelay > 0) SimpleKeyPressDelay--;
-
- if( (SimpleKeyState == 0) && ((KEY1 ==0) || (KEY2 ==0) || (KEY3 ==0) || (KEY4 ==0)) ) //某个按钮被按下
- {
- SimpleKeyState = 1;
- SimpleKeyPressDelay = 7; //从接触按键到把按键按下所需要的大概时间:70毫秒
- }
- if(SimpleKeyState == 1)
- {
- if(SimpleKeyPressDelay == 0) //按键按实
- {
- // Beep = 0;
- SimpleKeyState = 2;
- if(KEY1 ==0)
- {
- SimpleKeyCode = 1;
- }
- else if(KEY2 ==0)
- {
- SimpleKeyCode = 2;
- }
- else if(KEY3 ==0)
- {
- SimpleKeyCode = 3;
- }
- else if(KEY4 ==0)
- {
- SimpleKeyCode = 4;
- }
-
- SimpleKeyAction();//按键处理
- }
- }
- if(SimpleKeyState == 2)
- {
- if( (KEY1 == 1) && (KEY2 == 1) && (KEY3 == 1) && (KEY4 == 1) ) //所有的按键松开
- {
- SimpleKeyPressDelay = 7; //松开按键所需要的大概时间:70毫秒
- SimpleKeyState = 3;
- }
- }
- if(SimpleKeyState == 3)
- {
- if(SimpleKeyPressDelay == 0) //已松开按键
- {
- SimpleKeyState = 0;
- SimpleKeyCode = 0xFF;
- }
- }
- }
- //******************************************************************
- void T0_int(void)
- {
- //定时器0初始化设置
- TR0 = 1;
- TH0=THO_int0;
- TL0=TLO_int0;
- ET0=1;
- EA=1;
- }
- //*********************************************************************
- void MCU_init(void)
- {
- P1=0xff;
- P2=0xff;
- P3=0xff;
- T0_int();
- // Beep = 0;
- }
-
- //*******************************************************************
-
- void timer0_(void)interrupt 1 // 定时器溢出周期为100us
- {
- TR0=0;
- TH0=THO_int0;
- TL0=TLO_int0;
- TR0=1;
- // tiaoshi =~ tiaoshi;
- if(flg_send_start == 0)
- {
- //红外接收代码
- P3=(P3|0x08); //把红外管脚的电平设为1
- infrared_old_state = infrared_new_state; //保留上一次的电平
- infrared_new_state = (P3&0x08); //读取现在的管脚电平值
- if(infrared_new_state == infrared_old_state) infrared_cont++; //如果两次的电平相同 变量infrared_cont 自加1
- else if((P3&0x08) == 0x00) //如果两次的电平不相同 且 该管脚的电平为低电平
- {
- infrared_cont_now = infrared_cont; //保留infrared_cont的值
- infrared_cont = 0; //变量清零
- if(flg_infrared == 1)
- {
- if((infrared_cont_now >= 6)&&(infrared_cont_now <= 25)) //如果变量值在6到15这个范围之内
- {
- if(infrared_cont_now <= 14) //小于的话 设置为0
- {
- infrared_new_data[infrared_byte] = infrared_new_data[infrared_byte]<<1;
- infrared_new_data[infrared_byte] = (infrared_new_data[infrared_byte]&0xfe);
- infrared_byte_cont++;
- }
- else //大于的话 设置为1
- {
- infrared_new_data[infrared_byte] = infrared_new_data[infrared_byte]<<1;
- infrared_new_data[infrared_byte] = (infrared_new_data[infrared_byte]|0x01);
- infrared_byte_cont++;
- }
- if(infrared_byte_cont == 8) //当存储玩8位数据infrared_new_data[0]之后,再存储infrared_new_data[1]
- {
-
- infrared_byte++;
- infrared_byte_cont = 0;
- if(infrared_byte == 4) //当存储完32位数据之后
- {
- infrared_byte = 0;
- flg_infrared = 0; //标志位清零
- infrared_old_data[0] = infrared_new_data[0]; //保留数据
- infrared_old_data[1] = infrared_new_data[1];
- infrared_old_data[2] = infrared_new_data[2];
- infrared_old_data[3] = infrared_new_data[3];
- flg_infrared_ok = 1;
- }
-
- }
- }else flg_infrared = 0;
- }else
- {
- if((infrared_cont_now >= 80)&&(infrared_cont_now <= 150)) //头吗解读
- {
- flg_infrared = 1; //标志位置1
- infrared_byte = 0; //变量清零
- infrared_byte_cont = 0;
- }else flg_infrared = 0;
- }
- }
- }
- cont_4ms++;
- if(cont_4ms >= 40)
- {
- cont_4ms=0;
- flg_display = 1;
- //蜂鸣器控制****************************
- if(Beep == 0)
- {
- beef_delay++;
- if(beef_delay > 10) Beep = 1;
- }else beef_delay = 0;
- //**************************************
- }
- cont_1s++;
- if(cont_1s>=10000)
- {
- cont_1s=0;
- // dis_num++;
- flg_change =~ flg_change;
- }
- }
- void main(void)
- {
- MCU_init();
- tiaoshi = 0;
- for(;;)
- {
- SimpleKeyScan();
- if(flg_infrared_ok == 1)
- {
- flg_infrared_ok = 0;
- LED4 =~ LED4;
- Beep = 0;
- }
-
- if(flg_display)
- {
- flg_display = 0;
- P2 = (P2&0x0f);
- if(flg_change)
- {
- dis_play[3] = dsp_tap[infrared_old_data[2]%256/16];
- dis_play[2] = dsp_tap[infrared_old_data[2]%16];
- dis_play[1] = dsp_tap[infrared_old_data[3]%256/16];
- dis_play[0] = dsp_tap[infrared_old_data[3]%16];
- }else
- {
- dis_play[3] = dsp_tap[infrared_old_data[0]%256/16];
- dis_play[2] = dsp_tap[infrared_old_data[0]%16];
- dis_play[1] = dsp_tap[infrared_old_data[1]%256/16];
- dis_play[0] = dsp_tap[infrared_old_data[1]%16];
- }
- P0 = dis_play[seg_step];
- P2 = (P2|dsp_cs[seg_step]);
- seg_step++;
- if(seg_step>=4)seg_step = 0;
- }
- }
- }
- void EXT0_INT(void) interrupt 0 {;}
- void EXT1_INT(void) interrupt 2 {;}
- void timer1_(void) interrupt 3 {;}
- void serial_(void) interrupt 4 {;}
复制代码
|