找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4022|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

STC15W408AS單片機(jī)+DS3231+OLED便攜式鋰電池手表制作 附程序

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:249376 發(fā)表于 2022-10-14 21:13 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本文介紹了STC15W408AS單片機(jī)+DS3231+OLED便攜式鋰電池手表。 由于DS3231備用電池供電,可以記錄當(dāng)時(shí)時(shí)間,所以沒(méi)有用到睡眠,用按鍵開(kāi)關(guān)機(jī)來(lái)查看時(shí)間。
附件包括嘉立創(chuàng)格式PCB項(xiàng)目文件(3D外殼文件還沒(méi)改好),源代碼。
由于計(jì)時(shí)的問(wèn)題還是存在(有時(shí)莫名其妙為零),用另外3個(gè)按鍵控制對(duì)時(shí)間的修改。歡迎大家討論。
制作出來(lái)的實(shí)物圖如下:


單片機(jī)源程序如下:
  1. #include "reg51.h"
  2. #include "intrins.h"
  3. #include "codetab.h"

  4. #define uint  unsigned int
  5. #define uchar unsigned char
  6. // ------------------------------------------------------------
  7. // IO口模擬I2C通信
  8. // ------------------------------------------------------------
  9. sbit SCL=P1^2; //串行時(shí)鐘
  10. sbit SDA=P1^3; //串行數(shù)據(jù)

  11. sbit KEY1=  P1^5;                  //調(diào)整
  12. sbit KEY2=  P1^4;                  //++
  13. sbit KEY3=  P1^6;                  //--

  14. uchar  a1,a2,a3;                      //按鍵消斗用
  15. uchar  disflag=0; //時(shí)間調(diào)整相關(guān)

  16. #define        Brightness        0xcf //
  17. #define X_WIDTH         128
  18. #define Y_WIDTH         64

  19. /********************************************************************************************************
  20. **         DS3231常數(shù)定義
  21. ********************************************************************************************************/
  22. #define DS3231_WriteAddress 0xD0    //器件寫(xiě)地址
  23. #define DS3231_ReadAddress  0xD1    //器件讀地址
  24. #define DS3231_SECOND       0x00    //秒
  25. #define DS3231_MINUTE       0x01    //分
  26. #define DS3231_HOUR         0x02    //時(shí)
  27. #define DS3231_WEEK         0x03    //星期
  28. #define DS3231_DAY          0x04    //日
  29. #define DS3231_MONTH        0x05    //月
  30. #define DS3231_YEAR         0x06    //年

  31. #define NACK    1
  32. #define ACK     0

  33. char hour,minute,second,year,month,day,week;
  34. uchar TH3231;
  35. bit        ack;                                                        //應(yīng)答標(biāo)志位
  36. /*********************OLED驅(qū)動(dòng)程序用的延時(shí)程序************************************/
  37. /*void delay(unsigned int z)
  38. {
  39.         unsigned int x,y;
  40.         for(x=z;x>0;x--)
  41.                 for(y=1100;y>0;y--);  
  42. }        */
  43. void        Delay5US()           //@12.000MHz           延時(shí)5us
  44. {
  45.    _nop_(); _nop_();        _nop_();_nop_();
  46. }

  47. /**********************************************
  48. //IIC Start
  49. **********************************************/
  50. void IIC_Start()
  51. {
  52.    SCL = 1;               
  53.    SDA = 1;
  54.    SDA = 0;
  55.    SCL = 0;
  56. }

  57. /**********************************************
  58. //IIC Stop
  59. **********************************************/
  60. void IIC_Stop()
  61. {
  62.    SCL = 0;
  63.    SDA = 0;
  64.    SCL = 1;
  65.    SDA = 1;
  66. }


  67. /********************************************************************************************************
  68. **         3231
  69. ********************************************************************************************************/


  70. uchar        BCD2HEX(uchar val)
  71. {
  72.         return        ((val>>4)*10)+(val&0x0f);
  73. }

  74. uchar        HEX2BCD(uchar val)
  75. {
  76.         return        (((val%100)/10)<<4)|(val%10);
  77. }


  78. void SendByte(uchar c)
  79. {
  80.     uchar BitCnt;
  81.    
  82.     for(BitCnt=0;BitCnt<8;BitCnt++)         //要傳送的數(shù)據(jù)長(zhǎng)度為8位
  83.     {
  84.         if((c<<BitCnt)&0x80)
  85.             SDA=1;                          //判斷發(fā)送位
  86.         else
  87.             SDA=0;     
  88.         SCL=1;                            //置時(shí)鐘線為高,通知被控器開(kāi)始接收數(shù)據(jù)位
  89.         Delay5US();                       //保證時(shí)鐘高電平周期大于4μs   
  90.         SCL=0;
  91.     }
  92.     SDA=1;                                  //8位發(fā)送完后釋放數(shù)據(jù)線,準(zhǔn)備接收應(yīng)答位
  93.     SCL=1;
  94.     Delay5US();
  95.     if(SDA==1)
  96.         ack=0;   
  97.     else
  98.         ack=1;                              //判斷是否接收到應(yīng)答信號(hào)
  99.     SCL=0;
  100.     Delay5US();
  101. }          

  102. uchar RcvByte()
  103. {
  104.    uchar retc;
  105.    uchar BitCnt;

  106.    retc=0;
  107.    SDA=1;                           //置數(shù)據(jù)線為輸入方式
  108.    for(BitCnt=0;BitCnt<8;BitCnt++)
  109.    {
  110.         SCL=0;                      //置時(shí)鐘線為低,準(zhǔn)備接收數(shù)據(jù)位      
  111.         Delay5US();                 //時(shí)鐘低電平周期大于4.7μs                       
  112.         SCL=1;                      //置時(shí)鐘線為高使數(shù)據(jù)線上數(shù)據(jù)有效
  113.         Delay5US();
  114.         retc=retc<<1;
  115.         if(SDA==1)
  116.             retc=retc+1;            //讀數(shù)據(jù)位,接收的數(shù)據(jù)位放入retc中
  117.         Delay5US();
  118.    }
  119.    SCL=0;
  120.    return(retc);
  121. }                            

  122. void Ack_I2C(bit a)
  123. {
  124.         SDA        =        a;  
  125.     SCL=1;                     
  126.     Delay5US();             //時(shí)鐘低電平周期大于4us   
  127.     SCL=0;                  //清時(shí)鐘線,鉗住I2C總線以便繼續(xù)接收
  128.     Delay5US();   
  129. }                                            

  130. uchar write_byte(uchar addr, uchar write_data)
  131. {
  132.     IIC_Start();
  133.     SendByte(DS3231_WriteAddress);
  134.     if (ack == 0)
  135.         return 0;
  136.    
  137.     SendByte(addr);   
  138.     if (ack == 0)
  139.         return 0;
  140.    
  141.     SendByte(write_data);
  142.     if (ack == 0)
  143.         return 0;
  144.    
  145.     IIC_Stop();
  146.     Delay5US();      
  147.     Delay5US();      
  148.     return 1;
  149. }                                          

  150. uchar read_current()
  151. {
  152.     uchar read_data;
  153.     IIC_Start();
  154.     SendByte(DS3231_ReadAddress);
  155.     if(ack==0)
  156.         return(0);             
  157.     read_data = RcvByte();
  158.     Ack_I2C(1);
  159.     IIC_Stop();
  160.     return read_data;
  161. }                                                 

  162. uchar read_random(uchar random_addr)
  163. {
  164.     uchar Tmp;
  165.         IIC_Start();
  166.     SendByte(DS3231_WriteAddress);
  167.     if(ack==0)
  168.         return(0);            
  169.     SendByte(random_addr);
  170.     if(ack==0)
  171.         return(0);
  172.         Tmp=read_current();
  173.         if(random_addr==DS3231_HOUR)
  174.                 Tmp&=0x3f;
  175.                                             
  176.     return(BCD2HEX(Tmp));//都轉(zhuǎn)10進(jìn)制輸出
  177. }                                  

  178. void ModifyTime(uchar address,uchar num)
  179. {
  180.     uchar temp=0;
  181.            if(address>6 && address <0) return;
  182.     temp=HEX2BCD(num);
  183.         write_byte(address,temp);
  184. }
  185. uint    read_temp()      
  186. {
  187.                 int     itemp;
  188.                 float   ftemp;
  189.                 //溫度數(shù)據(jù)是以2 進(jìn)制格式存儲(chǔ)的并不需要數(shù)制轉(zhuǎn)換
  190.                 write_byte(0x0e,0x20);//0x0e寄存器的CONV位置1開(kāi)啟溫度轉(zhuǎn)換

  191.         itemp = ( (int) read_random(0x11) << 5 );          //放大32倍
  192.         itemp += ( read_random(0x12)>> 3);
  193.         IIC_Stop();
  194.         if(itemp & 0x1000)
  195.                         itemp += 0xe000;      

  196.         ftemp = 0.3125 * (float) itemp+0.5;    //放大10倍
  197.                 return  (uint) ftemp;
  198. }

  199. /*********************OLED寫(xiě)數(shù)據(jù)************************************/
  200. void OLED_WrDat(unsigned char IIC_Data)
  201. {
  202.         IIC_Start();
  203.         SendByte(0x78);
  204.         SendByte(0x40);                        //write data
  205.         SendByte(IIC_Data);
  206.         IIC_Stop();
  207. }
  208. /*********************OLED寫(xiě)命令************************************/
  209. void OLED_WrCmd(unsigned char IIC_Command)
  210. {
  211.         IIC_Start();
  212.         SendByte(0x78);            //Slave address,SA0=0
  213.         SendByte(0x00);                        //write command
  214.         SendByte(IIC_Command);
  215.         IIC_Stop();
  216. }
  217. /*********************OLED 設(shè)置坐標(biāo)************************************/
  218. void OLED_Set_Pos(unsigned char x, unsigned char y)
  219. {
  220. ……………………

  221. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei附件下載:
Keil代碼.7z (30.11 KB, 下載次數(shù): 83)
eprj文件.7z (158.67 KB, 下載次數(shù): 45)

評(píng)分

參與人數(shù) 1黑幣 +90 收起 理由
admin + 90 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏8 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:430492 發(fā)表于 2022-10-19 16:15 | 只看該作者
樓主可以考慮把它設(shè)計(jì)得美觀一點(diǎn),這樣自己用起來(lái)不是更好點(diǎn)??
回復(fù)

使用道具 舉報(bào)

板凳
ID:249376 發(fā)表于 2022-10-27 10:07 | 只看該作者
OLED和DS3231是共用SDA和SCL口的哦
回復(fù)

使用道具 舉報(bào)

地板
ID:34298 發(fā)表于 2023-6-14 11:05 | 只看該作者
我做了一個(gè) 用半天就沒(méi)有電了   不實(shí)用
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表