ir
来源:互联网 发布:西门子plc中文编程软件 编辑:程序博客网 时间:2024/04/29 09:12
#include <reg52.h>#include "uart.h"#include "delay.h"#include "1602.h" unsigned char irtime;//unsigned char num;bit startflag = 0;bit irok = 0;bit irpro_ok = 0;unsigned char irdata[33];unsigned char ircord[4];unsigned char putbuf[35];void exit0_init(void) {IT0 = 1;EA = 1;EX0 = 1;}void exit0_isr(void) interrupt 0{static unsigned char i; EX0 = 0;if(startflag){if(irtime > 40 && irtime < 60){i = 0;}irdata[i]=irtime;//存储每个电平的持续时间,用于以后判断是0还是1 irtime=0; i++; if(i==33) { irok=1;i=0;startflag = 0; }}else{irtime = 0;startflag = 1;}EX0 = 1;}void time0_init(void){EA = 1;TMOD |= 0x02;TH0 = 0X00;ET0 = 1;TR0 = 1;}void time0_isr(void) interrupt 1{ET0 = 0;irtime++;//0.256ms;13.5/0.256---53;0===1.12/0.256---4;1===2.245/0.256---9;ET0 = 1;}void ircordpro(void)//红外码值处理函数{ unsigned char i, j, k = 0; unsigned char value = 0; for(i = 0; i < 4; i++) //处理4个字节 { for(j = 1; j <= 8; j++) //处理1个字节8位 { value >>= 1; if(irdata[++k] > 7)//大于某值为1,这个和晶振有绝对关系,这里使用12M计算,此值可以有一定误差 value |= 0x80; } ircord[i] = value; value = 0; } if((ircord[0] + ircord[1] == 0xff) &&(ircord[2] + ircord[3] == 0xff)) irpro_ok = 1;//处理完毕标志位置1}sbit LED0 = P1^0;sbit LED1 = P1^1;sbit LED2 = P1^2;sbit LED3 = P1^3;sbit LED4 = P1^4;sbit LED5 = P1^5;sbit LED6 = P1^6;sbit LED7 = P1^7;//#define data = P1;void ir_work(){/*unsigned char i;for(i = 0; i < 4; i++){uart_send_byte(ircord[i]);DelayMs(1);}*/switch(ircord[2]){case 0x45:LED0 = ~LED0;LCD_Write_Char(0,0,'1');break;case 0x46:LED1 = ~LED1;LCD_Write_Char(0,0,'2');break;case 0x47:LED2 = ~LED2;LCD_Write_Char(0,0,'3');break;case 0x44:LED3 = ~LED3;LCD_Write_Char(0,0,'4');break;case 0x40:LED4 = ~LED4;LCD_Write_Char(0,0,'5');break;case 0x43:LED5 = ~LED5;LCD_Write_Char(0,0,'6');break;case 0x07:LED6 = ~LED6;LCD_Write_Char(0,0,'7');break;case 0x15:LED7 = ~LED7;LCD_Write_Char(0,0,'8');break;case 0x09:LCD_Write_Char(0,0,'9');break;case 0x16:LCD_Write_String(0,0,"10");break;case 0x19:LCD_Write_String(0,0,"11");break;case 0x0d:LCD_Write_String(0,0,"12");break;case 0x0c:LCD_Write_String(0,0,"13");break;case 0x18:LCD_Write_String(0,0,"14");break;case 0x5e:LCD_Write_String(0,0,"15");break;case 0x08:LCD_Write_String(0,0,"16");break;case 0x1c:LCD_Write_String(0,0,"17");break;case 0x5a:LCD_Write_String(0,0,"18");break;case 0x42:LCD_Write_String(0,0,"19");break;case 0x52:LCD_Write_String(0,0,"20");break;case 0x4a:LCD_Write_String(0,0,"21");break;default:break;}}void ir_time(void){unsigned char i;//putbuf[0] = irdata[0]/10 + 0x30;//putbuf[1] = irdata[0]%10 + 0x30;putbuf[0] = 's';putbuf[1] = 't';for(i = 0; i < 32; i++){//putbuf[i+2] = irdata[i+1] + 0x30;if(irdata[i + 1] > 6){putbuf[i + 2] = 1 + 0x30; }else{putbuf[i + 2] = 0 + 0x30;}}putbuf[34] = '\0';uart_send_str(putbuf);}/*void time1_init(void){TMOD |= 0X10;TH1 = 0XFC;TL1 = 0X18;EA = 1;TR1 = 1;ET1 = 1;}void time1_isr(void) interrupt 3{ET1 = 0;TH1 = 0XFC;TL1 = 0X18;LCD_Write_Char(0,0,num + 0x30);ET1 = 1;} */void main(void){exit0_init();time0_init();//time1_init();//uart_init();LCD_Init();LCD_Clear();while(1){if(irok){ircordpro();if(irpro_ok){ir_work();irpro_ok = 0;}irok = 0;}}}
#include <reg52.h>#include "uart.h"void uart_init(void){SCON = 0x50;//设置工作方式为方式1,允许串口接收TMOD |= 0X20;//定时器1工作于方式2TH1 = 0XFD;//波特率为9600TR1 = 1;EA = 1;//ES = 1;}void uart_send_byte(unsigned char byte){SBUF = byte;while(!TI);//TI为自动至1,手动清零TI = 0;}void uart_send_str(unsigned char *s){while(*s){uart_send_byte(*s);s++;}}
/*----------------------------------------------- 名称:LCD1602 论坛:www.doflye.net 编写:shifang 日期:2009.5 修改:无 内容: 引脚定义如下:1-VSS 2-VDD 3-V0 4-RS 5-R/W 6-E 7-14 DB0-DB7 15-BLA 16-BLK------------------------------------------------*/#include "1602.h"#include "delay.h"sbit RS = P2^4; //定义端口 sbit RW = P2^5;sbit EN = P2^6;#define RS_CLR RS=0 #define RS_SET RS=1#define RW_CLR RW=0 #define RW_SET RW=1 #define EN_CLR EN=0#define EN_SET EN=1#define DataPort P0/*------------------------------------------------ 判忙函数------------------------------------------------*/ bit LCD_Check_Busy(void) { DataPort= 0xFF; RS_CLR; RW_SET; EN_CLR; _nop_(); EN_SET; return (bit)(DataPort & 0x80); }/*------------------------------------------------ 写入命令函数------------------------------------------------*/ void LCD_Write_Com(unsigned char com) { // while(LCD_Check_Busy()); //忙则等待 DelayMs(5); RS_CLR; RW_CLR; EN_SET; DataPort= com; _nop_(); EN_CLR; }/*------------------------------------------------ 写入数据函数------------------------------------------------*/ void LCD_Write_Data(unsigned char Data) { //while(LCD_Check_Busy()); //忙则等待 DelayMs(5); RS_SET; RW_CLR; EN_SET; DataPort= Data; _nop_(); EN_CLR; }/*------------------------------------------------ 清屏函数------------------------------------------------*/ void LCD_Clear(void) { LCD_Write_Com(0x01); DelayMs(5); }/*------------------------------------------------ 写入字符串函数------------------------------------------------*/ void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { LCD_Write_Com(0x80 + x); //表示第一行 } else { LCD_Write_Com(0xC0 + x); //表示第二行 } while (*s) { LCD_Write_Data( *s); s ++; } }/*------------------------------------------------ 写入字符函数------------------------------------------------*/void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) { if (y == 0) { LCD_Write_Com(0x80 + x); } else { LCD_Write_Com(0xC0 + x); } LCD_Write_Data( Data); }/*------------------------------------------------ 初始化函数------------------------------------------------*/ void LCD_Init(void) { LCD_Write_Com(0x38); /*显示模式设置*/ DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); LCD_Write_Com(0x08); /*显示关闭*/ LCD_Write_Com(0x01); /*显示清屏*/ LCD_Write_Com(0x06); /*显示光标移动设置*/ DelayMs(5); LCD_Write_Com(0x0C); /*显示开及光标设置*/ } #include "delay.h"void DelayUs2x(unsigned char t) //2t+7{while(--t);}void DelayMs(unsigned char t){while(t--){DelayUs2x(200);DelayUs2x(254);}}void DelayS(unsigned char t){unsigned char i;for(i = 0; i < t; i++){ DelayMs(200); DelayMs(200); DelayMs(200); DelayMs(200); DelayMs(200);}}
0 0
- IR
- ir
- IR
- ir
- IR资料
- GR/IR
- GR/IR
- GR/IR
- IR 遥控器
- Verilog IR
- GR/IR
- IR介绍
- VEX IR
- ir-runner
- LLVM IR
- ir穷途末路
- ir.c
- nerual IR
- React 入门实例教程
- (Ryan的Redis系列博客)4.Redis的键
- java poi的简单应用
- 跑路 洛谷1613 倍增 floyd
- go-访问redis
- ir
- CocoaPods构建私有库文档(一)
- aws 防火墙策略调整注意10/8段
- windows 下单机安装Spark开发环境
- 基于单片机的光立方设计
- Duilib程序中弹出文件对话框
- Reverse Integer
- 任务调度(Schedule)-(优先队列->建堆)
- Eclipse平台搭建Python-Selenium的WEB自动化平台