banner
ZetoHkr

ZetoHkr

摸🐟从未停止,努力从未开始
github

LM4229 driver code for AT89C51

About this article#

  • References:
    1. LM4229 Data sheet (1996/08/05)
    2. T6963C Data sheet (1998/10/20)
  • Environment: Proteus simulation

About LM4229#

  • LM4229 is an LCD display screen with a Toshiba T6963C driver chip, with a graphic resolution of 240 * 128.

Circuit Connection#

  • Pin Connection
    LM4229 PinConnection TargetDescription
    VSSGroundGround
    VDDConnect to +5V Power SupplyPositive Voltage Power Supply
    VOLeave EmptyContrast Drive Voltage
    C/DAT89C51 P2.2 (D can be low level)Command/Data Switching
    RDAT89C51 P2.1 (RD can be low level)Read
    WRAT89C51 P2.0 (WR can be low level)Write
    D0 ~ D7AT89C51 P1.0 ~ P1.7Data Line
    CEAT89C51 P2.3 (CE can be low level)Chip Enable
    RSTAT89C51 P2.4 (RST can be low level)Reset
    VEELeave EmptyNegative Voltage Power Supply
    MD2AT89C51 P2.5Mode Selection
    FS1AT89C51 P2.6Font Selection
    HALTAT89C51 P2.7Stop

Driver Code#

#include <reg51.h>

// define type abbreviation
#define uchar unsigned char
#define uint unsigned int
// define data port
#define DATAPORT P1

// define control pins
sbit halt = P2^7;
sbit fs1 = P2^6;
sbit md2 = P2^5;
sbit rst = P2^4;
sbit ce = P2^3;
sbit cd = P2^2;
sbit rd = P2^1;
sbit wr = P2^0;

// fonts
// method of obtaining: https://www.zhetao.com/fontarray.html
// parameter:
//     byte width: 8bit
//     font height: 16
//     columns: 2
//     font: Songti abc, size: 0 (automatic)
unsigned char code font[][16] = {
  /*--  0  --*/
  {0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00,},
  /*--  1  --*/
  {0x00,0x00,0x00,0x08,0x38,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x3E,0x00,0x00,},
  /*--  2  --*/
  {0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x02,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00,},
  /*--  3  --*/
  {0x00,0x00,0x00,0x3C,0x42,0x42,0x02,0x04,0x18,0x04,0x02,0x42,0x42,0x3C,0x00,0x00,},
  /*--  4  --*/
  {0x00,0x00,0x00,0x04,0x0C,0x0C,0x14,0x24,0x24,0x44,0x7F,0x04,0x04,0x1F,0x00,0x00,},
  /*--  5  --*/
  {0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x78,0x44,0x02,0x02,0x42,0x44,0x38,0x00,0x00,},
  /*--  6  --*/
  {0x00,0x00,0x00,0x18,0x24,0x40,0x40,0x5C,0x62,0x42,0x42,0x42,0x22,0x1C,0x00,0x00,},
  /*--  7  --*/
  {0x00,0x00,0x00,0x7E,0x42,0x04,0x04,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x00,0x00,},
  /*--  8  --*/
  {0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00,},
  /*--  9  --*/
  {0x00,0x00,0x00,0x38,0x44,0x42,0x42,0x42,0x46,0x3A,0x02,0x02,0x24,0x18,0x00,0x00,},
  /*--  space  --*/
  {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,},
};

// delay function
void delay_lcd(uchar t) {
  for (; t != 0; t--);
}

// write data to lcd display
void write_data(uchar dat) {
  // The D of the C/D pin is available at low level, so CD = 0;
  cd = 0;
  // CE (Chip Enable) pin is available at low level, so CE = 0;
  ce = 0;
  // WR (Write) pin is available at low level, so WR = 0;
  wr = 0;
  // Send the data of dat to the data port;
  DATAPORT = dat;
  delay_lcd(1);
  // end write.
  wr = 1; ce = 1;
}

// write command to lcd display
void write_cmd(uchar cmd) {
  // The C of the C/D pin is available at high level, so CD = 1;
  cd = 1;
  // CE (Chip Enable) pin is available at low level, so CE = 0;
  ce = 0;
  // WR (Write) pin is available at low level, so WR = 0;
  wr = 0;
  // Send the command data of cmd to the data port;
  DATAPORT = cmd;
  delay_lcd(1);
  // end write.
  wr = 1; ce = 1;
}

// show half-width characters
// x: line position
// y: column position
// flag: whether to display in reverse color
// ch: font index pointer (array)
// count: number of characters
void display_hw(uchar x, uchar y, int flag, uchar ch, int count) {
  unsigned int place, i = 0, k;
  // iterate over character array
  for (i = 0; i < count; i++) {
    // One character 8bit width, one line 240bit, 30 characters in total
    // y: the vertical offset
    place = x * 30 + y;
    // half-width character contains 16 rows of 8-bit dot matrix
    for (k = 0; k <= 15; k++) {
      // write high address
      write_data(place & 0xff);
      // write low address
      write_data(place >> 8);
      // set address
      write_cmd(0x24);
      // Set continuous read and write
      write_cmd(0xb0);
      // Find the dot matrix data of the corresponding character in the font array according to the element index of ch
      write_data(flag ? font[ch[i]][k] : ~font[ch[i]][k]);
      place += 30;
    }
    // A line with more than 30 characters needs to be wrapped
    if (y > 30) {
      // One character 16bit height
      x += 16;
      y = 0;
    } else {
      // Continue write
      y += 2;
    }
  }
}

// initial lcd display
void lcd_initial() {
  // When FS1 is at low level, the font is 8*8, So fs1 = 0;
  fs1 = 0;
  // set graphics area
  write_data(30);
  write_data(0);
  write_cmd(0x43);
  // Turn off text display, turn on graphic display
  write_cmd(0x98);
}

Usage#

void main(void) {
  unsigned char c[1] = {1};
  lcd_init();
  while (1) {
    display_hw(16, 0, 1, c, 1);
  }
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.