51单片机外设LCD12864显示字符串

#include <reg52.h>

typedef  signed    char    int8;    // 8位有符号整型数
typedef  signed    int     int16;   //16位有符号整型数
typedef  signed    long    int32;   //32位有符号整型数
typedef  unsigned  char    uint8;   // 8位无符号整型数
typedef  unsigned  int     uint16;  //16位无符号整型数
typedef  unsigned  long    uint32;  //32位无符号整型数

#define LCD1602_DB  P0   //1602液晶数据端口
sbit LCD1602_RS = P1^0;  //1602液晶指令/数据选择引脚
sbit LCD1602_RW = P1^1;  //1602液晶读写引脚
sbit LCD1602_E  = P1^5;  //1602液晶使能引脚

#define LCD12864_DB   LCD1602_DB
#define LCD12864_RS   LCD1602_RS
#define LCD12864_RW   LCD1602_RW
#define LCD12864_E    LCD1602_E

void LcdShowString(uint8 x, uint8 y, uint8 *str);
void InitLcd12864();
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);


void main()
{
	unsigned char str[] = "Kingst Studio";

	InitLcd12864();
	LcdShowStr(16, 32, "金沙滩工作室");
    LcdShowStr(0, 48, " www.kingst.org ");
	while(1);		
}

/* 等待液晶准备好 */
void LcdWaitReady()
{
    uint8 sta;
    
    LCD12864_DB = 0xFF;
    LCD12864_RS = 0;
    LCD12864_RW = 1;
    do {
        LCD12864_E = 1;
        sta = LCD12864_DB;
        LCD12864_E = 0;
    } while (sta & 0x80); //bit7等于1表示液晶正忙,重复检测直到其等于0为止
}


/* 向液晶写入一字节命令,cmd-待写入命令值 */
void LcdWriteCmd(uint8 cmd)
{
    LcdWaitReady();
    LCD12864_RS = 0;
    LCD12864_RW = 0;
    LCD12864_DB = cmd;
    LCD12864_E  = 1;
    LCD12864_E  = 0;
}

void LcdWriteDat(uint8 dat)
{
    LcdWaitReady();
    LCD12864_RS = 1;
    LCD12864_RW = 0;
    LCD12864_DB = dat;
    LCD12864_E  = 1;
    LCD12864_E  = 0;
}
/*
void LcdShowString(uint8 x, uint8 y, uint8 *str)
{
    uint8 addr;
    
    //由输入的显示坐标计算DDRAM的地址
    x >>= 4;
    y >>= 4;
    if (y >= 2)
    {
        y -= 2;
        x += 8;
    }
    addr = y*16 + x;
    //由起始DDRAM地址连续写入字符串
    LcdWriteCmd(0x30); //启动DDRAM操作
    LcdWriteCmd(0x80|addr);
    while (*str != '\0')
    {
        LcdWriteDat(*str);
        str++;
    }
}
 */



void LcdSetCursor(unsigned char x, unsigned char y)
{
	uint8 addr;
    
    //由输入的显示坐标计算DDRAM的地址
    x >>= 4;
    y >>= 4;
    if (y >= 2)
    {
        y -= 2;
        x += 8;
    }
    addr = y*16 + x;
    //由起始DDRAM地址连续写入字符串
    LcdWriteCmd(0x30); //启动DDRAM操作
    LcdWriteCmd(0x80|addr);
}

void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
	LcdSetCursor(x, y);
	while (*str != '\0')
    {
        LcdWriteDat(*str);
        str++;
    }
}

void InitLcd12864()
{
    uint8 x, y;

    //字符模式初始化
    LcdWriteCmd(0x30);  //基本指令集
    LcdWriteCmd(0x01);  //清零字符显示
    LcdWriteCmd(0x02);  //地址归位
    LcdWriteCmd(0x0C);  //开显示
    
    //图形模式初始化
    LcdWriteCmd(0x34);    //启动扩展指令,关闭图形显示
    for (y=0; y<32; y++)  //清零图形显示缓冲区
    {
        LcdWriteCmd(0x80|y);
        LcdWriteCmd(0x80|0);
        for (x=0; x<32; x++)
        {
            LcdWriteDat(0x00);
        }
    }
    LcdWriteCmd(0x36);  //开启图形模式显示
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值