jennyzhaojie 发表于 2021-2-2 11:22

【RISC-V MCU CH32V103测评】驱动OLED屏显示

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">OLED<span style="font-family:宋体">屏是一种小巧的显示器件,其面对</span>LCD1602<span style="font-family:宋体">等液晶显示屏也毫不逊色,显示的内容也并不少。此外,它按接口方式可分为</span>I2C<span style="font-family:宋体">接口和</span>SPI<span style="font-family:宋体">接口等,所占用的引脚也不多于</span>LCD1602<span style="font-family:宋体">等。</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">这里选用的是</span>I2C<span style="font-family:宋体">接口的</span>OLED<span style="font-family:宋体">屏,它最有特点,用</span>2<span style="font-family:宋体">个</span>I/O<span style="font-family:宋体">口就能完成显示任务。</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">在掌握</span>GPIO<span style="font-family:宋体">口使用的基础上,就可十分轻松地实现驱动</span>OLED<span style="font-family:宋体">屏显示的目标。</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">这里的</span>OLED<span style="font-family:宋体">屏与</span>MCU<span style="font-family:宋体">的连接关系为:</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">SCL&nbsp;---PA2</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">SDA--- PA3</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">驱动</span>OLED<span style="font-family:宋体">屏所定义的输出高低电平的语句定义为:</span></span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define&nbsp;SCL_high GPIO_WriteBit(GPIOA, GPIO_Pin_2, Bit_SET)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define&nbsp;SCL_low&nbsp;&nbsp;GPIO_WriteBit(GPIOA, GPIO_Pin_2, Bit_RESET)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define&nbsp;SDA_high GPIO_WriteBit(GPIOA, GPIO_Pin_3, Bit_SET)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">#define&nbsp;SDA_low&nbsp;&nbsp;GPIO_WriteBit(GPIOA, GPIO_Pin_3, Bit_RESET)</span></span></p>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">配置</span>2<span style="font-family:宋体">个引脚为输出功能的函数为:</span></span></span></p>

<pre>
<code class="language-cpp">void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_Init(GPIOA,&amp;GPIO_InitStructure); 
GPIO_SetBits(GPIOA,GPIO_Pin_2|GPIO_Pin_3);
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">辅助驱动的相关函数为:</span></span></span></p>

<pre>
<code class="language-cpp">void IIC_Start()
{
   SCL_high;
   SDA_high;
   SDA_low;
   SCL_low;
}

void IIC_Stop()
{
 SCL_low;
   SDA_low;
   SCL_high;
   SDA_high;
}

void IIC_Wait_Ack()
{
    SCL_high;
    SCL_low;
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif">OLED<span style="font-family:宋体">屏的初始化函数为:</span></span></span></p>

<pre>
<code class="language-cpp">void OLED_Init(void)
{
    SCL_high;
    SDA_high;
    Delay_Ms(800);
    OLED_WR_Byte(0xAE,OLED_CMD);//--display off
    OLED_WR_Byte(0x40,OLED_CMD);//--set start line address
    OLED_WR_Byte(0xB0,OLED_CMD);//--set page address
    OLED_WR_Byte(0xC8,OLED_CMD);//Com scan direction
    OLED_WR_Byte(0x81,OLED_CMD); // contract control
    OLED_WR_Byte(0xFF,OLED_CMD);//--128
    OLED_WR_Byte(0xA1,OLED_CMD);//set segment remap
    OLED_WR_Byte(0xA6,OLED_CMD);//--normal / reverse
    OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
    OLED_WR_Byte(0x1F,OLED_CMD);//--1/32 duty
    OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset
    OLED_WR_Byte(0x00,OLED_CMD);//
    OLED_WR_Byte(0xD5,OLED_CMD);//set osc division
    OLED_WR_Byte(0xf0,OLED_CMD);//
    OLED_WR_Byte(0xD9,OLED_CMD);//Set Pre-Charge Period
    OLED_WR_Byte(0x22,OLED_CMD);//
    OLED_WR_Byte(0xDA,OLED_CMD);//set com pin configuartion
    OLED_WR_Byte(0x02,OLED_CMD);//
    OLED_WR_Byte(0xDB,OLED_CMD);//set Vcomh
    OLED_WR_Byte(0x49,OLED_CMD);//
    OLED_WR_Byte(0x8D,OLED_CMD);//set charge pump enable
    OLED_WR_Byte(0x14,OLED_CMD);//
    OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
    OLED_Clear();
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">显示字符串的函数为:</span></span></span></p>

<pre>
<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&gt;120){x=0;y+=2;}
           j++;
    }
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">实现显示功能的主程序为:</span></span></span></p>

<pre>
<code class="language-cpp">int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    Delay_Init();
    OLED_IO_Init();
    OLED_Init();
    OLED_Clear();
    OLED_ShowString(0,0,"CH32V103",16);
    OLED_ShowString(0,2,"OLED TEST",16);
    while(1) ;
}</code></pre>

<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:Calibri,sans-serif"><span style="font-family:宋体">经编译下载,其运行结果如下图所示。</span></span></span></p>

<p style="text-align:justify"></p>

<p>&nbsp;<span style="font-size:10.5pt"><span style="font-family:宋体">运行效果图</span></span></p>

freebsder 发表于 2021-2-2 15:56

<p>谢谢分享!显示屏的繁琐操作令人厌烦。</p>

w494143467 发表于 2021-2-2 16:51

<p>看这个屏幕有点像华大开发板上的屏幕哈~感谢分享!!!</p>

jennyzhaojie 发表于 2021-2-2 20:45

w494143467 发表于 2021-2-2 16:51
看这个屏幕有点像华大开发板上的屏幕哈~感谢分享!!!

<p><img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/handshake.gif" width="48" /></p>

jennyzhaojie 发表于 2021-2-2 20:48

freebsder 发表于 2021-2-2 15:56
谢谢分享!显示屏的繁琐操作令人厌烦。

<p>哈哈,在单片机和ARM上使用显示屏还算方便些,若换在FPGA上那就有些头大了,真称得上麻烦的。</p>

别吵我睡觉 发表于 2021-7-10 16:13

编译出现OLED_CMD报错该怎么解决

littleshrimp 发表于 2021-7-16 06:25

<p>&nbsp;</p>

<p> &nbsp;</p>
页: [1]
查看完整版本: 【RISC-V MCU CH32V103测评】驱动OLED屏显示