⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd1602.c

📁 包括力天电子MINI 16板的各项程序
💻 C
字号:
/*1602B驱动库函数
在主程序中加入: #include<stdio.h> 
语句:
static int uart_putchar(unsigned char c,FILE *stream) ;
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,_FDEV_SETUP_WRITE);
即可使用printf()实现标准格式输出
Redstone编写-2005.11.8
翟宁改写-2008-07-06
*/
#include <avr/io.h>
#include "lcd1602.h"

void Delay10uS(void)
{
	unsigned char i;
	for(i=80;i>0;i--)
	{
		asm("nop");
	}
}
void Delay1mS(void)
{
	unsigned char i,j;
	for(i=20;i>0;i--)
	{
		for(j=199;j>0;j--)
		{
			asm("nop");
		}
	}
}
void DelayMS(unsigned int time)
{
	unsigned char i,j;
	for(;time>0;time--)
	{
		for(i=20;i>0;i--)
		{
			for(j=199;j>0;j--)
			{
				asm("nop");
			}
		}
	}
}
/********************************************************************
函 数 名: Lcd_Init
功能描述: 液晶初始化函数
函数说明: 液晶被初始化为8位数据端口,2行显示,5*7点阵
调用函数: DelayMS,LCD_write_char
全局变量: 无
输    入: 无
返    回: 无
**********************************************************************/
void LCD_Init(void)
{
	LCD_DATA_DDR=0xff;//设置液晶数据口为输出
	LCD_RS_DDR |=  _BV(LCD_RS_PIN);       //RS引脚设为输出
	LCD_RW_DDR |=  _BV(LCD_RW_PIN);       //RW引脚设为输出
	LCD_EN_DDR |=  _BV(LCD_EN_PIN);       //EN引脚设为输出
	
    unsigned char i;    // 用于3次设置显示模式
    DelayMS(4);            // 等待LCD进入工作状态
    for (i=0; i<3; i++)
    {
        // 3次设置显示模式,不检测忙信号
        LCD_write_char(LCD_FUNCTION_SET3,0);//8位数据端口,2行显示,5*7点阵
		Delay1mS();
    }

    // 设置显示模式,开始要求每次检测忙信号

    LCD_write_char(LCD_FUNCTION_SET3,0);    // 8位数据端口,2行显示,5*7点阵
    DelayMS(1);
	LCD_write_char(LCD_DISPLAY_ON,0);// 关闭显示
    DelayMS(1);
	LCD_write_char(LCD_CLEAR_DISPLAY,0);// 清屏
    DelayMS(1);
	LCD_write_char(LCE_MODE_SET3,0);// 光标右移一格,AC值加1,字符全部不动
    DelayMS(1);
	LCD_write_char(LCD_CURSOR_SET3,0);// 光标会出现在地址计数器所指的位置,光标不出现
    DelayMS(1);
	
}

/********************************************************************
函 数 名: LCD_set_xy
功能描述: 设置显示的位置
函数说明: 无
调用函数: LCD_write_char
全局变量: 无
输    入: x,y(设置显示位置坐标)
返    回: 无
**********************************************************************/
/*void LCD_set_xy( unsigned char x, unsigned char y )  
  {
    unsigned char address;
    if (y == 0) 
		address = 0x80 + x;
    else 
       address = 0xc0 + x;
    LCD_write_char( address, 0 );
  }*/

/********************************************************************
函 数 名: LCD_write_char
功能描述: 写入LCD命令或数据函数,时序参照LCD1602的Datasheet
函数说明: 当命令变量(LCD_Command)为0时写入数据,否则写入命令
调用函数: 无
全局变量: 无
输    入: LCD_Command(欲写入的命令),LCD_Data(欲写入的数据)
返    回: 无
**********************************************************************/
void LCD_write_char(unsigned char LCD_Command,unsigned char LCD_Data)
{
	LCD_DATA_DDR=0xff;//设置液晶数据口为输出
    /*if (busy)    // 是否判忙
    {
        ReadStatusLcd();
    }*/
	LCD_EN_LOW();
	if(LCD_Command==0)
	{
		LCD_RS_HIGH();
		LCD_RW_LOW();
		Delay10uS();
		LCD_EN_HIGH();
		Delay10uS();
		LCD_DATA_PORT=LCD_Data;//写入数据
		Delay10uS();
		LCD_EN_LOW();
		Delay10uS();
		LCD_RW_HIGH();
		Delay10uS();
	}
	else
	{
		LCD_RS_LOW();
		LCD_RW_LOW();
		
		Delay10uS();
		LCD_EN_HIGH();
		Delay10uS();
		LCD_DATA_PORT=LCD_Command;//写入命令
		Delay10uS();
		LCD_EN_LOW();
		Delay10uS();
		LCD_RW_HIGH();
		Delay10uS();
	}
}
/********************************************************************
函 数 名: DisplayOneChar
功能描述: LCD1602显示一个字节函数
函数说明: 无
调用函数: 无
全局变量: 无
输    入: XStart(显示位置X),YStart(显示位置Y),DData(显示的内容)
返    回: 无
**********************************************************************/
void DisplayOneChar(unsigned char XStart, unsigned char YStart, unsigned char DData)
{
    // 限制x不能大于16,y不能大于1
    YStart &= 0x01;    // Y的变化范围0 ~ 1
    XStart &= 0x0f;    // X的变化范围0 ~ 16

    if (YStart != 0)    // 若Y=1,显示第二行
    {
        // 当要显示第2行时地址码+40h
        XStart |= 0x40;
    }
    // 指令码为地址+0x80
    XStart |= DD_RAM;

	LCD_write_char(XStart,0);// 不检测忙信号,发送地址码
    DelayMS(1);
	LCD_write_char(0,DData);
    DelayMS(1);
}
/********************************************************************
函 数 名: DisplayListChar
功能描述: LCD1602显示字符串函数
函数说明: 无
调用函数: 无
全局变量: 无
输    入: XStart(显示位置X),YStart(显示位置Y),pData(显示字符串指针)
返    回: 无
**********************************************************************/
void DisplayListChar(unsigned char XStart, unsigned char YStart,char *pData)
{
    unsigned char ListLength = 0;    // 字符串长度

    // 限制x不能大于16,y不能大于1
    YStart &= 0x01;
    XStart &= 0x0f;

    // XStart坐标应小于16并且若到达字串尾则退出
    while (XStart <= 0x0f)
    {
        if (pData[ListLength] == '\0')
        {
            return;
        }
        // 显示单个字符
        DisplayOneChar(XStart, YStart, pData[ListLength]);
        ListLength++;
        XStart++;
    }
	DDRB = 0x00;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -