#include"IIC.h"
#include"OLEDFont.h"
#include "Timer0.h"
/************************************************************
*函数名: void Init_IIC(void)
*函数功能:IIC 初始化
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void Init_IIC(void)
{
TRISCbits.TRISC3 = 1; //Configure SCL
TRISCbits.TRISC4 = 1; //Configure SDA
SSPSTAT = 0x80; //Disable SMBus &
SSPCON1 = 0x00; //
SSPCON1bits.SSPM=0B1000;//I2C 主模式,时钟为FOSC/(4*(SSPAD+1))
//SSPCON1 = 0x28; //Enable MSSP Master
SSPADD =0x27;//0x9F; //Should be 0x9F 100kHz @FOSC=64MHz
SSPCON2 = 0x00; //Clear MSSP Conrol Bits
PIR1bits.SSPIF=0; //中断标志清零
SSPCON1bits.SSPEN=1;//使能串口并将SDA和SCL引脚配置为串口引脚
}
/************************************************************
*函数名:signed char WriteI2C( unsigned char data_out )
*函数功能:IIC写时序
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
signed char WriteI2C( unsigned char data_out )
{
SSPBUF = data_out; //Load byte to I2C1 Transmit buffer
while (SSPSTATbits.BF); //等待发送完成 //发送状态位 1 ,正在发送
if(SSPCON1bits.WCOL)
SSPCON1bits.WCOL=0;
}
/*
函数:I2C_GetAck()
功能:读取从机应答位
返回:
0:从机应答
1:从机非应答
说明:
从机在收到每个字节的数据后,要产生应答位
从机在收到最后1个字节的数据后,一般要产生非应答位
*/
char I2C_GetAck()
{
return (SSPCON2bits.ACKSTAT); //Return Ack Status
}
/************************************************************************
* Function Name: LDByteWriteI2C *
* Parameters: EE memory ControlByte, address and data *
* Description: Writes data one byte at a time to I2C EE *
* device. This routine can be used for any I2C *
* EE memory device, which only uses 1 byte of *
* address data as in the 24LC01B/02B/04B/08B/16B. *
* *
************************************************************************/
unsigned char LDByteWriteI2C( unsigned char ControlByte, unsigned char LowAdd, unsigned char data )
{
IdleI2C(); // ensure module is idle
StartI2C(); // initiate START condition
while ( SSPCON2bits.SEN ); // wait until start condition is over
WriteI2C( ControlByte ); // write 1 byte - R/W bit should be 0
IdleI2C(); // ensure module is idle
if ( I2C_GetAck() ) //读从机应答信号,0,表示收到从机应答,1表示没有收到从机应答
{
StopI2C();
return 1;
}
WriteI2C( LowAdd ); // write address byte to EEPROM
IdleI2C(); // ensure module is idle
if ( I2C_GetAck() ) //读从机应答信号,0,表示收到从机应答,1表示没有收到从机应答
{
StopI2C();
return 1;
}
WriteI2C ( data ); // Write data byte to EEPROM
IdleI2C(); // ensure module is idle
if ( I2C_GetAck() ) //读从机应答信号,0,表示收到从机应答,1表示没有收到从机应答
{
StopI2C();
return 1;
}
StopI2C(); // send STOP condition
while ( SSPCON2bits.PEN ); // wait until stop condition is over
usDelay(5);
return ( 0 ); // return with no error
}
/************************************************************
*函数名: void Write_IIC_Command(unsigned char IIC_Command)
*函数功能: IIC 写命令到12864
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
LDByteWriteI2C(0x78,0x00,IIC_Command);
}
/************************************************************
*函数名: void Write_IIC_Data(unsigned char IIC_Data)
*函数功能: IIC 写数据到12864
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
LDByteWriteI2C(0x78,0x40,IIC_Data);
}
/************************************************************
*函数名: void OLED_WR_Byte(unsigned dat,unsigned cmd)
*函数功能: OLED写一个字节
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
if(cmd)
{
LDByteWriteI2C(0x78,0x40,dat);
}
else
{
LDByteWriteI2C(0x78,0x00,dat);
}
}
/************************************************************
*函数名: void OLED_Set_Pos(unsigned char x, unsigned char y)
*函数功能://坐标设置
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
OLED_WR_Byte(0xB0+y,OLED_CMD); //设置page 0-7
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); //set high column address 显存的地址为0-132,液晶的地址为0-128,所以设置起点为2(列地址2-130对应0-128)
OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD); //set low column address
}
/************************************************************
*函数名: void OLED_Display_On(void)
*函数功能:开启OLED显示
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}
/************************************************************
*函数名: void OLED_Display_On(void)
*函数功能:关闭OLED显示
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
/************************************************************
*函数名: void OLED_Clear(void)
*函数功能:清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void OLED_Clear(void)
{
unsigned char i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte (0x02,OLED_CMD); //设置显示位置?列低地址
OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置?列高地址
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
} //更新显示
}
/************************************************************
*函数名: void OLED_OnAll(void)
*函数功能:全部点亮
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void OLED_OnAll(void)
{
unsigned char i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte (0x02,OLED_CMD); //设置显示位置?列低地址
OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置?列高地址
for(n=0;n<128;n++)OLED_WR_Byte(0xFF,OLED_DATA);
} //更新显示
}
/************************************************************
*函数名: void OLED_Init(void)
*函数功能://初始化SSD1306
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
****************************************************************/
void OLED_Init(void)
{
Init_IIC();
ms_Delay(1000);
ms_Delay(1000);
ms_Delay(1000);
ms_Delay(1000);
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x02,OLED_CMD);//---set low column address
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address //显存的地址为0-132,液晶的地址为0-128,所以设置起点为2(列地址2-130对应0-128)
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address //设置行地址,0-63,0x40 对应0行,0x7F 对应63行
OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
OLED_WR_Byte(0x81,OLED_CMD); // contract control 对比度设置命令
OLED_WR_Byte(0xFF,OLED_CMD);//--128 对比度设置参数00~FF
OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap 扫描顺序向右
OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse 正常显示,RAM为1,点亮对应OLED
OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)//多路复用
OLED_WR_Byte(0x3F,OLED_CMD);//--1/64 duty //多路复用
OLED_WR_Byte(0xAD,OLED_CMD);//set charge pump enable // DC-DC Control Mode
OLED_WR_Byte(0x8B,OLED_CMD);//-0x8B 内供 VCC // DC-DC Control Mode
OLED_WR_Byte(0x33,OLED_CMD);//-0X30---0X33 set VPP 9V pump 电压
OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction //设置扫描方向When C8, Scan from COM [N -1] to COM0.
OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset//行显示偏移
OLED_WR_Byte(0x00,OLED_CMD);// //行显示偏移
OLED_WR_Byte(0xD5,OLED_CMD);//set osc division //设置时钟
OLED_WR_Byte(0x80,OLED_CMD);// //设置时钟
OLED_WR_Byte(0xD8,OLED_CMD);//set area color mode off
OLED_WR_Byte(0x05,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
OLED_WR_Byte(0x1F,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
OLED_WR_Byte(0x12,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
OLED_WR_Byte(0x40,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
}
/************************************************************
*函数名: void OLED_ShowCHinese(u8 x,u8 y,u8 no)
*函数功能:显示汉字
*输入参数: x,=0-128;y=0,2,4,6
*输出参数:
*Design By: huangrliang
* 说明:
{0x00,0x00,0x00,0xF8,0x88,0x8C,0x8A,0x89,0x88,0x88,0x88,0xF8,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0xFF,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0x00,0x00,0x00,0x00},
****************************************************************/
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{
u8 t;
OLED_Set_Pos(x,y);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
}
}
/************************************************************
*函数名: void OLED_ShowNum24x24(u8 x,u8 y,u8 no)
*函数功能:显示24x24的数字,对应每天工作 x 小时
*输入参数:
*输出参数:
*Design By: huangrliang
* 说明:
{0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x40,0x40,0x40,0x80,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0xF0,0xFE,0x0F,0x01,0x00,0x00,0x00,0x00,0x01,0x07,0xFE,0xF0,0x00,0x00},
{0x00,0x00,0x3F,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xFF,0x3F,0x00,0x00},
{0x00,0x00,0x00,0x01,0x03,0x06,0x0C,0x08,0x08,0x08,0x06,0x03,0x01,0x00,0x00,0x00},
****************************************************************/
void OLED_ShowNum16x32(u8 x,u8 y,u8 no) //一个数字占 16列,32行
{
u8 t;
OLED_Set_Pos(x,y);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Num16x32[2*no][t],OLED_DATA);
}
OLED_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
OLED_WR_Byte(Num16x32[2*no+1][t],OLED_DATA);
}
}
|