找回密码
 立即注册

QQ登录

只需一步,快速开始

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

DS1302 C语言源程序

[复制链接]
跳转到指定楼层
楼主
内含1302PDF文件
C语言源程序


单片机源程序如下:
  1. #include<reg52.h>
  2. #define uchar unsigned char
  3. #define unit  unsigned int
  4. #define DS1302_WRITE 0x80
  5. #define DS1302_READ 0x81
  6. uchar time[]={0,0,8};
  7. uchar code tab0[]={"11-21    :  :   "};
  8. uchar code tab1[]={"STATE:          "};
  9. sbit en=P3^3;
  10. static uchar string[3],state[3];

  11. sbit RS=P1^5;
  12. sbit RW=P1^6;
  13. sbit E=P1^7;
  14. sbit busy=P2^7;


  15. sbit ce =P1^0;
  16. sbit scl=P1^1;
  17. sbit sda=P1^2;

  18. /****
  19. *******        延时函数
  20. *****/
  21. void delay(unit x)
  22. {
  23.     while(x--);
  24. }
  25. /****
  26. *******        串口初始化函数
  27. *****/
  28. void uart_init()
  29. {
  30.         TMOD=0x20;                //9600bps
  31.         SCON=0x50;
  32.         TH1=0xFD;
  33.         TL1=0xFD;
  34.         TR1=1;
  35.         ES=1;
  36.         EA=1;
  37.         en=0;           //串口一直处于接收状态
  38. }

  39. /****
  40. ********1602检测忙函数
  41. *****/
  42. void check_busy()
  43. {  
  44.         P2=0xff;
  45.         do
  46.         {
  47.                 RS=0;
  48.                 RW=1;
  49.                 E=0;
  50.                 E=1;
  51.         } while(busy==1);
  52.             E=0;
  53. }
  54. /****
  55. *******1602写数据函数
  56. *****/
  57. void lcd_write_dat(uchar dat)
  58. {
  59.         check_busy();
  60.         P2=dat;
  61.         delay(5);
  62.         RS=1;
  63.         RW=0;
  64.         E=1;
  65.         delay(5);
  66.         E=0;
  67.         delay(5);
  68. }
  69. /****
  70. *******1602写命令函数
  71. *****/
  72. void lcd_write_com(uchar com)
  73. {
  74.         check_busy();
  75.         P2=com;
  76.         delay(5);
  77.         RS=0;
  78.         RW=0;
  79.         E=1;
  80.         delay(5);
  81.         E=0;
  82.         delay(5);
  83. }
  84. /****
  85. *******1602写字符串函数
  86. *****/
  87. void lcd_write_str(uchar add,uchar *dat)
  88. {
  89.         lcd_write_com(add);
  90.         while(*dat != '\0')
  91.         {
  92.                 lcd_write_dat(*dat++);
  93.        
  94.         }
  95. }
  96. /****
  97. *******1602写时间函数
  98. *****/
  99. void lcd_write_time(uchar add,uchar date)
  100. {
  101.         uchar shi,ge;
  102.         shi=date/16;
  103.         ge=date%16;
  104.         lcd_write_com(add);
  105.         lcd_write_dat(shi+'0');
  106.         lcd_write_dat(ge+'0');
  107. }
  108. /****
  109. *******1602初始化函数
  110. *****/
  111. void lcd_init()
  112. {       
  113.         lcd_write_com(0x38);
  114.         delay(20000);
  115.         lcd_write_com(0x0c);
  116.         delay(20000);
  117.         lcd_write_com(0x06);
  118.         delay(20000);
  119.         lcd_write_com(0x01);
  120.         delay(20000);
  121. }
  122. /****
  123. *******        DS1302复位函数
  124. *****/
  125. void DS1302_ret()
  126. {
  127.         ce=0;
  128.         scl=0;
  129.         ce=1;
  130. }
  131. /****
  132. *******        DS1302写一个字节函数
  133. *****/
  134. void  DS1302_write_byte(uchar byte)
  135. {
  136.         uchar i;
  137.         for(i=0;i<8;i++)
  138.         {
  139.                 sda=byte&0x01;
  140.                 scl=1;
  141.                 byte>>=1;
  142.                 scl=0;
  143.         }
  144. }

  145. /****
  146. *******        DS1302读一个字节函数
  147. *****/
  148. uchar DS1302_read_byte()
  149. {
  150.         uchar i,byte=0;;
  151.         for(i=0;i<8;i++)
  152.         {
  153.                 byte>>=1;
  154.                 if(sda)
  155.                         byte|=0x80;
  156.                 scl=1;
  157.                 scl=0;
  158.         }
  159.         return byte;
  160. }
  161. /****
  162. *******        DS1302设置些保护函数
  163. *****/
  164. void  DS1302_set_WP()
  165. {
  166.         DS1302_ret();
  167.         DS1302_write_byte(0x8e);
  168.         DS1302_write_byte(0x80);
  169.         sda=0;          
  170.         ce=0;
  171. }

  172. /****
  173. *******        DS1302删除保护函数
  174. *****/
  175. void  DS1302_chear_WP()
  176. {
  177.         DS1302_ret();
  178.         DS1302_write_byte(0x8e);
  179.         DS1302_write_byte(0);
  180.         sda=0;          
  181.         ce=0;
  182. }

  183. /****
  184. *******        DS1302写地址与数据函数
  185. *****/
  186. void  DS1302_write_date(uchar add,uchar date)
  187. {
  188.         DS1302_ret();
  189.         DS1302_write_byte(add);
  190.         DS1302_write_byte(date);
  191.         sda=0;          
  192.         ce=0;
  193. }
  194. /****
  195. *******        DS1302指定地址读数据函数
  196. *****/
  197. uchar  DS1302_read_date(uchar add)
  198. {
  199.         uchar date=0;
  200.         DS1302_ret();
  201.         DS1302_write_byte(add);
  202.         date=DS1302_read_byte();
  203.         sda=0;
  204.         ce=0;
  205.         return (date);
  206. }

  207. /****
  208. *******        DS1302设定时间函数
  209. *****/
  210. void  DS1302_set_time(uchar *timedate)
  211. {
  212.         uchar i,temp;
  213.         for(i=0;i<7;i++)
  214.         {
  215.                 temp=timedate[i]/10;
  216.                 timedate[i]=timedate[i]%10;
  217.                 timedate[i]=timedate[i]+temp*16;
  218.         }
  219.         DS1302_chear_WP();
  220.         temp=DS1302_WRITE;
  221.         for(i=0;i<7;i++)
  222.         {
  223.                 DS1302_write_date(temp,timedate[i]);
  224.                 temp += 2;
  225.         }
  226.         DS1302_set_WP();
  227. }

  228. /****
  229. *******        DS1302读出时间函数
  230. *****/
  231. void  DS1302_read_time(uchar *timedate)
  232. {
  233.         uchar i,temp=0;
  234.         temp=DS1302_READ;
  235.         for(i=0;i<7;i++)
  236.         {
  237.                 timedate[i]=DS1302_read_date(temp);
  238.                 temp += 2;
  239.         }
  240. }

  241. /****
  242. *******        主函数
  243. *****/
  244. int main()
  245. {
  246.         uart_init();
  247.         lcd_init();       
  248.         lcd_write_str(0x80,tab0);
  249.         delay(20000);
  250.         lcd_write_str(0xc0,tab1);
  251.         DS1302_set_time(&time);                                 //设置时钟初值               
  252.         while(1)
  253.         {       
  254.                         DS1302_read_time(&time);          
  255.                         lcd_write_time(0x87,time[2]);  //时
  256.                         lcd_write_time(0x8a,time[1]);  //分
  257.                         lcd_write_time(0x8d,time[0]);  //秒                       
  258.                         lcd_write_com(0xc8);
  259.                         lcd_write_dat(state[0]);
  260.                         lcd_write_dat(state[1]);
  261.                         lcd_write_dat(state[2]);
  262.         }
  263. }
  264. /****
  265. *******        串口中断服务程序函数
  266. *****/
  267. void ser() interrupt 4
  268. {
  269.     uchar i;
  270.         if(RI)
  271.         {
  272.                 RI=0;
  273.                 string[i++]=SBUF;
  274.                 if(i>2)
  275.                 i=0;
  276.         }
  277.         state[i]=string[i];
  278. }  
复制代码

所有资料51hei提供下载:
DS1302.rar (453.71 KB, 下载次数: 12)


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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