微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 菜鸟上路,自己制作的ds1302时钟日历

菜鸟上路,自己制作的ds1302时钟日历

时间:10-02 整理:3721RD 点击:
以下为部分代码。

  1. #include <reg52.h>
  2. [code]#include <reg52.h>
  3. #include <intrins.h>
  4. #include "ds1302.h"

  5. unsigned char code dis[]="20  -  -        ";
  6. unsigned char code dis1[]="    :  :        ";

  7. unsigned char nian,yue,ri,zhou,shi,fen,miao,key_fun_count;

  8. struct timer
  9. {
  10.         unsigned char nian;
  11.         unsigned char yue;
  12.         unsigned char ri;
  13.         unsigned char zhou;
  14.         unsigned char shi;
  15.         unsigned char fen;
  16.         unsigned char miao;       
  17. };

  18. struct timer temp;

  19. sbit key_fun=P1^0;                   //按键定义
  20. sbit key_jia=P1^1;
  21. sbit key_jian=P1^2;
  22. sbit key_yes=P1^3;

  23. sbit Sled_en=P3^6;                   //数码管使能端

  24. sbit LCD_RW=P1^6;                   //1602液晶引脚设置
  25. sbit LCD_RS=P1^7;
  26. sbit LCD_EN=P2^4;

  27. void delay(unsigned int z)       //延时子函数
  28. {
  29.         unsigned int x,y;
  30.         for(x=z;x>0;x--)
  31.                 for(y=125;y>0;y--);
  32. }

  33. void write_com(unsigned char com)    //写命令
  34. {
  35.         LCD_RS=0;
  36.         LCD_RW=0;
  37.         LCD_EN=0;
  38.         P0=com;
  39.         delay(5);
  40.         LCD_EN=1;
  41.         delay(5);
  42.         LCD_EN=0;       
  43. }

  44. void write_dat_pos(unsigned char x,unsigned char y,unsigned char dat)           //直接写在显示屏的写数据函数
  45. {                                                                                                   //x为显示第几行{0或1},y为显示第几个{0到15}。dat为数据
  46.         if(x==0)
  47.        x=0x80+y;
  48.          else if(x==1)
  49.                          x=0x80+0x40+y;
  50.         write_com(x);
  51.         LCD_RW=0;
  52.         LCD_RS=1;
  53.         LCD_EN=0;
  54.         delay(5);
  55.         P0=dat;
  56.         delay(5);
  57.         LCD_EN=1;
  58.         delay(5);
  59.         LCD_EN=0;       
  60. }

  61. void write_sfm(unsigned char pos,unsigned char dat)                        //显示屏上写入时分秒
  62. {
  63.         unsigned char shi_wei,ge_wei;
  64.         shi_wei=dat/10;
  65.         ge_wei=dat%10;
  66.         write_dat_pos(1,pos,shi_wei+'0');
  67.         write_dat_pos(1,pos+1,ge_wei+'0');
  68. }

  69. void write_week(unsigned char dat)                        //显示屏上写入星期
  70. {
  71.         unsigned char uc_step;
  72.         uc_step=dat;
  73.         switch(uc_step)
  74.         {
  75.                 case 1 :
  76.                         write_dat_pos(0,12,'M');
  77.                         write_dat_pos(0,13,'o');
  78.                         write_dat_pos(0,14,'n');
  79.                         break;
  80.                 case 2 :
  81.                         write_dat_pos(0,12,'T');
  82.                         write_dat_pos(0,13,'u');
  83.                         write_dat_pos(0,14,'e');
  84.                         break;
  85.                 case 3 :
  86.                         write_dat_pos(0,12,'W');
  87.                         write_dat_pos(0,13,'e');
  88.                         write_dat_pos(0,14,'n');
  89.                         break;
  90.                 case 4 :
  91.                         write_dat_pos(0,12,'T');
  92.                         write_dat_pos(0,13,'h');
  93.                         write_dat_pos(0,14,'u');
  94.                         break;
  95.                 case 5 :
  96.                         write_dat_pos(0,12,'F');
  97.                         write_dat_pos(0,13,'r');
  98.                         write_dat_pos(0,14,'i');
  99.                         break;
  100.                 case 6 :
  101.                         write_dat_pos(0,12,'S');
  102.                         write_dat_pos(0,13,'a');
  103.                         write_dat_pos(0,14,'t');
  104.                         break;
  105.                 case 7 :
  106.                         write_dat_pos(0,12,'S');
  107.                         write_dat_pos(0,13,'u');
  108.                         write_dat_pos(0,14,'n');
  109.                         break;
  110.                 default : break;
  111.         }
  112. }

  113. void write_nyr(unsigned char pos,unsigned char dat)                        //显示屏上写入年月日
  114. {
  115.         unsigned char shi_wei,ge_wei;
  116.         shi_wei=dat/10;
  117.         ge_wei=dat%10;
  118.         write_dat_pos(0,pos,shi_wei+'0');
  119.         write_dat_pos(0,pos+1,ge_wei+'0');
  120. }

  121. unsigned char bcd_to_shijinzhi(unsigned char bcd)                 //BCD码转换为十进制数
  122. {
  123.         return (bcd/16)*10+bcd%16;
  124. }

  125. void struct_bcd_shijinzhi()                //结构体的BCD码转换为十进制数
  126. {
  127.         miao=bcd_to_shijinzhi(CurrentTime.Second);
  128.         fen=bcd_to_shijinzhi(CurrentTime.Minute);
  129.         shi=bcd_to_shijinzhi(CurrentTime.Hour);
  130.         ri=bcd_to_shijinzhi(CurrentTime.Day);
  131.         yue=bcd_to_shijinzhi(CurrentTime.Month);
  132.         zhou=bcd_to_shijinzhi(CurrentTime.Week);
  133.         nian=bcd_to_shijinzhi(CurrentTime.Year);
  134. }

复制代码


真的非常非常不错啊!

真的非常非常不错啊!

真的非常非常不错啊!

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top