使用树莓派3B+的I2C协议调用wiringPiI2c驱动 0.96寸OLED(附完整可运行代码)
一、基本配置
先阅读以下博客较为详解,完成基本配置以及相关代码的说明
0.96寸OLED 使用树莓派的I2C协议调用wiringPiI2c驱动
二、代码
以上博客的代码中包括头文件、全局变量等没有写出来 ,故我对其进行了优化,以下代码可以直接在树莓派上运行,可对照着以上博客进行学习
代码实现的功能为清屏、全屏填充
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<assert.h>
#include<termios.h>
#include<string.h>
#include<sys/time.h>
#include<time.h>
#include<sys/types.h>
#include<errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <wiringPiI2C.h>
#include <unistd.h>unsigned char i2cHand;
unsigned char DataBuffer[7][127];void WriteCmd(int fd,unsigned char I2C_Command)//写命令
{
wiringPiI2CWriteReg8(fd,0x00, I2C_Command);
}void OLED_Init(int fd)
{
WriteCmd(fd,0xAE); //display offWriteCmd(fd, 0x20); //Set Memory Addressing Mode WriteCmd(fd, 0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,InvalidWriteCmd(fd, 0xb0); //Set Page Start Address for Page Addressing Mode,0-7WriteCmd(fd, 0xc8); //Set COM Output Scan DirectionWriteCmd(fd, 0x00); //---set low column addressWriteCmd(fd, 0x10); //---set high column addressWriteCmd(fd, 0x40); //--set start line addressWriteCmd(fd, 0x81); //--set contrast control registerWriteCmd(fd, 0xff); //亮度调节 0x00~0xffWriteCmd(fd, 0xa1); //--set segment re-map 0 to 127WriteCmd(fd, 0xa6); //--set normal displayWriteCmd(fd, 0xa8); //--set multiplex ratio(1 to 64)WriteCmd(fd, 0x3F); //WriteCmd(fd, 0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM contentWriteCmd(fd, 0xd3); //-set display offsetWriteCmd(fd, 0x00); //-not offsetWriteCmd(fd, 0xd5); //--set display clock divide ratio/oscillator frequencyWriteCmd(fd, 0xf0); //--set divide ratioWriteCmd(fd, 0xd9); //--set pre-charge periodWriteCmd(fd, 0x22); //WriteCmd(fd, 0xda); //--set com pins hardware configurationWriteCmd(fd, 0x12);WriteCmd(fd, 0xdb); //--set vcomhWriteCmd(fd, 0x20); //0x20,0.77xVccWriteCmd(fd, 0x8d); //--set DC-DC enableWriteCmd(fd, 0x14); //WriteCmd(fd, 0xaf); //--turn on oled panel
}void WriteData(int fd,unsigned char I2C_Data)//写数据
{
wiringPiI2CWriteReg8(fd,0x40, I2C_Data);
}void OLED_SetPos(int fd,unsigned char x, unsigned char y) //设置起始点坐标
{
WriteCmd(fd, (unsigned char)(0xb0 + x));WriteCmd(fd,((y & 0x0f) | 0x00));//LOWWriteCmd(fd,(((y & 0xf0) >> 4) | 0x10));//HIGHT
}/*写 缓存数据*/
void Write_DataBuffer()//这个是将DataBuffer数组里面的值,全部写进屏里去
{
unsigned char i, j;for (i = 0; i < 8; i++){
OLED_SetPos(i2cHand, i, 0); //设置起始点坐标for (j = 0; j < 128; j++){
WriteData(i2cHand, DataBuffer[i][j]);//写数据}}
}void OLED_Fill(void)//全屏填充
{
int i, j;for (i = 0; i < 8; i++){
for (j = 0; j < 128; j++){
DataBuffer[i][j] = 0xff;}}Write_DataBuffer();
}void OLED_CLS(void)//清屏
{
unsigned char i, j;for (i = 0; i < 8; i++){
for (j = 0; j < 128; j++){
DataBuffer[i][j] = 0x00;}}Write_DataBuffer();
}int main(void)
{
delay(1000); // 毫秒wiringPiSetupSys();i2cHand=wiringPiI2CSetup(0x3c); /*加载i2c设备*/OLED_Init(i2cHand);while (true){
OLED_Fill();delay(1000);OLED_CLS();delay(1000); // 毫秒}return 0;
}
二、树莓派上运行
输入以下两行命令
实物图结果如下