|
- #include<reg52.h>
- #include<intrins.h>
- #include<stdio.h>
- #define DataPort P2 //8位數(shù)據(jù)口(硬件)
- #define Busy 0x80
- #define LED P0
- //#define HLED P3
- #define uchar unsigned char
- #define uint unsigned int
- sbit jia=P1^1;
- sbit jian=P1^2;
- sbit IR=P3^2;
- sbit P1_6=P1^6;
- sbit P1_7=P1^7;
- sbit jiguang=P1^5;
- unsigned char a[4];
- unsigned int LowTime,HighTime; //
- uchar yaokong;
- uchar SecondL = 0; //時(shí)間秒低位
- uchar SecondH = 0; //時(shí)間秒高位
- uchar MinuteL =2; //時(shí)間分低位
- uchar MinuteH =3; //時(shí)間分高
- uchar hourh=0;
- uchar hourl=7;
- bit kai=0;
- uchar yueh=0,yuel=5;
- uchar rih=2,ril=8;
- uchar xingqi=3;
- unsigned char IRCode[4],num, shu;
- //uchar ircount;
- //uchar irtime[33];
- //uchar irdate[33];
- //uchar irnum;
- //uchar irflag;
- //sbit irflag=P3^2;
- // irrecOK;
- //uchar ircode[4];
- //uchar irvalue;
- //uchar dsirOK;
- //uchar code16H[8];
- //uchar irtranOK;
- /*********************************************
- 引腳位定義
- **********************************************/
- sbit RS=P3^7; //寄存器選擇輸入端(硬件)
- sbit RW=P3^6; //讀寫(xiě)控制輸入端(硬件)
- sbit E =P3^5; //使能信號(hào)輸入端(硬件)
- /*********************************************
- 函數(shù)名稱: delay
- 函數(shù)功能: 簡(jiǎn)易延時(shí)函數(shù),us級(jí)延時(shí)
- 函數(shù)輸入: j-延時(shí)時(shí)間控制變量
- 函數(shù)輸出: 無(wú)
- **********************************************/
- void delay(unsigned int j)
- {
- for(j;j>0;j--);
- }
- /*********************************************
- 函數(shù)名稱: Delay5Ms
- 函數(shù)功能: 固定延時(shí)時(shí)間延時(shí)函數(shù),延時(shí)5ms
- 函數(shù)輸入: 無(wú)
- 函數(shù)輸出: 無(wú)
- **********************************************/
-
- void DelayA(void)
- {
- unsigned int TempCyc;
- for (TempCyc=0;
- TempCyc<3500; TempCyc++); //3.5ms
- }
- void DelayB(void)
- {
- unsigned int TempCyc;
- for (TempCyc=0; TempCyc<840; TempCyc++); // 0.84ms
- }
-
- void Delay5Ms(void)
- {
- unsigned int TempCyc = 4552;
- while(TempCyc--);
- }
- /*********************************************
- 函數(shù)名稱: Lcddelay
- 函數(shù)功能: LCD毫秒級(jí)延時(shí)函數(shù)
- 函數(shù)輸入: MS-延時(shí)時(shí)間為MS*ms
- 函數(shù)輸出: 無(wú)
- **********************************************/
- void Lcddelay(uint MS)
- {
- unsigned char i,j;
- while(MS!=0)
- {
- j = 4;
- while(j!=0)
- {
- i=0xf0;
- while(i!=0){i--;}
- j--;
- }
- MS--;
- }
- }
- /*********************************************
- 函數(shù)名稱: WaitForEnable
- 函數(shù)功能: 檢測(cè)lcd狀態(tài)。注意有的LCD檢測(cè)忙時(shí)會(huì)一直處
- 于忙狀態(tài),導(dǎo)致程序不能正常工作,這時(shí)可以
- 講忙檢測(cè)屏蔽掉試試。
- 函數(shù)輸入: 無(wú)
- 函數(shù)輸出: 無(wú)
- **********************************************/
- void WaitForEnable(void)
- {
- DataPort=0xff;
- RS=0;
- RW=1;
- Lcddelay(5);
- _nop_();
- E=1;
- _nop_();
- _nop_();
- while(DataPort&Busy);
- E=0;
- }
- /*********************************************
- 函數(shù)名稱: LcdWriteCommand
- 函數(shù)功能: 向LCD寫(xiě)命令
- 函數(shù)輸入: CMD-LCD命令
- AttribC-忙檢測(cè)使能變量
- 函數(shù)輸出: 無(wú)
- **********************************************/
- void LcdWriteCommand(unsigned char CMD)
- {
- RS=0;
- RW=0;
- _nop_();
- DataPort=CMD;
- Lcddelay(5);
- _nop_();
- E=1;
- _nop_();
- _nop_();
- E=0;
- }
- /*********************************************
- 函數(shù)名稱: LocateXY
- 函數(shù)功能: 確定LCD的坐標(biāo)
- 函數(shù)輸入: polx-LCD橫坐標(biāo)
- poly-LCD縱坐標(biāo)
- 函數(shù)輸出: 無(wú)
- **********************************************/
- void LocateXY(char polx,char poly)
- {
- unsigned char temp;
- temp=polx&0xf;
- poly&=0x01;
- if(poly) temp|=0x40;
- temp|=0x80;
- LcdWriteCommand(temp);
- }
- /*********************************************
- 函數(shù)名稱: LcdWriteData
- 函數(shù)功能: 向LCD寫(xiě)數(shù)據(jù)
- 函數(shù)輸入: DataW-LCD數(shù)據(jù)
- 函數(shù)輸出: 無(wú)
- **********************************************/
- void LcdWriteData(char DataW)
- {
- RS=1;
- RW=0;
- _nop_();
- DataPort=DataW;
- Lcddelay(5);
- _nop_();
- E=1;
- _nop_();
- _nop_();
- E=0;
- }
- /*********************************************
- 函數(shù)名稱: DispOneChar
- 函數(shù)功能: 在指定位置顯示單個(gè)字符
- 函數(shù)輸入: x-橫坐標(biāo)
- y-縱坐標(biāo)
- Wlata-字符數(shù)據(jù)(單個(gè))
- 函數(shù)輸出: 無(wú)
- **********************************************/
- void DispOneChar(unsigned char x,unsigned char y,unsigned char WData)
- {
- LocateXY(x,y);
- LcdWriteData(WData);
- }
- /*********************************************
- 函數(shù)名稱: LcdReset
- 函數(shù)功能: LCD1602初始化
- 函數(shù)輸入: 無(wú)
- 函數(shù)輸出: 無(wú)
- **********************************************/
- void LcdReset(void)
- {
- LcdWriteCommand(0x38);
- Lcddelay(5);
- LcdWriteCommand(0x38);
- Lcddelay(5);
- LcdWriteCommand(0x38);
- Lcddelay(5);
- LcdWriteCommand(0x38);// 清屏
- LcdWriteCommand(0x08);
- LcdWriteCommand(0x01);
- LcdWriteCommand(0x06);
- LcdWriteCommand(0x0c);
- }
- /*********************************************
- 函數(shù)名稱: DisplayListChar
- 函數(shù)功能: 在指定位置顯示字符串
- 函數(shù)輸入: x-橫坐標(biāo)
- y-縱坐標(biāo)
- *Dlata-字符串?dāng)?shù)據(jù)指針
- 函數(shù)輸出: 無(wú)
- **********************************************/
- void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *SData,unsigned char L)
- {
- unsigned char i;
- for(i=0;i<L;i++)
- DispOneChar(X++,Y,SData[i]);
- }
- void Init_Timer(void)
- {
- TMOD = 0x11; //設(shè)定T1的工作模式為1,設(shè)定T0工作模式1
- //----裝入定時(shí)器初值 -----//
- TH0 = 0x95; //100us定時(shí)
- TL0 = 0x95;
- TH1 = (65536-50000)/256; //50ms定時(shí)
- TL1 = (65536-50000)%256;
- IT0 = 1;
- IP = 0x03;
- EA = 1; // 開(kāi)總中斷
- ET0 = 1; // 定時(shí)器0允許中斷
- ET1 = 1; // 定時(shí)器1允許中斷
- TR1 = 1; // 啟動(dòng)定時(shí)器1
- TR0 = 1; // 啟動(dòng)定時(shí)器0
- }
- void Timer1_ISR() interrupt 3
- {
- uint cnt;
- TH1 = (65536-50000)/256;
- TL1 = (65536-50000)%256;
- cnt++;
- if(cnt >=20)
- {
- cnt = 0;
- ++SecondL;
- if(SecondL == 10)
- {
- SecondL = 0;
- ++SecondH;
- if(SecondH == 6)
- {
- SecondH = 0;
- ++MinuteL;
- if(MinuteL == 10)
- {
- MinuteL = 0;
- ++MinuteH;
- if(MinuteH==6)
- {MinuteH=0;
- ++hourl;
- if(hourl==10)
- { hourl=0;
- ++hourh;
- if((hourh==2)&(hourl=4))
- {hourh=0;hourl=0;
- ++ril;++xingqi;if(xingqi==8) xingqi=1;
- if(ril==10)
- {ril=0;++rih;
- if((rih==3)&ril==1)
- {
- rih=1;ril=1;
- ++yuel;if(yuel==10)
- {yuel=0;++yueh;
- if((yueh==1)&(yuel==3)){ yuel=1;yueh=1; }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- /*void ComOutChar(unsigned char OutData)
- {
- SBUF = OutData; //輸出字符
- while(!TI); //空語(yǔ)句判斷字符是否發(fā)完
- TI = 0; //清TI
- }
- void INT0Fun(void) interrupt 0 using 2
- {
- //unsigned char IRCode[2], IROK;
- unsigned int TempCyc, TempCycA; EX0 = 0; //外部中斷0關(guān)閉
- IROK = 0;
- DelayA(); //延時(shí)等待引導(dǎo)碼的前半部結(jié)束
- DelayA();
- if (!P3_2) //檢驗(yàn)前半部是否過(guò)早結(jié)束,防干擾
- {
-
- {
- DelayA();
- if (P3_2) //檢驗(yàn)前半部是否過(guò)早結(jié)束,防干擾 ,檢驗(yàn)是否是正確的輸入信號(hào)
- {
- for (TempCyc=0; TempCyc<3500; TempCyc++)
- if (!P3_2) break; //等待第一個(gè)位,
- if (TempCyc<3500) //超時(shí)檢驗(yàn)
- for (TempCycA=0; TempCycA<4; TempCycA++)
- {
- for (TempCyc=0; TempCyc<8; TempCyc++)
- {
- while(!P3_2); //等待P3_2拉高,開(kāi)始位的下部分
- DelayB(); //這里沒(méi)設(shè)超時(shí),實(shí)際應(yīng)用在多功能的設(shè)計(jì)時(shí)應(yīng)設(shè)超時(shí)
- IRCode[TempCycA] = IRCode[TempCycA]>>1;
- if (P3_2) //當(dāng)延時(shí)750us后P3_2仍為高則當(dāng)前位為1
- {
- IRCode[TempCycA] = IRCode[TempCycA] | 0x80;
- // for (TempCycB=0; TempCycB<840; TempCycB++)
- // if (!P3_2) break; //等待下個(gè)位 當(dāng)位1時(shí)高電平為1.5ms,
- // if (TempCycB>840) //之前已延時(shí)了750us, 所以超時(shí)應(yīng)大于1.5ms-750us
- //; // goto endchk; //這里最大為1ms
- }
- else IRCode[TempCycA] = IRCode[TempCycA]&0x00;
- }
- }
- // else // goto endchk;
- }
- // else // goto endchk;
- // IROK++; //當(dāng)自定碼和數(shù)據(jù)碼都完成時(shí)為2
- LcdWriteCommand(0x01);
- LcdWriteCommand(0x0c);
- if(IRCode[2]=IRCode[3])
- DispOneChar(0,1,IRCode[2]+0x30);
- }
-
- } */
- /*endchk: if (IROK==2)
- {
- ComOutChar(IRCode[0]);
- ComOutChar(IRCode[1]); //連接PC串口查看自定義碼和數(shù)據(jù)碼
-
- if (IRCode[1]==0x10) //1號(hào)鍵 //只演示點(diǎn)亮2只LED,讀者可以自行擴(kuò)展控制別的器件
- P1_7 = ~P1_7; if (IRCode[1]==0x11) //2號(hào)鍵
- P1_6 = ~P1_6; for (TempCyc=0; TempCyc<300; TempCyc++)
- DelayA(); //延時(shí)
-
- }
- EX0 = 1;
-
- } */
- void busy()
- {
- LcdWriteCommand(0x01);
- LcdWriteCommand(0x0c);
- DisplayListChar(0,0,"STC89c52Rc",10);
- DisplayListChar(0,1,"hongwai yaokong",15);
- LcdWriteCommand(0x01);
- LcdWriteCommand(0x0c);
- }
- void hled()
- {char i, j;
- LcdWriteCommand(0x01);
- LcdWriteCommand(0x0c);
- DisplayListChar(0,0,"JIGUANFDENG$laba",16);
- DisplayListChar(0,1,"note:p1.5kou",12);
- for(j=30;j>0;j--)
- for(i=30;i>0;i--)
- {jiguang=0;
- delay(500);
- jiguang=1;
- delay(500); }
- }
- void led()
- {char i,j;
- LcdWriteCommand(0x01);
- LcdWriteCommand(0x0c);
- DisplayListChar(0,0,"liushuideng mode ",16);
- DisplayListChar(0,1,"da dang jia zuo",15);
- for(j=30;j>0;j--)
- for(i=0;i<8;i++)
- {LED=~(1<<i);
- delay(500); }
- }
- void time()
- { uchar i; ET1=1;TR1=1;
- LcdWriteCommand(0x01);
- LcdWriteCommand(0x0c);
- DisplayListChar(0,0,"Time",4);
- DispOneChar(9,0,':');
- DispOneChar(6,0,':');
- DispOneChar(4,0,hourh+0x30); //顯示時(shí)間”分“的十位
- DispOneChar(5,0,hourl+0x30); //顯示時(shí)間”分“的個(gè)
- DispOneChar(7,0,MinuteH+0x30); //顯示時(shí)間”分“的十位
- DispOneChar(8,0,MinuteL+0x30);
- DisplayListChar(0,1,"2014",4);
- DispOneChar(5,1,yueh+0x30); //顯示時(shí)間”分“的十位
- DispOneChar(6,1,yuel+0x30); //顯示時(shí)間”分“的個(gè)
- DispOneChar(7,1,'-');
- DispOneChar(8,1,rih+0x30); //顯示時(shí)間”分“的十位
- DispOneChar(9,1,ril+0x30); //顯示時(shí)間”分“的個(gè)位
- DispOneChar(11,1,xingqi+0x30); //顯示時(shí)間”秒“的十位
- for(i=255;i>0;i--)
- {
- DispOneChar(8,0,MinuteL+0x30); //顯示時(shí)間”分“的個(gè)位
- DispOneChar(10,0,SecondH+0x30); //顯示時(shí)間”秒“的十位
- DispOneChar(11,0,SecondL+0x30); //顯示時(shí)間”秒“的個(gè)位
- }
- // DispOneChar(12,1,SecondL+0x30); //顯示
- // delay(1000);
- }
-
- bit DeCode(void)
- {
- unsigned char i,j;
- unsigned char temp;
- LcdWriteCommand(0x01);
- LcdWriteCommand(0x0c);
- DisplayListChar(0,0,"start",4);
- DispOneChar(4,0,':');
- for(i=0;i<4;i++)
- {
- for(j=0;j<8;j++)
- {
- temp=temp>>1;
- TH0=0;
- TL0=0;
- TR0=1;
- while(IR==0);
- TR0=0;
- LowTime=TH0*256+TL0;
- TH0=0;
- TL0=0;
- TR0=1;
- while(IR==1);
- TR0=0;
- HighTime=TH0*256+TL0;
- if((LowTime<370)||(LowTime>640))
- return 0;
- if((HighTime>420)&&(HighTime<620))
- temp=temp&0x7f;
- if((HighTime>1300)&&(HighTime<1800))
- temp=temp|0x80;
- }
- a[i]=temp; }
- if(a[2]=~a[3])return 1;
- }
- void codedisplay()
- {uchar i;
- i=0;
- while(a[i]!="\0")
- { i++;
- }
- }
- void yaokong1()
- {
- LcdWriteCommand(0x0c);
- DisplayListChar(0,0,"enter the label",15);
- EA=1;
- ET1=0;
- TR1=0;
- ET0=1;
- DispOneChar(0,1,a[0]/100+0x30);
- DispOneChar(1,1,a[0]%100/10+0x30);
- DispOneChar(2,1,a[0]%10+0x30);
- while(1);
- }
- void key()
- {uchar k;
- P1=0xff;
- k=P1;
- if(k==0xff)
- return;
- Lcddelay(20);
- if(k==0xff)
- return;
- while(P1!=0xff)
- switch(k)
- {case 0xf3:LcdWriteCommand(0x01); break;
- case 0xFF:LcdWriteCommand(0x0c);DisplayListChar(0,0,"open the 1602",13); break;
- case 0xfd:busy();yaokong1();break;
- case 0xfb: hled(); break;
- case 0xf7:time(); break;
- case 0xef:led(); break;
- }
- }
- void main(void)
- {
-
- shu=0;
- LcdReset(); //液晶初始化
- Init_Timer(); //Timer0、Timer1初始化
- DisplayListChar(0,0,"name:da dang jia",16);
- DisplayListChar(0,1,"note:20121003920",16);
- yaokong=1; kai=0;
- while(1)
- {
- key();
- }
- }
-
- void Int0(void) interrupt 0
- {EX0=0;
- TH0=0;
- TL0=0;
- TR0=1;
- while(IR==0);
- TR0=0;
- LowTime=TH0*256+TL0;
- TH0=0;
- TL0=0;
- TR0=1;
- while(IR==1);
- TR0=0;
- HighTime=TH0*256+TL0;
- if((LowTime>7800)&&(LowTime<8800)&&(HighTime>3600)&&(HighTime<4700))
- {
- if(DeCode()==1)
- {
- codedisplay();}}
- EX0=1;
-
- }
復(fù)制代碼
|
|