微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 硬件工程师文库 > LCD使用时需要注意的几个小问题,AT89C51写入字符LCD点阵源代码分享

LCD使用时需要注意的几个小问题,AT89C51写入字符LCD点阵源代码分享

时间:04-26 来源:网络整理 点击:

LCDE="0";

  delay_us(5);

  do{

  status="lcd"_status();

  }while(status&0x80);

  LCDE="1";

  }

  void lcd_write_data(unsigned char dat)

  {

  unsigned char status;

  P0=dat;

  LCDRS="1";

  LCDRW="0";

  LCDE="0";

  delay_us(5);

  do{

  status="lcd"_status();

  }while(status&0x80);

  LCDE="1";

  }

  void lcd_clear(void)

  {

  lcd_write_cmd(0x01);

  }

  /*显示屏字符串写入函数*/

  void lcd_write_str(unsigned char x,unsigned char y,unsigned char *s)

  {

  if (y == 0) {

  lcd_write_cmd(0x80 + x);

  }

  else {

  lcd_write_cmd(0xC0 + x);

  }

  while (*s) {

  lcd_write_data( *s);

  s ++;

  }

  /*

  unsigned char i;

  for(i=0;i《16&&s!=0;i++)

  {

  lcd_write_char(x+i,y,s);

  }

  */

  }

  void lcd_write_char(unsigned char x,unsigned char y, unsigned char d)

  {

  if(y==0)

  {

  lcd_write_cmd(0x80+x);

  }

  else

  {

  lcd_write_cmd(0xc0+x);

  }

  lcd_write_data(d);

  }

  //光标复位

  void lcd_reset_cursor(void)

  {

  lcd_write_cmd(0x02);

  }

  //设置显示模式

  void lcd_set_mode(unsigned char cursor, unsigned char text)

  {

  unsigned char mode="0x04";

  if(cursor){//光标右移

  mode|=0x02;

  }

  else{//光标左移

  mode|=0x00;

  }

  if(text){//文字移动

  mode|=0x01;

  }

  else{//文字不移动

  mode|=0;

  }

  lcd_write_cmd(mode);

  }

  void lcd_init(void)

  {

  P0=0;

  LCDE="1";

  delay_ms(500);

  lcd_clear();

  lcd_write_cmd(0x38);//设置lcd功能:8位总线,双行显示,5X7点阵字符

  lcd_write_cmd(0x0f);//显示开关控制:显示ON,光标ON,闪烁ON

  lcd_write_cmd(0x06);//光标输入方式增量移位

  lcd_write_cmd(0x80);

  //lcd_write_cmd(0x0c);

  //lcd_clear();

  }

  //读取状态,是否忙

  unsigned char lcd_status(void)

  {

  unsigned char tmp="0";

  P0=0xff;

  LCDRS="0";

  LCDRW="1";

  LCDE="0";

  _nop_();

  LCDE="1";

  //_nop_();

  tmp="P0";

  return tmp;

  }

  //FileName:test.c

  /*******************************

  ** Start51 study board test software

  ** test 1602LCD

  ** author: bluehacker《kernelpaniccrash AT gmail.com》

  ** QQ:282074921

  ***********************************/

  #include "lcd.h"

  #include "delay.h"

  #include "intrins.h"

  void main(void)

  {

  //初始化串口

  SCON="0x50";/*mode 1,1 start bit ,8 data bit ,1 stop bit,enable receive*/

  PCON="0x80";/*SMOD=1, Baud Rate twice*/

  TMOD="0x21";/*timer 0--mode 1 and timer 1 ---mode2*/

  //set baud rate,use timer 1 as baud rate generator

  TH1=BAUD_19200;

  TL1=TH1;

  TR1=TRUE;

  ET1=FALSE;//disable timer 1 interrupt

  EA="0";

  //delay_ms(400);

  lcd_init();

  // lcd_set_mode(1,0);

  //lcd_reset_cursor();

  // lcd_write_char(3,1,‘c’);

  lcd_write_str(2,0,"bluehacker");

  lcd_write_str(2,1,"QQ:282074921");

  while(1)

  {

  }

  }

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top