首页 > 代码库 > Zedboard OLED 小演示
Zedboard OLED 小演示
玩Zedboard上自带的OLED,网上一位博主对这颗OLED做了比较底层的分析和开发。http://blog.chinaaet.com/detail/30962 。程序和硬件构思基本移植了在单片机开发上的经验。大致的想法是用PL部分搭建OLED控制和通讯的SPI协议,再通过PS控制发送OLED需要显示的信息。有兴趣可以展开看一下。不过Diligent为OLED已经制作好了驱动,正如我们在用SD卡启动时看到的Diligent的logo。我们可以基于此做一些简单的演示开发。这里借助img2lcd工具,可以轻松实现一些DIY的图型显示。
简单的原理说明,OLED与Zync芯片之间通过SPI接口连接,在此我们先忽略之间的驱动细节。 Diligent为我们做好了驱动程序,并一个驱动映射为/dev/zed_oled。我们只需将数据压入这个文件,就可以在OLED上显示需要的图像。在用ramdisk方式启动zedboard时,可以找到 load_oled的脚本程序。打开可以看到里面的内容是这样的。
#!/bin/shinsmod /lib/modules/`uname -r`/pmodoled-gpio.ko;if [ -c /dev/zed_oled ]; then cat /root/logo.bin > /dev/zed_oled;fi;
据此可以理解这里进行了两步操作,insmod命令安装了oled的驱动pmodoled-gpio.ko,紧接着用cat xx.bin > /dev/zed_oled 写入图像的二进制数据,完成显示。
于是我们可以用 img2lcd 工具把需要显示的图像转化成.bin文件,然后放入SD卡cat一下就OK了。设置如下图,选择输出数据类型*.bin,扫描模式:数据水平-位元垂直,宽高为128*32,勾选位元组内像素倒序+自底至顶扫描。可以简单玩一下各种组合,看一下显示有什么变化,也可以帮助理解这颗OLED是怎么工作的。
保存之后就可以得到我们需要的*.bin文件了,这里存成oed1.bin。copy至SD卡,然后运行命令。
cat /root/oled1.bin > /dev/zed_oled;
静态的显示就这么轻松的搞定了,是不是可以做一些炫酷的动态演示?当然可以。
一个简单的例子,按照上面的流程,在img2lcd工具上保持其他的设置不变,勾选颜色反转。保存成oled2.bin。copy到SD卡。创建如下的一个shell脚本(注意修改*.bin文件的所在目录)。运行一下看一下效果吧。
#!/bin/bashwhile truedo cat /root/oled1.bin > /dev/zed_oled sleep 1 cat /root/oled2.bin > /dev/zed_oled sleep 1done
除了用shell,我们也可以用其他的程序来搞定OLED的显示。以下以C为例。
还是回到img2lcd里,选择输出数据类型C语言数组*.c。保存之后会弹出文本文档,显示一个16进制的数组。这个数组就是图像转化成的像素信息了。根据这个数组开始编写C程序。
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <fcntl.h>#include <unistd.h>unsigned char dis_buf[128*4]={0};const unsigned char gImage_oled1[512] = { /* 0X62,0X01,0X80,0X00,0X20,0X00, */0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X38,0X78,0X78,0XF8,0XB8,0X38,0X38,0X38,0X38,0X38,0X00,0X00,0XF0,0XF0,0XF0,0XB0,0XB0,0XB0,0X60,0X00,0X00,0XF0,0XF0,0X30,0X30,0X30,0X30,0XF0,0XE0,0X00,0X30,0XF0,0XB0,0X30,0X30,0X30,0X60,0XE0,0X80,0X00,0XF0,0XF8,0X38,0X30,0X30,0X60,0XC0,0X80,0X70,0XF0,0XF8,0X38,0X30,0X30,0XF8,0XF0,0X00,0X78,0XF0,0XC0,0X00,0X00,0X00,0X00,0X00,0XE0,0XF0,0X30,0X30,0X30,0X30,0XF0,0XF0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X70,0XF8,0XB8,0X18,0X18,0X38,0X30,0X70,0XE0,0XC0,0X00,0XE0,0XF0,0XF0,0XB0,0XB0,0XB0,0X30,0X60,0X00,0X78,0XF0,0X80,0X00,0X18,0XF8,0XE0,0X00,0X00,0XF8,0XF0,0X80,0X60,0XF0,0XF8,0X38,0X30,0X30,0XE0,0XC0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X40,0XE1,0XE3,0XE6,0XEC,0XF8,0XF8,0XF0,0XE0,0XE1,0X43,0X07,0X0E,0X0D,0X0D,0X0F,0X0E,0X04,0X01,0X03,0X06,0X0E,0X0C,0X0C,0X0F,0X0F,0X3E,0XF8,0XC1,0X0F,0X7E,0XEC,0X8C,0X0C,0X0F,0X07,0X00,0X01,0X03,0X07,0X0E,0X0C,0X0C,0X0F,0X07,0X00,0X01,0X03,0X06,0X0E,0X0C,0X0D,0X0F,0X0E,0X00,0X03,0X0F,0X0E,0X06,0X0C,0X0F,0X0F,0X0C,0X03,0X07,0X06,0X0C,0X0C,0X0C,0X0F,0X1F,0XF8,0XE0,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X03,0X1F,0X7C,0XE0,0XE0,0X70,0X70,0X38,0X1F,0X0F,0X00,0X03,0X07,0X06,0X0D,0X0D,0X0F,0X0F,0X06,0X00,0X03,0X0F,0X1F,0X06,0X0D,0X0F,0X0E,0X0E,0X0C,0X0F,0X0F,0X00,0X01,0X03,0X06,0X0C,0X0C,0X0E,0X0F,0X07,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,};int main(){ int fd = open("/dev/zed_oled",O_RDWR); int i,j,m; if( fd == -1){ printf("Can‘t open zed_oled device!\n"); return 0; } else{ printf("/dev/zed_oled open success!\n"); } for(i=0; i<512 ; i++){ dis_buf[i]=gImage_oled1[i]; }while (1) { unsigned char temp[4] = {0}; temp[3] = dis_buf[384]; temp[2] = dis_buf[256]; temp[1] = dis_buf[128]; temp[0] = dis_buf[0]; for(j=0;j<4;j++){ for (m=0;m<127;m++){ dis_buf[128*j+m]=dis_buf[128*j+m+1]; } } dis_buf[511] = temp[3]; dis_buf[383] = temp[2]; dis_buf[255] = temp[1]; dis_buf[127] = temp[0]; sleep(1); write(fd,dis_buf,128*4); } return 0;}
copy到SD卡上,在boot之后再编译运行一下,看到什么了?哈哈,搞定!
一个问题,为什么这里的数组是128*4=512呢?可以留作思考吧,哈。
这个C程序的移位算法不够优化只是做简单的演示,更多数组移位的优化算法可以参考此链接 http://blog.csdn.net/hackbuteer1/article/details/6699837