|
過年時,有塊開發板,剛好有空,就寫了個c52紅外遙控1602顯示的萬年歷,網上有很多的代碼,都大同小異,自己也獻丑下。
下面有幾個文件 大家復制出來整理一下 我這邊測試成功了.
- //ir.h
- #define uchar unsigned char //???
- #define uint unsigned int
- sbit HWRx=P3^3; //???
- uchar Table_Rx[67];//?????????1?0???
- void Delay() //???????31us,??12M
- {
- uchar i=13;
- while(i)
- i--;
- }
- void Delay_ms(uint z) //?????? 1ms*X ???12M
- {
- uint x=0,y=0;
- for(x=z;x>0;x--)
- for(y=54;y>0;y--);
- }
- void hongwai() interrupt 2 //???? 1 ,INT1(P3^3)???????IC???
- {
- uchar i,j,tmp;
- EX1=0; //????
- j=33; //??????????1?,4?????,??33?
- i=0; //????????
-
- Delay_ms(10);
- if(HWRx)
- { //????????????????,???,?????
- EX1=1;
- return;
- }
-
- while(j--)
- {
- tmp=0;
- Table_Rx[i]=1; //????1??
- while(!HWRx) //?????????,?????????
- {
- Table_Rx[i]++; //?????1
- Delay(); //???????,??????????
- tmp++; //?1
- if(tmp==250)
- break;
- }
- i++;
- tmp=0;
- Table_Rx[i]=1; //????1??
- while(HWRx) //?????????,?????????
- {
- Table_Rx[i]++; //?????1
- Delay(); //??
- tmp++; //?1,?????1??0?,?????
- if(tmp==250)
- break;
- }
- i++;
- }
- P1=0xf8;
- i=200; //??????,???
- while(i) //?????????????H
- {
- tmp=255;
- while(tmp)
- {
- tmp--;
- P2=0x76;
- }
- i--;
- }
- tmp=0;
- for(i=3;i<19;i++,i++) //???????
- {
- tmp>>=1; //????,??????
- if(Table_Rx[i]>30) //?????????30????1
- tmp+=0x80;
- }
- Table_Data[0]=tmp/16; //??2??16????,????????
- Table_Data[1]=tmp%16;
- tmp=0;
- for(i=19;i<35;i++,i++) //??
- {
- tmp>>=1;
- if(Table_Rx[i]>30)
- tmp+=0x80;
- }
- Table_Data[2]=tmp/16;
- Table_Data[3]=tmp%16;
- tmp=0;
- for(i=35;i<51;i++,i++)
- {
- tmp>>=1;
- if(Table_Rx[i]>30)
- tmp+=0x80;
- }
- Table_Data[4]=tmp/16;
- Table_Data[5]=tmp%16;
- tmp=0;
- for(i=51;i<67;i++,i++)
- {
- tmp>>=1;
- if(Table_Rx[i]>30)
- tmp+=0x80;
- }
- Table_Data[6]=tmp/16;
- Table_Data[7]=tmp%16;
- EX1=1; //??????????,?????
- }
- //1602.c
- #include<reg52.H>
- #include <stdio.h>
- #include<string.h>
- #include"TIME.C"
- #include"ir.c"
- #define uchar unsigned char
- sfr lcd_data=0x80;
- sbit lcd_E=P3^5;
- sbit lcd_RW=P3^6;
- sbit lcd_RS=P3^7;
- unsigned char code show1[]="Dear lu yun:";
- unsigned char code show2[]="Happy new year!";
- uchar i,j,value;
- void f_v_delay5ms(void)
- {
- unsigned char i;
- unsigned char j;
- for(i=0;i<20;i++)
- {
- for(j=0;j<250;j++);
- }
- }
- void f_v_delay50us(void)
- {
- unsigned char i;
- for(i=0;i<50;i++)
- {;}
- }
- void f_v_delay2ms(void)
- {
- unsigned char i;
- unsigned char j;
- for(i=0;i<40;i++)
- {
- for(j=0;j<50;j++);
- }
- }
- void f_v_delay10ms(void)
- {
- f_v_delay5ms();
- f_v_delay5ms();
- }
- void f_uc_lcdReadStatus(void)
- {
- lcd_data=0xff;
- lcd_RS = 0;
- lcd_RW = 1;
- lcd_E = 1;
- f_v_delay50us();
- while(P2&0x80);//?????
- lcd_E = 0;
- }
- void f_v_lcdWriteData(unsigned char WDLCM)
- {
- P2=0XFF;
- f_uc_lcdReadStatus(); //???
- P2 = WDLCM;
- lcd_RS = 1;
- lcd_RW = 0;
- lcd_E = 1;//?????????????????
- f_v_delay50us();//??
- lcd_E = 0;
- }
- void f_v_lcdWriteCommand(unsigned char WCLCM,unsigned char BuysC)//BuysC?0??????
- {P2=0XFF;
- if (BuysC)
- f_uc_lcdReadStatus();//???????
- P2 = WCLCM;
- lcd_RS = 0;
- lcd_RW = 0;
- lcd_E = 1;
- f_v_delay50us();//??
- lcd_E = 0;
- }
- void f_v_displayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
- {
- Y &= 0x1;
- X &= 0xF;//??X????15,Y????1
-
- if (Y)
- X |= 0x40;//???????????+0x40;
- X |= 0x80;// ?????
-
- f_v_lcdWriteCommand(X, 0);//????????,?????
- f_v_lcdWriteData(DData);
-
- }
- void f_v_displayListChar(unsigned char X, unsigned char Y, unsigned char *DData)
- {
- unsigned char ListLength;
- ListLength = 0;
- Y &= 0x1;
- X &= 0xF;//??X????15,Y????1
-
- while (DData[ListLength]>=0x20)//?????????
- {
- if (X <= 0xF)//X?????0xF
- {
- f_v_displayOneChar(X, Y, DData[ListLength]);//??????
- ListLength++;
- X++;
- }
- }
- }
- void f_v_lcdInit(void)
- { P1=0X00;P0=0XFF;
- lcd_data = 0;
- f_v_lcdWriteCommand(0x38,0);//????????,??????
- f_v_delay5ms();
- f_v_lcdWriteCommand(0x38,0);
- f_v_delay5ms();
- f_v_lcdWriteCommand(0x38,0);
- f_v_delay5ms();
- f_v_lcdWriteCommand(0x38,1);//??????,???????????
-
- f_v_lcdWriteCommand(0x08,1);//????
- f_v_lcdWriteCommand(0x01,1);//????
- f_v_delay2ms();
- f_v_lcdWriteCommand(0x06,1);// ????????
- f_v_lcdWriteCommand(0x0C,1);// ????????
- }
- void main(void)
- { intertime();
- i=0;
- j=0;
-
- for(i=0;i<16;i++)
- {
-
- f_v_lcdInit();
-
- for(j=0;j<strlen(show1);j++)
- {
- f_v_displayOneChar((i+j)%16,0,show1[j]);
- if((i+j)/16==1)
- f_v_displayOneChar((i+j)%16,0,' ');
- }
- for(j=0;j<strlen(show2);j++)
- {
- f_v_displayOneChar((i+j)%16,1,show2[j]);
- if((i+j)/16==1)
- f_v_displayOneChar((i+j)%16,1,' ');
- }
- }
-
- f_v_lcdInit();
- while(1)
- {
- for(j=0;j<strlen(distime);j++)
- {
- f_v_displayOneChar(j%16,0,distime[j]);
- }
- for(j=0;j<strlen(disnian);j++)
- {
- f_v_displayOneChar(j%16,1,disnian[j]);
- }
- }
- }
- //time.c
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- uchar time[5];
- uchar geshu;
- uchar distime[16],disnian[14];
- uint nian=2015;
- uchar count1,count0,count2;
- uchar data yue[12]={31,28,31,30,31,30,31,31,30,31,30,31};
- unsigned char code table2[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F',};
- void caculate(void)
- {
- if(time[3]==0)
- time[3]=1;
- if(time[4]==0)
- time[4]=1;
- distime[0]=time[4]/10+0x30;
- distime[1]=time[4]%10+0x30;
- distime[2]='-';
- distime[3]=time[3]/10+0x30;
- distime[4]=time[3]%10+0x30;
- distime[5]=' ';
- distime[6]=time[2]/10+0x30;
- distime[7]=time[2]%10+0x30;
- distime[8]=':';
- distime[9]=time[1]/10+0x30;
- distime[10]=time[1]%10+0x30;
- distime[11]=':';
- distime[12]=time[0]/10+0x30;
- distime[13]=time[0]%10+0x30;
- disnian[0]=nian/1000+0x30;
- disnian[1]=nian%1000/100+0x30;
- disnian[2]=nian%1000/10%10+0x30;
- disnian[3]=nian%10+0x30;
- //disnian[4]=table2[key_code/16];
- //disnian[5]=table2[key_code%16];
- }
- void caculate1(void)
- {
- distime[6]=time[2]/10+0x30;
- distime[7]=time[2]%10+0x30;
- distime[9]=time[1]/10+0x30;
- distime[10]=time[1]%10+0x30;
- distime[12]=time[0]/10+0x30;
- distime[13]=time[0]%10+0x30;
- }
- void pannian(void)
- {
- if(nian%4!=0)
- yue[1]=28;
- else if(nian%100!=0)
- yue[1]=29;
- else if(nian%400==0)
- yue[1]=28;
- }
- void intertime(void)
- {
- ET0=1;
- TMOD=0x21;
- TH0=(65536-51200)/256;
- TL0=(65536-51200)%256;
- IP=0X84;
- TR0=1;
- ET1=1;
- TH1=0xd1;
- TL1=0xd1;
- IT1=1;
- TR1=0;
- EX1=1; //??????
- EA=1;
- disnian[4]=' ';
- disnian[5]=' ';
- disnian[6]='0';
- disnian[7]=' ';
- disnian[8]='0';
- disnian[9]='0';
- disnian[10]=' ';
- disnian[11]='0';
- }
- void changegeshu(void)
- {
- geshu++;
- if(geshu>=6)
- geshu=0;
- }
- void changeadd(void)
- {
- if(geshu==5)
- nian++;
- else if(geshu<5)
- {
- time[geshu]++;
- if(geshu<=1)
- {
- if(time[geshu]>=60)
- time[geshu]=0;
- }
- if(geshu==2)
- {
- if(time[2]>=24)
- time[2]=0;
- }
- if(geshu==3)
- {
- if(time[3]>=(yue[time[4]-1]+1))
- time[3]=1;
- }
- if(geshu==4)
- {
- if(time[4]>=13||time[4]==0)
- time[4]=1;
- }
- }
- }
- void changedel(void)
- {
- if(geshu==5)
- nian--;
- else if(geshu<5)
- {
- time[geshu]--;
- if(geshu<=1)
- {
- if(time[geshu]>=60)
- time[geshu]=59;
- }
- if(geshu==2)
- {
- if(time[2]>=24)
- time[2]=23;
- }
- if(geshu==3)
- {
- if(time[3]==0)
- time[3]=yue[time[4]-1];
- }
- if(geshu==4)
- {
- if(time[4]==0)
- time[4]=12;
- }
- }
- }
復制代碼
|
|