找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3641|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机红外遥控1602显示的万年历源程序

[复制链接]
跳转到指定楼层
楼主
ID:76244 发表于 2015-4-7 00:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    过年时,有块开发板,刚好有空,就写了个c52红外遥控1602显示的万年历,网上有很多的代码,都大同小异,自己也献丑下。
下面有几个文件 大家复制出来整理一下 我这边测试成功了.
  1. //ir.h
  2. #define uchar unsigned char    //???
  3. #define uint  unsigned int

  4. sbit HWRx=P3^3;      //???
  5. uchar Table_Rx[67];//?????????1?0???


  6. void Delay()        //???????31us,??12M
  7. {
  8.           uchar i=13;
  9.           while(i)
  10.         i--;
  11. }

  12. void Delay_ms(uint z) //?????? 1ms*X  ???12M
  13. {
  14.           uint x=0,y=0;
  15.           for(x=z;x>0;x--)
  16.                 for(y=54;y>0;y--);
  17. }
  18. void hongwai()   interrupt 2    //???? 1 ,INT1(P3^3)???????IC???
  19. {
  20.           uchar i,j,tmp;
  21.           EX1=0;   //????

  22.           j=33;   //??????????1?,4?????,??33?
  23.           i=0;      //????????
  24.   
  25.           Delay_ms(10);
  26.           if(HWRx)
  27.           {  //????????????????,???,?????
  28.                    EX1=1;
  29.                    return;
  30.           }


  31.           while(j--)
  32.           {
  33.                tmp=0;
  34.         Table_Rx[i]=1;  //????1??
  35.          while(!HWRx) //?????????,?????????
  36.        {  
  37.               Table_Rx[i]++;  //?????1
  38.                Delay();       //???????,??????????
  39.                tmp++;     //?1
  40.                if(tmp==250)
  41.                         break;
  42.          }
  43.         i++;

  44.                 tmp=0;
  45.         Table_Rx[i]=1;     //????1??
  46.                 while(HWRx)     //?????????,?????????
  47.         {  
  48.                      Table_Rx[i]++; //?????1
  49.                      Delay();       //??
  50.                      tmp++;     //?1,?????1??0?,?????
  51.                      if(tmp==250)
  52.                 break;
  53.         }
  54.                  i++;  
  55.          }

  56.         P1=0xf8;
  57.         i=200;      //??????,???
  58.         while(i)   //?????????????H
  59.         {
  60.        tmp=255;
  61.       while(tmp)
  62.       {
  63.                tmp--;
  64.                P2=0x76;
  65.        }
  66.        i--;   
  67.       }

  68.         tmp=0;
  69.         for(i=3;i<19;i++,i++)   //???????
  70.         {  
  71.                   tmp>>=1;              //????,??????
  72.         if(Table_Rx[i]>30)    //?????????30????1
  73.                tmp+=0x80;
  74.        }
  75.        Table_Data[0]=tmp/16;     //??2??16????,????????
  76.        Table_Data[1]=tmp%16;

  77.         tmp=0;
  78.         for(i=19;i<35;i++,i++)    //??
  79.         {  
  80.                    tmp>>=1;
  81.         if(Table_Rx[i]>30)
  82.              tmp+=0x80;
  83.       }
  84.         Table_Data[2]=tmp/16;
  85.         Table_Data[3]=tmp%16;

  86.          tmp=0;
  87.          for(i=35;i<51;i++,i++)
  88.          {  
  89.         tmp>>=1;
  90.         if(Table_Rx[i]>30)
  91.               tmp+=0x80;
  92.          }
  93.         Table_Data[4]=tmp/16;
  94.         Table_Data[5]=tmp%16;

  95.          tmp=0;
  96.          for(i=51;i<67;i++,i++)
  97.          {  
  98.          tmp>>=1;
  99.                  if(Table_Rx[i]>30)
  100.                        tmp+=0x80;
  101.         }
  102.         Table_Data[6]=tmp/16;
  103.         Table_Data[7]=tmp%16;
  104.         EX1=1;       //??????????,?????
  105. }
  106. //1602.c
  107. #include<reg52.H>
  108. #include <stdio.h>
  109. #include<string.h>
  110. #include"TIME.C"
  111. #include"ir.c"
  112. #define uchar unsigned char

  113. sfr lcd_data=0x80;
  114. sbit lcd_E=P3^5;
  115. sbit lcd_RW=P3^6;
  116. sbit lcd_RS=P3^7;


  117. unsigned char code show1[]="Dear lu yun:";
  118. unsigned char code show2[]="Happy new year!";

  119. uchar i,j,value;


  120. void f_v_delay5ms(void)
  121. {
  122. unsigned char i;
  123. unsigned char j;
  124. for(i=0;i<20;i++)
  125. {
  126.   for(j=0;j<250;j++);
  127. }
  128. }
  129. void f_v_delay50us(void)
  130. {
  131. unsigned char i;
  132. for(i=0;i<50;i++)
  133.   {;}
  134. }
  135. void f_v_delay2ms(void)
  136. {
  137. unsigned char i;
  138. unsigned char j;
  139. for(i=0;i<40;i++)
  140. {
  141.   for(j=0;j<50;j++);
  142. }
  143. }

  144. void f_v_delay10ms(void)
  145. {
  146. f_v_delay5ms();
  147. f_v_delay5ms();
  148. }



  149. void f_uc_lcdReadStatus(void)
  150. {
  151.     lcd_data=0xff;   
  152.     lcd_RS = 0;   
  153.     lcd_RW = 1;        
  154.     lcd_E = 1;
  155. f_v_delay50us();
  156. while(P2&0x80);//?????
  157. lcd_E = 0;   
  158. }


  159. void f_v_lcdWriteData(unsigned char WDLCM)
  160. {  
  161. P2=0XFF;
  162. f_uc_lcdReadStatus(); //???
  163. P2 = WDLCM;
  164.     lcd_RS = 1;
  165.     lcd_RW = 0;
  166.     lcd_E = 1;//?????????????????
  167. f_v_delay50us();//??
  168.     lcd_E = 0;
  169. }

  170. void f_v_lcdWriteCommand(unsigned char WCLCM,unsigned char BuysC)//BuysC?0??????
  171. {P2=0XFF;        
  172. if (BuysC)
  173.   f_uc_lcdReadStatus();//???????
  174.   P2 = WCLCM;   
  175.      lcd_RS = 0;   
  176.      lcd_RW = 0;   
  177.      lcd_E = 1;
  178.   f_v_delay50us();//??   
  179.      lcd_E = 0;   
  180. }

  181. void f_v_displayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
  182. {        
  183. Y &= 0x1;        
  184. X &= 0xF;//??X????15,Y????1   

  185.    
  186. if (Y)
  187. X |= 0x40;//???????????+0x40;        
  188. X |= 0x80;// ?????        
  189.       
  190.     f_v_lcdWriteCommand(X, 0);//????????,?????        
  191. f_v_lcdWriteData(DData);
  192.          
  193. }

  194. void f_v_displayListChar(unsigned char X, unsigned char Y, unsigned char  *DData)
  195. {        
  196. unsigned char ListLength;        
  197. ListLength = 0;        
  198. Y &= 0x1;        
  199. X &= 0xF;//??X????15,Y????1   

  200.    
  201. while (DData[ListLength]>=0x20)//?????????        
  202. {               
  203.   if (X <= 0xF)//X?????0xF     
  204.   {   
  205.              f_v_displayOneChar(X, Y, DData[ListLength]);//??????                     
  206.     ListLength++;              
  207.     X++;
  208.   }        
  209. }
  210. }

  211. void f_v_lcdInit(void)
  212. {  P1=0X00;P0=0XFF;

  213. lcd_data = 0;   
  214.     f_v_lcdWriteCommand(0x38,0);//????????,??????     
  215.     f_v_delay5ms();   
  216.     f_v_lcdWriteCommand(0x38,0);   
  217.     f_v_delay5ms();   
  218.     f_v_lcdWriteCommand(0x38,0);   
  219.     f_v_delay5ms();         
  220.     f_v_lcdWriteCommand(0x38,1);//??????,???????????
  221.    
  222.     f_v_lcdWriteCommand(0x08,1);//????   
  223.     f_v_lcdWriteCommand(0x01,1);//????
  224. f_v_delay2ms();   
  225.     f_v_lcdWriteCommand(0x06,1);// ????????     
  226.     f_v_lcdWriteCommand(0x0C,1);// ????????
  227. }



  228. void main(void)
  229. { intertime();
  230. i=0;
  231.   j=0;


  232.    for(i=0;i<16;i++)
  233.    {
  234.    
  235.        f_v_lcdInit();
  236.       
  237.             for(j=0;j<strlen(show1);j++)
  238.        {
  239.            f_v_displayOneChar((i+j)%16,0,show1[j]);
  240. if((i+j)/16==1)
  241.          f_v_displayOneChar((i+j)%16,0,' ');  
  242.         }
  243.    for(j=0;j<strlen(show2);j++)
  244.        {
  245.          f_v_displayOneChar((i+j)%16,1,show2[j]);
  246. if((i+j)/16==1)
  247.          f_v_displayOneChar((i+j)%16,1,' ');  
  248.        }
  249.    }
  250.       
  251. f_v_lcdInit();         
  252. while(1)
  253.     {
  254. for(j=0;j<strlen(distime);j++)
  255. {         
  256. f_v_displayOneChar(j%16,0,distime[j]);       
  257. }       
  258.     for(j=0;j<strlen(disnian);j++)
  259. {         
  260. f_v_displayOneChar(j%16,1,disnian[j]);       
  261. }       
  262.     }
  263. }

  264. //time.c
  265. #define uchar unsigned char
  266. #define uint  unsigned int
  267. #define ulong unsigned long
  268. uchar  time[5];
  269. uchar geshu;
  270. uchar distime[16],disnian[14];
  271. uint nian=2015;
  272. uchar count1,count0,count2;
  273. uchar data yue[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  274. unsigned char code table2[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F',};
  275. void caculate(void)
  276. {
  277. if(time[3]==0)
  278. time[3]=1;
  279. if(time[4]==0)
  280.   time[4]=1;
  281. distime[0]=time[4]/10+0x30;
  282. distime[1]=time[4]%10+0x30;
  283. distime[2]='-';
  284. distime[3]=time[3]/10+0x30;       
  285. distime[4]=time[3]%10+0x30;
  286. distime[5]=' ';       
  287. distime[6]=time[2]/10+0x30;       
  288. distime[7]=time[2]%10+0x30;
  289. distime[8]=':';
  290. distime[9]=time[1]/10+0x30;       
  291. distime[10]=time[1]%10+0x30;       
  292. distime[11]=':';       
  293. distime[12]=time[0]/10+0x30;       
  294. distime[13]=time[0]%10+0x30;

  295. disnian[0]=nian/1000+0x30;
  296. disnian[1]=nian%1000/100+0x30;
  297. disnian[2]=nian%1000/10%10+0x30;
  298. disnian[3]=nian%10+0x30;
  299. //disnian[4]=table2[key_code/16];
  300. //disnian[5]=table2[key_code%16];
  301. }


  302. void caculate1(void)
  303. {
  304. distime[6]=time[2]/10+0x30;       
  305. distime[7]=time[2]%10+0x30;
  306. distime[9]=time[1]/10+0x30;       
  307. distime[10]=time[1]%10+0x30;       
  308. distime[12]=time[0]/10+0x30;       
  309. distime[13]=time[0]%10+0x30;

  310. }




  311. void pannian(void)
  312. {
  313. if(nian%4!=0)
  314. yue[1]=28;
  315. else if(nian%100!=0)
  316.   yue[1]=29;
  317. else if(nian%400==0)
  318. yue[1]=28;         
  319. }

  320. void intertime(void)
  321. {
  322. ET0=1;
  323. TMOD=0x21;
  324. TH0=(65536-51200)/256;
  325. TL0=(65536-51200)%256;
  326. IP=0X84;
  327. TR0=1;
  328. ET1=1;
  329. TH1=0xd1;
  330. TL1=0xd1;
  331. IT1=1;
  332. TR1=0;
  333. EX1=1; //??????
  334. EA=1;
  335. disnian[4]=' ';
  336. disnian[5]=' ';
  337. disnian[6]='0';
  338. disnian[7]=' ';
  339. disnian[8]='0';
  340. disnian[9]='0';       

  341. disnian[10]=' ';
  342. disnian[11]='0';
  343. }

  344. void changegeshu(void)
  345. {
  346. geshu++;
  347. if(geshu>=6)
  348. geshu=0;
  349. }

  350. void changeadd(void)
  351. {
  352. if(geshu==5)
  353. nian++;
  354. else if(geshu<5)
  355. {
  356.   time[geshu]++;
  357. if(geshu<=1)
  358. {
  359. if(time[geshu]>=60)
  360. time[geshu]=0;
  361. }
  362. if(geshu==2)
  363. {
  364. if(time[2]>=24)
  365. time[2]=0;
  366. }
  367. if(geshu==3)
  368. {
  369. if(time[3]>=(yue[time[4]-1]+1))
  370. time[3]=1;
  371. }
  372. if(geshu==4)
  373. {
  374. if(time[4]>=13||time[4]==0)
  375. time[4]=1;
  376. }
  377. }

  378. }

  379. void changedel(void)
  380. {
  381. if(geshu==5)
  382. nian--;
  383. else if(geshu<5)
  384. {
  385.   time[geshu]--;
  386. if(geshu<=1)
  387. {
  388. if(time[geshu]>=60)
  389. time[geshu]=59;
  390. }
  391. if(geshu==2)
  392. {
  393. if(time[2]>=24)
  394. time[2]=23;
  395. }
  396. if(geshu==3)
  397. {
  398. if(time[3]==0)
  399. time[3]=yue[time[4]-1];
  400. }
  401. if(geshu==4)
  402. {
  403. if(time[4]==0)
  404. time[4]=12;
  405. }
  406. }
  407. }
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表