|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2010-11-4 18:42:26
|
显示全部楼层
还是只显示一行16个字符"12345abcde12345a"
完整的程序如下
//******** EL1602 test *********
#include <iom16v.h>
#include <macros.h>
//LCD数据端口PC0-PC7
#define LCD_DATA_PORT PORTC
#define LCD_DATA_DDR DDRC
#define LCD_DATA_PIN PINC
//LCD控制端口PA5,PA7
#define LCD_CONTROL_PORT PORTA
#define LCD_CONTROL_DDR DDRA
#define RS 5
#define E 7
#define LINEWIDTH 16 //行宽
#define LINES 2 //行数
//--------------------------------------------------------------------------------------------------
//Public function prototypes
//--------------------------------------------------------------------------------------------------
void Delay_1us(void);
void Delay_1ms(void);
void Delay_nus(unsigned int n);
void Delay_nms(unsigned int n);
#define SETBIT(x,y) (x|=(1<<y)) //set bit y in byte x
#define CLRBIT(x,y) (x&=(~(1<<y))) //clear bit y in byte x
#define CHKBIT(x,y) (x&(1<<y)) //check bit y in byte x
#define uchar unsigned char // 0~255
#define uint unsigned int // 0~65535
void LCD_Init(void); //LCD初始化
void LCD_Clear(void); //LCD清屏
void LCD_CheckState(void); //--保留
void LCD_WriteCmd(unsigned char scmd); //写一个命令到LCD
void LCD_WriteData(unsigned char DData); //写一个数据到LCD
void LCD_Set_xy(unsigned char Y, unsigned char X); //设置光标位置
void LCD_WriteChar(unsigned char X,unsigned char Y,unsigned char c1); //写一个字符到LCD
void LCD_WriteStr(unsigned char Y,unsigned char X,unsigned char *pstr); //写字符串到LCD显示
void LCD_WriteStrAuto(unsigned char *pstr); //从显示屏的初始位置写入数据,自动换行
void LCD_Init(void)
{
Delay_nms(100);
LCD_WriteCmd(0x01); // LCD清屏
LCD_WriteCmd(0x06); //数据读、写操作后,鼠标右移动,AC自动增一
LCD_WriteCmd(0x0C); //开显示;关光标;关闪烁
LCD_WriteCmd(0x38); //8位数据接口;两行显示;5x8点阵字符
}
//LCD清屏
void LCD_Clear(void)
{
LCD_WriteCmd(0x01);
Delay_nms(1);
}
void LCD_WriteCmd(unsigned char scmd) //send command
{
CLRBIT(LCD_CONTROL_PORT,E);
CLRBIT(LCD_CONTROL_PORT,RS);
LCD_DATA_DDR=0xFF;
LCD_DATA_PORT=scmd;
NOP();
SETBIT(LCD_CONTROL_PORT,E);
NOP();
CLRBIT(LCD_CONTROL_PORT,E);
Delay_nus(20);
}
void LCD_WriteData(unsigned char DData)
{
CLRBIT(LCD_CONTROL_PORT,E);
SETBIT(LCD_CONTROL_PORT,RS);
LCD_DATA_DDR=0xFF;
LCD_DATA_PORT=DData;
NOP();
SETBIT(LCD_CONTROL_PORT,E);
NOP();
CLRBIT(LCD_CONTROL_PORT,E);
Delay_nus(20);
}
//设置光标位置,先Y坐标后X坐标
void LCD_Set_xy(unsigned char Y, unsigned char X)
{
unsigned char i=0x00;
switch(Y & 0x0f) //确定行号
{
case 0x00:
i=0x80; //0x80
break;
case 0x01:
i=0xC0; //0x90;
break;
default :
break;
}
i = (X & 0x0f)|i; //确定列号
LCD_WriteCmd(i);
}
//从显示屏指定的行(Y),列(X)的位置写入字符
void LCD_WriteChar(unsigned char X,unsigned char Y,unsigned char c1)
{
LCD_WriteCmd(0x38); // 8bit I/F, basic command, graphic off
LCD_Set_xy(X,Y);
LCD_WriteData(c1);
Delay_nms(1); //2006.11.19
}
//从显示屏指定的行(Y),列(X)的位置写入字符串
void LCD_WriteStr(unsigned char X,unsigned char Y,unsigned char *pstr1) //英文LCD
{
unsigned char j;
unsigned char *pstr = pstr1;
LCD_WriteCmd(0x38); // 8bit I/F, basic command, graphic off
LCD_Set_xy(Y,X);
j=Y * LINEWIDTH + X;
while (*pstr && j< (LINEWIDTH * LINES))
{
if (j%LINEWIDTH==0) //判断是否换行
{
LCD_Set_xy(j/LINEWIDTH,0); //如换行, 则光标移动到行首
Delay_nms(1);
}
LCD_WriteData(*pstr++);
j++;
Delay_nms(1);
}
Delay_nms(1); //2006.11.19
}
//从显示屏的初始位置写入字符串,自动换行
void LCD_WriteStrAuto(unsigned char *pstr1) //英文LCD
{
unsigned char i;
unsigned char j;
unsigned char *pstr = pstr1;
LCD_WriteCmd(0x38); // 8bit I/F, basic command, graphic off
LCD_Clear();
LCD_Set_xy(0,0);
Delay_nms(1);
j=0;
while (*pstr && j<LINEWIDTH*LINES)
{
if (j%LINEWIDTH==0) //判断是否换行
{
LCD_Set_xy(j/LINEWIDTH,0); //如换行, 则光标移动到行首
Delay_nms(1);
}
LCD_WriteData(*pstr++);
j++;
Delay_nms(1);
}
Delay_nms(1);
}
void main(void)
{
CLI(); // disable interrupts
Delay_nms(1000);
//LCD端口初始化
LCD_DATA_DDR=0xFF;
LCD_CONTROL_DDR|=(1<<RS|1<<E); //置位RS,E等
CLRBIT(LCD_CONTROL_PORT,E);
CLRBIT(LCD_CONTROL_PORT,RS);
LCD_DATA_PORT|=0xFF;
//LCD功能初始化
LCD_Init();
Delay_nms(10);
while(1)
{
LCD_WriteStrAuto("12345abcde12345abcde");
Delay_nms(2000);
}
}
/*-----------------------------------------------------------------------
延时函数
系统时钟:8M
-----------------------------------------------------------------------*/
void Delay_1us(void) //1us延时函数
{
asm("nop");
}
void Delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
Delay_1us();
}
void Delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void Delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
Delay_1ms();
} |
|