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

  • Post author:
  • Post category:其他


#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);  //开启图形模式显示
}



版权声明:本文为weixin_47454452原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。