参考了OURAVR的程序,自己写了一个,加上光标和屏幕滚动代码
#define LCD_EN_PORT PORTA //以下2个要设为同一个口
#define LCD_EN_DDR DDRA
#define LCD_RS_PORT PORTA //以下2个要设为同一个口
#define LCD_RS_DDR DDRA
#define LCD_DATA_PORT PORTA //以下3个要设为同一个口
#define LCD_DATA_DDR DDRA //一定要用高4位
#define LCD_DATA_PIN PINA
#define LCD_RS (1<<PA1) //0x02 portA1 out
#define LCD_EN (1<<PA3) //0x08 portA3 out
#define LCD_DATA ((1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7)) //0xf0 portA4/5/6/7 out
uchar LCD="1" ; //是否让LCD显示 0,不显示;;;1显示
/*--------------------------------------------------------------------------------------------------
函数说明
--------------------------------------------------------------------------------------------------*/
void LCD_init(void);
void LCD_en_write(void);
void LCD_write_command(unsigned char command) ;
void LCD_write_data(unsigned char data);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data);
void LCD_Init(void) //液晶初始化
{
LCD_DATA_DDR|=LCD_DATA; //数据口方向为输出
LCD_EN_DDR|=LCD_EN; //设置EN方向为输出
LCD_RS_DDR|=LCD_RS; //设置RS方向为输出
CLI();
PORTA&=~0x04;//RW=0
DDRA=0xff;
LCD_write_command(0x30);
Delay_nms(10);
LCD_write_command(0x30);
Delay_nms(5);
LCD_write_command(0x30);
Delay_nms(5);
LCD_write_command(0x28);//写指令,4位数据线,5*10字体
LCD_en_write();
Delay_nms(4);
LCD_write_command(0x28); //4位显示
LCD_en_write();
LCD_write_command(0x0c); //显示开 光标关,字符不闪烁
LCD_en_write();
SEI();
Delay_nms(2);
}
void LCD_en_write(void) //液晶使能
{
LCD_EN_PORT|=LCD_EN; //
Delay_nus(1);
LCD_EN_PORT&=~LCD_EN; // 给出下降沿
}
void LCD_write_command(unsigned char command) //写指令 E下降,RS=0,RW=0
{
Delay_nms(6);
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写高四位
LCD_en_write(); //E下降
command="command"<<4; //低四位移到高四位
LCD_DATA_PORT&=0x0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写这次低四位到数据线高4位
LCD_en_write();
}
void LCD_write_data(unsigned char data) //写数据 E下降,RS=1,RW=0
{
Delay_nms(6);
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();
}
void LCD_set_xy( unsigned char x, unsigned char y ) //写地址函数
{
unsigned char address;
if (y == 0) address = 0x80 + x; // 最高位是1,另七位才是地址 故有+0x80
else address = 0xc0 + x;
LCD_write_command( address);
}
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
while (*s) // 写显示字符
{
LCD_write_data( *s );
s ++;
}
}
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
LCD_write_data( data);
}
/*光标移动*/
void CursorScroll(void)
{
LCD_write_command(0x10); //光标左移
}
/*画面滚动*/
void ScreenScroll(void)
{
//while(1)
{
LCD_write_command(0x18); //画面左滚
Delay_nms(300);
}
}
文章评论(0条评论)
登录后参与讨论