硬件連接方式很簡單,因為3231和OLED都是IIC,所以共用接口
sbit SCL=P3^4; //
sbit SDA=P3^3; //
sbit KEY1= P3^2; //set
sbit KEY2= P3^0; //+
sbit KEY3= P3^5; //-
IMG_20171001_123156.jpg (2.3 MB, 下載次數(shù): 105)
下載附件
2017-10-1 12:46 上傳
IMG_20171001_122609.jpg (2.37 MB, 下載次數(shù): 104)
下載附件
2017-10-1 12:45 上傳
IMG_20171001_114940.jpg (2.57 MB, 下載次數(shù): 119)
下載附件
2017-10-1 12:44 上傳
IMG_20171001_114928.jpg (2.59 MB, 下載次數(shù): 116)
下載附件
2017-10-1 12:43 上傳
IMG_20171001_111453.jpg (2.4 MB, 下載次數(shù): 129)
下載附件
2017-10-1 12:42 上傳
IMG_20171001_111436.jpg (2.29 MB, 下載次數(shù): 112)
下載附件
2017-10-1 12:41 上傳
IMG_20171001_111345.jpg (2.25 MB, 下載次數(shù): 109)
下載附件
2017-10-1 12:40 上傳
0.png (7.59 KB, 下載次數(shù): 119)
下載附件
2017-10-1 17:09 上傳
固件和源代碼
單片機源程序如下:
- #include "reg51.h"
- #include "intrins.h"
- #include "codetab.h"
- #define uint unsigned int
- #define uchar unsigned char
- // ------------------------------------------------------------
- // IO口模擬I2C通信
- // ------------------------------------------------------------
- sbit SCL=P3^4; //串行時鐘
- sbit SDA=P3^3; //串行數(shù)據(jù)
- sbit KEY1= P3^2; //調(diào)整
- sbit KEY2= P3^0; //++
- sbit KEY3= P3^5; //--
- uchar a1,a2,a3; //按鍵消斗用
- uchar disflag=0; //時間調(diào)整相關(guān)
- #define Brightness 0xcf //
- #define X_WIDTH 128
- #define Y_WIDTH 64
- /********************************************************************************************************
- ** DS3231常數(shù)定義
- ********************************************************************************************************/
- #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; //應(yīng)答標志位
- /*********************OLED驅(qū)動程序用的延時程序************************************/
- /*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++) //要傳送的數(shù)據(jù)長度為8位
- {
- if((c<<BitCnt)&0x80)
- SDA=1; //判斷發(fā)送位
- else
- SDA=0;
- SCL=1; //置時鐘線為高,通知被控器開始接收數(shù)據(jù)位
- Delay5US(); //保證時鐘高電平周期大于4μs
- SCL=0;
- }
- SDA=1; //8位發(fā)送完后釋放數(shù)據(jù)線,準備接收應(yīng)答位
- SCL=1;
- Delay5US();
- if(SDA==1)
- ack=0;
- else
- ack=1; //判斷是否接收到應(yīng)答信號
- SCL=0;
- Delay5US();
- }
- uchar RcvByte()
- {
- uchar retc;
- uchar BitCnt;
-
- retc=0;
- SDA=1; //置數(shù)據(jù)線為輸入方式
- for(BitCnt=0;BitCnt<8;BitCnt++)
- {
- SCL=0; //置時鐘線為低,準備接收數(shù)據(jù)位
- Delay5US(); //時鐘低電平周期大于4.7μs
- SCL=1; //置時鐘線為高使數(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(); //時鐘低電平周期大于4us
- SCL=0; //清時鐘線,鉗住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進制輸出
- }
- 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 進制格式存儲的并不需要數(shù)制轉(zhuǎn)換
- write_byte(0x0e,0x20);//0x0e寄存器的CONV位置1開啟溫度轉(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寫數(shù)據(jù)************************************/
- 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 設(shè)置坐標************************************/
- void OLED_Set_Pos(unsigned char x, unsigned char y)
- {
- OLED_WrCmd(0xb0+y);
- OLED_WrCmd(((x&0xf0)>>4)|0x10);
- OLED_WrCmd((x&0x0f)|0x01);
- }
- /*********************OLED全屏************************************/
- void OLED_Fill(unsigned char bmp_dat)
- {
- unsigned char y,x;
- for(y=0;y<8;y++)
- {
- OLED_WrCmd(0xb0+y);
- OLED_WrCmd(0x01);
- OLED_WrCmd(0x10);
- for(x=0;x<X_WIDTH;x++)
- OLED_WrDat(bmp_dat);
- }
- }
- /*********************OLED復(fù)位************************************/
- void OLED_CLS(void)
- {
- unsigned char y,x;
- for(y=0;y<8;y++)
- {
- OLED_WrCmd(0xb0+y);
- OLED_WrCmd(0x01);
- OLED_WrCmd(0x10);
- for(x=0;x<X_WIDTH;x++)
- OLED_WrDat(0);
- }
- }
- /*********************OLED初始化************************************/
- void OLED_Init(void)
- {
- Delay5US();//初始化之前的延時很重要!
- OLED_WrCmd(0xae);//--turn off oled panel
- OLED_WrCmd(0x00);//---set low column address
- OLED_WrCmd(0x10);//---set high column address
- OLED_WrCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
- OLED_WrCmd(0x81);//--set contrast control register
- OLED_WrCmd(Brightness); // Set SEG Output Current Brightness
- OLED_WrCmd(0xa1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
- OLED_WrCmd(0xc8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
- OLED_WrCmd(0xa6);//--set normal display
- OLED_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
- OLED_WrCmd(0x3f);//--1/64 duty
- OLED_WrCmd(0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
- OLED_WrCmd(0x00);//-not offset
- OLED_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
- OLED_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
- OLED_WrCmd(0xd9);//--set pre-charge period
- OLED_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- OLED_WrCmd(0xda);//--set com pins hardware configuration
- OLED_WrCmd(0x12);
- OLED_WrCmd(0xdb);//--set vcomh
- OLED_WrCmd(0x40);//Set VCOM Deselect Level
- OLED_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
- OLED_WrCmd(0x02);//
- OLED_WrCmd(0x8d);//--set Charge Pump enable/disable
- OLED_WrCmd(0x14);//--set(0x10) disable
- OLED_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
- OLED_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
- OLED_WrCmd(0xaf);//--turn on oled panel
- OLED_Fill(0x00); //初始清屏
- OLED_Set_Pos(0,0);
- }
- /*******************功能描述:顯示8*16一組標準ASCII字符串 顯示的坐標(x,y),y為頁范圍0~7****************/
- /*void OLED_P8x16List(unsigned char x, y,unsigned char ch[])
- {
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
OLED.rar
(48.69 KB, 下載次數(shù): 456)
2017-10-1 12:38 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|