欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索

用ds1302編程時出現的問題

查看數: 3634 | 評論數: 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 單片機教程網

快速回復 返回頂部 返回列表