找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2409|回复: 3
收起左侧

STC15W408AS单片机+DS3231+OLED便携式锂电池手表制作 附程序

  [复制链接]
ID:249376 发表于 2022-10-14 21:13 | 显示全部楼层 |阅读模式
本文介绍了STC15W408AS单片机+DS3231+OLED便携式锂电池手表。 由于DS3231备用电池供电,可以记录当时时间,所以没有用到睡眠,用按键开关机来查看时间。
附件包括嘉立创格式PCB项目文件(3D外壳文件还没改好),源代码。
由于计时的问题还是存在(有时莫名其妙为零),用另外3个按键控制对时间的修改。欢迎大家讨论。
制作出来的实物图如下:
2020300418.jpg

单片机源程序如下:
  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; //串行时钟
  10. sbit SDA=P1^3; //串行数据

  11. sbit KEY1=  P1^5;                  //调整
  12. sbit KEY2=  P1^4;                  //++
  13. sbit KEY3=  P1^6;                  //--

  14. uchar  a1,a2,a3;                      //按键消斗用
  15. uchar  disflag=0; //时间调整相关

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

  19. /********************************************************************************************************
  20. **         DS3231常数定义
  21. ********************************************************************************************************/
  22. #define DS3231_WriteAddress 0xD0    //器件写地址
  23. #define DS3231_ReadAddress  0xD1    //器件读地址
  24. #define DS3231_SECOND       0x00    //秒
  25. #define DS3231_MINUTE       0x01    //分
  26. #define DS3231_HOUR         0x02    //时
  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;                                                        //应答标志位
  36. /*********************OLED驱动程序用的延时程序************************************/
  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           延时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++)         //要传送的数据长度为8位
  83.     {
  84.         if((c<<BitCnt)&0x80)
  85.             SDA=1;                          //判断发送位
  86.         else
  87.             SDA=0;     
  88.         SCL=1;                            //置时钟线为高,通知被控器开始接收数据位
  89.         Delay5US();                       //保证时钟高电平周期大于4μs   
  90.         SCL=0;
  91.     }
  92.     SDA=1;                                  //8位发送完后释放数据线,准备接收应答位
  93.     SCL=1;
  94.     Delay5US();
  95.     if(SDA==1)
  96.         ack=0;   
  97.     else
  98.         ack=1;                              //判断是否接收到应答信号
  99.     SCL=0;
  100.     Delay5US();
  101. }          

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

  106.    retc=0;
  107.    SDA=1;                           //置数据线为输入方式
  108.    for(BitCnt=0;BitCnt<8;BitCnt++)
  109.    {
  110.         SCL=0;                      //置时钟线为低,准备接收数据位      
  111.         Delay5US();                 //时钟低电平周期大于4.7μs                       
  112.         SCL=1;                      //置时钟线为高使数据线上数据有效
  113.         Delay5US();
  114.         retc=retc<<1;
  115.         if(SDA==1)
  116.             retc=retc+1;            //读数据位,接收的数据位放入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();             //时钟低电平周期大于4us   
  127.     SCL=0;                  //清时钟线,钳住I2C总线以便继续接收
  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));//都转10进制输出
  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.                 //温度数据是以2 进制格式存储的并不需要数制转换
  190.                 write_byte(0x0e,0x20);//0x0e寄存器的CONV位置1开启温度转换

  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写数据************************************/
  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写命令************************************/
  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 设置坐标************************************/
  218. void OLED_Set_Pos(unsigned char x, unsigned char y)
  219. {
  220. ……………………

  221. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei附件下载:
Keil代码.7z (30.11 KB, 下载次数: 76)

评分

参与人数 1黑币 +90 收起 理由
admin + 90 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:430492 发表于 2022-10-19 16:15 | 显示全部楼层
楼主可以考虑把它设计得美观一点,这样自己用起来不是更好点??
回复

使用道具 举报

ID:249376 发表于 2022-10-27 10:07 | 显示全部楼层
OLED和DS3231是共用SDA和SCL口的哦
回复

使用道具 举报

ID:34298 发表于 2023-6-14 11:05 | 显示全部楼层
我做了一个 用半天就没有电了   不实用
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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