(十三)基于Linux的IIC接口的oled开发

学习日志(十三)

基于Linux的IIC接口的oled开发

oled屏幕

(十三)基于Linux的IIC接口的oled开发_第1张图片

接线

由 26pin 的原理图可知,Orange Pi Zero 2 可用的 i2c 为 i2c3

(十三)基于Linux的IIC接口的oled开发_第2张图片

查看设备节点是否存在:

启动 linux 系统后, 先确认下/dev 下存在 i2c-3 的设备节点,从命令运行结果能观察到,系统支持I2C-3和I2C-5的驱动,而H616的外设我们看到只有一个IIC接口,用的是IIC-3。

如图:

(十三)基于Linux的IIC接口的oled开发_第3张图片

安装应用层能查看iic上挂载设备地址的工具

因为:Linux一切皆文件,每个硬件设备“对应”一个文件,由驱动程序提供映射

命令:

sudo apt-get install i2c-tools

安装提示:

(十三)基于Linux的IIC接口的oled开发_第4张图片

测试能查看iic上挂载设备地址的工具是否有效

命令:

sudo i2cdetect -y 3

(十三)基于Linux的IIC接口的oled开发_第5张图片

此时的3c就是挂载在iic上的oled屏幕的地址

先看官方给的测试代码:

官方库的例子位置:

在这里插入图片描述

代码:

  1 /*
  2  * Copyright (c) 2015, Vladimir Komendantskiy
  3  * MIT License
  4  *
  5  * SSD1306 demo of block and font drawing.
  6  */
  7
  8 //
  9 // fixed for OrangePiZero by HypHop
 10 //
 11
 12 #include <errno.h>
 13 #include <string.h>
 14 #include <stdio.h>
 15 #include <stdlib.h>
 16 #include <time.h>
 17 #include <stdint.h>
 18
 19 #include "oled.h"
 20 #include "font.h"
 21
 22 int oled_demo(struct display_info *disp) {
 23     int i;
 24     char buf[100];
 25
 26     //putstrto(disp, 0, 0, "Spnd spd  2468 rpm");
 27     //  oled_putstrto(disp, 0, 9+1, "Spnd cur  0.46 A");
 28     oled_putstrto(disp, 0, 9+1, "Welcome       to");
 29     disp->font = font1;
 30     //  oled_putstrto(disp, 0, 18+2, "Spnd tmp    53 C");
 31     oled_putstrto(disp, 0, 18+2, "----OrangePi----");
 32     disp->font = font2;
 33     //  oled_putstrto(disp, 0, 27+3, "DrvX tmp    64 C");
 34     oled_putstrto(disp, 0, 27+3, "This is 0.96OLED");
 35     oled_putstrto(disp, 0, 36+4, "");
 36     oled_putstrto(disp, 0, 45+5, "");
 37     disp->font = font1;
 38     //  oled_putstrto(disp, 0, 54, "Total cur  2.36 A");
 39     oled_putstrto(disp, 0, 54, "*****************");
 40     oled_send_buffer(disp);
 41
 42     disp->font = font3;
 43     for (i=0; i<100; i++) {
 44         sprintf(buf, "Spnd spd  %d rpm", i);
 45         oled_putstrto(disp, 0, 0, buf);
 46         oled_putstrto(disp, 135-i, 36+4, "===");
 47         oled_putstrto(disp, 100, 0+i/2, ".");
 48         oled_send_buffer(disp);
 49     }
 50     //oled_putpixel(disp, 60, 45);
 51     //oled_putstr(disp, 1, "hello");
 52
 53 return 0;
 54 }
 55
 56 void show_error(int err, int add) {
 57     //const gchar* errmsg;
 58     //errmsg = g_strerror(errno);
 59     printf("\nERROR: %i, %i\n\n", err, add);
 60     //printf("\nERROR\n");
 61 }
 62
 63 void show_usage(char *progname) {
 64     printf("\nUsage:\n%s \n", progname);
 65 }
 66
 67 int main(int argc, char **argv) {
 68     int e;
 69     char filename[32];
 70     struct display_info disp;
 71
 72     if (argc < 2) {
 73         show_usage(argv[0]);
 74
 75         return -1;
 76     }
 77
 78     memset(&disp, 0, sizeof(disp));
 79     sprintf(filename, "%s", argv[1]);
 80     disp.address = OLED_I2C_ADDR;
 81     disp.font = font2;
 82
 83     e = oled_open(&disp, filename);
 84
 85     if (e < 0) {
 86         show_error(1, e);
 87     } else {
 88         e = oled_init(&disp);
 89     if (e < 0) {
 90         show_error(2, e);
 91     } else {
 92         printf("---------start--------\n");
 93         if (oled_demo(&disp) < 0)
 94             show_error(3, 777);
 95             printf("----------end---------\n");
 96         }
 97     }
 98
 99     return 0;
100 }

额代码很长。。。。

大家可以借助SourceInsight工具查看带代码.

测试:

在这里插入图片描述

这里执行代码的时候要加上驱动 /dev/i2c-3

从代码中也能看出来为什么要加/dev/i2c-3:

(十三)基于Linux的IIC接口的oled开发_第6张图片

现象:

(十三)基于Linux的IIC接口的oled开发_第7张图片

基于官方库修改的代码:

功能描述:

显示font1,2,3字号的"Welcome to linux "

  1 #include <errno.h>
  2 #include <string.h>
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 #include <time.h>
  6 #include <stdint.h>
  7
  8 #include "oled.h"
  9 #include "font.h"
 10
 11
 12 int oled_show(struct display_info *disp) {
 13     int i;
 14     char buf[100];
 15
 16     disp->font = font1;
 17     oled_putstrto(disp, 0,1 , "Welcome to linux ");
 18
 19     disp->font = font2;
 20     oled_putstrto(disp, 0,10 , "Welcome to linux ");
 21
 22     disp->font = font3;
 23     oled_putstrto(disp, 0,20 , "Welcome to linux ");
 24
 25     oled_send_buffer(disp);
 26
 27     return 0;
 28 }
 29
 30 void show_error(int err, int add) {
 31     //const gchar* errmsg;
 32     //errmsg = g_strerror(errno);
 33     printf("\nERROR: %i, %i\n\n", err, add);
 34     //printf("\nERROR\n");
 35 }
 36
 37 void show_usage(char *progname) {
 38     printf("\nUsage:\n%s \n", progname);
 39 }
 40
 41 int main(int argc, char **argv) {
 42     int e;
 43     char filename[32];
 44     struct display_info disp;
 45
 46     if (argc < 2) {
 47         show_usage(argv[0]);
 48         return -1;
 49     }
 50
 51     memset(&disp, 0, sizeof(disp));
 52     sprintf(filename, "%s", argv[1]);
 53     disp.address = OLED_I2C_ADDR;
 54 //  disp.font = font3;
 55
 56     e = oled_open(&disp, filename);
 57     e = oled_init(&disp);
 58     oled_show(&disp);
 59     return 0;
 60 }

说明:

先设置好disp->font = font1;的字体。

再去调用: oled_putstrto(disp, 0,1 , "Welcome to linux ");字体才能生效。

如果不初始化disp->font = font1;的字体,直接不显示。

个人测试1号字体最大,3号最小(没去深究font对应字的大小)。

现象:

(十三)基于Linux的IIC接口的oled开发_第8张图片

你可能感兴趣的:(Orangepi,Zero2学习日志,linux,运维,服务器,c语言)