硬件连接方式很简单,因为3231和OLED都是IIC,所以共用接口
sbit SCL=P3^4; //
sbit SDA=P3^3; //
sbit KEY1= P3^2; //set
sbit KEY2= P3^0; //+
sbit KEY3= P3^5; //-
固件和源代码
单片机源程序如下:
- #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; //串行数据
- sbit KEY1= P3^2; //调整
- sbit KEY2= P3^0; //++
- sbit KEY3= P3^5; //--
- 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)
- {
- 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复位************************************/
- 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黑下载附件…………
复制代码
所有资料51hei提供下载:
OLED.rar
(48.69 KB, 下载次数: 456)
|