上一篇文章講了OLED文字顯示代碼:http://ttokpm.com/d/1749110.html
這片文章將講解如何用OLED顯示圖片,包括長(zhǎng)度為128像素,64像素,48像素,24像素,16像素的bmp單色圖片,各種像素的圖片可以根據(jù)自己的要求進(jìn)行修改,本文只是舉例。
一、生成圖片
找一張想要顯示的圖片,使用PS或者其他圖片處理工具將圖片變換成長(zhǎng)度為128像素大小。
本文使用電子發(fā)燒友的logo來(lái)實(shí)驗(yàn)。
1. 保存電子發(fā)燒有的圖片,并使用PS工具將圖片大小修改為128像素,寬度不用設(shè)置,然后打開(kāi)畫圖工具將圖片另存為單色bmp格式。
2. 打開(kāi)PCtoLCD取模工具,選擇模式→圖形模式
3. 打開(kāi)圖片,選擇需要取模的圖片,然后點(diǎn)擊生成字模
4. 將生成的字模復(fù)制到bmp.c中,放在EleFans_Logo數(shù)組中
const unsigned char EleFans_logo[][16] =
{
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,0x00,0x7F},
{0xC0,0x00,0x00,0x00,0x03,0xFF,0xF8,0x00,0x00,0x00,0x0F,0xFF,0xFE,0x00,0x00,0x00},
{0x1F,0xFF,0xFF,0x00,0x00,0x00,0x3F,0xFF,0xFF,0x80,0x00,0x00,0x7F,0xF1,0xFF,0xC0},
{0x00,0x00,0xFF,0xE4,0xFF,0xC0,0x00,0x01,0xFF,0xEE,0xFF,0xC0,0x00,0x01,0xFF,0xE4},
{0xFF,0xC0,0x00,0x03,0xFF,0xE1,0xFF,0xC8,0x00,0x03,0xFF,0xCF,0xFF,0xD8,0x00,0x07},
{0xFF,0x9F,0xFF,0x9C,0x00,0x07,0xFF,0xBF,0xFF,0x3C,0x00,0x07,0xFF,0x3F,0xFF,0x7C},
{0x00,0x0F,0xFE,0x7F,0xFE,0x7E,0x00,0x0F,0xFC,0xFF,0xFC,0xFE,0x00,0x0F,0xFD,0xFF},
{0xF9,0xFE,0x00,0x0F,0xF9,0xFF,0xF3,0xFE,0x00,0x0F,0xF3,0xFF,0xF7,0xFE,0x00,0x1F},
{0xF7,0xFF,0xE7,0xFE,0x00,0x1F,0xEF,0xFF,0xCF,0xFE,0x00,0x3F,0xEF,0xFF,0x9F,0xFC},
{0x00,0x7F,0xEF,0xFF,0x3F,0xFC,0x00,0xFF,0xCF,0xFF,0x3F,0xFC,0x0F,0xFF,0xCF,0xFE},
{0x7F,0xF8,0x07,0xFF,0x9F,0xF0,0xFF,0xF8,0x00,0x00,0x3F,0xE4,0xFF,0xF0,0x00,0x00},
{0xFF,0xEE,0xFF,0xF0,0x00,0x00,0xFF,0xE4,0xFF,0xE0,0x00,0x00,0x7F,0xF1,0xFF,0xC0},
{0x00,0x00,0x3F,0xFF,0xFF,0x80,0x00,0x00,0x0F,0xFF,0xFF,0x00,0x00,0x00,0x07,0xFF},
{0xFC,0x00,0x00,0x00,0x01,0xFF,0xF0,0x00,0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00},
{0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xFC,0x07,0x80,0x00,0x00},
{0x0F,0xFC,0x00,0x40,0x00,0x00,0x0C,0xCC,0x00,0x40,0x00,0x00,0x0C,0xCC,0x07,0xC0},
{0x00,0x00,0x0C,0xCC,0x00,0x40,0x00,0x00,0x0C,0xCC,0x00,0x40,0x00,0x00,0x1D,0xFE},
{0x07,0x80,0x00,0x00,0x1F,0xFF,0x80,0x00,0x00,0x00,0x1F,0xFF,0x87,0x80,0x00,0x00},
{0x0C,0xCC,0x80,0x40,0x00,0x00,0x0C,0xCC,0x80,0x40,0x00,0x00,0x0C,0xCC,0x87,0xC0},
{0x00,0x00,0x0F,0xFC,0x80,0x40,0x00,0x00,0x0F,0xFD,0x80,0x40,0x00,0x00,0x0F,0xB0},
{0x87,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x07,0x80,0x00,0x00},
{0x00,0x60,0x00,0x40,0x00,0x00,0x18,0x60,0x05,0x40,0x00,0x00,0x18,0x60,0x07,0xC0},
{0x00,0x00,0x18,0x60,0x00,0x40,0x00,0x00,0x18,0x60,0x87,0x80,0x00,0x00,0x18,0x60},
{0x80,0x00,0x00,0x00,0x18,0x61,0x80,0x40,0x00,0x00,0x18,0xFF,0x80,0x00,0x00,0x00},
{0x19,0xFF,0x03,0x80,0x00,0x00,0x1F,0xE0,0x05,0x40,0x00,0x00,0x1E,0x60,0x05,0x40},
{0x00,0x00,0x1C,0x60,0x05,0x40,0x00,0x00,0x18,0x60,0x03,0x40,0x00,0x00,0x00,0x60},
{0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x1E,0x03,0x83,0x80,0x00,0x00,0x1E,0x1F,0x85,0x40,0x00,0x00,0x07,0xFC,0x05,0x40},
{0x00,0x00,0x07,0xE0,0x85,0x40,0x00,0x00,0x1F,0xC1,0x83,0x40,0x00,0x00,0x1E,0xD9},
{0x81,0x00,0x00,0x00,0x06,0xDF,0x80,0x00,0x00,0x00,0x06,0xC7,0x02,0x00,0x00,0x00},
{0x06,0xC7,0x02,0x00,0x00,0x00,0x06,0xC7,0x0F,0xC0,0x00,0x00,0x06,0xDF,0x8A,0x00},
{0x00,0x00,0x0E,0xF9,0x8A,0x00,0x00,0x00,0x0E,0xF1,0x80,0x00,0x00,0x00,0x00,0x00},
{0x83,0x80,0x00,0x00,0x00,0x00,0x04,0x40,0x00,0x00,0x00,0x00,0x04,0x40,0x00,0x00},
{0x0F,0xE1,0x84,0xC0,0x00,0x00,0x00,0xFF,0x8F,0x40,0x00,0x00,0x1F,0xFE,0x00,0x40},
{0x00,0x00,0x0B,0xFF,0x00,0x00,0x00,0x00,0x07,0xE3,0x83,0xC0,0x00,0x00,0x0C,0x80},
{0x06,0x00,0x00,0x00,0x08,0x98,0x86,0x00,0x00,0x00,0x18,0x9F,0x83,0xC0,0x00,0x00},
{0x18,0x9E,0x00,0x00,0x00,0x00,0x1D,0x98,0x06,0x40,0x00,0x00,0x0F,0x98,0x05,0x40},
{0x00,0x00,0x0F,0x9F,0x85,0x40,0x00,0x00,0x09,0xDB,0x84,0xC0,0x00,0x00,0x09,0x99},
{0x80,0x00,0x00,0x00,0x09,0x10,0x00,0x40,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00},
{0x00,0x00,0x06,0xC0,0x00,0x00,0x0C,0x1F,0x84,0x40,0x00,0x00,0x0F,0xFF,0x84,0x40},
{0x00,0x00,0x1F,0xE0,0x00,0x00,0x00,0x00,0x1D,0x81,0x83,0x80,0x00,0x00,0x0D,0xB1},
{0x84,0x40,0x00,0x00,0x0D,0xBB,0x84,0x40,0x00,0x00,0x0D,0x9F,0x03,0x80,0x00,0x00},
{0x0D,0x8F,0x00,0x00,0x00,0x00,0x0D,0x87,0x07,0xC0,0x00,0x00,0x0D,0x8F,0x04,0x00},
{0x00,0x00,0x0D,0x9F,0x85,0x40,0x00,0x00,0x0D,0xF9,0x87,0x80,0x00,0x00,0x0D,0xE1},
{0x84,0x00,0x00,0x00,0x0C,0x81,0x87,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
};
5. 在bmp.h中寫一個(gè)函數(shù)獲取圖片數(shù)據(jù)數(shù)組的大小,后邊顯示圖片的時(shí)候會(huì)使用到
unsigned int Get_EleFans_logo_len(void)
{
return sizeof(EleFans_logo)/sizeof(unsigned char);
}
6. 在bmp.h中聲明數(shù)組與函數(shù),用于app_oled.c中調(diào)用
extern unsigned int Get_EleFans_logo_len(void);
extern const unsigned char EleFans_logo[][16];
二、圖片顯示函數(shù)編輯
根據(jù)取模規(guī)則,編寫圖片顯示函數(shù)
static void OLED_DrawBmpImg(uint8_t x,
uint8_t y,
SSD1306_COLOR color,
const uint8_t bmp[][16],
uint32_t len)
{
uint16_t t,t1;
uint8_t y0 = y;
uint8_t temp = 0u;
uint16_t len2 = (uint16_t)(len>>4u);
for(t=0; t {
temp=(White == color) ?
(bmp[(uint8_t)(t/16)][(uint8_t)(t%16)]):(~bmp[(uint8_t)(t/16)][(uint8_t)(t%16)]);;>
for(t1=0; t1<8; t1++)
{
if(temp&0x80)
ssd1306_DrawPixel(x, y, White);
temp <<= 1u;
y++;
if((y-y0) == len2)
{
y = y0;
x ++;
break;
}
}
}
}
三、對(duì)顯示圖片函數(shù)進(jìn)行改進(jìn),可以顯示不同規(guī)格的圖片,其中size是圖片的長(zhǎng)度,目前支持16,24,48,64長(zhǎng)度的圖片的顯示
static void OLED_DrawBmpImg2(uint8_t x,
uint8_t y,
SSD1306_COLOR color,
const uint8_t bmp[][8],
uint32_t len,
uint8_t size)
{
uint16_t t, t1;
uint8_t y_end;
uint8_t temp;
uint16_t len2 = 0u;
y_end = y;
if(size == 24)
len2 = (uint16_t)(len/3.2);
else if(size == 16)
len2 = (uint16_t)(len/2);
else if(size == 48)
len2 = (uint16_t)(len/6);
else if(size == 64)
len2 = (uint16_t)(len/16);
for(t=0; t {
temp=(White == color) ?
(bmp[(uint8_t)(t/8)][(uint8_t)(t%8)]):(~bmp[(uint8_t)(t/8)][(uint8_t)(t%8)]);;>
for(t1=0; t1<8; t1++)
{
if(temp&0x80)
ssd1306_DrawPixel(x, y, White);
temp <<= 1u;
y++;
if((y-y_end) == len2)
{
y = y_end;
x ++;
break;
}
}
}
}
四、顯示電子發(fā)燒友logo
ssd1306_Fill(Black);
OLED_DrawBmpImg(0, 10, White, EleFans_logo, Get_EleFans_logo_len());
ssd1306_UpdateScreen();
五、效果
1.下圖是顯示的電子發(fā)燒友的logo
2. 顯示其他尺寸的圖片效果
六、完整代碼
參考https://gitee.com/hehung/ab32-vg1_-rt-thread/tree/master/OLED_test
-
OLED技術(shù)
+關(guān)注
關(guān)注
1文章
66瀏覽量
13259 -
OLED
+關(guān)注
關(guān)注
119文章
6171瀏覽量
223691 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4284瀏覽量
62328 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1261瀏覽量
39842
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論