Orangepi配合IIC驱动OLED屏幕

目录

一、OLED屏幕

二、Orangepi的IIC接口及OLED屏幕硬件接线

2.1 Orangepi的IIC接口:

2.2 Orangepi与OLED屏幕硬件接线:

三、wiringPi库示例代码

3.1 wiringPi库OLED屏幕示例代码:

3.2 OLED显示自己想要的字符:


一、OLED屏幕

二、Orangepi的IIC接口及OLED屏幕硬件接线

2.1 Orangepi的IIC接口:

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

  • 启动Linux系统后先确认一下在/dev/目录下的IIC设备节点,全志H616用的是I2C-3设备节点,但是我们使用的是Linux5.16的内核系统,I2C-3默认是关闭的需要手动打开才能使用:

在/boot/orangepiEnv.txt中加入overlays=i2c3这个配置,然后重启Linux系统就能打开i2c-3
    
sudo vim /boot/orangepiEnv.txt

overlays=i2c3

从命令运行结果能观察到系统支持I2C-3、I2C-3和I2C-5的驱动,而H616的外设我们看到只有一个IIC接口,用的是IIC-3,Linux一切皆文件,每个硬件设备“对应”一个文件,由驱动程序提供映射

  • 开始测试I2C,首先安装i2c-tools

sudo apt-get install i2c-tools

  • 列出连接到I2C总线的设备的地址

sudo i2cdetect -y 3

2.2 Orangepi与OLED屏幕硬件接线:

三、wiringPi库示例代码

3.1 wiringPi库OLED屏幕示例代码:

/*
 * 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;
}
cp /home/orangepi/wiringOP/examples/oled_demo.c .	//拷贝oled_demo.c文件到waishe路径下
./build.sh oled_demo.c								//编译oled_demo.c
sudo ./a.out /dev/i2c-3								//运行程序

3.2 OLED显示自己想要的字符:

/*
 * Copyright (c) 2015, Vladimir Komendantskiy
 * MIT License
 *
 * SSD1306 demo of block and font drawing.
 */
#include <errno.h> 		// 包含错误处理相关的头文件
#include <string.h> 	// 包含字符串处理相关的头文件
#include <stdio.h> 		// 包含标准输入输出相关的头文件
#include <stdlib.h> 	// 包含标准库函数相关的头文件
#include <time.h> 		// 包含时间处理相关的头文件
#include <stdint.h> 	// 包含标准整数类型相关的头文件
#include "oled.h" 		// 包含OLED显示相关的头文件
#include "font.h" 		// 包含字体相关的头文件

// 在OLED显示屏上显示文本和图形
int oled_show(struct display_info *disp)
{
	int i;
	char buf[100];

	oled_putstrto(disp, 0, 0, "***  SYH LOVE RY  ***");						// 显示欢迎信息
	disp->font = font2;														// 设置字体为font2

	oled_putstrto(disp, 10, 20, "Welcome       to");						// 显示欢迎信息
	disp->font = font2;														// 设置字体为font2

	oled_putstrto(disp, 10, 30, "----OrangePi----");						// 显示欢迎信息
	disp->font = font2;														// 设置字体为font2

	oled_putstrto(disp, 0, 50, "-- Mr.shi handsome --");					// 显示欢迎信息
	disp->font = font2;														// 设置字体为font2

	oled_send_buffer(disp);													// 发送显示缓冲区到OLED显示屏
	return 0;
}

// 显示错误信息
void show_error(int err, int add)
{
 	printf("\nERROR: %i, %i\n\n", err, add);
}

// 显示程序使用方法
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;													// 设置OLED显示屏的I2C地址
	disp.font = font2;																// 设置字体为font2

	e = oled_open(&disp, filename);													// 打开OLED显示屏
	e = oled_init(&disp);															// 初始化OLED显示屏

	oled_show(&disp);																// 显示欢迎信息
	return 0;
}

下面是一个简单的示例,使用STM32HAL库的I2C驱动OLED屏幕的源代码。 ```c #include "stm32f1xx_hal.h" #define OLED_ADDR 0x78 // OLED屏幕I2C地址 I2C_HandleTypeDef hi2c1; // I2C句柄 // OLED屏幕初始化函数 void OLED_Init() { uint8_t init_data[] = { 0xAE, // 关闭OLED屏幕显示 0xD5, 0x80, // 设置时钟分频因子,震荡器频率 0xA8, 0x3F, // 设置行地址范围 0xD3, 0x00, // 设置显示偏移 0x40, // 设置起始行 0x8D, 0x14, // 启用电荷泵 0x20, 0x00, // 设置内存地址模式 0xA1, // 设置段重定向 0xC8, // 设置COM扫描方向 0xDA, 0x12, // 设置COM引脚硬件配置 0x81, 0xCF, // 设置对比度 0xD9, 0xF1, // 设置预充电周期 0xDB, 0x40, // 设置VCOMH电压倍率 0xA4, // 关闭输出所有点 0xA6, // 设置正常显示 0xAF, // 打开OLED屏幕显示 }; HAL_I2C_Master_Transmit(&hi2c1, OLED_ADDR, init_data, sizeof(init_data), 100); } // OLED屏幕写入数据函数 void OLED_Write_Data(uint8_t data) { uint8_t write_data[] = {0x40, data}; HAL_I2C_Master_Transmit(&hi2c1, OLED_ADDR, write_data, sizeof(write_data), 100); } // OLED屏幕写入命令函数 void OLED_Write_Command(uint8_t cmd) { uint8_t write_data[] = {0x00, cmd}; HAL_I2C_Master_Transmit(&hi2c1, OLED_ADDR, write_data, sizeof(write_data), 100); } // OLED屏幕清屏函数 void OLED_Clear() { for (uint8_t i = 0; i < 8; i++) { OLED_Write_Command(0xB0 + i); // 设置页地址 OLED_Write_Command(0x00); // 设置列地址低位 OLED_Write_Command(0x10); // 设置列地址高位 for (uint8_t j = 0; j < 128; j++) { OLED_Write_Data(0x00); // 写入数据 } } } // OLED屏幕显示字符函数 void OLED_Show_Char(uint8_t x, uint8_t y, uint8_t ch) { uint8_t i, j; ch -= 32; OLED_Write_Command(0xB0 + y); // 设置页地址 OLED_Write_Command(((x & 0xF0) >> 4) | 0x10); // 设置列地址高位 OLED_Write_Command(x & 0x0F); // 设置列地址低位 for (i = 0; i < 6; i++) { OLED_Write_Data(font6x8[ch][i]); } } // 主函数 int main(void) { HAL_Init(); // 初始化HAL库 __HAL_RCC_GPIOB_CLK_ENABLE(); // 使能GPIOB时钟 __HAL_RCC_I2C1_CLK_ENABLE(); // 使能I2C1时钟 GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; // 设置PB6和PB7管脚 GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; // 设置开漏输出模式 GPIO_InitStruct.Pull = GPIO_PULLUP; // 设置上拉电阻 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; // 设置GPIO高速模式 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); hi2c1.Instance = I2C1; // 设置I2C句柄 hi2c1.Init.ClockSpeed = 400000; // 设置I2C时钟速度 hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; // 设置I2C时钟占空比 hi2c1.Init.OwnAddress1 = 0; // 设置I2C自身地址 hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; // 设置I2C地址模式 hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; // 禁用I2C双地址模式 hi2c1.Init.OwnAddress2 = 0; // 设置I2C自身地址2 hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; // 禁用I2C广播模式 hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; // 禁用I2C时钟拉伸 HAL_I2C_Init(&hi2c1); OLED_Init(); // 初始化OLED屏幕 OLED_Clear(); // 清屏 OLED_Show_Char(0, 0, 'H'); // 显示字符'H' OLED_Show_Char(8, 0, 'e'); // 显示字符'e' OLED_Show_Char(16, 0, 'l'); // 显示字符'l' OLED_Show_Char(24, 0, 'l'); // 显示字符'l' OLED_Show_Char(32, 0, 'o'); // 显示字符'o' while (1) {} } ``` 上面的代码使用了6x8的字体库,需要在程序中定义。你可以在网上找到一些免费的字体库,或者自己手动制作一个。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值