找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

用ds1302编程时出现的问题

查看数: 3568 | 评论数: 4 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2019-12-7 12:09

正文摘要:

#include "stc15.h" #include "ds1302.h" #include <intrins.h> u8 shi,fen,miao,a[];  //全局变量 bit time_flag; u8 code table[] =             ...

回复

ID:213173 发表于 2019-12-12 15:59
给你把文件合并在一起,能通过编译,文件中有两处错误。
错误u8 shi,fen,miao,a[];  //全局变量
正确u8 shi,fen,miao,a[8];  //全局变量
错误if(SDA = 1)
正确if(SDA == 1)

  1. #include "stc15.h"
  2. #include <intrins.h>
  3. typedef unsigned char u8;
  4. typedef unsigned int u16;

  5. #define SCK_CLR SCK = 0;
  6. #define SCK_SET SCK = 1;
  7. #define RST_CLR RST = 0;
  8. #define RST_SET RST = 1;
  9. #define SDA_CLR SDA = 0;
  10. #define SDA_SET SDA = 1;
  11. void Timer0Init(void);
  12. void num_display();
  13. void Refresh_time();
  14. void Delay2ms();
  15. void DS1302_Write_Byte(u8 dat);
  16. void DS1302_Write(u8 addr,u8 dat);
  17. unsigned char DS1302_Read_Byte(void);
  18. unsigned char DS1302_Read(u8 addr);        
  19. void DS1302_Init(u8 shi,u8 fen,u8 miao);

  20. sbit SCK = P1^7;      //时钟芯片引脚
  21. sbit RST = P1^3;
  22. sbit SDA = P2^3;
  23. u8 shi,fen,miao,a[8];  //全局变量
  24. bit time_flag;

  25. u8 code table[] =
  26. {   //共阳极编码表
  27. 0xc0,0xf9,0xa4,0xb0,
  28. //0,        1,          2,         3
  29. 0x99,0x92,0x82,0xf8,
  30. //4,        5,          6,         7
  31. 0x80,0x90,0xff,0xbf
  32. //8,        9,                空,-,
  33. };

  34. u8 code order[] =
  35. {   //数码管位码
  36. 0x01,0x02,0x04,0x08,
  37. 0x10,0x20,0x40,0x80
  38. };

  39. void Delay2ms()                //@11.0592MHz 2ms延时
  40. {
  41.         unsigned char i, j;
  42.         _nop_();
  43.         _nop_();
  44.         i = 22;
  45.         j = 128;
  46.         do
  47.         {
  48.         while (--j);
  49.         } while (--i);
  50. }

  51. void Refresh_time()            //数码管显示时间
  52. {
  53.         if(time_flag == 1)
  54.         {
  55.                 time_flag = 0;
  56.                 shi = DS1302_Read(0x85);  
  57.                 fen  = DS1302_Read(0x83);
  58.                 miao = DS1302_Read(0x81);
  59.         }
  60.         a[0] = shi/16;
  61.         a[1] = shi%16;
  62.         a[2] = 11;
  63.         a[3] = fen/16;
  64.         a[4] = fen%16;
  65.         a[5] = 11;
  66.         a[6] = miao/16;
  67.         a[7] = miao%16;
  68. }

  69. void num_display()        //数码管显示
  70. {
  71.         static u8 i=0;
  72.         P0 = 0x00;
  73.         P2 = (P2 & 0x1f) | 0xc0;
  74.         P2 = (P2 & 0x1f);         
  75.         P0 = table[a[i]];
  76.         P2 = (P2 & 0x1f) | 0xe0;  
  77.         P2 = (P2 & 0x1f);
  78.         P0 = order[i];
  79.         P2 = (P2 & 0x1f) | 0xc0;
  80.         P2 = (P2 & 0x1f);
  81.         i++;
  82.         i &= 0x07;
  83.         Delay2ms();
  84. }

  85. void Timer0Init(void)                //2毫秒@11.0592MHz
  86. {
  87.         AUXR |= 0x80;                //定时器时钟1T模式
  88.         TMOD &= 0xF0;                //设置定时器模式
  89.         TL0 = 0x9A;                  //设置定时初值
  90.         TH0 = 0xA9;                  //设置定时初值
  91.         TF0 = 0;                    //清除TF0标志
  92.         TR0 = 1;
  93.         ET0 = 1;
  94.         EA = 1;               //定时器0开始计时
  95. }

  96. void main()
  97. {
  98.         Timer0Init();
  99.         DS1302_Init(12,00,00);
  100.         while(1)
  101.         {
  102.                 Refresh_time();
  103.                 num_display();
  104.         }
  105. }

  106. void T0_time() interrupt 1
  107. {
  108.         static u8 time_count = 0;         
  109.         num_display();
  110.         if(++time_count >= 200)
  111.         {
  112.                 time_count  = 0;
  113.                 time_flag = 1;
  114.         }
  115. }

  116. void DS1302_Write_Byte(u8 dat)   //单字节写入一字节数据
  117. {
  118.         u8 i;
  119.         SCK_CLR;           //SCLK管脚置低,初始化DS1302
  120.         for(i = 0;i < 8;i++)
  121.         {
  122.                 if(dat & 0x01)   
  123.                 {
  124.                         SCK_SET;       //I/O口置高电平
  125.                 }
  126.                 else
  127.                 {
  128.                         SCK_CLR;       //I/O口置低电平
  129.                 }
  130.                 SCK_SET;         //SCLK上升沿,读取数据
  131.                 SCK_CLR;         //拉低SCLK,传输下一位数据
  132.                 dat = dat >> 1;
  133.         }
  134. }

  135. void DS1302_Write(u8 addr,u8 dat)   //向 DS1302 单字节写入一字节数据
  136. {
  137.         RST_CLR;                                      //RST管脚置低,初始化DS1302
  138.         SCK_CLR;                                             //SCLK管脚置低,初始化DS1302
  139.         RST_SET;                                             //启动DS1302总线,RST置高电平
  140.         addr &= 0xfe;                                                     //最低位置零,保证稳定环境
  141.         DS1302_Write_Byte(addr);           //写入目标地址
  142.         DS1302_Write_Byte(dat);            //写入数据
  143.         RST_CLR;                           //停止DS1302总线
  144.         SCK_CLR;            
  145. }

  146. unsigned char DS1302_Read_Byte(void)//单字节读一字节数据
  147. {
  148.         u8 i,dat =  0;
  149.         for(i = 0;i < 8;i++)
  150.         {
  151.                 dat = dat >> 1;
  152.                 if(SDA == 1)              
  153.                 {
  154.                         dat |= 0x80;        
  155.                 }
  156.                 else
  157.                 {
  158.                         dat &= 0x7f;
  159.                 }
  160.                 SCK_SET;                   //SCLK下降沿,锁存数据
  161.                 SCK_CLR;        
  162.         }
  163.         return dat;
  164. }

  165. unsigned char DS1302_Read(u8 addr)              //从 DS1302 单字节读出一字节数据
  166. {
  167.         u8 temp;
  168.         RST_CLR;
  169.         SCK_CLR;
  170.         RST_SET;
  171.         addr |= 0x01;               //最低位置高,保证是读操作
  172.         DS1302_Write_Byte(addr);
  173.         temp = DS1302_Read_Byte();        //从 DS1302 单字节读出一字节数据
  174.         RST_CLR;
  175.         SDA_CLR;
  176.         return temp;
  177. }

  178. void DS1302_Init(u8 shi,u8 fen,u8 miao)          //时钟初始化
  179. {
  180.         DS1302_Write(0x8e,0x00);                     //关闭写保护
  181.         DS1302_Write(0x80,((miao/10)<<4)|(miao%10)); //分别写秒分时地址
  182.         DS1302_Write(0x82,((fen/10)<<4)|(fen%10));
  183.         DS1302_Write(0x84,((shi/10)<<4)|(shi%10));
  184.         DS1302_Write(0x8e,0x80);                     //开启写保护
  185. }
复制代码
ID:643131 发表于 2019-12-12 13:06
xuyaqi 发表于 2019-12-8 09:58
u8 shi,fen,miao,a[];  //全局变量
改成:
u8 shi,fen,miao;  //全局变量

好的,谢谢了
ID:647528 发表于 2019-12-8 12:42
51头文件可能有问题,换reg51试一下
ID:94031 发表于 2019-12-8 09:58
u8 shi,fen,miao,a[];  //全局变量
改成:
u8 shi,fen,miao;  //全局变量
u8 a[8];

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

Powered by 单片机教程网

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