|
|
1行目: |
1行目: |
| OLED(SO1602AWYB-UC-XX)表示処理用プログラム
| | [[カテゴリ:PIAST]] |
| ==OLED初期化==
| | |
| #include <stdio.h>
| | OLED実装時<br> |
| #include <stdlib.h>
| | <br> |
| #include <strings.h>
| | [[画像:PIAST PCB-OLED.png]]<br> |
| #include <unistd.h>
| | <br> |
| #include <fcntl.h>
| | *フロント部にOLED(16x2)表示器を付ける場合の実装 |
| #include <errno.h>
| | *対応部品 |
| #include <string.h>
| | :JP2の位置にピンソケットを実装 |
| #include <sys/ioctl.h>
| | <br> |
|
| | [[画像:PIAST_OLED_CARRY.png]]<br> |
| #include <linux/i2c-dev.h>
| | OLED用キャリーボードを使用すると直結可能。キャリーボードは以下のように組み立て |
|
| | :SA0 - I2Cアドレス選択用ハンダジャンパ―、L側で0、H側で1 |
|
| | :C - パスコン用 |
| main(int argc, char *argv[])
| | :R1,R2 - 実装しない |
| {
| | :注意1:アングルピンヘッダは写真の位置に実装するが、写真のように長ピン側に黒いプラスチックがあるものを選択のこと(ピンの長さの関係)。 |
| int i2c_fd; //I2Cバス用のファイル・ディスクリプタ
| | :注意2:アングルピンヘッダの裏面飛び出し量に注意。OLED本体のパーツと接触しないように。 |
|
| | :対応OLEDモジュール - 秋月電子扱いの16×2文字表示タイプ |
| char *i2cbus = "/dev/i2c-1"; //バス1側のデバイス名
| | ::白 http://akizukidenshi.com/catalog/g/gP-08277/ |
| int oled_addr = 0x3c; //OLEDのアドレス
| | ::緑 http://akizukidenshi.com/catalog/g/gP-08276/ |
| unsigned char oled_reg = 0x40; //書き込み対象のレジスタ
| | ::黄 http://akizukidenshi.com/catalog/g/gP-08278/ |
|
| | :OLEDのピン数に対してOLEDキャリーボード側のピン数が少ないが、1番ピン側からあわせて取り付けること。 |
| int i; //汎用カウンタ
| | *参考 |
| unsigned char buf[4]; //書き込み用バッファ
| | :要するにI2C接続タイプのディスプレイを搭載するためのものなので他のI2Cディスプレイでも接続可能。その場合の接続はJP2のピンアサインが以下のようになっているので、これに合わせてキャリーボードを製作すること。 |
| char dmesg[256];
| | :1 - GND |
|
| | :2 - +3.3v |
| if(argc < 1) exit(1);
| | :3 - NC |
|
| | :4 - NC |
| //I2Cバスをオープンする
| | :5 - NC |
| if((i2c_fd = open(i2cbus, O_RDWR))<0){
| | :6 - NC |
| perror("oledinit open");
| | :7 - SDA |
| exit(1);
| | :8 - SCL |
| }
| | :9 - NC |
|
| | :10 - NC |
| //スレーブのアドレスをioctlで設定
| |
| if(ioctl(i2c_fd, I2C_SLAVE, oled_addr) < 0){
| |
| perror("oledinit can't set slave");
| |
| close(i2c_fd);
| |
| exit(1);
| |
| }
| |
|
| |
|
| |
| usleep(100);
| |
| //OLED初期化
| |
| //ディスプレイ消去
| |
| buf[0] = 0x00;
| |
| buf[1] = 0x01;
| |
| write(i2c_fd,buf,2);
| |
| usleep(20);
| |
| //カーソルをホームへ
| |
| buf[0] = 0x00;
| |
| buf[1] = 0x02;
| |
| write(i2c_fd,buf,2);
| |
| usleep(20);
| |
| //表示ON,カーソル表示なし
| |
| buf[0] = 0x00;
| |
| buf[1] = 0x0c;
| |
| write(i2c_fd,buf,2);
| |
| usleep(20);
| |
| //表示ON
| |
| buf[0] = 0x00;
| |
| buf[1] = 0x01;
| |
| write(i2c_fd,buf,2);
| |
| usleep(20);
| |
|
| |
| close(i2c_fd);
| |
| }
| |
| ==OLED1行目表示用==
| |
| #include <stdio.h>
| |
| #include <stdlib.h>
| |
| #include <strings.h>
| |
| #include <unistd.h>
| |
| #include <fcntl.h>
| |
| #include <errno.h>
| |
| #include <string.h>
| |
| #include <sys/ioctl.h>
| |
|
| |
| #include <linux/i2c-dev.h>
| |
|
| |
|
| |
| main(int argc, char *argv[])
| |
| {
| |
| int i2c_fd; //I2Cバス用のファイル・ディスクリプタ
| |
|
| |
| char *i2cbus = "/dev/i2c-1"; //バス1側のデバイス名
| |
| int oled_addr = 0x3c; //OLEDのアドレス
| |
| unsigned char oled_reg = 0x40; //書き込み対象のレジスタ
| |
|
| |
| int i; //汎用カウンタ
| |
| unsigned char buf[4]; //書き込み用バッファ
| |
| char dmesg[256];
| |
|
| |
| if(argc < 2) exit(1);
| |
|
| |
| //I2Cバスをオープンする
| |
| if((i2c_fd = open(i2cbus, O_RDWR))<0){
| |
| perror("oledinit open");
| |
| exit(1);
| |
| }
| |
|
| |
| //スレーブのアドレスをioctlで設定
| |
| if(ioctl(i2c_fd, I2C_SLAVE, oled_addr) < 0){
| |
| perror("oledinit can't set slave");
| |
| close(i2c_fd);
| |
| exit(1);
| |
| }
| |
|
| |
| //カーソルをホームへ
| |
| buf[0] = 0x00;
| |
| buf[1] = 0x02;
| |
| write(i2c_fd,buf,2);
| |
| usleep(20);
| |
|
| |
| //1行目処理
| |
| strncpy(dmesg, argv[1], 16);
| |
| //メッセージ書き込み
| |
| for(i=0;i<16;i++) {
| |
| buf[0] = 0x40;
| |
| if(dmesg[i] == 0) break;
| |
| buf[1] = dmesg[i];
| |
| write(i2c_fd,buf,2);
| |
| usleep(20);
| |
| }
| |
|
| |
| close(i2c_fd);
| |
| }
| |
| ==OLED2行目表示用==
| |
| #include <stdio.h>
| |
| #include <stdlib.h>
| |
| #include <strings.h>
| |
| #include <unistd.h>
| |
| #include <fcntl.h>
| |
| #include <errno.h>
| |
| #include <string.h>
| |
| #include <sys/ioctl.h>
| |
|
| |
| #include <linux/i2c-dev.h>
| |
|
| |
|
| |
| main(int argc, char *argv[])
| |
| {
| |
| int i2c_fd; //I2Cバス用のファイル・ディスクリプタ
| |
|
| |
| char *i2cbus = "/dev/i2c-1"; //バス1側のデバイス名
| |
| int oled_addr = 0x3c; //OLEDのアドレス
| |
| unsigned char oled_reg = 0x40; //書き込み対象のレジスタ
| |
|
| |
| int i; //汎用カウンタ
| |
| unsigned char buf[4]; //書き込み用バッファ
| |
| char dmesg[256];
| |
|
| |
| if(argc < 2) exit(1);
| |
|
| |
| //I2Cバスをオープンする
| |
| if((i2c_fd = open(i2cbus, O_RDWR))<0){
| |
| perror("oledinit open");
| |
| exit(1);
| |
| }
| |
|
| |
| //スレーブのアドレスをioctlで設定
| |
| if(ioctl(i2c_fd, I2C_SLAVE, oled_addr) < 0){
| |
| perror("oledinit can't set slave");
| |
| close(i2c_fd);
| |
| exit(1);
| |
| }
| |
|
| |
| //DDRAMを2行目へ
| |
| buf[0] = 0x00;
| |
| buf[1] = 0xa0;
| |
| write(i2c_fd,buf,2);
| |
| usleep(20);
| |
|
| |
| //2行目処理
| |
| strncpy(dmesg, argv[1], 16);
| |
| //メッセージ書き込み
| |
| for(i=0;i<16;i++) {
| |
| buf[0] = 0x40;
| |
| if(dmesg[i] == 0) break;
| |
| buf[1] = dmesg[i];
| |
| write(i2c_fd,buf,2);
| |
| usleep(20);
| |
| }
| |
|
| |
| close(i2c_fd);
| |
| }
| |