[AB32VG1 development board review] OLED screen display driver
[Copy link]
This post was last edited by jinglixixi on 2021-9-3 23:58
OLED screen is a self-luminous display device. It has many types. According to the interface, there are I2C interface and SPI interface. According to the color, it can be divided into monochrome, dual-color and color screens. According to the display specifications, it can be divided into 0.91, 0.96 inches, etc.
In order to save I/O ports, the display screen used here is a 0.91-inch monochrome screen with an I2C interface.
The connection relationship between the display screen and the development board is:
SDA--PE2
SCL--PE3
The statement that makes the corresponding pin output high and low levels is defined as:
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)
The initialization function of the OLED screen is:
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
}
The function of simulating I2C byte data with I/O port is:
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);
}
}
The function to clear the OLED screen is:
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);
}
}
The function that makes the OLED screen display characters is:
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);
}
}
The function that makes the OLED screen display the string is:
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++;
}
}
The main program to achieve the graphic effect is:
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);
}
}
Pin connection and display effect diagram
|