红外遥控系统代码
单片机源程序如下:
- #include <reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
- #include "1602.h"
- #include "delay.h"
- #include "stdio.h"
- sbit IR=P3^2; //红外接口标志
- /*------------------------------------------------
- 全局变量声明
- ------------------------------------------------*/
- unsigned char irtime;//红外用全局变量
- bit irpro_ok,irok;
- unsigned char IRcord[4];
- unsigned char irdata[33];
- unsigned char TempData[16];
- char code Tab[16]="0123456789ABCDEF";
- //选定的红外遥控器按键,键值为提前读取的
- unsigned char code LED_data[16][4]={
- {0x00,0xff,0x16,0xe9},//0
- {0x00,0xff,0x0C,0xf3},//1
- {0x00,0xff,0x18,0xe7},//2
- {0x00,0xff,0x5e,0xa1},//3
- {0x00,0xff,0x08,0xf7},//4
- {0x00,0xff,0x1c,0xe3},//5
- {0x00,0xff,0x5a,0xa5},//6
- {0x00,0xff,0x42,0xbd},//7
- {0x00,0xff,0x52,0xad},//8
- {0x00,0xff,0x4a,0xb5},//9
- {0x00,0xff,0x45,0xba},//关闭
- {0x00,0xff,0x46,0xb9},//Mode
- {0x00,0xff,0x47,0xb8},//静音
- {0x00,0xff,0x44,0xbb},//下一首
- {0x00,0xff,0x40,0xbf},//后退
- {0x00,0xff,0x43,0xbc} //前进
- };
-
- bit check_array(unsigned char *array1,unsigned char *array2,unsigned char len)
- {
- int i;
- for(i=0;i<len;i++)
- {
- if(array1[i]!=array2[i])
- break;//如果不等,结束循环,此时i的值是小于5的
- }
- if(i==len) //相同返回1,不同返回0
- return 1;
- else
- return 0;
- }
- unsigned char KEY_SCANF()
- {
- unsigned char KEY_i=0;
- for(KEY_i=0;KEY_i<16;KEY_i++)
- if(check_array(IRcord,LED_data[KEY_i],4)) //如果是选定的按键
- return KEY_i;
- return 20;
- }
- /*------------------------------------------------
- 定时器0中断处理
- ------------------------------------------------*/
- void tim0_isr (void) interrupt 1
- {
- irtime++; //用于计数2个下降沿之间的时间
- }
- /*------------------------------------------------
- 外部中断0中断处理
- ------------------------------------------------*/
- void EX0_ISR (void) interrupt 0 //外部中断0服务函数
- {
- static unsigned char i; //接收红外信号处理
- static bit startflag; //是否开始处理标志位
- if(startflag)
- {
- if(irtime<63&&irtime>=33)//引导码 TC9012的头码,9ms+4.5ms
- i=0;
- irdata[i]=irtime;//存储每个电平的持续时间,用于以后判断是0还是1
- irtime=0;
- i++;
- if(i==33)
- {
- irok=1;
- i=0;
- }
- }
- else
- {
- irtime=0;
- startflag=1;
- }
- }
- /*------------------------------------------------
- 定时器0初始化
- ------------------------------------------------*/
- void TIM0init(void)//定时器0初始化
- {
- TMOD=0x02;//定时器0工作方式2,TH0是重装值,TL0是初值
- TH0=0x00; //重载值
- TL0=0x00; //初始化值
- ET0=1; //开中断
- TR0=1;
- }
- /*------------------------------------------------
- 外部中断0初始化
- ------------------------------------------------*/
- void EX0init(void)
- {
- IT0 = 1; //指定外部中断0下降沿触发,INT0 (P3.2)
- EX0 = 1; //使能外部中断
- EA = 1; //开总中断
- }
- /*------------------------------------------------
- 键值处理
- ------------------------------------------------*/
- void Ir_work(void)
- {
- unsigned char temp[16],yyy;
- TempData[0] = Tab[IRcord[0]/16]; //处理客户码
- TempData[1] = Tab[IRcord[0]%16];
- TempData[2] = '-';
- TempData[3] = Tab[IRcord[1]/16]; //处理客户码
- TempData[4] = Tab[IRcord[1]%16];
- TempData[5] = '-';
- TempData[6] = Tab[IRcord[2]/16]; //处理数据码
- TempData[7] = Tab[IRcord[2]%16];
- TempData[8] = '-';
- TempData[9] = Tab[IRcord[3]/16]; //处理数据反码
- TempData[10] = Tab[IRcord[3]%16];
- LCD_Write_String(5,0,TempData);
- yyy=KEY_SCANF();
- if(yyy!=20)
- {
- sprintf(temp,"%d " ,(unsigned int)yyy);
- LCD_Write_String(10,1,temp);
- switch(yyy)
- {
- case 0: {P1=0X7F;P2=0XFF;};break;
- case 1: {P1=0XBF;P2=0XFF;};break;
- case 2: {P1=0XDF;P2=0XFF;};break;
- case 3: {P1=0XEF;P2=0XFF;};break;
- case 4: {P1=0XF7;P2=0XFF;};break;
- case 5: {P1=0XFB;P2=0XFF;};break;
- case 6: {P1=0XFD;P2=0XFF;};break;
- case 7: {P1=0XFe;P2=0XFF;};break;
-
- case 8: {P1=0XFF;P2=0XFE;};break;
- case 9: {P1=0XFF;P2=0XFD;};break;
- case 10: {P1=0XFF;P2=0XFB;};break;
- case 11: {P1=0XFF;P2=0XF7;};break;
- case 12: {P1=0XFF;P2=0XEF;};break;
- case 13: {P1=0XFF;P2=0XDF;};break;
- case 14: {P1=0XFF;P2=0XBF;};break;
- case 15: {P1=0XFF;P2=0X7F;};break;
- default: {P1=0XFF;P2=0XfF;};break;
- }
- }
- else
- {
- LCD_Write_String(10,1,"Error!");
- }
-
-
- irpro_ok=0;//处理完成标志
- }
- /*------------------------------------------------
- 红外码值处理
- ------------------------------------------------*/
- void Ircordpro(void)//红外码值处理函数
- {
- unsigned char i, j, k;
- unsigned char cord,value;
- k=1;
- for(i=0;i<4;i++) //处理4个字节
- {
- for(j=1;j<=8;j++) //处理1个字节8位
- {
- cord=irdata[k];
- if(cord>7)//大于某值为1,这个和晶振有绝对关系,这里使用12M计算,此值可以有一定误差
- value|=0x80;
- if(j<8)
- {
- value>>=1;
- }
- k++;
- }
- IRcord[i]=value;
- value=0;
- }
- irpro_ok=1;//处理完毕标志位置1
- }
- /*------------------------------------------------
- 主函数
- ------------------------------------------------*/
- void main(void)
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
无线遥控系统代码.rar
(59.74 KB, 下载次数: 18)
|