51单片机LCD1602程序

学习LCD1602过程的一个入门程序,在proteus8.3验证通过

/* Main.c file generated by New Project wizard
 *
 * Created:   周三 6月 28 2017
 * Processor: AT89C52
 * Compiler:  Keil for 8051
 */

#include <stdio.h>
#include <reg52.h>
#include <string.h>
typedef unsigned char uchar;
typedef unsigned int uint;

sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
sbit BTN = P3^3;
//判断液晶忙,如果忙则等待,因为1602也是一个CPU,要处理原来的指令,如果不判断会导致数据紊乱
void Read_Busy() //读写检查函数
{
    uchar busy;
    P0 = 0xff;   //P0口作为数据端
    RS = 0;         
    RW = 1;             //读状态的操作时序为 RS=L,RW=H,E=H,D0~D7输出状态字
    do
    {
        EN = 1;
        busy = P0;
        EN = 0;
    }while(busy & 0x80); 
    //状态字为busy(8位2进制数)的最高位,
    //若为1则禁止读写,为0则允许读写,该状态用busy&0x80的结果表示
}


void Write_Cmd(uchar cmd)  //写指令函数
{
    Read_Busy();//对控制器每次进行读写操作都要判断是否正忙,即要进行读写检测
    RS = 0;
    RW = 0;
    P0 = cmd;   //写入十六进制形式的指令(command)
    EN = 1;     //写指令的操作时序:RS=0,RW=0,EN=高脉冲
    EN = 0;         //获得高脉冲后使能端重新置零
}


void Write_Dat(uchar dat)  //写入数据
{
    Read_Busy(); //写入数据前进行读写检测
    RS = 1;
    RW = 0;
    P0 = dat;    //P0口写入数据
    EN = 1;          //写数据操作时序:RS=0,RW=0,EN=高脉冲
    EN = 0;          //获得高脉冲后使能端重新置零
}
void LCD1602_Init()
{
    Write_Cmd(0x38);//设置16*2显示
    Write_Cmd(0x0f);//开显示 显示光标,光标闪烁
    Write_Cmd(0x01);//清屏

    Write_Cmd(0x06);//地址指针移位命令
    Write_Cmd(0x80 | 0x00);//显示地址,0x80是第一行的的首地址。0x80|0x06表示数据从第一行第7个字符位置开始显示
}

void PrintStr(char *str)
{

   char i,len;
   len = strlen(str); // 获取字符串长度
   for(i=0;i<len;i++)
   {
      Write_Dat(*str);
      str++;
   }
}
void main()
{
    char *str="hello123";
    LCD1602_Init();  // 初始化LCD1602

    Write_Dat('H');
    Write_Dat('e');
    Write_Dat('l');
    Write_Dat('l');
    Write_Dat('o');
   Write_Dat(' ');
   Write_Dat(' ');
   Write_Dat(' ');
   Write_Dat('2');


    Write_Cmd(0x80 |0x40| 0x01);    // 显示第二行
//  显示地址,0x80|0x40表示第二行,0x40是第二行的的首地址。也可以写成0x80+0x40或者0xc0,
//      0xc0|0x0c表示数据从第一行第13个字符位置开始显示
//  由于1602一行只显示16个字符,所以从第十三个字符位置显示的话只能显示4位

   PrintStr(str);

    while(1);
}

实验结果如下:
这里写图片描述

参考文章:http://blog.csdn.net/u013151320/article/details/46663167


2017/6/28更正

更正main函数显示LCD第二行的程序,之前初始化错了

添加PrintStr(char *str)函数,输出字符串
使用到了strlen函数,记得头文件要include “string.h”

void PrintStr(char *str)
{

   char i,len;
   len = strlen(str); // 获取字符串长度
   for(i=0;i<len;i++)
   {
      Write_Dat(*str);
      str++;
   }
}
  • 5
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是51单片机使用LCD1602显示的程序(基于C语言): ```c #include <reg51.h> #define LCD1602_DB P0 sbit LCD1602_RS = P2^6; sbit LCD1602_RW = P2^5; sbit LCD1602_EN = P2^7; void delay(unsigned int t) // 延时函数 { unsigned int i, j; for(i = 0; i < t; i++) for(j = 0; j < 120; j++); } void LCD1602_WriteCommand(unsigned char Command) // 写入指令 { LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_EN = 0; LCD1602_DB = Command; delay(5); LCD1602_EN = 1; delay(5); LCD1602_EN = 0; } void LCD1602_WriteData(unsigned char Data) // 写入数据 { LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_EN = 0; LCD1602_DB = Data; delay(5); LCD1602_EN = 1; delay(5); LCD1602_EN = 0; } void LCD1602_Init() // LCD1602初始化函数 { LCD1602_WriteCommand(0x38); // 8位数据接口,2行显示,5x7点阵字符 LCD1602_WriteCommand(0x0c); // 显示开,光标关,光标位置不显示 LCD1602_WriteCommand(0x06); // 光标加1,写入字符后移 LCD1602_WriteCommand(0x01); // 显示清屏 } void LCD1602_DisplayString(unsigned char x, unsigned char y, unsigned char *String) // 在指定位置显示字符串 { unsigned char i; if(x < 16) { if(y == 0) LCD1602_WriteCommand(0x80 + x); else if(y == 1) LCD1602_WriteCommand(0x80 + 0x40 + x); else return; } else return; for(i = 0; String[i] != '\0'; i++) LCD1602_WriteData(String[i]); } void main() { LCD1602_Init(); // 初始化LCD1602 LCD1602_DisplayString(0, 0, "Hello, world!"); // 在第一行第一列显示字符串 while(1); } ``` 以上程序实现了在LCD1602上显示字符串“Hello, world!”,并且可以通过修改`LCD1602_DisplayString`函数的参数来在其他位置显示其他字符串。如果需要显示其他内容,可以根据LCD1602的指令集自行编写相应的函数并调用即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值