LCD1602驱动

/*****************************************************
                液晶屏lcd1602驱动
                未定义各I/0口(RS,RW,E,DB),需头文件外定义
******************************************************/

#ifndef    _LCD1602_H_
#define    _LCD1602_H_    1
#define    LCD1602_ROW    2     //最大显示的行数
#define    LCD1602_COL    16    //最大显示的列数

/*
入口参数:time:延时长度
出口参数:无
函数功能:非精确延时
*/
void lcd1602_delay(unsigned int time)
{
    unsigned int    i;
    
    for(i = 0; i < time; i++)
        ;
}


/*
入口参数:无
出口参数:无
函数功能:检测忙状态
*/
void lcd1602_busy(void)
{
    DB = 0x80;
    RS = 0;
    RW = 1;
    E = 1;
    while(DB & 0x80)
        ;
    E = 0;   
}

/*
入口参数:combuf:写入的指令;busy:是否检测忙状态(1检测,0不检测)
出口参数:无
函数功能:写入一个指令
*/
void lcd1602_write_com(unsigned char combuf, bit busy)
{
    if(busy)
        lcd1602_busy();
    RS = 0;
    RW = 0;
    DB = combuf;
    E = 1;
    E = 0;
}

/*
入口参数:databuf:写入的数据
出口参数:无
函数功能:写入一个数据
*/
void lcd1602_write_data(unsigned char databuf)
{
    lcd1602_busy();
    RS = 1;
    RW = 0;
    DB = databuf;
    E = 1;
    E = 0;
}

/*
入口参数:x,y:写入地址的坐标
出口参数:无
函数功能:写入地址
*/
void lcd1602_write_address(unsigned char x, unsigned char y)
{
    x &= 0x0f;   //限定x在0~15
    y &= 0x01;   //限定x在0~1
    
    if(y == 0)
        lcd1602_write_com(0x80 + x, 1);
    else
        lcd1602_write_com(0x80 + 0x40 + x, 1);
}

/*
入口参数:无
出口参数:无
函数功能:初始化lcd1602
*/
void lcd1602_initial(void)
{
    lcd1602_delay(1500);
    lcd1602_write_com(0x38, 0);
    lcd1602_delay(500);
    lcd1602_write_com(0x38, 0);
    lcd1602_delay(500);
    lcd1602_write_com(0x38, 0);
    lcd1602_write_com(0x38, 1);
    lcd1602_write_com(0x08, 1);
    lcd1602_write_com(0x01, 1);
    lcd1602_write_com(0x06, 1);
    lcd1602_write_com(0x0C, 1);    
}

/*
入口参数:x,y:显示字符的坐标; databuf:显示的字符
出口参数:无
函数功能:指定位置显示一个字符
*/
void lcd1602_putchar(unsigned char x, unsigned char y, const unsigned char databuf)
{
    lcd1602_write_address(x, y);
    lcd1602_write_data(databuf);
}

/*
入口参数:x,y:显示字符串的起始位置; databuf:显示的字符串
出口参数:count: 成功显示的字符个数
函数功能:显示一个字符串(从起始位置开始输出,会替换起始
         位置后所有已显示的字符,输出过程中会自动换行,
         若超出显示位最后一位(16,1),自动转到起始位(0,0)
         继续输出),并返回成功输出的个数
*/
unsigned char lcd1602_puts(unsigned char x, unsigned char y, const unsigned char databuf[])
{
    unsigned char    count    = 0;
    unsigned char    i;
    
    for(i = 0; databuf[i] != '\0'; i++)
    {
        lcd1602_putchar(x, y, databuf[i]);
        count++;
        
        if(++x >= LCD1602_COL)
        {
            x = 0;
            if(++y >= LCD1602_ROW)
                y = 0;
        }
    }
    
    return count;
}

#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值