基于51单片机的Lcd1602屏驱动程序源码

/******************************************************************************/
// 包含头文件
/******************************************************************************/
#include "config.h"
#include "delay.h"
#include "lcd1602.h"


/******************************************************************************/
// 函数名称:Lcd1602_Custom_Char 
// 输入参数:row-行 col-列 pos-CGRAM位置 *customValueTable-自定义字符数据
// 输出参数:无 
// 函数功能:显示自定义字符
/******************************************************************************/
void Lcd1602_Custom_Char(uint8 row, uint8 col, uint8 pos, uint8 *customValueTable){
    uint8 i = 0;
    for(i=0;i<8;i++){
        Lcd1602_Write_Cmd(0x40+pos*8+i);
        Lcd1602_Write_Data(*(customValueTable+i));
    }
    if(row==1){
        Lcd1602_Write_Cmd(0x00+0x80+col-1);
        Lcd1602_Write_Data(0x00+pos);
    }else{
        Lcd1602_Write_Cmd(0x40+0x80+col-1);
        Lcd1602_Write_Data(0x00+pos);    
    }
}


/******************************************************************************/
// 函数名称:Lcd1602_Show_String 
// 输入参数:row-行 col-列 *str-字符
// 输出参数:无 
// 函数功能:LCD1602显示字符
/******************************************************************************/
void Lcd1602_Show_String(uint8 row, uint8 col,uint8 *str){
    uint8 *defaultValue = "parameter error" ;
    switch(row){
        case 1:
                Lcd1602_Write_Cmd(0x00+0x80+col-1);//第一行第y个空格
                while(*str!='\0'){
                    Lcd1602_Write_Data(*str);
                    str++;
                }
                break;    
        case 2:
                Lcd1602_Write_Cmd(0x40+0x80+col-1);//第二行第y个空格
                while(*str!='\0'){
                    Lcd1602_Write_Data(*str);
                    str++;
                }
                break;

       default:
                   Lcd1602_Write_Cmd(0x00+0x80);//parameter error
                while(*defaultValue!='\0'){
                    Lcd1602_Write_Data(*defaultValue);
                    defaultValue++;
                }                    
    }    
}


/******************************************************************************/
// 函数名称:Lcd_Busy_Check 
// 输入参数:无
// 输出参数:无 
// 函数功能:LCD1602忙信号检查 
/******************************************************************************/
void Lcd1602_Busy_Check(void){
    uint8 tmp;
    lcdPort = 0xff;//一开始的时候,lcdPort设置成输入状态

    do{
        RS = 0;
        RW = 1;
        EN = 0;
        EN = 1;
        _nop_();
        _nop_();//延时CP
        tmp = lcdPort;
        _nop_();//延时PQ
        EN = 0;
        _nop_();
    }while(tmp&0x80);    
}


/******************************************************************************/
// 函数名称:Lcd_Init 
// 输入参数:无
// 输出参数:无 
// 函数功能:LCD1602初始化
/******************************************************************************/
void Lcd1602_Init(void){
    Delay15ms();
    Lcd1602_Write_Cmd(0x38);//写指令38H
    Delay5ms();    
    Lcd1602_Write_Cmd(0x38);//显示模式设置    
    Lcd1602_Write_Cmd(0x08);//显示关闭    
    Lcd1602_Write_Cmd(0x01);//显示清屏    
    Lcd1602_Write_Cmd(0x06);//显示光标移动设置    
    Lcd1602_Write_Cmd(0x0c);//显示开及光标设置
}


/******************************************************************************/
// 函数名称:Lcd_Write_Cmd 
// 输入参数:myCmd-指令
// 输出参数:无 
// 函数功能:LCD1602写指令
/******************************************************************************/
void Lcd1602_Write_Cmd(uint8 cmd){
    Lcd1602_Busy_Check();
    RS = 0;
    RW = 0;
    EN = 0;
    //delay AB 没有说明,取消延时
    lcdPort = cmd;
    _nop_();//delay BC 延时1us
    EN = 1;
    _nop_();//delay CD 延时1us
    EN = 0;
    _nop_();//delay DE 延时1us
    _nop_();//delay EF 延时1us
}


/******************************************************************************/
// 函数名称:Lcd_Write_Data 
// 输入参数:myData-数据
// 输出参数:无 
// 函数功能:LCD1602写数据
/******************************************************************************/
void Lcd1602_Write_Data(uint8 Data){
    Lcd1602_Busy_Check();
    RS = 1;
    RW = 0;
    EN = 0;
    //delay AB 没有说明,取消延时
    lcdPort = Data;
    _nop_();//delay BC 延时1us
    EN = 1;
    _nop_();//delay CD 延时1us
    EN = 0;
    _nop_();//delay DE 延时1us
    _nop_();//delay EF 延时1us
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GJZGRB

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值