標(biāo)題:
基于STM32的IIC_OLED驅(qū)動(dòng)源程序
[打印本頁(yè)]
作者:
沐浴
時(shí)間:
2021-1-14 17:20
標(biāo)題:
基于STM32的IIC_OLED驅(qū)動(dòng)源程序
話不多說(shuō) 本人的項(xiàng)目中設(shè)計(jì)到了OLED顯示一些花里胡哨的東西 來(lái)和大家一起分享一下成果 (已經(jīng)調(diào)試直接用)
void WriteCmd(unsigned char I2C_Command)
{
write_i2c(OLED_DEV_ADDR,0x00, I2C_Command);
}
void WriteDat(unsigned char I2C_Data)
{
write_i2c(OLED_DEV_ADDR,0x40, I2C_Data);
}
void OLED_SetPos(unsigned char x, unsigned char y)
{
WriteCmd(0xb0+y);
WriteCmd(((x&0xf0)>>4)|0x10);
WriteCmd((x&0x0f)|0x01);
}
void OLED_Fill(unsigned char fill_Data)
{
unsigned char m,n;
for(m=0;m<8;m++)
{
WriteCmd(0xb0+m); //page0-page1
WriteCmd(0x00); //low column start address
WriteCmd(0x10); //high column start address
for(n=0;n<128;n++)
{
WriteDat(fill_Data);
}
}
}
void OLED_CLS(void)
{
OLED_Fill(0x00);
}
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
{
unsigned char c = 0,i = 0,j = 0;
switch(TextSize)
{
case 1:
{
while(ch[j] != '\0')
{
c = ch[j] - 32;
if(x > 126)
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<6;i++)
WriteDat(F6x8[c][i]);
x += 6;
j++;
}
}break;
case 2:
{
while(ch[j] != '\0')
{
c = ch[j] - 32;
if(x > 120)
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<8;i++)
WriteDat(F8X16[c*16+i]);
OLED_SetPos(x,y+1);
for(i=0;i<8;i++)
WriteDat(F8X16[c*16+i+8]);
x += 8;
j++;
}
}break;
}
}
void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
{
unsigned char wm=0;
unsigned int adder=32*N;
OLED_SetPos(x , y);
for(wm = 0;wm < 16;wm++)
{
WriteDat(F16x16[adder]);
adder += 1;
}
OLED_SetPos(x,y + 1);
for(wm = 0;wm < 16;wm++)
{
WriteDat(F16x16[adder]);
adder += 1;
}
}
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
unsigned int j=0;
unsigned char x,y;
if(y1%8==0)
y = y1/8;
else
y = y1/8 + 1;
for(y=y0;y<y1;y++)
{
OLED_SetPos(x0,y);
for(x=x0;x<x1;x++)
{
WriteDat(BMP[j++]);
}
}
}
void OLED_ShowChar(u8 x, u8 y, u8 chr,u8 size)
{
unsigned char c = 0, i = 0;
c = chr - ' '; //μÃμ½Æ«òÆoóμÄÖμ
if(x > 128 - 1)
{
x = 0;
y = y + 2;
}
if(size == 0)
{
OLED_SetPos(x, y);
for(i = 0; i < 8; i++)
WriteDat(F8X16[c * 16 + i]);
OLED_SetPos(x, y + 1);
for(i = 0; i < 8; i++)
WriteDat(F8X16[c * 16 + i + 8]);
}
else
{
OLED_SetPos(x, y + 1);
for(i = 0; i < 6; i++)
WriteDat(F6x8[c][i]);
}
}
void OLED_ShowNum(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);
continue;
}
else enshow = 1;
}
OLED_ShowChar(x + (size / 2)*t, y, temp + '0',size);
}
}
復(fù)制代碼
這一個(gè)函數(shù) :void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]); 是用來(lái)顯示圖片的
這就需要用到工具 請(qǐng)百度搜索下載“ 9. Image2Lcd 2.9” 和工具“ PCtoLCD2002完美版 ”
首先下載一張圖片打開(kāi)將其轉(zhuǎn)化為BMP格式
打開(kāi)Image2Lcd 工具導(dǎo)入圖片點(diǎn)擊保存。
打開(kāi)PCtoLCD2002 配置如
416591c917aa85f27ccee2c4d04c35ae.png
(74.87 KB, 下載次數(shù): 60)
下載附件
2021-1-14 20:30 上傳
然后導(dǎo)入BMP圖片得到字符串即可
全部資料51hei下載地址:
Hardwareddata.7z
(1.55 MB, 下載次數(shù): 35)
2021-1-14 20:31 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1