树梅派硬件编程_QT_OLED显示字符

Posted Leslie X徐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树梅派硬件编程_QT_OLED显示字符相关的知识,希望对你有一定的参考价值。

OLED显示字符

树梅派I2C使用

安装i2c tools

sudo apt-get install i2c-tools

检查i2c设备

pi@raspberrypi:~ $ sudo i2cdetect -l
i2c-1	i2c       	bcm2835 (i2c@7e804000)          	I2C adapter
pi@raspberrypi:~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         
pi@raspberrypi:~ $ 

0x3c 则为该设备地址

SSD1306OLed的使用

  1. SSD1306支持多种控制方式,I2C、6800、8080、4线SPI、3线SPI,通过BS0、BS1、BS2引脚接不同的电平来选择控制方式. 使用I2C,则BS0接GND、BS1接VCC、BS2接GND

  2. 根据0.96寸OLED使用说明可知控制信号是后五位,对于I2C协议来说后两位SA0指的的此设备的设备号。可理解为0x00寄存器是对0LED屏幕进行控制命令写入,0x40寄存器是对OLED屏幕数据写入。

  3. OLED 0.96寸为 64行,128列 ,从上至下一共8页, 每页上面4位低位,下面4位高位.
    传输时,一个一个字节传, 传入一个字节就是画一个列.
    传输时,先要定位在哪一行或页的哪一列开始画,画完一列会自动开始下一列绘制.

在这里插入图片描述
在这里插入图片描述

字模

字符大小为6列,每列8位(一个字节)
例如:

 //'0'-48, 'A'-(65-10)55 'a'-(97-36)61
static unsigned char F68[][6] =
{
{0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E},// 0
{0x00, 0x00, 0x42, 0x7F, 0x40, 0x00},// 1
{0x00, 0x42, 0x61, 0x51, 0x49, 0x46},// 2
{0x00, 0x21, 0x41, 0x45, 0x4B, 0x31},// 3
{0x00, 0x18, 0x14, 0x12, 0x7F, 0x10},// 4
{0x00, 0x27, 0x45, 0x45, 0x45, 0x39},// 5
{0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30},// 6
{0x00, 0x01, 0x71, 0x09, 0x05, 0x03},// 7
{0x00, 0x36, 0x49, 0x49, 0x49, 0x36},// 8
{0x00, 0x06, 0x49, 0x49, 0x29, 0x1E},// 9
};

命令写入和数据写入

//写命令
void WriteCmd(int fd,unsigned char I2C_Command)
{
	wiringPiI2CWriteReg8(fd,0x00, I2C_Command);
	//往0x00寄存器写入数据,数据为命令
}

void WriteData(int fd, unsigned char I2C_Data)//写数据
{
	wiringPiI2CWriteReg8(fd, 0x40, I2C_Data);
	//往0x40寄存器写入数据,数据为图像信息
}

定位开始写入地址

void OLED_SetPos(int fd, unsigned char x, unsigned char y) //设置起始点坐标
{
	//WriteCmd(fd, (unsigned char)(0x40 + x));  /*设置开始行*/
	
	
	WriteCmd(fd, (unsigned char)(0xb0 + y));  /*设置开始页*/
	WriteCmd(fd, ((x & 0x0f) | 0x00));//LOW
	WriteCmd(fd, (((x & 0xf0) >> 4) | 0x10));//HIGHT
}

写入字符

void OLED_Char(int fd, unsigned char x, unsigned char y, char curChar)
{
    //OLED_SetPos(fd,x%128,y+x/128);
    OLED_SetPos(fd,x,y);

    if(curChar>='0'&&curChar<='9')
    {
        for (int i=0;i<6;++i) {
            WriteData(fd,F68[curChar-48][i]);
        }
    }
    else{
        if(curChar>='A'&&curChar<='Z')
        {
            for (int i=0;i<6;++i) {
                WriteData(fd,F68[curChar-55][i]);
            }
        }
        else{
            if(curChar>='a'&&curChar<='z')
            {
                for (int i=0;i<6;++i) {
                    WriteData(fd,F68[curChar-61][i]);
                }
            }else {
                if(curChar == 32){//空格
                    for (int i=0;i<6;++i) {
                        WriteData(fd,F68[62][i]);
                    }
                }
            }
        }
    }
}

字符串

void MainWindow::on_btnshow_clicked()
{
    OLED_DisplayOn(i2cfd);
    QString text = ui->lineEdit->text();
    for (int i=0;i<text.size();++i) {
      OLED_Char(i2cfd,i*6,1,text[i].cell());
    }

}

清屏

void OLED_CLR(int fd)//清屏
{
 OLED_SetPos(fd,0,0);
 for (int i=0;i<8;++i) {
     for (int j=0;j<128;++j) {
         WriteData(fd,0x00);
     }
 }

显示和关闭

 void OLED_DisplayOff(int fd)
 {
	WriteCmd(fd,0x8D); 
	WriteCmd(fd,0x10); 
    WriteCmd(fd,0xAE);
 }
 
  void OLED_DisplayOn(int fd)
 {
	WriteCmd(fd,0x8D); 
	WriteCmd(fd,0x14); 
	WriteCmd(fd,0xAF); 
 }
 

所有代码

界面:
在这里插入图片描述

OLED头文件

#ifndef __OLED_H__
#define __OLED_H__

#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <string.h>

#define I2C_ADDR 0x3c//通过查找I2C设备获得地址

//char DataBuffer[8][128]={0};

 //'0'-48, 'A'-(65-10)55 'a'-(97-36)61
static unsigned char F68[][6] =
{
{0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E},// 0
{0x00, 0x00, 0x42, 0x7F, 0x40, 0x00},// 1
{0x00, 0x42, 0x61, 0x51, 0x49, 0x46},// 2
{0x00, 0x21, 0x41, 0x45, 0x4B, 0x31},// 3
{0x00, 0x18, 0x14, 0x12, 0x7F, 0x10},// 4
{0x00, 0x27, 0x45, 0x45, 0x45, 0x39},// 5
{0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30},// 6
{0x00, 0x01, 0x71, 0x09, 0x05, 0x03},// 7
{0x00, 0x36, 0x49, 0x49, 0x49, 0x36},// 8
{0x00, 0x06, 0x49, 0x49, 0x29, 0x1E},// 9

{0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C},// A10
{0x00, 0x7F, 0x49, 0x49, 0x49, 0x36},// B
{0x00, 0x3E, 0x41, 0x41, 0x41, 0x22},// C
{0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C},// D
{0x00, 0x7F, 0x49, 0x49, 0x49, 0x41},// E
{0x00, 0x7F, 0x09, 0x09, 0x09, 0x01},// F
{0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A},// G
{0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F},// H
{0x00, 0x00, 0x41, 0x7F, 0x41, 0x00},// I
{0x00, 0x20, 0x40, 0x41, 0x3F, 0x01},// J
{0x00, 0x7F, 0x08, 0x14, 0x22, 0x41},// K
{0x00, 0x7F, 0x40, 0x40, 0x40, 0x40},// L
{0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F},// M
{0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F},// N
{0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E},// O
{0x00, 0x7F, 0x09, 0x09, 0x09, 0x06},// P
{0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E},// Q
{0x00, 0x7F, 0x09, 0x19, 0x29, 0x46},// R
{0x00, 0x46, 0x49, 0x49, 0x49, 0x31},// S
{0x00, 0x01, 0x01, 0x7F, 0x01, 0x01},// T
{0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F},// U
{0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F},// V
{0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F},// W
{0x00, 0x63, 0x14, 0x08, 0x14, 0x63},// X
{0x00, 0x07, 0x08, 0x70, 0x08, 0x07},// Y
{0x00, 0x61, 0x51, 0x49, 0x45, 0x43},// Z35

{0x00, 0x20, 0x54, 0x54, 0x54, 0x78},// a36
{0x00, 0x7F, 0x48, 0x44, 0x44, 0x38},// b
{0x00, 0x38, 0x44, 0x44, 0x44, 0x20},// c
{0x00, 0x38, 0x44, 0x44, 0x48, 0x7F},// d
{0x00, 0x38, 0x54, 0x54, 0x54, 0x18},// e
{0x00, 0x08, 0x7E, 0x09, 0x01, 0x02},// f
{0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C},// g
{0x00, 0x7F, 0x08, 0x04, 0x04, 0x78},// h
{0x00, 0x00, 0x44, 0x7D, 0x40, 0x00},// i
{0x00, 0x40, 0x80, 0x84, 0x7D, 0x00},// j
{0x00, 0x7F, 0x10, 0x28, 0x44, 0x00},// k
{0x00, 0x00, 0x41, 0x7F, 0x40, 0x00},// l
{0x00, 0x7C, 0x04, 0x18, 0x04, 0x78},// m
{0x00, 0x7C, 0x08, 0x04, 0x04, 0x78},// n
{0x00, 0x38, 0x44, 0x44, 0x44, 0x38},// o
{0x00, 0xFC, 0x24, 0x24, 0x24, 0x18},// p
{0x00, 0x18, 0x24, 0x24, 0x18, 0xFC},// q
{0x00, 0x7C, 0x08, 0x04, 0x04, 0x08},// r
{0x00, 0x48, 0x54, 0x54, 0x54, 0x20},// s
{0x00, 0x04, 0x3F, 0x44, 0x40, 0x20},// t
{0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C},// u
{0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C},// v
{0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C},// w
{0x00, 0x44, 0x28, 0x10, 0x28, 0x44},树梅派硬件编程_按键中断

树梅派硬件编程_超声波测距

树梅派硬件编程_红外遥控器

树梅派硬件编程_DTH11温湿度检测

树梅派硬件学习_多线程任务

树梅派硬件学习_xbox手柄驱动