|
- #include<reg52.h>
- #include<intrins.h>
- #include<stdio.h>
- #define DataPort P2 //8位數據口(硬件)
- #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; //時間秒低位
- uchar SecondH = 0; //時間秒高位
- uchar MinuteL =2; //時間分低位
- uchar MinuteH =3; //時間分高
- 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; //讀寫控制輸入端(硬件)
- sbit E =P3^5; //使能信號輸入端(硬件)
- /*********************************************
- 函數名稱: delay
- 函數功能: 簡易延時函數,us級延時
- 函數輸入: j-延時時間控制變量
- 函數輸出: 無
- **********************************************/
- void delay(unsigned int j)
- {
- for(j;j>0;j--);
- }
- /*********************************************
- 函數名稱: Delay5Ms
- 函數功能: 固定延時時間延時函數,延時5ms
- 函數輸入: 無
- 函數輸出: 無
- **********************************************/
-
- 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--);
- }
- /*********************************************
- 函數名稱: Lcddelay
- 函數功能: LCD毫秒級延時函數
- 函數輸入: MS-延時時間為MS*ms
- 函數輸出: 無
- **********************************************/
- void Lcddelay(uint MS)
- {
- unsigned char i,j;
- while(MS!=0)
- {
- j = 4;
- while(j!=0)
- {
- i=0xf0;
- while(i!=0){i--;}
- j--;
- }
- MS--;
- }
- }
- /*********************************************
- 函數名稱: WaitForEnable
- 函數功能: 檢測lcd狀態。注意有的LCD檢測忙時會一直處
- 于忙狀態,導致程序不能正常工作,這時可以
- 講忙檢測屏蔽掉試試。
- 函數輸入: 無
- 函數輸出: 無
- **********************************************/
- void WaitForEnable(void)
- {
- DataPort=0xff;
- RS=0;
- RW=1;
- Lcddelay(5);
- _nop_();
- E=1;
- _nop_();
- _nop_();
- while(DataPort&Busy);
- E=0;
- }
- /*********************************************
- 函數名稱: LcdWriteCommand
- 函數功能: 向LCD寫命令
- 函數輸入: CMD-LCD命令
- AttribC-忙檢測使能變量
- 函數輸出: 無
- **********************************************/
- void LcdWriteCommand(unsigned char CMD)
- {
- RS=0;
- RW=0;
- _nop_();
- DataPort=CMD;
- Lcddelay(5);
- _nop_();
- E=1;
- _nop_();
- _nop_();
- E=0;
- }
- /*********************************************
- 函數名稱: LocateXY
- 函數功能: 確定LCD的坐標
- 函數輸入: polx-LCD橫坐標
- poly-LCD縱坐標
- 函數輸出: 無
- **********************************************/
- void LocateXY(char polx,char poly)
- {
- unsigned char temp;
- temp=polx&0xf;
- poly&=0x01;
- if(poly) temp|=0x40;
- temp|=0x80;
- LcdWriteCommand(temp);
- }
- /*********************************************
- 函數名稱: LcdWriteData
- 函數功能: 向LCD寫數據
- 函數輸入: DataW-LCD數據
- 函數輸出: 無
- **********************************************/
- void LcdWriteData(char DataW)
- {
- RS=1;
- RW=0;
- _nop_();
- DataPort=DataW;
- Lcddelay(5);
- _nop_();
- E=1;
- _nop_();
- _nop_();
- E=0;
- }
- /*********************************************
- 函數名稱: DispOneChar
- 函數功能: 在指定位置顯示單個字符
- 函數輸入: x-橫坐標
- y-縱坐標
- Wlata-字符數據(單個)
- 函數輸出: 無
- **********************************************/
- void DispOneChar(unsigned char x,unsigned char y,unsigned char WData)
- {
- LocateXY(x,y);
- LcdWriteData(WData);
- }
- /*********************************************
- 函數名稱: LcdReset
- 函數功能: LCD1602初始化
- 函數輸入: 無
- 函數輸出: 無
- **********************************************/
- 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);
- }
- /*********************************************
- 函數名稱: DisplayListChar
- 函數功能: 在指定位置顯示字符串
- 函數輸入: x-橫坐標
- y-縱坐標
- *Dlata-字符串數據指針
- 函數輸出: 無
- **********************************************/
- 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; //設定T1的工作模式為1,設定T0工作模式1
- //----裝入定時器初值 -----//
- TH0 = 0x95; //100us定時
- TL0 = 0x95;
- TH1 = (65536-50000)/256; //50ms定時
- TL1 = (65536-50000)%256;
- IT0 = 1;
- IP = 0x03;
- EA = 1; // 開總中斷
- ET0 = 1; // 定時器0允許中斷
- ET1 = 1; // 定時器1允許中斷
- TR1 = 1; // 啟動定時器1
- TR0 = 1; // 啟動定時器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); //空語句判斷字符是否發完
- TI = 0; //清TI
- }
- void INT0Fun(void) interrupt 0 using 2
- {
- //unsigned char IRCode[2], IROK;
- unsigned int TempCyc, TempCycA; EX0 = 0; //外部中斷0關閉
- IROK = 0;
- DelayA(); //延時等待引導碼的前半部結束
- DelayA();
- if (!P3_2) //檢驗前半部是否過早結束,防干擾
- {
-
- {
- DelayA();
- if (P3_2) //檢驗前半部是否過早結束,防干擾 ,檢驗是否是正確的輸入信號
- {
- for (TempCyc=0; TempCyc<3500; TempCyc++)
- if (!P3_2) break; //等待第一個位,
- if (TempCyc<3500) //超時檢驗
- for (TempCycA=0; TempCycA<4; TempCycA++)
- {
- for (TempCyc=0; TempCyc<8; TempCyc++)
- {
- while(!P3_2); //等待P3_2拉高,開始位的下部分
- DelayB(); //這里沒設超時,實際應用在多功能的設計時應設超時
- IRCode[TempCycA] = IRCode[TempCycA]>>1;
- if (P3_2) //當延時750us后P3_2仍為高則當前位為1
- {
- IRCode[TempCycA] = IRCode[TempCycA] | 0x80;
- // for (TempCycB=0; TempCycB<840; TempCycB++)
- // if (!P3_2) break; //等待下個位 當位1時高電平為1.5ms,
- // if (TempCycB>840) //之前已延時了750us, 所以超時應大于1.5ms-750us
- //; // goto endchk; //這里最大為1ms
- }
- else IRCode[TempCycA] = IRCode[TempCycA]&0x00;
- }
- }
- // else // goto endchk;
- }
- // else // goto endchk;
- // IROK++; //當自定碼和數據碼都完成時為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串口查看自定義碼和數據碼
-
- if (IRCode[1]==0x10) //1號鍵 //只演示點亮2只LED,讀者可以自行擴展控制別的器件
- P1_7 = ~P1_7; if (IRCode[1]==0x11) //2號鍵
- P1_6 = ~P1_6; for (TempCyc=0; TempCyc<300; TempCyc++)
- DelayA(); //延時
-
- }
- 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); //顯示時間”分“的十位
- DispOneChar(5,0,hourl+0x30); //顯示時間”分“的個
- DispOneChar(7,0,MinuteH+0x30); //顯示時間”分“的十位
- DispOneChar(8,0,MinuteL+0x30);
- DisplayListChar(0,1,"2014",4);
- DispOneChar(5,1,yueh+0x30); //顯示時間”分“的十位
- DispOneChar(6,1,yuel+0x30); //顯示時間”分“的個
- DispOneChar(7,1,'-');
- DispOneChar(8,1,rih+0x30); //顯示時間”分“的十位
- DispOneChar(9,1,ril+0x30); //顯示時間”分“的個位
- DispOneChar(11,1,xingqi+0x30); //顯示時間”秒“的十位
- for(i=255;i>0;i--)
- {
- DispOneChar(8,0,MinuteL+0x30); //顯示時間”分“的個位
- DispOneChar(10,0,SecondH+0x30); //顯示時間”秒“的十位
- DispOneChar(11,0,SecondL+0x30); //顯示時間”秒“的個位
- }
- // 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;
-
- }
復制代碼
|
|