全志H616:OLED屏应用 — IIC协议

目录

● OLED屏外观

● OrangePi的IIC接口

● 查看设备是否支持IIC节点

● 查看设备地址是否挂载到IIC上的工具

①安装i2c-tools

②测试工具是否有效

● 查看关于OLED屏的官方原码

● 基于OLED源码修改成的测试代码


● OLED屏外观

● OrangePiIIC接口

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

● 查看设备是否支持IIC节点

    ● 启动 linux 系统后, 在命令行中输入:ls /dev 查看H616是否存在 i2c-3 的设备节点;

    ● 从命令运行结果能观察到系统支持I2C-3I2C-5的驱动,而H616的外设我们看到只有一个IIC接口,用的是IIC-3;

● 查看设备地址是否挂载到IIC上的工具

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

①安装i2c-tools

  安装 i2c-tools 命令: sudo apt-get install i2c-tools

②测试工具是否有效

  输入命令:sudo i2cdetect -y 3

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

● 查看关于OLED屏的官方原码

——> wiringOP ——> examples ——> oled_demo.c

 源码展示:

 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 <I2C bus device node >\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 }

● 基于OLED源码修改成的测试代码

  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 int oled_show(struct display_info *disp) {
 12     int i;
 13     char buf[100];
 14
 15     oled_putstrto(disp, 0, 13, "Welcome to My Linux");
 16     disp->font = font1;
 17
 18     oled_putstrto(disp, 1, 27, "-This is OLED screen-");
 19     disp->font = font1;
 20
 21     oled_putstrto(disp, 63, 50, "2023.2.19");
 22     disp->font = font1;
 23     oled_send_buffer(disp);
 24
 25     return 0;
 26 }
 27
 28 void show_error(int err, int add) {
 29     printf("\nERROR: %i, %i\n\n", err, add);
 30     //printf("\nERROR\n");
 31 }
 32
 33 void show_usage(char *progname) {
 34     printf("\nUsage:\n%s <I2C bus device node >\n", progname);
 35 }
 36
 37 int main(int argc, char **argv) {
 38     int e;
 39     char filename[32];
 40     struct display_info disp;
 41
 42     if (argc < 2) {
 43         show_usage(argv[0]);
 44
 45         return -1;
 46     }
 47
 48     memset(&disp, 0, sizeof(disp));
 49     sprintf(filename, "%s", argv[1]);
 50     disp.address = OLED_I2C_ADDR;
 51     disp.font = font2;
 52
 53     e = oled_open(&disp, filename);
 54     e = oled_init(&disp);
 55
 56     oled_show(&disp);
 57
 58     return 0;
 59 }
 60

 编译过程注意的地方如图

 oled屏上显示内容:

猜你喜欢

转载自blog.csdn.net/m0_74985965/article/details/129113934
今日推荐