jinglixixi 发表于 2021-9-3 23:50

【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()&nbsp; rt_pin_write(pin_scl, PIN_LOW)</p>

<p>#define OLED_SCLK_Set()&nbsp; rt_pin_write(pin_scl, PIN_HIGH)</p>

<p>#define OLED_SDIN_Clr()&nbsp; rt_pin_write(pin_sda, PIN_LOW)</p>

<p>#define OLED_SDIN_Set()&nbsp; 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&lt;8;i++)
    {
        m=da;
        m=m&amp;0x80;
        if(m==0x80)
        {
          OLED_SDIN_Set();
      }
        else OLED_SDIN_Clr();
        rt_hw_us_delay(2);
            da=da&lt;&lt;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&lt;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&lt;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)
{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned char c=0,i=0;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c=chr-&#39; &#39;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(x&gt;Max_Column-1)
      {
       x=0;
       y=y+2;
      }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(Char_Size ==16)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OLED_Set_Pos(x,y);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(i=0;i&lt;8;i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OLED_WR_Byte(F8X16,OLED_DATA);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OLED_Set_Pos(x,y+1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(i=0;i&lt;8;i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OLED_WR_Byte(F8X16,OLED_DATA);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else
      {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OLED_Set_Pos(x,y);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(i=0;i&lt;6;i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OLED_WR_Byte(F6x8<i>,OLED_DATA);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
}</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)
{
&nbsp;&nbsp;&nbsp; unsigned char j=0;
&nbsp;&nbsp;&nbsp; while (chr!=&#39;\0&#39;)
&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;
          OLED_ShowChar(x,y,chr,Char_Size);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x+=8;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(x&gt;120){x=0;y+=2;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j++;
&nbsp;&nbsp;&nbsp; }
}</code></i></pre>

<p><i>实现图示效果的主程序为:</i></p>

<pre>
<i>
<code class="language-cpp">int main(void)
{
&nbsp;&nbsp;&nbsp; uint32_t cnt = 0;
&nbsp;&nbsp;&nbsp; uint32_t i,j;
&nbsp;&nbsp;&nbsp; pin = rt_pin_get(&quot;PE.1&quot;);
&nbsp;&nbsp;&nbsp; rt_pin_mode(pin, PIN_MODE_OUTPUT);
&nbsp;&nbsp;&nbsp; pin_scl = rt_pin_get(&quot;PE.3&quot;);
&nbsp;&nbsp;&nbsp; pin_sda = rt_pin_get(&quot;PE.2&quot;);
&nbsp;&nbsp;&nbsp; rt_pin_mode(pin_scl, PIN_MODE_OUTPUT);
&nbsp;&nbsp;&nbsp; rt_pin_mode(pin_sda, PIN_MODE_OUTPUT);
&nbsp;&nbsp;&nbsp; OLED_Init();
&nbsp;&nbsp;&nbsp; OLED_Clear();
&nbsp;&nbsp;&nbsp; OLED_ShowString(0,0,&quot;AB32VG1 RISC-V&quot;,16);
&nbsp;&nbsp;&nbsp; OLED_ShowString(0,2,&quot;OLED DISPLAY&quot;,16);
&nbsp;&nbsp;&nbsp; while(1)
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rt_pin_write(pin, PIN_LOW);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rt_thread_mdelay(500);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rt_pin_write(pin, PIN_HIGH);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rt_thread_mdelay(500);
&nbsp;&nbsp;&nbsp; }
}</code></i></pre>

<p><i> &nbsp;</i></p>

<p>引脚连接与显示效果图</p>

w494143467 发表于 2021-9-6 11:08

<p>OLED老哥专属!</p>

jinglixixi 发表于 2021-9-6 15:00

w494143467 发表于 2021-9-6 11:08
OLED老哥专属!

<p>哈哈,因为后面的应用要用到。</p>
页: [1]
查看完整版本: 【AB32VG1开发板测评】OLED屏的显示驱动