【安信可蓝牙开发板PB-02-Kit测评】OLED 屏的显示驱动
<div class='showpostmsg'><p style="text-align:justify">尽管安信可蓝牙开发板PB-02-Kit是面向蓝牙应用而设计的,但也不妨碍我们为它配置更多的外设以丰富功能,例如为它配上一个OLED屏来显示信息。</p><p>为了节省引脚资源,这里使用的是I2C接口的OLED屏,该显示屏与开发板的连接关系为:</p>
<p>SCL—P1</p>
<p>SDA—P2</p>
<p>为此,对所用引脚的配置语句为:</p>
<p>hal_gpio_pin_init(P1, OEN);</p>
<p>hal_gpio_write(P1, 1);</p>
<p>hal_gpio_pin_init(P2, OEN);</p>
<p>hal_gpio_write(P2, 1);</p>
<p>为输出高低电平,所定义的语句为:</p>
<p>#define OLED_SCLK_Clr() hal_gpio_write(P1, 0);</p>
<p>#define OLED_SCLK_Set() hal_gpio_write(P1, 1)</p>
<p>#define OLED_SDIN_Clr() hal_gpio_write(P2, 0);</p>
<p>#define OLED_SDIN_Set() hal_gpio_write(P2, 1);</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>为了测试OLED屏的可驱动性,所设计的测试函数为:</p>
<pre>
<code class="language-cpp">void OLED_Clearb(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(0xf0,OLED_DATA);
}
}</code></pre>
<p>在下载程序后,其测试效果如图1所示。</p>
<p> </p>
<p>图1测试效果</p>
<p>在完成了驱动测试后,通过添加字符显示及字符串显示函数即可实现字符显示功能。</p>
<p>实现字符串显示的函数为:</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>120)
{
x=0;
y+=2;
}
j++;
}
}</code></pre>
<p>实现图2所示显示效果的主程序为:</p>
<pre>
<code class="language-cpp">int app_main(void)
{
/* Initialize the operating system */
osal_init_system();
osal_pwrmgr_device( PWRMGR_BATTERY );
hal_gpio_pin_init(P1, OEN);
hal_gpio_write(P1, 1);
hal_gpio_pin_init(P2, OEN);
hal_gpio_write(P2, 1);
hal_gpio_pin_init(P31, IE);
OLED_Init();
OLED_Clearb();
while(hal_gpio_read(P31));
OLED_Clear();
OLED_ShowString(20,0,"PB-02-kit",16);
OLED_ShowString(20,2,"jinglixixi",16);
while(1)
{
delay(1000);
hal_gpio_write(P34, 1);
hal_gpio_write(P0, 1);
delay(1000);
hal_gpio_write(P34, 0);
hal_gpio_write(P0, 0);
}
/* Start OSAL */
osal_start_system(); // No Return from here
return 0;
}</code></pre>
<p>经程序下载,其显示效果如图2所示。</p>
<p> </p>
<p>图2字符显示效果</p>
<p>有了OLED屏的显示功能,后面再有信息其显示的任务就可以交它完成了!</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>用I2C接口配备一个OLED屏,也节省了资源</p>
<p>IIC的oled,很适合消费类的产品。瞬间高大上。</p>
machinnneee 发表于 2021-12-1 09:34
IIC的oled,很适合消费类的产品。瞬间高大上。
<p><img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/handshake.gif" width="48" /></p>
Jacktang 发表于 2021-12-1 07:22
用I2C接口配备一个OLED屏,也节省了资源
<p>两者的确很搭!</p>
页:
[1]