t49's blog

電子工作初心者のブログ

【AE-AQM1248A】ArduinoでグラフィックLCDを使う

実行結果

概要

Arduino Unoを使って、自作した32*32フォントの数字をグラフィックLCDに表示するデモを行いました。

部品

マイコンボード: Arduino Uno
・グラフィックLCD : AE-AQM1248A
・レベルシフタ: BSS138使用 4ビット双方向ロジックレベル変換モジュール(AE-LCNV4-MOSFET)

Arduino Unoの動力及び信号の電圧は5V、グラフィックLCDは3.3Vで動作するため、レベルシフタを使用しています。

回路図

arduino-AE-AQM1248A

プログラム

1ms毎にカウントした値をグラフィックLCDに表示しています。

#include<SPI.h>
// SPI Write = LOW,SPI not Write = HIGH
#define CS 10

// cmd = LOW, data = HIGH
#define RS 9

#define SCLK 13
#define SDI 11

#define PAGE_MAX 6
#define COL_MAX 128

// 32×32 bitmap fonts 0-9
char zero[][32] = {
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFE,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00}
};

char one[][32] = {
  {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00}
};

char two[][32] = {
  {0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x00,0x00,0x00,0x00,0x00,0x00}
};

char three[][32] {
  {0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00}
};

char four[][32] {
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00},
};

char five[][32] {
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFE,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00}
};

char six[][32] {
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFE,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00}
};

char seven[][32]{
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFE,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00}
};

char eight[][32] {
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFE,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x7F,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00}
};

char nine[][32] {
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFE,0xFE,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0x0E,0xFE,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},
  {0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x70,0x7F,0x7F,0x7F,0x00,0x00,0x00,0x00,0x00,0x00}
};

void sendCmd(char cmd) {
  // SPI connection enable
  digitalWrite(CS,LOW);
  digitalWrite(RS,LOW);
  
  // send data
  SPI.transfer(cmd);

  // SPI connection disable
  digitalWrite(CS,HIGH);
  digitalWrite(RS,HIGH);
}

void sendData(char data){
  digitalWrite(CS,LOW);
  digitalWrite(RS,HIGH);
  SPI.transfer(data);
  digitalWrite(CS,HIGH);
}

int dec2array(unsigned int num,unsigned int* nums){
  unsigned int shou = num;
  unsigned int cnt = 0;
  
  while(shou != 0){
    nums[cnt++] = shou%10;
    shou = shou/10;
  }

  return cnt;
}

void printNum(int num){
  unsigned int nums[64];
  int page,col;
  int cmd_msb,cmd_lsb;

  int cnt = dec2array(num,nums);
  
  for(int i=0;i<cnt;i++){
    for(page=0;page<4;page++){
      sendCmd(0xB1+page);
      for(col=i*32;col<i*32+32;col++){
        cmd_msb = 0x10 | col >> 4;
        sendCmd(cmd_msb);
        cmd_lsb = 0x00 | col & 0x0F;
        sendCmd(cmd_lsb);

        switch(nums[cnt-1-i]){
          case 0:
            sendData(zero[page][col-i*32]); break;
          case 1:
            sendData(one[page][col-i*32]); break;
          case 2:
            sendData(two[page][col-i*32]); break;
          case 3:
            sendData(three[page][col-i*32]); break;
          case 4:
            sendData(four[page][col-i*32]); break;
          case 5:
            sendData(five[page][col-i*32]); break;
          case 6:
            sendData(six[page][col-i*32]); break;
          case 7:
            sendData(seven[page][col-i*32]); break;
          case 8:
            sendData(eight[page][col-i*32]); break;
          case 9:
            sendData(nine[page][col-i*32]); break;
          default:
            sendData(eight[page][col-i*32]); Serial.println(nums[cnt-1-i]); break;
        }
      }
    }
  }
}

/*
 * 1011 0000 (0xB0) = Page 0
 * 1011 0001 (0xB1) = Page 1
 * 1011 0010 (0xB2) = Page 2
 * 1011 0011 (0xB3) = Page 3
 * 1011 0100 (0xB4) = Page 4
 * 1011 0101 (0xB5) = Page 5
 */
void lcd_clear(){
  int page,col;
  int col_address = 0;
  int cmd_msb,cmd_lsb;
  
  for(page=0;page<PAGE_MAX;page++){
    // ページアドレスを指定
    sendCmd(0xB0+page);
    for(col=0;col<COL_MAX;col+=8){
      cmd_msb = 0x10 | col >> 4;
      sendCmd(cmd_msb);
      cmd_lsb = 0x00 | col & 0x0F;
      sendCmd(cmd_lsb);

      for(int i=0;i<8;i++){
        sendData(0);
      }
    }
  } 
}

void lcd_all_on(){
  int page,col;
  int col_address = 0;
  int cmd_msb,cmd_lsb;
  
  for(page=0;page<PAGE_MAX;page++){
    // ページアドレスを指定
    sendCmd(0xB0+page);
    for(col=0;col<COL_MAX;col+=8){
      cmd_msb = 0x10 | col >> 4;
      sendCmd(cmd_msb);
      cmd_lsb = 0x00 | col & 0x0F;
      sendCmd(cmd_lsb);

      for(int i=0;i<8;i++){
        sendData(0xFF);
      }
    }
  } 
}

void setup() {
  pinMode(CS,OUTPUT);
  pinMode(RS,OUTPUT);
  pinMode(SCLK,OUTPUT);
  pinMode(SDI,OUTPUT);
  
  Serial.begin(9600);

  SPI.setBitOrder(MSBFIRST);
  
  // SPI function enable
  SPI.begin();
  
  // Display = OFF
  sendCmd(0xAE);
  // ADC = normal
  sendCmd(0xA0);
  // Common output = revers
  sendCmd(0xC8);
  // bias = 1/7
  sendCmd(0xA3);

  // regulator on
  sendCmd(0x2C);
  delay(2);
  sendCmd(0x2E);
  delay(2);
  sendCmd(0x2F);

  // contrast
  sendCmd(0x23);
  sendCmd(0x81);
  sendCmd(0x1C);

  // display setting
  sendCmd(0xA4);
  sendCmd(0x40);
  sendCmd(0xA6);
  sendCmd(0xAF);

  lcd_all_on();
  delay(3000);
  lcd_clear();
}

unsigned int test = 0;
void loop() {
  printNum(test++);
  delay(1);
}