本帖最后由 发货后已经wi 于 2021-5-25 14:18 编辑
三、点亮OLED
//OLED初始化
void OLED_Init(void)
{
I2C_GPIO_Write_Byte(0x7800, 0xAE);
I2C_GPIO_Write_Byte(0x7800, 0xD5);
I2C_GPIO_Write_Byte(0x7800, 0x50);
I2C_GPIO_Write_Byte(0x7800, 0xA8);
I2C_GPIO_Write_Byte(0x7800, 0X3F);
I2C_GPIO_Write_Byte(0x7800, 0xD3);
I2C_GPIO_Write_Byte(0x7800, 0X00);
I2C_GPIO_Write_Byte(0x7800, 0x40);
I2C_GPIO_Write_Byte(0x7800, 0x8D);
I2C_GPIO_Write_Byte(0x7800, 0x14);
I2C_GPIO_Write_Byte(0x7800, 0x20);
I2C_GPIO_Write_Byte(0x7800, 0x02);
I2C_GPIO_Write_Byte(0x7800, 0xA0);
I2C_GPIO_Write_Byte(0x7800, 0xC0);
I2C_GPIO_Write_Byte(0x7800, 0xDA);
I2C_GPIO_Write_Byte(0x7800, 0x12);
I2C_GPIO_Write_Byte(0x7800, 0x81);
I2C_GPIO_Write_Byte(0x7800, 0xEF);
I2C_GPIO_Write_Byte(0x7800, 0xD9);
I2C_GPIO_Write_Byte(0x7800, 0xf1);
I2C_GPIO_Write_Byte(0x7800, 0xDB);
I2C_GPIO_Write_Byte(0x7800, 0x30);
I2C_GPIO_Write_Byte(0x7800, 0xA4);
I2C_GPIO_Write_Byte(0x7800, 0xA6);
I2C_GPIO_Write_Byte(0x7800, 0xAF);
CLK_SysTickDelay(10000);//延时10MS
OLED_Clear();//清屏
}
//OLED Clear
void OLED_Clear(void)
{
u8 i,n;
for(i=0;i<8;i++)
{
I2C_GPIO_Write_Byte(0x7800,0xb0+i);
I2C_GPIO_Write_Byte(0x7800,0x00);
I2C_GPIO_Write_Byte(0x7800,0x10);
for(n=0;n<128;n++)
{
I2C_GPIO_Write_Byte(0x7840,0x00);
}
}
}
//OLED Set pos
void OLED_Set_Pos(unsigned char x,unsigned char y)
{
I2C_GPIO_Write_Byte(0x7800,0xb0+y);
I2C_GPIO_Write_Byte(0x7800,((x&0xf0)>>4)|0x10);
I2C_GPIO_Write_Byte(0x7800,(x&0x0f));
}
//显示字符
void OLED_ShowChar(u8 x,u8 y,u8 string,u8 size)
{
unsigned char data=0,i=0;
data = string - ' ';//得到偏移后的值
if(x>128-1)
{
x = 0;
y = y + 2;
}
if(size == 16)
{
OLED_Set_Pos(x,y);
for(i=0; i<8; i++)
I2C_GPIO_Write_Byte(0x7840,F8X16[data*16+i]);
OLED_Set_Pos(x,y+1);
for(i=0; i<8; i++)
I2C_GPIO_Write_Byte(0x7840,F8X16[data*16+i+8]);
}
else
{
OLED_Set_Pos(x,y);
for(i=0; i<6; i++)
I2C_GPIO_Write_Byte(0x7840,F6x8[data][i]);
}
}
//显示字符串
void OLED_ShowString(u8 x,u8 y,u8* string,u8 size)
{
unsigned char i=0;
while(string[i]!='\0')
{
OLED_ShowChar(x,y,string[i],size);
x+=8;
if(x > 120)//大于一行
{
x=0;
y+=2;
}
i++;
}
}
//main添加初始化
OLED_Init();
OLED_ShowString(1,1,"Hello World",16);
将代码烧录到MCU
效果如下
|