|
本文介紹了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í)物圖如下:
2020300418.jpg (128.91 KB, 下載次數(shù): 56)
下載附件
2022-10-14 21:10 上傳
單片機(jī)源程序如下:
- #include "reg51.h"
- #include "intrins.h"
- #include "codetab.h"
- #define uint unsigned int
- #define uchar unsigned char
- // ------------------------------------------------------------
- // IO口模擬I2C通信
- // ------------------------------------------------------------
- sbit SCL=P1^2; //串行時(shí)鐘
- sbit SDA=P1^3; //串行數(shù)據(jù)
- sbit KEY1= P1^5; //調(diào)整
- sbit KEY2= P1^4; //++
- sbit KEY3= P1^6; //--
- uchar a1,a2,a3; //按鍵消斗用
- uchar disflag=0; //時(shí)間調(diào)整相關(guān)
- #define Brightness 0xcf //
- #define X_WIDTH 128
- #define Y_WIDTH 64
- /********************************************************************************************************
- ** DS3231常數(shù)定義
- ********************************************************************************************************/
- #define DS3231_WriteAddress 0xD0 //器件寫(xiě)地址
- #define DS3231_ReadAddress 0xD1 //器件讀地址
- #define DS3231_SECOND 0x00 //秒
- #define DS3231_MINUTE 0x01 //分
- #define DS3231_HOUR 0x02 //時(shí)
- #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; //應(yīng)答標(biāo)志位
- /*********************OLED驅(qū)動(dòng)程序用的延時(shí)程序************************************/
- /*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 延時(shí)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++) //要傳送的數(shù)據(jù)長(zhǎng)度為8位
- {
- if((c<<BitCnt)&0x80)
- SDA=1; //判斷發(fā)送位
- else
- SDA=0;
- SCL=1; //置時(shí)鐘線為高,通知被控器開(kāi)始接收數(shù)據(jù)位
- Delay5US(); //保證時(shí)鐘高電平周期大于4μs
- SCL=0;
- }
- SDA=1; //8位發(fā)送完后釋放數(shù)據(jù)線,準(zhǔn)備接收應(yīng)答位
- SCL=1;
- Delay5US();
- if(SDA==1)
- ack=0;
- else
- ack=1; //判斷是否接收到應(yīng)答信號(hào)
- SCL=0;
- Delay5US();
- }
- uchar RcvByte()
- {
- uchar retc;
- uchar BitCnt;
-
- retc=0;
- SDA=1; //置數(shù)據(jù)線為輸入方式
- for(BitCnt=0;BitCnt<8;BitCnt++)
- {
- SCL=0; //置時(shí)鐘線為低,準(zhǔn)備接收數(shù)據(jù)位
- Delay5US(); //時(shí)鐘低電平周期大于4.7μs
- SCL=1; //置時(shí)鐘線為高使數(shù)據(jù)線上數(shù)據(jù)有效
- Delay5US();
- retc=retc<<1;
- if(SDA==1)
- retc=retc+1; //讀數(shù)據(jù)位,接收的數(shù)據(jù)位放入retc中
- Delay5US();
- }
- SCL=0;
- return(retc);
- }
-
- void Ack_I2C(bit a)
- {
- SDA = a;
- SCL=1;
- Delay5US(); //時(shí)鐘低電平周期大于4us
- SCL=0; //清時(shí)鐘線,鉗住I2C總線以便繼續(xù)接收
- 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));//都轉(zhuǎn)10進(jìn)制輸出
- }
- 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;
- //溫度數(shù)據(jù)是以2 進(jìn)制格式存儲(chǔ)的并不需要數(shù)制轉(zhuǎn)換
- write_byte(0x0e,0x20);//0x0e寄存器的CONV位置1開(kāi)啟溫度轉(zhuǎn)換
- 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寫(xiě)數(shù)據(jù)************************************/
- void OLED_WrDat(unsigned char IIC_Data)
- {
- IIC_Start();
- SendByte(0x78);
- SendByte(0x40); //write data
- SendByte(IIC_Data);
- IIC_Stop();
- }
- /*********************OLED寫(xiě)命令************************************/
- 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 設(shè)置坐標(biāo)************************************/
- void OLED_Set_Pos(unsigned char x, unsigned char y)
- {
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei附件下載:
Keil代碼.7z
(30.11 KB, 下載次數(shù): 83)
2022-10-14 22:51 上傳
點(diǎn)擊文件名下載附件
源代碼 下載積分: 黑幣 -5
eprj文件.7z
(158.67 KB, 下載次數(shù): 45)
2022-10-14 22:51 上傳
點(diǎn)擊文件名下載附件
嘉立創(chuàng)PCB+3D 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|