乐趣区

关于c++:LX-COG-12864R1-LCD-Sample-Code

Introduction

Sample code for driving LX COG 12864R1 LCD.

NOTE

For arudino only.But it's very easy to apply on STM.
To run the code, you'd better to have a Arduino Mega 2560 which can print the log.

Code

// The .ino file.
// Define PINs
#define CS  2
#define RST 3
#define DC  4
#define SCL 21
#define SDA 20

#define setPin(pin, x) digitalWrite(pin, (x) ? HIGH : LOW)
#define cs(x) setPin(CS, (x))
#define reset(x) setPin(RST, (x))
#define dc(x) setPin(DC, (x))
#define scl(x) setPin(SCL, (x))
#define sda(x) setPin(SDA, (x))

void log(const char* str) {Serial.println(str);
}

void spi_delay(uint16_t x) {while(x > 0) {x--;}
}

void spi_send(uint8_t val) {for(int i = 0; i < 8; i++) {if (val & 0x80) {sda(1);
    } else {sda(0);
    }
    spi_delay(1);
    
    scl(1);
    scl(0);
    val <<= 1;
  }
}

void lcd_cmd(uint8_t val) {cs(0);
  dc(0);
  spi_send(val);
  cs(1);
  dc(1);
}

void lcd_data(uint8_t val) {cs(0);
  dc(1);
  spi_send(val);
  cs(1);
  dc(1);
}

void lcd_init() {log("Reset 1"); reset(1); delay(1000);
  log("Reset 2"); reset(0); delay(1000);
  log("Reset 3"); reset(1); delay(1000);

  log("Reset internal");
  lcd_cmd(0xe2); // Internal reset
  lcd_cmd(0xe3);  // reset signal
  // delay(3000);

  log("Display off"); lcd_cmd(0xae); // display off
  // delay(3000);

  log("ADC select");
  lcd_cmd(0xa0);  // ADC select
  lcd_cmd(0xc8);  // command output select
  lcd_cmd(0x2f);  // power control
  // delay(3000);

  // SET dir
  // log("Set direction");
  // log("Reversed"); lcd_cmd(0xa1); //0b10100001); // Reverse
  // log("Normal"); lcd_cmd(0xa0); // 0b10100000); // Normal
  // delay(3000);


  log("Inverse display");
  // Inverse display
  // log("Reversed"); lcd_cmd(0xa7); // 0b10100111); // Reverse
  // log("No"); lcd_cmd(0xa6); // 0b10100110); // Normal
  // delay(3000);

  log("All pixels on");
  // // All pixels on
  // lcd_cmd(0xa5); // 0b10100101); // All on
  // lcd_cmd(0xa4); // 0b10100100); // Normal
  // delay(3000);

  log("Set bias");
  // bias
  log("1/9 bias"); lcd_cmd(0xa2);  //(0xa2 1/9 bias,1/65 duty)
  // log("1/7 bias"); lcd_cmd(0xa3);  // 1/7
  // delay(3000);

  // log("Page blink");
  // // Page Blink Page
  // lcd_cmd(0xd5);
  // lcd_cmd(0x0); // Blink all page: 0xF 
  // delay(3000);
  
  // Ratio is a key parameter which haverily affects the invisibility.
  log("Select resistor ratio");
  // select resistor ratio Rb/Ra: 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27
  lcd_cmd(0x22);
  // delay(3000);

  log("Select volume");
  lcd_cmd(0x81);  // select volume
  // delay(3000);
  
  log("vop");
  lcd_cmd(0x29); // 0x15); //0x29);   // vop
  // delay(3000);
  
  lcd_cmd(0xf8);  // x4
  lcd_cmd(0x08);  // x4
  // delay(3000);

  log("Initial page");
  lcd_cmd(0xb0);//set page address
  lcd_cmd(0x10);//set column address
  lcd_cmd(0x00);

  for(int col=0; col < 128; col++) {lcd_data(0x00);
    // lcd_data(0b11110000);
  }
  // delay(3000);

  log("Display on"); lcd_cmd(0xaf);  //display on
  // delay(3000);
}

uint8_t img[] = {
  0xFF,0x01,0x01,0x01,0x01,0x01,0x09,0x09,0xFD,0xFD,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,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,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,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,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,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x08,0x08,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x08,0x08,0xFC,0xFC,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,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,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,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x10,0x10,0x1F,0x1F,0x10,0x10,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,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,
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,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x08,0x08,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x08,0x08,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0xFC,
0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0xFC,0xFC,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x08,0xFC,0xFC,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,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,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,
0x1F,0x10,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,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,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x10,0x10,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x10,0x10,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0xF8,
0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0xF8,0xF8,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x10,0x10,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x10,0x10,0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,
0xF8,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0xA0,0xA0,0xBF,0xBF,0xA0,0xA0,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0xA0,0xA0,0xBF,0xBF,0xA0,0xA0,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0xA0,0xA0,0xBF,0xBF,0xA0,0xA0,0x80,0x80,0x80,0x80,0x80,0x80,0xA0,0xA0,0xBF,
0xBF,0xA0,0xA0,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xA0,0xA0,0xBF,0xBF,0xA0,0xA0,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xA0,0xA0,0xBF,0xBF,0xA0,0xA0,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0xA0,0xA0,0xBF,0xBF,0xA0,0xA0,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0xA0,0xA0,0xBF,0xBF,0xA0,0xA0,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xA0,0xA0,
0xBF,0xBF,0xA0,0xA0,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xFF
};

void lcd_display() {
  uint8_t* ptr = img;

  for(int row = 0xb0; row < 0xb8; row++) {lcd_cmd(row);
    lcd_cmd(0x10);
    lcd_cmd(0x00);
    for(int col = 0; col < 128; col++) {lcd_data(*ptr++);
    }
  }

  delay(2000);
}

void lcd_clean(uint8_t color = 0x00) {for(int row = 0xb0; row < 0xb8; row++) {lcd_cmd(row);
    lcd_cmd(0x10);
    lcd_cmd(0x00);

    for(int col = 0; col < 128; col++) {// lcd_data(0x81);
      // lcd_data(0x81);
      lcd_data(color);
    }
  }

  // delay(1000);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(CS, OUTPUT);
  pinMode(RST, OUTPUT);
  pinMode(DC, OUTPUT);
  pinMode(SCL, OUTPUT);
  pinMode(SDA, OUTPUT);

  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
}

void loop() {

  // put your main code here, to run repeatedly:
  lcd_init();

  uint8_t color = 0x00;

  while(1) {// delay(1500);
    digitalWrite(LED_BUILTIN, HIGH);
    log("Clean"); lcd_clean(color++);
    // if (color == 0xFE) color = 0x00;
    log("Display"); lcd_display();
    // reset(0);
    delay(100);
    // lcd_display();
    digitalWrite(LED_BUILTIN, LOW);
    // reset(1);
    delay(100);
  }
}

Good Luck!

退出移动版