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

📄 lcd1602.c

📁 LCD1602B的最详细的4线驱动方法.内部包含CGRAM的操作.汉字已经自定义图像的显示.闪烁显示等等.
💻 C
字号:
//---------------------------------------
//目标芯片:	ATmega16L
//芯片时钟:	11059200Hz 
 
//LCD引脚定义 
//1---GND  
//2---VCC 
//3---VO 
//4---RS 
//5---RW 
//6---EN 
//7到14--D0-D7 
//15--背景灯+ 
//16--背景灯- 

//下面是AVR与LCD连接信息 
//PA2 ->RS 
//PA3 ->EN 
//GND ->RW 
//PA4 ->D4 
//PA5 ->D5 
//PA6 ->D6 
//PA7 ->D7
//---------------------------------------
#include "LCD1602.h"

void LCD1602Init(void)			//液晶初始化 
{ 
	LCD_DATA_DDR|=LCD_DATA; 	//数据口方向为输出 
	LCD_EN_DDR|=LCD_EN;     	//设置EN方向为输出 
	LCD_RS_DDR|=LCD_RS;     	//设置RS方向为输出
	LCD1602Write(0,0x28);		//4位显示 
	LCD1602Write(0,0x0c);		//显示开
	LCD1602Write(0,0x01);		//清屏 
	_delay_ms(5);
} 

void LCD1602WriteEN(void)  		//液晶使能 
{ 
	LCD_EN_PORT|=LCD_EN; 
	_delay_us(20);
	LCD_EN_PORT&=~LCD_EN; 
} 

void LCD1602Write(unsigned char loworhigh,unsigned char commandordata) //写指令(low)或数据(high)
{ 
	_delay_us(20);
	if(!loworhigh)LCD_RS_PORT&=~LCD_RS;		//RS=0 
	else LCD_RS_PORT|=LCD_RS;        		//RS=1
	LCD_DATA_PORT&=0X0f;					//清高四位 
	LCD_DATA_PORT|=commandordata&0xf0;		//写高四位 
	LCD1602WriteEN(); 
	commandordata<<=4;						//低四位移到高四位 
	LCD_DATA_PORT&=0x0f;					//清高四位 
	LCD_DATA_PORT|=commandordata;			//写低四位 
	LCD1602WriteEN(); 
} 

void LCD1602SetXY(unsigned char x,unsigned char y)  //写地址函数 
{ 
    unsigned char address=(y==0)?(0x80+x):(0xc0+x);  
    LCD1602Write(0,address); 
} 
   
void LCD1602WriteString(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1 
{ 
    LCD1602SetXY(X,Y); //写地址     
    while(*s)LCD1602Write(1,*s++); // 写显示字符 
} 

void LCD1602WriteChar(unsigned char X,unsigned char Y,unsigned char data) //列x=0~15,行y=0,1 
{	//此时的data代表的是序号例如111代表的是"o";112代表的是"p";
	LCD1602SetXY(X,Y); //写地址 
	LCD1602Write(1,data); 
}

void LCD1602WriteCGRAM(unsigned char num,unsigned char *ptr)
{	//num:0~7组CGRAM	*ptr指向待写5*8数组
	unsigned char i;
	LCD1602Write(0,(0x40+(num<<3)));
	for(i=0;i<8;i++)
	{
		LCD1602Write(1,*(ptr+i));
	}
}


int main(void) 
{ 
	unsigned char i;
	unsigned char c1[8] = {0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02};	//年 
    unsigned char c2[8] = {0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11};	//月 
    unsigned char c3[8] = {0x0f,0x09,0x09,0x09,0x0f,0x09,0x09,0x0f};	//日 
	unsigned char c4[8] = {0x80,0x30,0x48,0x40,0x40,0x40,0x48,0x30};	//℃
	
	LCD1602Init();

	LCD1602WriteString(0,0,(unsigned char *)"^_^..I just made");
	LCD1602WriteString(0,1,(unsigned char *)"it at last.     "); 
	for(i=0;i<5;i++)_delay_ms(200);
	LCD1602WriteString(0,0,(unsigned char *)"Our AVR!Come on.");
	LCD1602WriteString(0,1,(unsigned char *)"Mingjun.Lu!     ");
	for(i=0;i<5;i++)_delay_ms(200);
	//LCD1602Clear();

	LCD1602WriteString(0,0,(unsigned char *)"Time:2009 03 07 ");
	LCD1602WriteString(0,1,(unsigned char *)"Temperature: 20 ");
	LCD1602WriteCGRAM(0,c1);
	LCD1602WriteChar(9,0,0);
	LCD1602WriteCGRAM(1,c2);
	LCD1602WriteChar(12,0,1);
	LCD1602WriteCGRAM(2,c3);
	LCD1602WriteChar(15,0,2);
	
	LCD1602Write(0,(0x40+(3<<3)));
	for(i=0;i<8;i++)LCD1602Write(1,*(c4+i)>>3);
	LCD1602WriteChar(15,1,3);

	while(1);
} 

⌨️ 快捷键说明

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