[MCU] 【AB32VG1開發板測評】OLED屏的顯示驅動

jinglixixi 樓主
2021-9-3 23:50
本帖最后由 jinglixixi 于 2021-9-3 23:58 編輯

OLED屏是一種自發光的顯示器件,它有多種類型,按接口分有I2C接口和SPI接口,按色彩分為單色、雙色及彩色屏,按顯示規格分為0.91、0.96寸等。

為了節省I/O口,這里所用的顯示屏為I2C接口的0.91寸的單色屏。

該顯示屏與開發板的連接關系為:

SDA-- PE2

SCL-- PE3

使相應引腳輸出高低電平的語句定義為:

uint8_t pin_scl;

uint8_t pin_sda;

#define OLED_SCLK_Clr()  rt_pin_write(pin_scl, PIN_LOW)

#define OLED_SCLK_Set()  rt_pin_write(pin_scl, PIN_HIGH)

#define OLED_SDIN_Clr()  rt_pin_write(pin_sda, PIN_LOW)

#define OLED_SDIN_Set()  rt_pin_write(pin_sda, PIN_HIGH)

OLED屏的初始化函數為:

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
}

以I/O口模擬I2C方式字節數據的函數為:

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);
        }
}

使OLED屏清屏的函數為:

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);
    }
}

使OLED屏顯示字符的函數為:

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[c*16+i],OLED_DATA);
            OLED_Set_Pos(x,y+1);
            for(i=0;i<8;i++)
            OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
      }
      else 
      {
            OLED_Set_Pos(x,y);
            for(i=0;i<6;i++)
                OLED_WR_Byte(F6x8[c],OLED_DATA);
      }
}

使OLED屏顯示字符串的函數為:


void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
    unsigned char j=0;
    while (chr[j]!='\0')
    {     
          OLED_ShowChar(x,y,chr[j],Char_Size);
          x+=8;
          if(x>120){x=0;y+=2;}
          j++;
    }
}

實現圖示效果的主程序為:


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);
    }
}

image-20210903234508-1.png  

引腳連接與顯示效果圖

回復評論 (2)

沙發 w494143467

2021-9-6 11:08

OLED老哥專屬!

板凳 jinglixixi

2021-9-6 15:00
引用: w494143467 發表于 2021-9-6 11:08 OLED老哥專屬!

哈哈,因為后面的應用要用到。

電子工程世界版權所有 京B2-20211791 京ICP備10001474號-1 京公網安備 11010802033920號
    我也要說兩句
    發送
    評論
    萝卜大香蕉