zoukankan      html  css  js  c++  java
  • 8051单片机的LCD1602使用

    LCD1602简介

    LCD:液晶显示器。

    1602:16个字符×2行。

    首先来看一下LCD1602在proteus8中的模型,在proteus8中,它叫做LM016L。我们从仿真元器件上可以看到它和实物图的区别,带有背光的LCD实物图上有16个引脚,分别是15号引脚BLA(正),16号引脚BLK(负),仿真元件省去了15,16引脚。




    仿真电路图如下:


    需要说明的是,3好引脚被称作是液晶驱动电压,作用是调整对比度,可以通过电位器改变阻值的大小来改变对比度。在此处,我将对比度设置为不可以调节的。(直接接一个5KΩ的不可变电阻)我使用了P3.4,P3.5,P3.6作为控制信号来操作1602。

    对外部硬件的操作需要知道外部设备的时序图才能操作。1602的速度对于8051而言是较慢的(12MHZ)。属于慢速设备,因此对于它的读写需要等待一会时间才可以进行。(其实从上面的指令表可以看出LCD1602提供了读操作的,但是我去读1602的忙信号总是没作用。由于我使用了P1口,所以读之前需要给P1口写高电平,即使这么做了,还是读不到1602)所以我选择等待一会儿再去给1602写。具体代码如下:

    #include<reg52.h>
    #include<string.h>
    #include<intrins.h>
    #define uchar unsigned char
    #define uint unsigned int
    sbit RS = P3^5;
    sbit EN = P3^4;
    sbit RW = P3^6;
    uchar code table[] = {"hello world!"};
    void delay1ms(uint time);//延时N毫秒
    void init_1602(void);	 //初始化1602
    void writecmd_1602(uint cmd);	//写指令函数
    void writedata_1602(uchar dat);	//写数据函数
    void display(uchar num);	//显示函数
    
    int main()
    {
    	delay1ms(10);
    	init_1602();			//初始化1602
    	delay1ms(5); 
    	writecmd_1602(0x82);	//设置显示的起始位置
    	delay1ms(5);
    	display(strlen(table));
    	while(1);
    	return 0;
    }	
    void delay1ms(uint time)   	
    {
        unsigned char a,b,i;
    	for(i = time; i != 0;i--)
    		for(b=199;b>0;b--)
    			for(a=1;a>0;a--);
    }
    void init_1602(void)
    {
    	delay1ms(15);	//延时15ms
    	writecmd_1602(0x38);	//模式设置
    
    	writecmd_1602(0x0c);	//显示设置
    	
    	writecmd_1602(0x06);	//显示模式
    	
    	writecmd_1602(0x01);	//清屏
    }
    void writecmd_1602(uint cmd)
    {
    	RS = 0;
    	RW = 0;
    	P1 = cmd;
    	EN = 1;
    	delay1ms(1);
    	EN = 0;
    }
    void writedata_1602(uchar dat)
    {
    	RS = 1;
    	RW = 0;
    	P1 = dat;
    	EN = 1;
    	delay1ms(1);
    	EN = 0;
    }
    void display(uchar num)
    {
    	uchar i;
    	for(i = 0; i < num; i++)
    	{
    		writedata_1602(table[i]);
    	}
    	delay1ms(5);
    }

    仿真结果如下:


    我们看到了LCD1602显示了hello world!这个字符串。关于这点是因为1602的内部有标准字符库存在,它是符合ASCII码的。所以可以直接识别,进行输出。但是对于汉字则是需要自己自定义字符库,最多可以自定义8个字符。但是我看了看网上的显示效果,并不怎么好。不建议显示中文。

  • 相关阅读:
    Python-Matplotlib 12 多图figure
    Python-Matplotlib 11 子图-subplot
    Python Day16
    Python Day15
    Python Day13-14
    Python Day12
    Python Day11
    Python Day9-10
    Python Day8
    Python Day8
  • 原文地址:https://www.cnblogs.com/zy666/p/10504316.html
Copyright © 2011-2022 走看看