29 LCD1602 BY GPT

#include <reg51.h>
#include <intrins.h>
#include <stdio.h>

#define LCD1602_DB P0 //LCD1602数据线

sbit LCD1602_RS = P3^5; //LCD1602 RS引脚
sbit LCD1602_RW = P3^6; //LCD1602 RW引脚
sbit LCD1602_EN = P3^4; //LCD1602 EN引脚

void delay(unsigned int t) //延时函数
{
    unsigned int i;
    while(t--)
    {
        for(i=0;i<125;i++);
    }
}

void lcd1602_write_cmd(unsigned char cmd) //LCD1602写命令
{
    LCD1602_RS = 0;
    LCD1602_RW = 0;
    LCD1602_DB = cmd;
    LCD1602_EN = 1;
    _nop_();
    _nop_();
    _nop_();
    LCD1602_EN = 0;
    delay(5);
}

void lcd1602_write_data(unsigned char dat) //LCD1602写数据
{
    LCD1602_RS = 1;
    LCD1602_RW = 0;
    LCD1602_DB = dat;
    LCD1602_EN = 1;
    _nop_();
    _nop_();
    _nop_();
    LCD1602_EN = 0;
    delay(5);
}

void lcd1602_init() //LCD1602初始化
{
    lcd1602_write_cmd(0x38); //设置16*2显示,5*7点阵,8位数据接口
      lcd1602_write_cmd(0x38); //设置16*2显示,5*7点阵,8位数据接口
    lcd1602_write_cmd(0x0c); //显示开,无光标,不闪烁
    lcd1602_write_cmd(0x06); //光标右移,字符不移动
    lcd1602_write_cmd(0x01); //清屏
}

void lcd1602_display(unsigned char x, unsigned char y, unsigned char *str) //LCD1602显示字符串
{
    unsigned char addr;
    if(y == 0)
    {
        addr = 0x80 + x;
    }
    else
    {
        addr = 0xc0 + x;
    }
    lcd1602_write_cmd(addr);
    while(*str != '\0')
    {
        lcd1602_write_data(*str++);
    }
}

void main()
{
    unsigned int a=100;
    unsigned char str[4];
    
    lcd1602_init();
    lcd1602_display(2, 0, "Hello, World!");
      lcd1602_display(2, 1, "Good Moring!");
         
     
    while(1);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值