您的位置 首页 报告

ATMEGA16四线驱动LCD

includeiom16vh>includemacrosh>defineucharunsignedchardefineuintunsignedint定义LCD1602的端口应用define

#include

#include

#define uchar unsigned char
#define uint unsigned int
//界说LCD1602的端口使用
#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)
//设置指令
#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 //写数据
RS_SEI;
RW_CLI;
PORTB&=0X0F; //先铲除PORTB的高四位
PORTB|=(data&0XF0); //将写入的数据取出高四位先发送
LCD_EN_Write(); //使能LCD
delay_us(35); //延时 保证高四位的写入正确
data=data<<4; //屏蔽高四位
PORTB&=0X0F; // 取出数据的低四位数据
PORTB|=(data&0XF0); //发送低四位数据
LCD_EN_Write(); //使能LCD
}

void LCD_Clear(void)
{
LCD_Write(0,0X01);
delay(5);
}

void lcd_init(void)
{
delay(15);
LCD_Write(0,0×28);//四线数据线、16X2显现、5×7点阵
LCD_EN_Write(); //这句很重要,切忌,丢掉或许LCD便是一块黑板,什么都没有
delay(5);
LCD_Write(0,0×28);//四线数据线、16X2显现、5×7点阵
LCD_Write(0,0×08);//封闭显现
delay(5);
LCD_Write(0,0×01);//铲除屏幕显现

LCD_Write(0,0×06);//当读写一字符后地址指针加一光标加一,整屏不移动
delay(5);

LCD_Write(0,0x0c);//开显现、显现光标、光标闪耀
delay(5);
}

void LCD_Write_Byte(uchar Line,uchar addr,uchar data)
{
if(1==Line)
LCD_Write(0,0×80+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,0×80+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为左移或右移的次数
{ //每翻滚一个字符的延时delay_nms
uchar i;
if(DR==left)
{
for(i=n;i>0;i–) //左移
{
LCD_Write(0,0×18);
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();
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ceping/baogao/261055.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部