基于MEGA16的LCD1602驱动程序(四位数据线)
扫描二维码
随时随地手机看文章
//编译器:ICC-AVRv6.31A
//目标芯片:M16
//时钟:8.0000MHz
/*-------------------------------------------------------------
LCD引脚定义
1---GND
2---VCC
3---VO
4---RS
5---RW
6---EN
7到14--D0-D7
15--背景灯+
16--背景灯-
-----------------------------------------------------------------*/
#include
#include
/*---------------------------------------------------------------
下面是AVR与LCD连接信息
PA2->RS
PA3->EN
地->RW
PA4->D4
PA5->D5
PA6->D6
PA7->D7
要使用本驱动,改变下面配置信息即可
-----------------------------------------------------------------*/
#defineLCD_EN_PORTPORTA//以下2个要设为同一个口
#defineLCD_EN_DDRDDRA
#defineLCD_RS_PORTPORTA//以下2个要设为同一个口
#defineLCD_RS_DDRDDRA
#defineLCD_DATA_PORTPORTA//以下3个要设为同一个口
#defineLCD_DATA_DDRDDRA//一定要用高4位
#defineLCD_DATA_PINPINA
#defineLCD_RS(1<
------------------------------------------------------------------------------*/
voidLCD_init(void);
voidLCD_en_write(void);
voidLCD_write_command(unsignedcharcommand);
voidLCD_write_data(unsignedchardata);
voidLCD_set_xy(unsignedcharx,unsignedchary);
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s);
voidLCD_write_char(unsignedcharX,unsignedcharY,unsignedchardata);
voiddelay_nus(unsignedintn);
voiddelay_nms(unsignedintn);
==============================================================================
#include"LCD.h"
voidLCD_init(void)//液晶初始化
{
LCD_DATA_DDR|=LCD_DATA;//数据口方向为输出
LCD_EN_DDR|=LCD_EN;//设置EN方向为输出
LCD_RS_DDR|=LCD_RS;//设置RS方向为输出
LCD_write_command(0x28);
LCD_en_write();
delay_nus(40);
LCD_write_command(0x28);//4位显示
LCD_write_command(0x0c);//显示开
LCD_write_command(0x01);//清屏
delay_nms(2);
}
voidLCD_en_write(void)//液晶使能
{
LCD_EN_PORT|=LCD_EN;
delay_nus(1);
LCD_EN_PORT&=~LCD_EN;
}
voidLCD_write_command(unsignedcharcommand)//写指令
{
delay_nus(16);
LCD_RS_PORT&=~LCD_RS;//RS=0
LCD_DATA_PORT&=0X0f;//清高四位
LCD_DATA_PORT|=command&0xf0;//写高四位
LCD_en_write();
command=command<<4;//低四位移到高四位
LCD_DATA_PORT&=0x0f;//清高四位
LCD_DATA_PORT|=command&0xf0;//写低四位
LCD_en_write();
}
voidLCD_write_data(unsignedchardata)//写数据
{
delay_nus(16);
LCD_RS_PORT|=LCD_RS;//RS=1
LCD_DATA_PORT&=0X0f;//清高四位
LCD_DATA_PORT|=data&0xf0;//写高四位
LCD_en_write();
data=data<<4;//低四位移到高四位
LCD_DATA_PORT&=0X0f;//清高四位
LCD_DATA_PORT|=data&0xf0;//写低四位
LCD_en_write();
}
voidLCD_set_xy(unsignedcharx,unsignedchary)//写地址函数
{
unsignedcharaddress;
if(y==0)address=0x80+x;
elseaddress=0xc0+x;
LCD_write_command(address);
}
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s)//列x=0~15,行y=0,1
{
LCD_set_xy(X,Y);//写地址
while(*s)//写显示字符
{
LCD_write_data(*s);
s++;
}
}
voidLCD_write_char(unsignedcharX,unsignedcharY,unsignedchardata)//列x=0~15,行y=0,1
{
LCD_set_xy(X,Y);//写地址
LCD_write_data(data);
}
=======================================================================
voiddelay_1us(void)//1us延时函数
{
asm("nop");
}
voiddelay_nus(unsignedintn)//Nus延时函数
{
unsignedinti=0;
for(i=0;i
voiddelay_1ms(void)//1ms延时函数
{
unsignedinti;
for(i=0;i<1140;i++);
}
voiddelay_nms(unsignedintn)//Nms延时函数
{
unsignedinti=0;
for(i=0;i
=========================================================================
#include"LCD.h"
voidinit_devICes(void)
{
CLI();//dISAbleallinterrupts
LCD_init();
MCUCR=0x00;
GICR=0x00;
TIMSK=0x00;//timerinterruptsources
SEI();//re-enableinterrupts
}
voidmain(void)
{
init_devices();
LCD_write_string(1,0,"Hi!!");
for(;;)
{
LCD_write_string(0,1,"archeng504");
LCD_write_char(6,0,'8');
}
}
补充:一个自定义图案的程序。
1602中还可以自定义8个字符图案
图案规格5*8
下面是一个向上的箭头↑
000|00100//0x04
|
000|01110//0x0e
|
000|10101//0x15
|
000|00100//0x04
|
000|00100//0x04
|
000|00100//0x04
|
000|00100//0x04
|
000|00000//0x00
unsignedchardata[8]={0x04,0x0e,0x15,0x04,0x04,0x04,0x04,0x00};//↑
unsignedchardata1[8]={0x04,0x04,0x04,0x04,0x15,0x0e,0x04,0x00};//↓
voidmy_signs(unsignedcharascii,unsignedchar*z)//0x00
{
unsignedcharaddress=0,i,temp=0;//ascii为与要定义的图案对应的ascii码值(0x00到
temp=(ascii&0x07)<<3;//0x007间任选)
for(i=0;i<8;i++)
{
address=0x40+temp+i;
LCD_write_command(address);
delay_nus(1);
LCD_write_data(*(z++));
delay_nus(1);
}
}
//建立图案
voidLCD_init(void)//液晶初始化
{
LCD_DATA_DDR|=LCD_DATA;//数据为输出
LCD_EN_DDR|=LCD_EN;//设置RS.EN方向
LCD_RS_DDR|=LCD_RS;
LCD_write_command(0x28);//4位显示
LCD_en_write();
delay_nms(15);
LCD_write_command(0x28);//4位显示
LCD_write_command(0x0c);//显示开
my_signs(0x00,data);//↑//建立图案
my_signs(0x01,data1);//↓
LCD_write_command(0x01);//清屏
}
显示自定义图案
LCD_write_char(9,0,0x00);//↑
LCD_write_char(9,0,0x01);//↓