首页 > 学院 > 开发设计 > 正文

用遥控器控制LCD显示按键数值

2019-11-08 19:50:23
字体:
来源:转载
供稿:网友
#include<reg52.h>#include<stdio.h>#include <intrins.h>#include"./uart/uart.h"#include"./delay/delay.h"#include"1602.h"sbit IR_IN = P3^2;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;unsigned int irtime;bit irok = 0;bit chang_ok = 0;bit startflag = 0;unsigned char irdata[33];unsigned char sbuf[5];unsigned char bitnum = 0;unsigned char str[35];unsigned char buf[4];unsigned char handle_ok = 0;void int0_init(){    IT0 = 1; //设置外部中断的触发方式	EA = 1;	EX0 = 1;}void timer0_init(){    EA = 1;	TMOD |= 0x02;	TH0 = 0;	ET0 = 1;	TR0 = 1;}void timer0_isr() interrupt 1{   irtime++;//0.256ms  引导码 13.5/0.256 = 52 		1.12/0.256 = 4   0.56+1.685/0.256 = 8}/*void chang_num(){	unsigned char i;	str[0] = irdata[0] / 10 + '0';	str[1] = irdata[0] % 10 + '0';	for(i = 0; i < 32; i++)	{		str[i + 2] = irdata[i + 1] + '0';				if(str[i + 2] <= (6 +'0'))		{			str[i + 2] = 0 + '0';		}		else		{			str[i + 2] = 1 + '0';		}	}	str[34] = '/0';	chang_ok = 1;} */void num_handle(){	 unsigned char i,j,k = 1;	 unsigned char temp = 0;	 if(irok == 1)	 {	 	for(i = 0; i < 4; i++)	 	{	 		for(j = 0; j < 8; j++)			{				temp >>= 1;			 	if(irdata[k] > 6)			 	{			 		temp += 0x80;			 	}	 			   			 	k++;			}			buf[i] = temp;	 	}		irok = 0;	}	if((buf[0] + buf[1] == 0xff) && (buf[2] + buf[3] == 0xff))	{		handle_ok = 1;		}}void main(){    timer0_init();	int0_init();	uart_init();	LCD_Init();	LCD_Clear();     while(1)	{	    if(irok == 1)		{//			chang_num();			num_handle();			switch(buf[2])			{			/*	case 0x45: LED0 = ~ LED0;break;				case 0x46: LED1 = ~ LED1;break;				case 0x47: LED2 = ~ LED2;break;				case 0x44: LED3 = ~ LED3;break;				case 0x40: LED4 = ~ LED4;break;				case 0x43: LED5 = ~ LED5;break;				case 0x07: LED6 = ~ LED6;break;				case 0x15: LED7 = ~ LED7;break;							default:break;*/				case 0x45:						sPRintf(sbuf,"%c",1 + 0X30); 						LCD_Write_String(0,0,sbuf);						break;				case 0x46: 							sprintf(sbuf,"%c",2 + 0X30); 						LCD_Write_String(0,0,sbuf);						break;				case 0x47: 							sprintf(sbuf,"%c",3 + 0X30); 						LCD_Write_String(0,0,sbuf);						break;				case 0x44: 							sprintf(sbuf,"%c",4 + 0X30); 						LCD_Write_String(0,0,sbuf);						break;				case 0x40: 							sprintf(sbuf,"%c",5 + 0X30); 						LCD_Write_String(0,0,sbuf);						break;				case 0x43: 							sprintf(sbuf,"%c",6 + 0X30); 						LCD_Write_String(0,0,sbuf);						break;				case 0x07: 						sprintf(sbuf,"%c",7 + 0X30); 						LCD_Write_String(0,0,sbuf);						break;				case 0x15: 							sprintf(sbuf,"%c",8 + 0X30); 						LCD_Write_String(0,0,sbuf);						break;							default:break;			}			if(handle_ok == 1)			{				str[0] = (buf[0] / 16) > 9 ? (buf[0] / 16) + 0x37 : (buf[0] / 16) + 0x30;				str[1] = (buf[0] % 16) > 9 ? (buf[0] % 16) + 0x37 : (buf[0] % 16) + 0x30;				str[2] = (buf[1] / 16) > 9 ? (buf[1] / 16) + 0x37 : (buf[1] / 16) + 0x30;				str[3] = (buf[1] % 16) > 9 ? (buf[1] % 16) + 0x37 : (buf[1] % 16) + 0x30;				str[4] = (buf[2] / 16) > 9 ? (buf[2] / 16) + 0x37 : (buf[2] / 16) + 0x30;				str[5] = (buf[2] % 16) > 9 ? (buf[2] % 16) + 0x37 : (buf[2] % 16) + 0x30;				str[6] = (buf[3] / 16) > 9 ? (buf[3] / 16) + 0x37 : (buf[3] / 16) + 0x30;				str[7] = (buf[3] % 16) > 9 ? (buf[3] % 16) + 0x37 : (buf[3] % 16) + 0x30;				str[8] = '/r';				str[9] = '/n';				handle_ok = 0;			}		   	uart_send_string(str);			irok = 0;		}/*		if(chang_ok == 1)		{			uart_send_string(str);		    uart_send_byte('/n');			chang_ok = 0;		}  */	}}void int0_isr() interrupt 0{    if(startflag)	{		if((irtime > 40)&&(irtime < 60))		{		    bitnum = 0;		} 		irdata[bitnum] = irtime;		bitnum++;		irtime = 0;		if(bitnum == 33)		{		    irok = 1;		//	irdata[33] = '/0';			bitnum = 0;			startflag = 0;		}	}	else	{	    irtime = 0;		startflag = 1;	}}
#include<reg52.h>#include"./uart/uart.h"void uart_init(){	SCON = 0x50; //设置串口工作模式,打开接受允许	//SM0 = 0 ; SM1 = 1 ; REN = 0 ;	//TMOD &= 0x0f;    TMOD |= 0x20; //设置定时器1为工作方式2	TH1 = 0xfd;  //波特率9600									 	//ET1 = 0;	TR1 = 1;	//EA = 1;	//ES = 1;}void uart_send_byte(unsigned char byte){	SBUF = byte;	//TI位自动置1,手动清0	while(!TI);//  while(TI != 1);	TI = 0;}void uart_send_string(unsigned char *string){    while(*string)	{	    uart_send_byte(*string);		string++;	}	  }
#include "1602.h"#include "./delay/delay.h"#define CHECK_BUSY//是否使用判断忙语句条件,后面使用条件编译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)  { #ifdef CHECK_BUSY DataPort= 0xFF;  RS_CLR;  RW_SET;  EN_CLR;  _nop_();  EN_SET; return (bit)(DataPort & 0x80);#else return 0;#endif }/*------------------------------------------------              写入命令函数------------------------------------------------*/ void LCD_Write_Com(unsigned char com)  {   while(LCD_Check_Busy()); //忙则等待 RS_CLR;  RW_CLR;  EN_SET;  DataPort= com;  _nop_();  EN_CLR; }/*------------------------------------------------              写入数据函数------------------------------------------------*/ void LCD_Write_Data(unsigned char Data)  {  while(LCD_Check_Busy()); //忙则等待 RS_SET;  RW_CLR;  EN_SET;  DataPort= Data;  _nop_(); EN_CLR; }/*------------------------------------------------                清屏函数------------------------------------------------*/ void LCD_Clear(void)  {  LCD_Write_Com(0x01);  delay_ms(5); }/*------------------------------------------------              写入字符串函数------------------------------------------------*/ void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)  {             while (*s)  	{      LCD_Write_Char(x,y,*s);      s ++;  x++;    	} }/*------------------------------------------------              写入字符函数------------------------------------------------*/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);    /*显示模式设置*/    delay_ms(5);    LCD_Write_Com(0x38);    delay_ms(5);    LCD_Write_Com(0x38);    delay_ms(5);    LCD_Write_Com(0x38);     LCD_Write_Com(0x08);    /*显示关闭*/    LCD_Write_Com(0x01);    /*显示清屏*/    LCD_Write_Com(0x06);    /*显示光标移动设置*/    delay_ms(5);    LCD_Write_Com(0x0C);    /*显示开及光标设置*/}   
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表