orangepi接口:
orangepi@orangepizero2:~$ gpio readall
+------+-----+----------+------+---+ H616 +---+------+----------+-----+------+
| GPIO | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | GPIO |
+------+-----+----------+------+---+----++----+---+------+----------+-----+------+
| | | 3.3V | | | 1 || 2 | | | 5V | | |
| 229 | 0 | SDA.3 | OFF | 0 | 3 || 4 | | | 5V | | |
| 228 | 1 | SCL.3 | OFF | 0 | 5 || 6 | | | GND | | |
| 73 | 2 | PC9 | OFF | 0 | 7 || 8 | 0 | ALT2 | TXD.5 | 3 | 226 |
| | | GND | | | 9 || 10 | 0 | ALT2 | RXD.5 | 4 | 227 |
| 70 | 5 | PC6 | ALT5 | 0 | 11 || 12 | 0 | OFF | PC11 | 6 | 75 |
| 69 | 7 | PC5 | ALT5 | 0 | 13 || 14 | | | GND | | |
| 72 | 8 | PC8 | OFF | 0 | 15 || 16 | 0 | OFF | PC15 | 9 | 79 |
| | | 3.3V | | | 17 || 18 | 0 | OFF | PC14 | 10 | 78 |
| 231 | 11 | MOSI.1 | ALT4 | 0 | 19 || 20 | | | GND | | |
| 232 | 12 | MISO.1 | ALT4 | 0 | 21 || 22 | 0 | OFF | PC7 | 13 | 71 |
| 230 | 14 | SCLK.1 | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE.1 | 15 | 233 |
| | | GND | | | 25 || 26 | 0 | OFF | PC10 | 16 | 74 |
| 65 | 17 | PC1 | OFF | 0 | 27 || 28 | | | | | |
| 272 | 18 | PI16 | OFF | 0 | 29 || 30 | | | | | |
| 262 | 19 | PI6 | OFF | 0 | 31 || 32 | | | | | |
| 234 | 20 | PH10 | ALT3 | 0 | 33 || 34 | | | | | |
+------+-----+----------+------+---+----++----+---+------+----------+-----+------+
| GPIO | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | GPIO |
+------+-----+----------+------+---+ H616 +---+------+----------+-----+------+
可以看出,0引脚为SDA,1引脚为SCL
1、启动之后查看根目录是否存在i2c设备节点
ls /dev/:根目录下的dev查看设备驱动底层,可以看到i2c-3和i2c-5
orangepi@orangepizero2:~$ ls /dev/
autofs lirc0 ram15 tty13 tty42 ttyS5
block log ram2 tty14 tty43 tv
btrfs-control loop0 ram3 tty15 tty44 uhid
bus loop1 ram4 tty16 tty45 uinput
cec loop2 ram5 tty17 tty46 urandom
cedar_dev loop3 ram6 tty18 tty47 vcs
char loop4 ram7 tty19 tty48 vcs1
console loop5 ram8 tty2 tty49 vcs2
core loop6 ram9 tty20 tty5 vcs3
cpu_dma_latency loop7 random tty21 tty50 vcs4
cuse loop-control rfkill tty22 tty51 vcs5
deinterlace mapper rtc tty23 tty52 vcs6
disk mem rtc0 tty24 tty53 vcs7
disp memory_bandwidth shm tty25 tty54 vcsa
dlm-control mmcblk0 slog_wcn0 tty26 tty55 vcsa1
dlm-monitor mmcblk0p1 slog_wcn1 tty27 tty56 vcsa2
dlm_plock mqueue snd tty28 tty57 vcsa3
fb0 net spidev0.0 tty29 tty58 vcsa4
fd network_latency spidev1.1 tty3 tty59 vcsa5
full network_throughput sst_storage tty30 tty6 vcsa6
fuse null stderr tty31 tty60 vcsa7
gpiochip0 ppp stdin tty32 tty61 vhci
gpiochip1 ptmx stdout tty33 tty62 watchdog
hdmi pts sunxi-reg tty34 tty63 watchdog0
hugepages ptyp0 sunxi_soc_info tty35 tty7 wcn_op
hwrng ram0 sunxi-wlan tty36 tty8 zero
i2c-3 ram1 tty tty37 tty9 zram0
i2c-5 ram10 tty0 tty38 ttyBT0 zram1
initctl ram11 tty1 tty39 ttyBT1 zram2
input ram12 tty10 tty4 ttyp0
kmem ram13 tty11 tty40 ttyS0
kmsg ram14 tty12 tty41 ttyS1
2、测试i2c,安装i2c-tools,装到默认路径下的
sudo apt-get inatall i2c-tools
3、安装完成,输入命令:sudo i2cdetect -y 3
能查看到3c表示oled的屏幕,
orangepi@orangepizero2:~$ sudo i2cdetect -y 3
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
4、复制代码,进行修改
cp ../wiringPi_from_windows/wiringOP-master/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;
}
编译:./build.sh oled_demo.c /dev/i2c-3
运行:sudo ./a.out /dev/i2c-3
运行结果:
orangepi@orangepizero2:~/hardwaresoft$ ./build.sh oled_demo.c /dev/i2c-3
./build.sh
oled_demo.c
orangepi@orangepizero2:~/hardwaresoft$ sudo ./a.out /dev/i2c-3
---------start--------
----------end---------
5、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_show(struct display_info *disp) {
int i;
char buf[100];
oled_putstrto(disp, 0, 9+1, "Welcome to my oled");//0代表行,9+1代表列,“welcome to my oled”是内容
disp->font = font2;//字体设置,一共有3种字体选择,1,2,3
oled_putstrto(disp, 0, 9+11, " ##yangzerui## ");
disp->font = font2;
oled_send_buffer(disp);
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);
e = oled_init(&disp);
oled_show(&disp);
return 0;
}
编译运行:
orangepi@orangepizero2:~/hardwaresoft$ ./build.sh oled_demo.c
./build.sh
oled_demo.c
orangepi@orangepizero2:~/hardwaresoft$ sudo ./a.out /dev/i2c-3