【AB32VG1开发板测评】OLED屏的显示驱动
本帖最后由 jinglixixi 于 2021-9-3 23:58 编辑<p style="text-align:justify">OLED屏是一种自发光的显示器件,它有多种类型,按接口分有I2C接口和SPI接口,按色彩分为单色、双色及彩色屏,按显示规格分为0.91、0.96寸等。</p>
<p>为了节省I/O口,这里所用的显示屏为I2C接口的0.91寸的单色屏。</p>
<p>该显示屏与开发板的连接关系为:</p>
<p>SDA-- PE2</p>
<p>SCL-- PE3</p>
<p>使相应引脚输出高低电平的语句定义为:</p>
<p>uint8_t pin_scl;</p>
<p>uint8_t pin_sda;</p>
<p>#define OLED_SCLK_Clr() rt_pin_write(pin_scl, PIN_LOW)</p>
<p>#define OLED_SCLK_Set() rt_pin_write(pin_scl, PIN_HIGH)</p>
<p>#define OLED_SDIN_Clr() rt_pin_write(pin_sda, PIN_LOW)</p>
<p>#define OLED_SDIN_Set() rt_pin_write(pin_sda, PIN_HIGH)</p>
<p>OLED屏的初始化函数为:</p>
<pre>
<code class="language-cpp">void OLED_Init(void)
{
Write_IIC_Command(0xAE); //display off
Write_IIC_Command(0x40);//--set start line address
Write_IIC_Command(0xb0);//Set Page Start Address for Page Addressing Mode,0-7
Write_IIC_Command(0xc8);//Set COM Output Scan Direction
Write_IIC_Command(0x81);//--set contrast control register
Write_IIC_Command(0xff);
Write_IIC_Command(0xa1);//--set segment re-map 0 to 127
Write_IIC_Command(0xa6);//--set normal display
Write_IIC_Command(0xa8);//--set multiplex ratio(1 to 64)
Write_IIC_Command(0x1F);
Write_IIC_Command(0xd3);//-set display offset
Write_IIC_Command(0x00);//-not offset
Write_IIC_Command(0xd5);//--set display clock divide ratio/oscillator frequency
Write_IIC_Command(0xf0);//--set divide ratio
Write_IIC_Command(0xd9);//--set pre-charge period
Write_IIC_Command(0x22);
Write_IIC_Command(0xda);//--set com pins hardware configuration
Write_IIC_Command(0x02);
Write_IIC_Command(0x8d);//--set DC-DC enable
Write_IIC_Command(0x14);
Write_IIC_Command(0xdb);//--set vcomh
Write_IIC_Command(0x49);//0x20,0.77xVcc
Write_IIC_Command(0xaf);//--turn on oled panel
}</code></pre>
<p>以I/O口模拟I2C方式字节数据的函数为:</p>
<pre>
<code class="language-cpp">void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
unsigned char m,da;
da=IIC_Byte;
OLED_SCLK_Clr();
for(i=0;i<8;i++)
{
m=da;
m=m&0x80;
if(m==0x80)
{
OLED_SDIN_Set();
}
else OLED_SDIN_Clr();
rt_hw_us_delay(2);
da=da<<1;
OLED_SCLK_Set();
rt_hw_us_delay(2);
OLED_SCLK_Clr();
rt_hw_us_delay(2);
}
}</code></pre>
<p>使OLED屏清屏的函数为:</p>
<pre>
<code class="language-cpp">void OLED_Clear(void)
{
uint8_t i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD);
OLED_WR_Byte (0x00,OLED_CMD);
OLED_WR_Byte (0x10,OLED_CMD);
for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
}
}</code></pre>
<p>使OLED屏显示字符的函数为:</p>
<pre>
<code class="language-cpp">void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
{
unsigned char c=0,i=0;
c=chr-' ';
if(x>Max_Column-1)
{
x=0;
y=y+2;
}
if(Char_Size ==16)
{
OLED_Set_Pos(x,y);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16,OLED_DATA);
OLED_Set_Pos(x,y+1);
for(i=0;i<8;i++)
OLED_WR_Byte(F8X16,OLED_DATA);
}
else
{
OLED_Set_Pos(x,y);
for(i=0;i<6;i++)
OLED_WR_Byte(F6x8<i>,OLED_DATA);
}
}</i></code></pre>
<p><i>使OLED屏显示字符串的函数为:</i></p>
<pre>
<i>
<code class="language-cpp">void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
unsigned char j=0;
while (chr!='\0')
{
OLED_ShowChar(x,y,chr,Char_Size);
x+=8;
if(x>120){x=0;y+=2;}
j++;
}
}</code></i></pre>
<p><i>实现图示效果的主程序为:</i></p>
<pre>
<i>
<code class="language-cpp">int main(void)
{
uint32_t cnt = 0;
uint32_t i,j;
pin = rt_pin_get("PE.1");
rt_pin_mode(pin, PIN_MODE_OUTPUT);
pin_scl = rt_pin_get("PE.3");
pin_sda = rt_pin_get("PE.2");
rt_pin_mode(pin_scl, PIN_MODE_OUTPUT);
rt_pin_mode(pin_sda, PIN_MODE_OUTPUT);
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"AB32VG1 RISC-V",16);
OLED_ShowString(0,2,"OLED DISPLAY",16);
while(1)
{
rt_pin_write(pin, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(pin, PIN_HIGH);
rt_thread_mdelay(500);
}
}</code></i></pre>
<p><i> </i></p>
<p>引脚连接与显示效果图</p>
<p>OLED老哥专属!</p>
w494143467 发表于 2021-9-6 11:08
OLED老哥专属!
<p>哈哈,因为后面的应用要用到。</p>
页:
[1]