本文介紹了STC15W408AS單片機+DS3231+OLED便攜式鋰電池手表。 由于DS3231備用電池供電,可以記錄當時時間,所以沒有用到睡眠,用按鍵開關機來查看時間。
附件包括嘉立創格式PCB項目文件(3D外殼文件還沒改好),源代碼。
由于計時的問題還是存在(有時莫名其妙為零),用另外3個按鍵控制對時間的修改。歡迎大家討論。
制作出來的實物圖如下:
2020300418.jpg (128.91 KB, 下載次數: 56)
下載附件
2022-10-14 21:10 上傳
單片機源程序如下:
- #include "reg51.h"
- #include "intrins.h"
- #include "codetab.h"
- #define uint unsigned int
- #define uchar unsigned char
- // ------------------------------------------------------------
- // IO口模擬I2C通信
- // ------------------------------------------------------------
- sbit SCL=P1^2; //串行時鐘
- sbit SDA=P1^3; //串行數據
- sbit KEY1= P1^5; //調整
- sbit KEY2= P1^4; //++
- sbit KEY3= P1^6; //--
- uchar a1,a2,a3; //按鍵消斗用
- uchar disflag=0; //時間調整相關
- #define Brightness 0xcf //
- #define X_WIDTH 128
- #define Y_WIDTH 64
- /********************************************************************************************************
- ** DS3231常數定義
- ********************************************************************************************************/
- #define DS3231_WriteAddress 0xD0 //器件寫地址
- #define DS3231_ReadAddress 0xD1 //器件讀地址
- #define DS3231_SECOND 0x00 //秒
- #define DS3231_MINUTE 0x01 //分
- #define DS3231_HOUR 0x02 //時
- #define DS3231_WEEK 0x03 //星期
- #define DS3231_DAY 0x04 //日
- #define DS3231_MONTH 0x05 //月
- #define DS3231_YEAR 0x06 //年
- #define NACK 1
- #define ACK 0
- char hour,minute,second,year,month,day,week;
- uchar TH3231;
- bit ack; //應答標志位
- /*********************OLED驅動程序用的延時程序************************************/
- /*void delay(unsigned int z)
- {
- unsigned int x,y;
- for(x=z;x>0;x--)
- for(y=1100;y>0;y--);
- } */
- void Delay5US() //@12.000MHz 延時5us
- {
- _nop_(); _nop_(); _nop_();_nop_();
- }
- /**********************************************
- //IIC Start
- **********************************************/
- void IIC_Start()
- {
- SCL = 1;
- SDA = 1;
- SDA = 0;
- SCL = 0;
- }
- /**********************************************
- //IIC Stop
- **********************************************/
- void IIC_Stop()
- {
- SCL = 0;
- SDA = 0;
- SCL = 1;
- SDA = 1;
- }
- /********************************************************************************************************
- ** 3231
- ********************************************************************************************************/
- uchar BCD2HEX(uchar val)
- {
- return ((val>>4)*10)+(val&0x0f);
- }
- uchar HEX2BCD(uchar val)
- {
- return (((val%100)/10)<<4)|(val%10);
- }
- void SendByte(uchar c)
- {
- uchar BitCnt;
-
- for(BitCnt=0;BitCnt<8;BitCnt++) //要傳送的數據長度為8位
- {
- if((c<<BitCnt)&0x80)
- SDA=1; //判斷發送位
- else
- SDA=0;
- SCL=1; //置時鐘線為高,通知被控器開始接收數據位
- Delay5US(); //保證時鐘高電平周期大于4μs
- SCL=0;
- }
- SDA=1; //8位發送完后釋放數據線,準備接收應答位
- SCL=1;
- Delay5US();
- if(SDA==1)
- ack=0;
- else
- ack=1; //判斷是否接收到應答信號
- SCL=0;
- Delay5US();
- }
- uchar RcvByte()
- {
- uchar retc;
- uchar BitCnt;
-
- retc=0;
- SDA=1; //置數據線為輸入方式
- for(BitCnt=0;BitCnt<8;BitCnt++)
- {
- SCL=0; //置時鐘線為低,準備接收數據位
- Delay5US(); //時鐘低電平周期大于4.7μs
- SCL=1; //置時鐘線為高使數據線上數據有效
- Delay5US();
- retc=retc<<1;
- if(SDA==1)
- retc=retc+1; //讀數據位,接收的數據位放入retc中
- Delay5US();
- }
- SCL=0;
- return(retc);
- }
-
- void Ack_I2C(bit a)
- {
- SDA = a;
- SCL=1;
- Delay5US(); //時鐘低電平周期大于4us
- SCL=0; //清時鐘線,鉗住I2C總線以便繼續接收
- Delay5US();
- }
- uchar write_byte(uchar addr, uchar write_data)
- {
- IIC_Start();
- SendByte(DS3231_WriteAddress);
- if (ack == 0)
- return 0;
-
- SendByte(addr);
- if (ack == 0)
- return 0;
-
- SendByte(write_data);
- if (ack == 0)
- return 0;
-
- IIC_Stop();
- Delay5US();
- Delay5US();
- return 1;
- }
- uchar read_current()
- {
- uchar read_data;
- IIC_Start();
- SendByte(DS3231_ReadAddress);
- if(ack==0)
- return(0);
- read_data = RcvByte();
- Ack_I2C(1);
- IIC_Stop();
- return read_data;
- }
- uchar read_random(uchar random_addr)
- {
- uchar Tmp;
- IIC_Start();
- SendByte(DS3231_WriteAddress);
- if(ack==0)
- return(0);
- SendByte(random_addr);
- if(ack==0)
- return(0);
- Tmp=read_current();
- if(random_addr==DS3231_HOUR)
- Tmp&=0x3f;
-
- return(BCD2HEX(Tmp));//都轉10進制輸出
- }
- void ModifyTime(uchar address,uchar num)
- {
- uchar temp=0;
- if(address>6 && address <0) return;
- temp=HEX2BCD(num);
- write_byte(address,temp);
- }
- uint read_temp()
- {
- int itemp;
- float ftemp;
- //溫度數據是以2 進制格式存儲的并不需要數制轉換
- write_byte(0x0e,0x20);//0x0e寄存器的CONV位置1開啟溫度轉換
- itemp = ( (int) read_random(0x11) << 5 ); //放大32倍
- itemp += ( read_random(0x12)>> 3);
- IIC_Stop();
- if(itemp & 0x1000)
- itemp += 0xe000;
- ftemp = 0.3125 * (float) itemp+0.5; //放大10倍
- return (uint) ftemp;
- }
- /*********************OLED寫數據************************************/
- void OLED_WrDat(unsigned char IIC_Data)
- {
- IIC_Start();
- SendByte(0x78);
- SendByte(0x40); //write data
- SendByte(IIC_Data);
- IIC_Stop();
- }
- /*********************OLED寫命令************************************/
- void OLED_WrCmd(unsigned char IIC_Command)
- {
- IIC_Start();
- SendByte(0x78); //Slave address,SA0=0
- SendByte(0x00); //write command
- SendByte(IIC_Command);
- IIC_Stop();
- }
- /*********************OLED 設置坐標************************************/
- void OLED_Set_Pos(unsigned char x, unsigned char y)
- {
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei附件下載:
Keil代碼.7z
(30.11 KB, 下載次數: 83)
2022-10-14 22:51 上傳
點擊文件名下載附件
源代碼 下載積分: 黑幣 -5
eprj文件.7z
(158.67 KB, 下載次數: 45)
2022-10-14 22:51 上傳
點擊文件名下載附件
嘉立創PCB+3D 下載積分: 黑幣 -5
|