|
有誰能教教我這個(gè)代碼怎么改嗎
內(nèi)容在附件里,謝謝
- //適用于市場(chǎng)上用量最大的HT6121/6122及其兼容的IC編碼。
- //使用外部中斷0接收P3.2
- //發(fā)射引腳(接PNP三極管b極)
- //PNP三極管e極接2Ω電阻,c極接紅外發(fā)射管
- #include<reg52.h>
- #include "lcd1602.h"
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define LCD_DATA P0
- #define SBM 0x80 //識(shí)別碼
- #define m9 (65536-9000) //約9mS
- #define m4_5 (65536-4500) //約4.5mS
- #define m1_6 (65536-1630) //約1.65mS
- #define m_65 (65536-580) //約0.65mS
- #define m_56 (65536-560) //約0.56mS
- #define m40 (65536-40000) //約40mS
- #define m56 (65536-56000) //56mS
- #define m2_25 (65536-2250) //約2.25mS
- sbit IRR = P3^6; //定義發(fā)射引腳(接PNP三極管基極)
- sbit LED = P2^3;
- uchar KEY(void);//按鍵
- void SanZhuan(void);//鍵值和ASCII碼值的轉(zhuǎn)換
- void ZZ(uchar x);//
- void Z0(uchar temp);
- void TT0(bit BT,uint x);
- void YS(uchar time);
- uchar code tab[] = {0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X39};
- uchar code set[] = {0X30,0X31,0X30,0X30,0X30,0X30,0X30,0X30,0X30,0X30};
- uchar code tab1[] = {0X4F,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X4F,0X39};
- uchar code tab2[] = {0X46,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X4E,0X39};
- sbit IR = P3^3; //接收引腳
- sbit LCD_RS=P2^0;
- sbit LCD_RW=P2^1;
- sbit LCD_EN=P2^2;
- uchar N[4]; //識(shí)別碼,操作碼存放
- uchar X1,X2,X3,X4;
- void XSQ(void);
- typedef unsigned int u16; //???????????
- typedef unsigned char u8;
- sbit IRIN=P3^2; //????
- u8 IrValue[4] = {'\0'};
- u8 Time;
- /*????,i=1?,????10us*/
- void delay(u16 i)
- {
- while(i--);
- }
- /*????*/
- void DigDisplay()
- {
- u8 i;
- write_com(0x83);
- for(i=0;i<4;i++)
- {
- if(IrValue[i]/16<10)
- {
- write_data(IrValue[i]/16 + 0x30);
- }
- else
- {
- write_data(IrValue[i]/16 + 0x37);
- }
- if(IrValue[i]%16<10)
- {
- write_data(IrValue[i]%16 + 0x30);
- }
- else
- {
- write_data(IrValue[i]%16 + 0x37);
- }
- }
- }
- /*??????0(????????)*/
- void IrInit()
- {
- IT0=1;//?????
- EX0=1;//????0??
- EA=1; //?????
- IRIN=1;//?????
- }
- /*???*
- /*???????????*/
- void ReadIr() interrupt 0
- {
- u8 j,k;
- u16 err;
- Time=0;
- delay(700); //7ms
- if(IRIN==0) //??????????????
- {
-
- err=1000; //1000*10us=10ms,????????????
- /*???????????,????????????????,????????
- ?,??????*/
- while((IRIN==0)&&(err>0)) //????9ms??????
- {
- delay(1);
- err--;
- }
- if(IRIN==1) //??????9ms???
- {
- err=500;
- while((IRIN==1)&&(err>0)) //??4.5ms????????
- {
- delay(1);
- err--;
- }
- for(k=0;k<4;k++) //??4???
- {
- for(j=0;j<8;j++) //??????
- {
- err=60;
- while((IRIN==0)&&(err>0))//???????560us?????
- {
- delay(1);
- err--;
- }
- err=500;
- while((IRIN==1)&&(err>0)) //???????????
- {
- delay(10); //0.1ms
- Time++;
- err--;
- if(Time>30) //??3ms ????
- {
- return;
- }
- }
- IrValue[k]>>=1; //k???????
- if(Time>=8) //?????????0.8ms,???1
- {
- IrValue[k]|=0x80;
- }
- Time=0; //?????????
- }
- }
- }
- if(IrValue[2]!=~IrValue[3]) //?? ??? ????? ???? ?????
- {
- return;
- }
- }
- }
- /***************************************************************
- 函數(shù)功能: 4×4矩陣鍵盤
- ***************************************************************/
- uchar KEY(void)
- {
- uchar H,L; //行值,列值
- H=0;
- L=0;
- P1 = 0xf0; //將鍵盤端口行值設(shè)為1,列值設(shè)為0
- if(P1!= 0xf0) //檢測(cè)是否有按鍵按下,如果有則hangval必不為0x0f
- {
- YS(10); //按鍵去抖動(dòng),延時(shí)10毫秒
- if(P1!=0xf0) //確實(shí)有按鍵按下
- {
- H = P1&0xf0; //按鍵后得到按鍵的行標(biāo)志位,將行標(biāo)志位賦值給hangval
- P1 = 0x0f; //翻轉(zhuǎn)鍵盤接口輸出
- L = P1&0x0f; //得到列標(biāo)志位
- }
- return (H+L);
- }
- return 0;
- }
- /***************************************************************
- 函數(shù)功能:散轉(zhuǎn)程序
- 入口參數(shù):v
- // 13 14 15 16
- // 9 10 11 12
- // 5 6 7 8
- // 1 2 3 4
- ***************************************************************/
- void SanZhuan(void)
- {
- uchar v;
- v =KEY();
- switch(v)
- {
- case 0x77:ZZ(0x00);v=0;break; //"0"
- case 0xb7:ZZ(0x01);v=0;break; //"1 "
- case 0xd7:ZZ(0x02);v=0;break; //"2 "
- case 0xe7:ZZ(0x03);v=0;break; //"3"
- case 0x7b:ZZ(0x04);v=0;break; //"4 "
- case 0xbb:ZZ(0x05);v=0;break; //"5"
- case 0xdb:ZZ(0x06);v=0;break; //"6"
- case 0xeb:ZZ(0x07);v=0;break; //"7"
- case 0x7d:ZZ(0x08);v=0;break; //"8"
- case 0xbd:ZZ(0x09);v=0;break; //"9"
- case 0xdd:ZZ(0x0a);v=0;break; //"a"
- case 0xed:ZZ(0x0b);v=0;break; //"b"
- case 0x7e:ZZ(0x0c);v=0;break; //"c"
- case 0xbe:ZZ(0x0d);v=0;break; //"d"
- case 0xde:ZZ(0x1e);v=0;break; //"e"
- case 0xee:ZZ(0x1f);v=0;break; //"f"
- default:v=0;break;
- }
- }
- /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
- 函數(shù)功能:發(fā)送主程序
- ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
- void ZZ(uchar x)
- {
- TT0(1,m9); //高電平9mS
- TT0(0,m4_5); //低電平4.5mS引導(dǎo)
- /*┈ 發(fā)送4幀數(shù)據(jù) ┈*/
- Z0(SBM); //識(shí)別碼
- Z0(~SBM);
- Z0(x); //數(shù)據(jù)碼
- Z0(~x);
- /*┈┈ 結(jié)束碼 ┈┈*/
- TT0(1,m_65);
- TT0(0,m40);
- /*┈┈ 重復(fù)碼 ┈┈*/
-
- while(KEY())
- {
- TT0(1,m9);
- TT0(0,m2_25);
- TT0(1,m_56); //38khz
- TT0(0,m40);
- TT0(0,m56);
- LED = !LED; //指示燈
- }
- LED = 1;
- }
- /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
- 函數(shù)功能:?jiǎn)螏l(fā)送程序
- 入口參數(shù):1幀數(shù)據(jù)
- ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
- void Z0(uchar temp)
- {
- uchar v;
- for (v=0;v<8;v++) //循環(huán)8次移位
- {
- TT0(1,m_65); //高電平0.65mS
- if(temp&0x01==1) TT0(0,m1_6); //發(fā)送最低位
- else TT0(0,m_56);
- temp >>= 1; //右移一位
- }
- }
- /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
- 函數(shù)功能:38KHz脈沖發(fā)射 + 延時(shí)程序
- 入口參數(shù):(是否發(fā)射脈沖,延時(shí)約 x (uS))
- ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/
- void TT0(bit BT,uint x)
- {
- TH0 = x/256; //輸入T0初始值
- TL0 =x%256;
- TF0=0; //清0
- TR0=1; //啟動(dòng)定時(shí)器0
- if(BT == 0)
- while(!TF0); //BT=0時(shí)不發(fā)射38KHz脈沖只延時(shí);BT=1發(fā)射38KHz脈沖且延時(shí);
- else while(1) //38KHz脈沖,占空比5:26
- {
- IRR = 0;
- if(TF0)break;
- if(TF0)break;
- IRR = 1;
- if(TF0)break;
- if(TF0)break;
- if(TF0)break;
- if(TF0)break;
- if(TF0)break;
- if(TF0)break;
- if(TF0)break;
- if(TF0)break;
- if(TF0)break;
- if(TF0)break;
- }
- TR0=0; //關(guān)閉定時(shí)器0
- TF0=0; //標(biāo)志位溢出則清0
- IRR =1; //脈沖停止后,發(fā)射端口常態(tài)為高電平
- }
- /***************************************************************
- 函數(shù)功能:按鍵消抖 延時(shí)程序
- 入口參數(shù):1ms×time (晶振=12MHz)
- ***************************************************************/
- void YS(uchar time)
- {
- uchar i,j;
- for(i=0; i<time; i++)
- for(j=0; j<247; j++)_nop_();
- }
- /*******************************接收*********************************************/
- void delay1(unsigned char t) {
- unsigned char i;
- while(t--)
- for(i = 200; i > 0; i--);
- }
- void LCD_write_instruction(unsigned char LCD_instruction) { //寫指令到LCD
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_DATA = LCD_instruction;
- LCD_EN = 1;
- _nop_();
- LCD_EN = 0;
- delay(1);
- }
- void LCD_write_data(unsigned char LCD_data) { //輸出一個(gè)字節(jié)數(shù)據(jù)到LCD
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_DATA = LCD_data;
- LCD_EN = 1;
- _nop_();
- LCD_EN = 0;
- delay(1);
- }
- void LCD_set_position(unsigned char x) { //LCD光標(biāo)定位到x處
- LCD_write_instruction(0x80|x);
- }
- void LCD_printc(unsigned char lcd_data) { //輸出一個(gè)字符到LCD
- LCD_write_data(lcd_data);
- }
- void LCD_prints(unsigned char *lcd_string) { //輸出一個(gè)字符串到 LCD
- unsigned char i = 0;
- while(lcd_string[i] != 0x00) {
- LCD_write_data(lcd_string[i]);
- i++;
- }
- }
- void LCD_initial(void) { //初始化 LCD
- LCD_write_instruction(0x38);
- LCD_write_instruction(0x0c);
- LCD_write_instruction(0x06);
- LCD_write_instruction(0x01);
- }
- /**********************LCD顯示函數(shù)************************/
- void XSQ(void)
- {
- while(1) {
- uchar temp1,temp2,temp3,temp4;
- temp1=tab[X4];
- temp2=set[X3];
- temp3=tab1[X1];
- temp4=tab2[X1];
- LCD_set_position(0x88); //LCD換行顯示
- LCD_printc(temp3);
- LCD_set_position(0x89); //LCD換行顯示
- LCD_printc(temp4);
- LCD_set_position(0xC8); //LCD換行顯示
- LCD_printc(temp2);
- LCD_set_position(0xC9); //LCD換行顯示
- LCD_printc(temp1);
- SanZhuan();
- }
- }
- /********************** 外部中斷函數(shù)************************/
- void exint0() interrupt 1
- {
- uint cnt;
- uchar i;
- EX0 =0;
- cnt = 0;
- while(!IR) cnt++; //記錄引導(dǎo)碼時(shí)間
- if(cnt < 1000){EX0=1;return;} //9ms的計(jì)數(shù)值(12MHz:1000< cnt <1500)
- cnt =0;
- while(IR) if(cnt++ > 400){EX0=1;return;} //防卡死,超時(shí)保護(hù)(12MHz: > 300)
- if(cnt < 200){EX0=1;return;} //(12MHz不分頻: <260)
- for(i=0; i<32; i++) //讀取32位位碼
- {
- cnt =0;
- while(!IR);
- while(IR) if(cnt++ > 200){EX0=1;return;} //超時(shí)保護(hù)(12MHz:>=200)
- N[i/8] >>= 1;
- if(cnt>60) N[i/8] |= 0x80; //0和1的計(jì)數(shù)界線(12MHz:< 109)
- }
- if(N[0] == ~N[1] && N[2] == ~N[3]) //校驗(yàn)識(shí)別碼,操作碼
- {
- X1 =N[0]/16;
- X2 =N[0]%16;
- X3 = N[2]/16;
- X4 = N[2]%16;
- }
- EX0 =1;
- }
- /****************** 主函數(shù) *******************/
- void main(void)
- {
- TMOD = 0x01; //T0 16位工作方式 //發(fā)射端口常態(tài)為高電平
- IRR=1; //發(fā)射端口常態(tài)為高電平
- IrInit(); //????0(?????)
- init_lcd(); //???1602
- while(1)
- {
- DigDisplay(); //??
- SanZhuan();
- }
-
-
- }
復(fù)制代碼
代碼.rar
(45.91 KB, 下載次數(shù): 5)
2021-7-8 17:58 上傳
點(diǎn)擊文件名下載附件
里面是代碼
|
|