Orangepi Zero2——OLED屏幕应用/IIC协议

Orangepi的IIC接口

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

在这里插入图片描述

开发步骤

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

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

  1. 开始测试 i2c, 首先安装 i2c-tools
sudo apt-get install i2c-tools
  1. i2c地址
sudo i2cdetect -y 3

在这里插入图片描述

wiringPi库示例代码

以下是examples/oled_demo.c

/*
 * Copyright (c) 2015, Vladimir Komendantskiy
 * MIT License
 *
 * SSD1306 demo of block and font drawing.
 */
 
//
// fixed for OrangePiZero by HypHop
//
 
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
 
#include "oled.h"
#include "font.h"
 
int oled_demo(struct display_info *disp) {
	int i;
	char buf[100];
 
	//putstrto(disp, 0, 0, "Spnd spd  2468 rpm");
	//	oled_putstrto(disp, 0, 9+1, "Spnd cur  0.46 A");
	oled_putstrto(disp, 0, 9+1, "Welcome       to");
	disp->font = font1;
	//	oled_putstrto(disp, 0, 18+2, "Spnd tmp    53 C");
	oled_putstrto(disp, 0, 18+2, "----OrangePi----");
	disp->font = font2;
	//	oled_putstrto(disp, 0, 27+3, "DrvX tmp    64 C");
	oled_putstrto(disp, 0, 27+3, "This is 0.96OLED");
	oled_putstrto(disp, 0, 36+4, "");
	oled_putstrto(disp, 0, 45+5, "");
	disp->font = font1;
	//	oled_putstrto(disp, 0, 54, "Total cur  2.36 A");
	oled_putstrto(disp, 0, 54, "*****************");
	oled_send_buffer(disp);//将缓存中的数据写到设备上
 
	disp->font = font3;
	for (i=0; i<100; i++) {
		sprintf(buf, "Spnd spd  %d rpm", i);
		oled_putstrto(disp, 0, 0, buf);
		oled_putstrto(disp, 135-i, 36+4, "===");
		oled_putstrto(disp, 100, 0+i/2, ".");
		oled_send_buffer(disp);
	}
	//oled_putpixel(disp, 60, 45);
	//oled_putstr(disp, 1, "hello");
 
return 0;
}
 
void show_error(int err, int add) {
	//const gchar* errmsg;
	//errmsg = g_strerror(errno);
	printf("\nERROR: %i, %i\n\n", err, add);
	//printf("\nERROR\n");
}
 
void show_usage(char *progname) {
	printf("\nUsage:\n%s <I2C bus device node >\n", progname);
}
 
int main(int argc, char **argv) {
	int e;
	char filename[32];
	struct display_info disp;//该结构体管理显示的所有内容的格式和地址
 
	if (argc < 2) {
		show_usage(argv[0]);
		
		return -1;
	}
 
	memset(&disp, 0, sizeof(disp));
	sprintf(filename, "%s", argv[1]);
	disp.address = OLED_I2C_ADDR;
	disp.font = font2;
 
	e = oled_open(&disp, filename);//访问硬件设备驱动,底层是执行文件操作,目的是为了识别外部的硬件设备
 
	if (e < 0) {
		show_error(1, e);
	} else {
		e = oled_init(&disp);
	if (e < 0) {
		show_error(2, e);
	} else {
		printf("---------start--------\n");
		if (oled_demo(&disp) < 0) 
			show_error(3, 777);
			printf("----------end---------\n");
		}
	}
 
	return 0;
}

运行:

sudo ./a.out /dev/i2c-3
//dev设备底下的i2c-3设备驱动
//访问外设需要打开设备驱动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值