在lcd屏上实现滚动(51单片机)

/*****************************************************************************
name: ls
description: 基于51单片机,滚动显示,lcd1602显示,第一行静止不动显
示“Welcome!”,
第二行滚动显示“Tall trees catch much wind!”
但是有个问题,当第二行的数组大于30的时候,会出现lcd第一行
被影响的现象。
*****************************************************************************/

#include 
#include 
#define dataport P0//定义11个端口
sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;
#define RS_clr RS = 0
#define RS_set RS = 1
#define RW_clr RW = 0
#define RW_set RW = 1
#define EN_clr EN = 0
#define EN_set EN = 1
void delayus2x(unsigned char t);
void delayms(unsigned char t);
void lcd_clear(void); //清屏函数
//bit lcd_check_busy();//检测lcd是否忙
void lcd_write_com(unsigned char com);//写命令
void lcd_write_data(unsigned char Data);//写数据
void lcd_write_char(unsigned char x,unsigned char y,unsigned char Data);//显示字符
void lcd_write_string(unsigned char x,unsigned char y,unsigned char *s);//显示字符串
void lcd_init(void);
unsigned char p[] = "             Tall trees catch much wind!";
void main()
{
	  unsigned char i=0,j=0,n=0; 
    lcd_init(); 
	  lcd_write_string(4,0,"Welcome!");
	  while(1)
		{
			  lcd_write_com(0x80+0x40);
			  for(j=n;j<40+n;j++)                  //相当于数组的移动,这个方法是我在一个论坛里看到的。
			  {
					  lcd_write_data(p[j]);
				}
				n++;
				if(n>=40-15)
				{
					  n=0;
					  delayms(500);
					  delayms(500);
				}
				delayms(500);
		}
}

void lcd_init(void) 
{
    lcd_write_com(0x38);    /*显示模式设置*/ 
    delayms(5); 
    lcd_write_com(0x38); 
    delayms(5); 
    lcd_write_com(0x38); 
    delayms(5); 
    lcd_write_com(0x38);  
    lcd_write_com(0x08);    /*显示关闭*/ 
    lcd_write_com(0x01);    /*显示清屏*/ 
    lcd_write_com(0x06);    /*显示光标移动设置*/ 
    delayms(5); 
    lcd_write_com(0x0C);    /*显示开及光标设置*/
}

/*
fun 显示char
param x (0-f),y (0-1)显示位置  Data显示内容
retval None
*/
void lcd_write_char(unsigned char x,unsigned char y,unsigned char Data)
{
	  if (y  = =  0 )
		{
			  lcd_write_com(0x80+x);
		}
		else
		{
			  lcd_write_com(0xc0+x);
		}
		lcd_write_data(Data);
}
void lcd_write_string(unsigned char x,unsigned char y,unsigned char *s)
{
	  if(y == 0)
		{
			  lcd_write_com(0x80 + x);
		}
		else
		{
			  lcd_write_com(0xc0 + x);
		}
		while(*s)
		{
			  lcd_write_data(*s);
			  s ++;
		}
}
 void lcd_clear(void) 
{ 
    lcd_write_com(0x01); 
    delayms(5);
}



void lcd_write_com(unsigned char com)
{
	  //while(lcd_check_busy( ) ) ;//忙则等待
	  delayms(5);      //5ms够系统变成idle
	  RS_clr;          
	  RW_clr;
	  EN_set;
	  dataport = com;
	  _nop_();
	  EN_clr;
}
void lcd_write_data(unsigned char Data)
{
	  delayms(5);          //5ms够系统变成idle
	  RS_set;
	  RW_clr;
	  EN_set;
	  dataport = Data;
	  _nop_();
	  EN_clr;
}
/*bit lcd_check_busy()//检测lcd是否忙
{
	  dataport = 0xff;
	  RS_clr;
	  RW_set;
	  EN_clr;
	  _nop_();
	  EN_set;
	  return (bit)(dataport & 0x80);
}*/
void delayus2x(unsigned char t)
{
	  while(--t);
}
void delayms(unsigned char t)
{
	  while(t--)
		{
			  delayus2x(245);
			  delayus2x(245);
		}
}

你可能感兴趣的:(my,Project)