51单片机第四讲(LCD1602)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hu_junhua/article/details/79221520

液晶屏需要按照芯片手册写程序:
直接上代码:

#define LCDPORT P0

sbit RS = P2^4;//定义的端口
sbit RW = P2^5;
sbit EN = P2^6;

unsigned char my[8] = {0x10, 0x06, 0x09, 0x08, 0x08, 0x09, 0x06, 0x00}; //自己定义的字符(摄氏度)


void bsp_Lcdwritedata(unsigned char dat)  //写数据
{
//  delay_ms(5);  第一种检查忙状态直接延时
    while(bsp_LcdCheckBusy() & 0x80);//通过忙函数检查
    RS = 1;
    RW = 0;
    LCDPORT = dat;
    EN = 1;
    delay_us(10);
    EN = 0;     //给一个高脉冲
}

void bsp_Lcdwritecomand(unsigned char com)   //写指令
{
//  delay_ms(5);
    while(bsp_LcdCheckBusy() & 0x80);
    RS = 0;
    RW = 0;
    LCDPORT = com;
    EN = 1;
    delay_us(10);
    EN = 0;
}    

void bsp_LcdInit(void)//LCD的初始化看着芯片手册写
{
    delay_ms(15);
    bsp_Lcdwritecomand(0x38);
    delay_ms(5);
    bsp_Lcdwritecomand(0x38);
    delay_ms(5);
    bsp_Lcdwritecomand(0x38);
    delay_ms(5);
    bsp_Lcdwritecomand(0x38);
    delay_ms(5);
    bsp_Lcdwritecomand(0x08);
    delay_ms(5);
    bsp_Lcdwritecomand(0x01);
    delay_ms(5);
    bsp_Lcdwritecomand(0x06);
    delay_ms(5);
    bsp_Lcdwritecomand(0x0c);
    delay_ms(5);     //最后每写一个指令延时一会
}

void bsp_Showchar(unsigned char x, unsigned char y, unsigned char a)//在任意位置显示一个字符,x代表行,y代表列
{
    if(x == 0)
        bsp_Lcdwritecomand(0x80 + y);//0x80是第一行的第一列的起始地址
    else
        bsp_Lcdwritecomand(0xc0 + y);//0xc0是第二行的第一列的起始地址

    bsp_Lcdwritedata(a);
}

void bsp_Showstring(unsigned char x, unsigned char y, unsigned char *s)//在任意位置显示一个字符串
{
    if(x == 0)
        bsp_Lcdwritecomand(0x80 + y);
    else
        bsp_Lcdwritecomand(0xc0 + y);
    while(*s)
    {
        bsp_Lcdwritedata(*s);
        s++;
    }
}

unsigned char bsp_LcdCheckBusy(void)//检查忙函数
{
    unsigned char temp = 0;
    RS = 0;
    RW = 1;
    EN = 1;
    delay_us(200);
    temp = LCDPORT;

    EN = 0;

    return temp;
}

void bsp_LcdFillCGRAM(void)     //自定义填充
{
    unsigned char i = 0;
    bsp_Lcdwritecomand(0x40);
    for(i = 0; i < 8; i++)
    {
         bsp_Lcdwritedata(my[i]);
    }
}

void bsp_LcdShowCGRAM(unsigned char x, unsigned char y, unsigned char index)//在任意位置显示自定义的字符
{
    if(x == 0)
        bsp_Lcdwritecomand(0x80 + y);
    else
        bsp_Lcdwritecomand(0xc0 + y);
    bsp_Lcdwritedata(index);
}

一个很重要的函数

#include <stdio.h>

unsigned char string[16] = {0};

sprintf(string, "time:%02d-%02d-%02d", hour, min, sec);//变量尽量定义成unsigned int 型,加一个02表示输出2个字符
bsp_Showstring(0, 0, string);

猜你喜欢

转载自blog.csdn.net/hu_junhua/article/details/79221520