stm32.oled 软件iic
oled.c文件
#include "oled.h"
#include "codetab.h"
#include "delay.h"
#define X_WIDTH 128
#define Y_WIDTH 64
void OLED_WrDat(unsigned char I2C_Data)//写数据
{
I2C_WriteByte(0x40, I2C_Data);
}
void OLED_WrCmd(unsigned char I2C_Command)//写命令
{
I2C_WriteByte(0x00, I2C_Command);
}
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);
}
void OLED_Fill(unsigned char bmp_data)//全屏填充
{
unsigned char y,x;
for(y=0;y<8;y++)
{
OLED_WrCmd(0xb0+y); //page0-page1
OLED_WrCmd(0x01);
OLED_WrCmd(0x10);
for(x=0;x<X_WIDTH;x++)//low column start address
OLED_WrDat(bmp_data);//high column start address
}
}
void OLED_CLS(void)
{
OLED_Fill(0x00);
}
void OLED_Init(void)
{
delay_ms(100);
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(0xcf); // 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);
}
显示函数
//==============================================================
//函数名:OLED_P6x8Str(unsigned char x,unsigned char y,unsigned char *p)
//功能描述:写入一组标准ASCII字符串
//参数:显示的位置(x,y),y为页范围0~7,要显示的字符串
//返回:无
//==============================================================
void OLED_P6x8Str(unsigned char x,unsigned char y,unsigned char ch[])
{
unsigned char c=0,i=0,j=0;
while (ch[j]!='\0')
{
c =ch[j]-32;
if(x>126){x=0;y++;}
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
OLED_WrDat(F6x8[c][i]);
x+=6;
j++;
}
}
//==============================================================
//函数名:OLED_P8x16Str(unsigned char x,unsigned char y,unsigned char *p)
//功能描述:写入一组标准ASCII字符串
//参数:显示的位置(x,y),y为页范围0~7,要显示的字符串
//返回:无
//==============================================================
void OLED_P8x16Str(unsigned char x,unsigned char y,unsigned char ch[])
{
unsigned char c=0,i=0,j=0;
while (ch[j]!='\0')
{
c =ch[j]-32;
if(x>120){x=0;y++;}
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WrDat(F8X16[c*16+i]);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WrDat(F8X16[c*16+i+8]);
x+=8;
j++;
}
}
//输出汉字字符串
void OLED_P14x16Str(unsigned char x,unsigned char y,unsigned char ch[])
{
unsigned char wm=0,ii = 0;
uint16_t adder=1;
while(ch[ii] != '\0')
{
wm = 0;
adder = 1;
while(F14x16_Idx[wm] > 127)
{
if(F14x16_Idx[wm] == ch[ii])
{
if(F14x16_Idx[wm + 1] == ch[ii + 1])
{
adder = wm * 14;
break;
}
}
wm += 2;
}
if(x>118){x=0;y++;}
OLED_Set_Pos(x , y);
if(adder != 1)// 显示汉字
{
OLED_Set_Pos(x , y);
for(wm = 0;wm < 14;wm++)
{
OLED_WrDat(F14x16[adder]);
adder += 1;
}
OLED_Set_Pos(x,y + 1);
for(wm = 0;wm < 14;wm++)
{
OLED_WrDat(F14x16[adder]);
adder += 1;
}
}
else //显示空白字符
{
ii += 1;
OLED_Set_Pos(x,y);
for(wm = 0;wm < 16;wm++)
{
OLED_WrDat(0);
}
OLED_Set_Pos(x,y + 1);
for(wm = 0;wm < 16;wm++)
{
OLED_WrDat(0);
}
}
x += 14;
ii += 2;
}
}
//输出汉字和字符混合字符串
void OLED_Print(unsigned char x, unsigned char y, unsigned char ch[])
{
unsigned char ch2[3];
unsigned char ii=0;
while(ch[ii] != '\0')
{
if(ch[ii] > 127)
{
ch2[0] = ch[ii];
ch2[1] = ch[ii + 1];
ch2[2] = '\0'; //汉字为两个字节
OLED_P14x16Str(x , y, ch2); //显示汉字
x += 14;
ii += 2;
}
else
{
ch2[0] = ch[ii];
ch2[1] = '\0'; //字母占一个字节
OLED_P8x16Str(x , y , ch2); //显示字母
x += 8;
ii+= 1;
}
}
}
//输出汉字字符串
void OLED_P14x16Str_Reverse(unsigned char x,unsigned char y,unsigned char ch[])
{
unsigned char wm=0,ii = 0;
uint16_t adder=1;
while(ch[ii] != '\0')
{
wm = 0;
adder = 1;
while(F14x16_Idx[wm] > 127)
{
if(F14x16_Idx[wm] == ch[ii])
{
if(F14x16_Idx[wm + 1] == ch[ii + 1])
{
adder = wm * 14;
break;
}
}
wm += 2;
}
if(x>118){x=0;y++;}
OLED_Set_Pos(x , y);
if(adder != 1)// 显示汉字
{
OLED_Set_Pos(x , y);
for(wm = 0;wm < 14;wm++)
{
OLED_WrDat(~F14x16[adder]);
adder += 1;
}
OLED_Set_Pos(x,y + 1);
for(wm = 0;wm < 14;wm++)
{
OLED_WrDat(~F14x16[adder]);
adder += 1;
}
}
else //显示空白字符
{
ii += 1;
OLED_Set_Pos(x,y);
for(wm = 0;wm < 16;wm++)
{
OLED_WrDat(0);
}
OLED_Set_Pos(x,y + 1);
for(wm = 0;wm < 16;wm++)
{
OLED_WrDat(0);
}
}
x += 14;
ii += 2;
}
}
//输出汉字和字符混合字符串
void OLED_Print_Reverse(unsigned char x, unsigned char y, unsigned char ch[])
{
unsigned char ch2[3];
unsigned char ii=0;
while(ch[ii] != '\0')
{
if(ch[ii] > 127)
{
ch2[0] = ch[ii];
ch2[1] = ch[ii + 1];
ch2[2] = '\0'; //汉字为两个字节
OLED_P14x16Str_Reverse(x , y, ch2); //显示汉字
x += 14;
ii += 2;
}
else
{
ch2[0] = ch[ii];
ch2[1] = '\0'; //字母占一个字节
OLED_P8x16Str_Reverse(x , y , ch2); //显示字母
x += 8;
ii+= 1;
}
}
}
oled.h文件
#ifndef __OLED_H
#define __OLED_H
#include "stm32f10x.h"
#define OLED_ADDRESS 0x78 //通过调整0R电阻,屏可以0x78和0x7A两个地址 -- 默认0x78
void I2C_Configuration(void);
void I2C_WriteByte(uint8_t addr,uint8_t data);
void OLED_Init(void);
void OLED_CLS(void);
void OLED_P6x8Str(uint8_t x,uint8_t y,unsigned char ch[]);
void OLED_P8x16Str(uint8_t x,uint8_t y,unsigned char ch[]);
void OLED_P14x16Str(uint8_t x,uint8_t y,uint8_t ch[]);
void OLED_Print(uint8_t x, uint8_t y, uint8_t ch[]);
void OLED_PutPixel(uint8_t x,uint8_t y);
void OLED_Rectangle(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t gif);
void Draw_LQLogo(void);
void Draw_LibLogo(void);
void Draw_BMP(uint8_t x0,uint8_t y0,uint8_t x1,uint8_t y1,uint8_t bmp[]);
void OLED_Fill(uint8_t dat);
void Dly_ms(int ms);
void OLED_TEST(void);
void OLED_show(uint8_t page);
void OLED_draw_picture(uint8_t row, uint8_t col, uint8_t* buffer);
uint8_t oled_sendnumber_int(uint8_t x,uint8_t y,int number,uint8_t size);//新加
unsigned char oled_sendnumber_int_Reverse(unsigned char x,unsigned char y,int number,unsigned char size);
uint8_t oled_get_byte_array_from_number(uint8_t array [], int * length, uint16_t number);
uint8_t oled_get_byte_array_from_number_int(uint8_t array [], int * length, int number);
//==============================================================
//字模取反输出
//==============================================================
void OLED_P8x16Str_Reverse(uint8_t x,uint8_t y,uint8_t ch[]);
//输出汉字字符串
void OLED_P14x16Str_Reverse(uint8_t x,uint8_t y,uint8_t ch[]);
//输出汉字和字符混合字符串
void OLED_Print_Reverse(uint8_t x, uint8_t y, uint8_t ch[]);
//jlw添加
void OLED_P6x8Str_Reverse(unsigned char x,unsigned char y,unsigned char ch[]);
unsigned char oled_floatnumber_to_str(unsigned char array[],unsigned char dotsize,float fNum);
void oled_sendnumber_float(unsigned char x,unsigned char y,float fNum,unsigned char dotsize);
#endif