#include
本文引用地址:
http://m.butianyuan.cn/article/201611/320230.htm#define uchar unsigned char
#define uint unsigned int
//定義LCD1602的端口應(yīng)用
#define RS_CLI PORTB&=~BIT(PB1)
#define RS_SEI PORTB|=BIT(PB1)
#define RW_CLI PORTB&=~BIT(PB2)
#define RW_SEI PORTB|=BIT(PB2)
#define EN_CLI PORTB&=~BIT(PB3)
#define EN_SEI PORTB|=BIT(PB3)
//設(shè)置命令
#define left 0 //左移整屏
#define right 1 //右移整屏
void delay(uint ms)
{
uint i=0,j=0;
for(i=ms;i>0;i--)
for(j=1141;j>0;j--);
}
void delay_us(uint us)
{
uint i,j;
for(i=0;i<8;i++)
{
for(j=0;j NOP();
}
}
void port_init(void)
{
DDRB=0XFF;
PORTB=0XFF;
}
void LCD_EN_Write(void)
{
EN_CLI;
delay_us(5);
EN_SEI;
}
void LCD_Write(uchar icom,uchar data)
{
if(0==icom) //寫命令
RS_CLI;
else //寫數(shù)據(jù)
RS_SEI;
RW_CLI;
PORTB&=0X0F; //先清除PORTB的高四位
PORTB|=(data&0XF0); //將寫入的數(shù)據(jù)取出高四位先發(fā)送
LCD_EN_Write(); //使能LCD
delay_us(35); //延時(shí) 確保高四位的寫入正確
data=data<<4; //屏蔽高四位
PORTB&=0X0F; // 取出數(shù)據(jù)的低四位數(shù)據(jù)
PORTB|=(data&0XF0); //發(fā)送低四位數(shù)據(jù)
LCD_EN_Write(); //使能LCD
}
void LCD_Clear(void)
{
LCD_Write(0,0X01);
delay(5);
}
void lcd_init(void)
{
delay(15);
LCD_Write(0,0x28);//四線數(shù)據(jù)線、16X2顯示、5x7點(diǎn)陣
LCD_EN_Write(); //這句很重要,切忌,丟失可能LCD就是一塊黑板,什么都沒有
delay(5);
LCD_Write(0,0x28);//四線數(shù)據(jù)線、16X2顯示、5x7點(diǎn)陣
LCD_Write(0,0x08);//關(guān)閉顯示
delay(5);
LCD_Write(0,0x01);//清除屏幕顯示
LCD_Write(0,0x06);//當(dāng)讀寫一字符后地址指針加一光標(biāo)加一,整屏不移動(dòng)
delay(5);
LCD_Write(0,0x0c);//開顯示、顯示光標(biāo)、光標(biāo)閃爍
delay(5);
}
void LCD_Write_Byte(uchar Line,uchar addr,uchar data)
{
if(1==Line)
LCD_Write(0,0x80+addr);
else if(2==Line)
LCD_Write(0,0xc0+addr);
LCD_Write(1,data);
}
void LCD_Write_Str(uchar Line,uchar addr,uchar *p)
{
if(1==Line)
LCD_Write(0,0x80+addr);
else if(2==Line)
LCD_Write(0,0xc0+addr);
while(*p)
{
LCD_Write(1,*p);
p++;
}
}
void lcdshift(uchar DR,uchar n,uint nms ) //DR=0 左移 DR=1 右移 n為左移或右移的次數(shù)
{ //每滾動(dòng)一個(gè)字符的延時(shí)delay_nms
uchar i;
if(DR==left)
{
for(i=n;i>0;i--) //左移
{
LCD_Write(0,0x18);
delay(nms);
}
}
else if(DR == right)
{
for(i = n; i > 0; i--) //右移
{
LCD_Write(0,0x1C);
delay(nms);
}
}
}
void LCD_Display(void)
{
uchar i=0,j=0;
uchar *p="I Love BeiJin!";
uchar *s="Marry Crismioss!";
LCD_Write_Str(1,0,"I Love AVR!");
delay(500);
LCD_Write_Str(2,0,"WWW.SINA.COM/");
delay(500);
LCD_Clear();
LCD_Write_Str(1,0,"my346270797");
delay(500);
LCD_Write_Str(2,0,"MY Home of AVR!");
delay(500);
LCD_Clear();
LCD_Write_Str(1,0,"GOOD studty!");
delay(500);
LCD_Write_Str(2,0,"DAY DAY UP");
delay(500);
LCD_Clear();
for(i=0;i<3;i++)
{
LCD_Write_Byte(1,i,a);
delay(500);
LCD_Write_Byte(2,i,b);
delay(500);
}
LCD_Clear();
while(*p)
{
LCD_Write_Byte(1,j,*p);
j++;
p++;
delay(200);
}
j=0;
LCD_Clear();
while(*s)
{
LCD_Write_Byte(2,j,*s);
j++;
s++;
delay(200);
}
LCD_Clear();
while(*s)
{
LCD_Write_Byte(2,50+j,*s);
j++;
s++;
delay(200);
}
LCD_Clear();
}
void main(void)
{
port_init();
delay(5);
lcd_init();
LCD_Clear();
while(1)
{
LCD_Display();
}
}
評(píng)論