void OLED_Refresh_Gram(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); //Set page address (0~7) //ʨ׃ҳַ֘è0~7é
OLED_WR_Byte (0x00,OLED_CMD); //Set the display location - column low address //ʨ׃Дʾλ׃j֍ַ֘
OLED_WR_Byte (0x10,OLED_CMD); //Set the display location - column height address //ʨ׃Дʾλ׃jַٟ֘
for(n=0;n<128;n++)OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
}
}
void OLED_Refresh_Line(void)
{
u8 i,n;
for(i=0;i<2;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); //Set page address (0~7) //ʨ׃ҳַ֘è0~7é
OLED_WR_Byte (0x00,OLED_CMD); //Set the display location - column low address //ʨ׃Дʾλ׃j֍ַ֘
OLED_WR_Byte (0x10,OLED_CMD); //Set the display location - column height address //ʨ׃Дʾλ׃jַٟ֘
for(n=0;n<128;n++)OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
}
}
/**************************************************************************
Function: Refresh the OLED screen
Input : Dat: data/command to write, CMD: data/command flag 0, represents the command;1, represents data
Output : none
گ˽٦ŜúвOLEDдɫһٶؖޚ
ɫࠚӎ˽údat:Ҫдɫք˽ߝ/ļ®ìcmd:˽ߝ/ļ®Ҫ־ 0,ҭʾļ®;1,ҭʾ˽ߝ
ܘ ֵúϞ
**************************************************************************/
void OLED_WR_Byte(u8 dat,u8 cmd)
{
u8 i;
if(cmd)
OLED_RS_Set();
else
OLED_RS_Clr();
for(i=0;i<8;i++)
{
OLED_SCLK_Clr();
if(dat&0x80)
OLED_SDIN_Set();
else
OLED_SDIN_Clr();
OLED_SCLK_Set();
dat<<=1;
}
OLED_RS_Set();
}
/**************************************************************************
Function: Turn on the OLED display
Input : none
Output : none
گ˽٦ŜúߪǴOLEDДʾ
ɫࠚӎ˽úϞ
ܘ ֵúϞ
**************************************************************************/
void OLED_Display_On(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC command //SET DCDCļ®
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}
/**************************************************************************
Function: Turn off the OLED display
Input : none
Output : none
گ˽٦ŜúژҕOLEDДʾ
ɫࠚӎ˽úϞ
ܘ ֵúϞ
**************************************************************************/
void OLED_Display_Off(void)
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC command //SET DCDCļ®
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}
/**************************************************************************
Function: Screen clear function, clear the screen, the entire screen is black, and did not light up the same
Input : none
Output : none
گ˽٦Ŝúȥǁگ˽,ȥΪǁ,ֻٶǁώۚɫքìۍû֣һҹ
ɫࠚӎ˽úϞ
ܘ ֵúϞ
**************************************************************************/
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)for(n=0;n<128;n++)OLED_GRAM[n][i]=0X00;
OLED_Refresh_Gram(); //Update the display //ټтДʾ
}
/**************************************************************************
Function: Draw point
Input : x,y: starting coordinate;T :1, fill,0, empty
Output : none
گ˽٦Ŝúۭ֣
ɫࠚӎ˽úx,y :ǰ֣ظҪ; t:1,ͮԤ,0,ȥࠕ
ܘ ֵúϞ
**************************************************************************/
void OLED_DrawPoint(u8 x,u8 y,u8 t)
{
u8 pos,bx,temp=0;
if(x>127||y>63)return;//ӬԶΧ.
pos=7-y/8;
bx=y%8;
temp=1<<(7-bx);
if(t)OLED_GRAM[x][pos]|=temp;
else OLED_GRAM[x][pos]&=~temp;
}
/**************************************************************************
Function: Displays a character, including partial characters, at the specified position
Input : x,y: starting coordinate;Len: The number of digits;Size: font size;Mode :0, anti-white display,1, normal display
Output : none
گ˽٦Ŝú՚ָ֨λ׃Дʾһٶؖػ,Ѽ(ҿؖؖػ
ɫࠚӎ˽úx,y :ǰ֣ظҪ; len :˽ؖքλ˽; size:ؖͥճС; mode:0,״їДʾ,1,ֽӣДʾ
ܘ ֵúϞ
**************************************************************************/
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode)
{
u8 temp,t,t1;
u8 y0=y;
chr=chr-' '; //Get the offset value //փսƫӆ۳քֵ
for(t=0;t<size;t++)
{
if(size==12)temp=oled_asc2_1206[chr][t]; //Invoke 1206 font //ַԃ1206ؖͥ
else temp=oled_asc2_1608[chr][t]; //Invoke the 1608 font //ַԃ1608ؖͥ
for(t1=0;t1<8;t1++)
{
if(temp&0x80)OLED_DrawPoint(x,y,mode);
else OLED_DrawPoint(x,y,!mode);
temp<<=1;
y++;
if((y-y0)==size)
{
y=y0;
x++;
break;
}
}
}
}
/**************************************************************************
Function: Find m to the NTH power
Input : m: base number, n: power number
Output : none
گ˽٦ŜúȳmքnՎքگ˽
ɫࠚӎ˽úmú֗˽ìnúՎ˽
ܘ ֵúϞ
**************************************************************************/
u32 oled_pow(u8 m,u8 n)
{
u32 result=1;
while(n--)result*=m;
return result;
}
/**************************************************************************
Function: Displays 2 numbers
Input : x,y: starting coordinate;Len: The number of digits;Size: font size;Mode: mode, 0, fill mode, 1, overlay mode;Num: value (0 ~ 4294967295);
Output : none
گ˽٦ŜúДʾ2ٶ˽ؖ
ɫࠚӎ˽úx,y :ǰ֣ظҪ; len :˽ؖքλ˽; size:ؖͥճС; mode:ģʽ, 0,ͮԤģʽ, 1,־ݓģʽ; num:˽ֵ(0~4294967295);
ܘ ֵúϞ
**************************************************************************/
void OLED_ShowNumber(u8 x,u8 y,u32 num,u8 len,u8 size)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/oled_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size/2)*t,y,' ',size,1);
continue;
}else enshow=1;
}
OLED_ShowChar(x+(size/2)*t,y,temp+'0',size,1);
}
}
/**************************************************************************
Function: Display string
Input : x,y: starting coordinate;*p: starting address of the string
Output : none
گ˽٦ŜúДʾؖػԮ
ɫࠚӎ˽úx,y :ǰ֣ظҪ; *p:ؖػԮǰʼַ֘
ܘ ֵúϞ
**************************************************************************/
void OLED_ShowString(u8 x,u8 y,const u8 *p)
{
#define MAX_CHAR_POSX 122
#define MAX_CHAR_POSY 58
while(*p!='\0')
{
if(x>MAX_CHAR_POSX){x=0;y+=16;}
if(y>MAX_CHAR_POSY){y=x=0;OLED_Clear();}
OLED_ShowChar(x,y,*p,12,1);
x+=8;
p++;
}
}
void OLED_ShowString16(u8 x,u8 y,const u8 *p)
{
#define MAX_CHAR_POSX 122
#define MAX_CHAR_POSY 58
while(*p!='\0')
{
if(x>MAX_CHAR_POSX){x=0;y+=16;}
if(y>MAX_CHAR_POSY){y=x=0;OLED_Clear();}
OLED_ShowChar(x,y,*p,16,1);
x+=8;
p++;
}
}
/**************************************************************************
Function: Initialize the OLED
Input : none
Output : none
گ˽٦ŜúԵʼۯOLED
ɫࠚӎ˽: Ϟ
ܘ ֵúϞ
**************************************************************************/
void OLED_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// 启用 GPIOD 时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
// 配置 PD11~PD14 为推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// OLED 复位
OLED_RST_Clr();
delay_ms(100); // 延时 100ms
OLED_RST_Set();
// OLED 寄存器初始化
OLED_WR_Byte(0xAE, OLED_CMD); // 关闭显示
OLED_WR_Byte(0xD5, OLED_CMD); // 设置时钟分频
OLED_WR_Byte(80, OLED_CMD); // 推荐值:80
OLED_WR_Byte(0xA8, OLED_CMD); // 设置驱动路数
OLED_WR_Byte(0x3F, OLED_CMD); // 默认 1/64
OLED_WR_Byte(0xD3, OLED_CMD); // 设置显示偏移
OLED_WR_Byte(0x00, OLED_CMD); // 默认 0
OLED_WR_Byte(0x40, OLED_CMD); // 设置起始行
OLED_WR_Byte(0x8D, OLED_CMD); // 充电泵
OLED_WR_Byte(0x14, OLED_CMD); // 开启充电泵
OLED_WR_Byte(0x20, OLED_CMD); // 内存地址模式
OLED_WR_Byte(0x02, OLED_CMD); // 页地址模式
OLED_WR_Byte(0xA1, OLED_CMD); // 段重定义
OLED_WR_Byte(0xC0, OLED_CMD); // COM 扫描方向
OLED_WR_Byte(0xDA, OLED_CMD); // COM 硬件配置
OLED_WR_Byte(0x12, OLED_CMD); // 默认配置
OLED_WR_Byte(0x81, OLED_CMD); // 对比度设置
OLED_WR_Byte(0xEF, OLED_CMD); // 默认值(可调整)
OLED_WR_Byte(0xD9, OLED_CMD); // 预充电周期
OLED_WR_Byte(0xF1, OLED_CMD); // 默认值
OLED_WR_Byte(0xDB, OLED_CMD); // VCOMH 电压
OLED_WR_Byte(0x30, OLED_CMD); // 默认值
OLED_WR_Byte(0xA4, OLED_CMD); // 全局显示开启
OLED_WR_Byte(0xA6, OLED_CMD); // 正常显示模式
OLED_WR_Byte(0xAF, OLED_CMD); // 开启显示
OLED_Clear(); // 清屏
}