(実装編)全角半角混在16ビット漢字キャラクタージェネレータ(ST7735S)(Arduino UNO)


x 過去ログをみよ

いきなりファイナル


![o_coq159.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/24f074df-2406-1f8e-4dfa-a4b81c6f5c5f.jpeg)



```c++



//ssd1306_cgp8x16_UNO_1


//インクルド
#include <Arduino.h>


//定義
//GPIO
#define GPIO_RESET_P  2 //RESET=1
#define GPIO_RS_P    A3 //RS=0
#define GPIO_WR_P     3 //WR=1
#define GPIO_RD_P    A2 //RD=1

#define GPIO_DB7      8
#define GPIO_DB6      7
#define GPIO_DB5      9
#define GPIO_DB4      6
#define GPIO_DB3     A0
#define GPIO_DB2      5
#define GPIO_DB1     A1
#define GPIO_DB0      4

// GPIO A4 I2Cで使用済み
// GPIO A5 I2Cで使用済み

#define GPIO1(s) digitalWrite(GPIO_RESET_P , s)  //RESET
#define GPIO2(s) digitalWrite(GPIO_RS_P    , s)  //A0
#define GPIO3(s) digitalWrite(GPIO_WR_P    , s)  //WR
#define GPIO4(s) digitalWrite(GPIO_RD_P    , s)  //RD

//全体のカラー
int s_color = 0xFFFF; //ハイホワイト


//パラレルポートにデータをセットする。
void GPIO_8BIT(int s)
{
  digitalWrite(GPIO_DB7, (s >> 7) & 0x01); //DB7
  digitalWrite(GPIO_DB6, (s >> 6) & 0x01); //DB6
  digitalWrite(GPIO_DB5, (s >> 5) & 0x01); //DB5
  digitalWrite(GPIO_DB4, (s >> 4) & 0x01); //DB4
  digitalWrite(GPIO_DB3, (s >> 3) & 0x01); //DB3
  digitalWrite(GPIO_DB2, (s >> 2) & 0x01); //DB2
  digitalWrite(GPIO_DB1, (s >> 1) & 0x01); //DB1
  digitalWrite(GPIO_DB0,  s       & 0x01); //DB0
} //GPIO_8BIT

//コマンドの書き込み
void LCD_Write_CMD(int a)
{
  GPIO2(0); //A0=0;
  GPIO_8BIT(a);//P1=a; data
  GPIO3(0);//WRB=0;
  GPIO3(1);//WRB=1;
} //LCD_Write_CMD


//データ書き込み
void LCD_Write_Data(int a)
{
  GPIO2(1);//A0=1;
  GPIO_8BIT(a);//P1=a; data
  GPIO3(0);//WRB=0;
  GPIO3(1);//WRB=1;
} //LCD_Write_Data


//液晶の初期化処理
void ATM0177B5_ST7735S_Init(void)
{

  //----------  ST7735S Reset Sequence  --------//

  GPIO1(1);//LCD_RESET=1;

  delay(1); //Delay 1ms

  GPIO1(0);//LCD_RESET=0;

  delay(1); //Delay 1ms

  GPIO1(1);//LCD_RESET=1;

  delay(120); //Delay 120ms

  //----------  ST7735S Reset Sequence  --------//


  LCD_Write_CMD(0x01);//SOFTWARE RESET
  delay(50);

  LCD_Write_CMD(0x11);//SLEEP OUT
  delay(200);

  LCD_Write_CMD(0x29);//display on
  delay(100);

  LCD_Write_CMD(0x3a);//Interface pixel format
  LCD_Write_Data(0x05);//16bit mode
  delay(100);

  LCD_Write_CMD(0x36);//RGB-RGR format
  LCD_Write_Data(0x08);//RGB mode
  delay(100);

} //ATM0177B5_ST7735S_Init


//全体のカラーの変更
void set_color(int color) {

  s_color = color;

}//set_color

//漢字ROM
int ch_data7[16 * 7] = {
  0x0000, 0x0000, 0x3800, 0x4400, 0xa200, 0xa200, 0x9200, 0x9200,
  0x9200, 0x8a00, 0x8a00, 0x4400, 0x3800, 0x0000, 0x0000, 0x0000, //0
  0x0000, 0x0000, 0x1000, 0x3000, 0x5000, 0x1000, 0x1000, 0x1000,
  0x1000, 0x1000, 0x1000, 0x1000, 0x1000, 0x0000, 0x0000, 0x0000, //1
  0x0000, 0x0000, 0x3800, 0x4400, 0x8200, 0x8200, 0x0200, 0x0400,
  0x0800, 0x1000, 0x2000, 0xc000, 0xfe00, 0x0000, 0x0000, 0x0000, //2
  0x0000, 0x43f8, 0x2208, 0x13f8, 0x4208, 0x23f8, 0x1000, 0x0000,
  0x13f8, 0x12a8, 0x22a8, 0x22a8, 0x22a8, 0x42a8, 0x47fc, 0x0000,//温 (3)
  0x0080, 0x1ffc, 0x1000, 0x1220, 0x2ff8, 0x2220, 0x23e0, 0x2000,
  0x2ffc, 0x2410, 0x4360, 0x4080, 0x4160, 0x4618, 0x0806, 0x0000,//度 (4)
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 //' '
};

char st_p[10][16] = {
//  1    2    3    4    5    6    7      8     9   10   11    12   13   14   15   16
  {'3', '3', '4', '4', '2', '1', '5', '5',  '5', '5', '5', '5', '5', '5', '5', '5' }, //1
  {'0', '0', '0', '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0', '0', '0' }, //2
  {'0', '0', '0', '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0', '0', '0' }, //3
  {'0', '0', '0', '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0', '0', '0' }, //4
  {'0', '0', '0', '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0', '0', '0' }, //5

  {'0', '0', '0', '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0', '0', '0' }, //6
  {'0', '0', '0', '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0', '0', '0' }, //7
  {'0', '0', '0', '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0', '0', '0' }, //8
  {'0', '0', '0', '0', '0', '0', '0', '0',  '0', '0', '0', '0', '0', '0', '0', '0' }, //9
  {'0', '0', '0', '0', '0', '0', '0', '1',  '0', '0', '0', '0', '0', '0', '0', '0' }  //10
};

char hz_p[10+1][16+1] = {
// 1  2  3  4  5  6  7  8   9  10 11 12 13 14 15 16
  {0, 8, 0, 8, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //1
  {0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //2
  {0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //3
  {0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //4
  {0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //5
  {0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //6
  {0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //7
  {0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //8
  {0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //9
  {0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0 }, //10
};




//パターンRAMの内容を液晶に転送
void p_ram_refresh() {

  //カラーの上位と下位と一時
  int color_h;
  int color_l;
  int color_vel;

  //画面の書き込み開始
  LCD_Write_CMD(0x2C); //memory write
  GPIO2(1);//A0=1;
  delay(2);


  int a; int ch;
  for (int y = 0; y < 160; y++) {
    for (int x = 0; x < 128; x++) {

      ch = st_p[y >> 4][x >> 3] - '0';
      a = (ch_data7[ (y & 0x0f ) + (ch * 16) ] << ( x & 0x07) ) ;
      a = a << hz_p[y >> 4][x >> 3];
      a = a & 0x8000;
      a = a >> 15;
      //printf("%d ",a);

      if (a == 0) {
        color_vel = 0x0000;
      } else {
        color_vel = 0xffff;
      }
      color_h = (color_vel >> 8) & 0xff;
      color_l =  color_vel       & 0xff;

      GPIO_8BIT( color_h );//P1=a; data
      GPIO3(0);//WRB=0;
      GPIO3(1);//WRB=1;

      GPIO_8BIT( color_l );//P1=a; data
      GPIO3(0);//WRB=0;
      GPIO3(1);//WRB=1;

    }//for x
    //printf("\n");
  }//for y

}//p_ram_refresh


//リフレッシュ(画像の再表示)
void refresh() {

  p_ram_refresh();//パターンRAMの反映

}//refresh


//初期化
void setup() {

  //ポートのモード設定
  //アウトプットモード
  pinMode(GPIO_RESET_P, OUTPUT); //REST
  pinMode(GPIO_RS_P,    OUTPUT); //RS
  pinMode(GPIO_WR_P,    OUTPUT); //WR
  pinMode(GPIO_RD_P,    OUTPUT); //RD

  pinMode(GPIO_DB0, OUTPUT); //DB0
  pinMode(GPIO_DB1, OUTPUT); //DB1
  pinMode(GPIO_DB2, OUTPUT); //DB2
  pinMode(GPIO_DB3, OUTPUT); //DB3

  pinMode(GPIO_DB4, OUTPUT); //DB4
  pinMode(GPIO_DB5, OUTPUT); //DB5
  pinMode(GPIO_DB6, OUTPUT); //DB6
  pinMode(GPIO_DB7, OUTPUT); //DB7

  //ポートの初期化
  GPIO4(1);//RD=1
  GPIO3(0);//WR=0
  GPIO2(0);//RS=0
  GPIO1(1);//RESET=1

  delay(500); //0.5秒待つ

  //液晶の初期化処理
  ATM0177B5_ST7735S_Init();

} //setup


//メインループ
void loop() {

  refresh(); //画面の再表示

  delay(3000); //3秒待つ debug

} //loop


```