我正在研究一个4位液晶显示器与8051微控制器的接口,但到目前为止我还没有成功。我有这个样本代码

/*
developed by S4silver
license type - open source
created on - 11/12/2014
modified on - 11/14/2014
modification details - refactored
developer - Frank Hust Jincson
contact mail id - [email protected]
web link - http://s4silver.blogspot.in/p/programmers-colony.html
*/

#include<reg51.h>

//#include"delay1s.h"

void Init_LCD(void);
void cmd_LCD(char cmd);
void display_LCD(char );
void write_LCD(char );

#define LCD P0

sbit RS=P0^0;
sbit RW=P0^1;
sbit EN=P0^2;

void delay()
{
    int i;
    TMOD=0X01;

    for(i=0;i<=16;i++)
    {
        TR0=1;
        while(!TF0);
        TF0=0;
        TR0=0;
    }
}

main()
{
    Init_LCD();
    display_LCD('A');
}

void Init_LCD()
{
    cmd_LCD(0x28);
    cmd_LCD(0x0e);
    cmd_LCD(0x01);
    cmd_LCD(0x06);
    cmd_LCD(0x80);
}

void cmd_LCD(char cmd)
{
    LCD=(cmd&0xf0)|0x04; //0x20,en=1,rs=0
    delay();
    EN=0;
    LCD=(cmd<<4)|0x04;  //0x80,en=1,rs=0
    delay();
    EN=0;
}

void display_LCD(char c)
{
    LCD=(c&0xf0)|0x05; //en=1,rs=1
    delay();
    EN=0;
    LCD=(c<<4)|0x05; //en=1,rs=1
    delay();
    EN=0;
}

但这对我不起作用,我也不知道怎么了。

最佳答案

我使用了下面的C语言代码

#include<reg51.h>
#define lcdport P2
sbit rs=P3^0;
sbit rw=P3^1;
sbit en=P3^2;
void lcdcmd(char);
void lcdint();
void lcddata(char);
void lcdstring(char *);
void delay(unsigned int);

void main()
{
lcdport=0x00;               // output port
 while(1)
  {
   lcdint();
   delay(500);
   lcdstring("hello welcome to");
   lcdcmd(0xc0);
   delay(500000);
   lcdstring("projectguru.net");
   delay(500000);
   }
}
void delay(unsigned int x)
{
 unsigned int i;
 for(i=0;i<x;i++);
}
void lcdint()               // lcd initializing
{
  lcdcmd(0x38);
  delay(500);
  lcdcmd(0x01);
  delay(500);
  lcdcmd(0x0c);
  delay(500);
  lcdcmd(0x80);
  delay(500);
  lcdcmd(0x0e);
  delay(500);
}
void lcdcmd(char value)        //  lcd command
{
  lcdport = value;
  rw=0;
  rs=0;
  en=1;
  delay(500);
  en=0;
}
void lcdstring(char *p)
{
  while(*p!='\0')
   {
     lcddata(*p);
     delay(2000);
     p++;
   }
}
void lcddata(char value)   // print data on lcd
{
  lcdport = value;
  rs=1;
  rw=0;
  en=1;
  delay(500);
  en=0;
}

这里提供完整的教程和视频http://www.mechaterrain.com/lcd-interfacing-8051

关于c - 8051微 Controller 接口(interface)4位LCD,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27846697/

10-08 22:14