LPC2124 与 LM016L
时间:2009-04-10 来源:creatory
/*LM016L control port defination
引脚说明:
VSS:接地 0V
VDD:+5V (Power supply)
VO:可调亮度
RS: 1:数据输入,0:指令输入
RW: 1:读,0:写
E: 1或1->(下降沿),使能信号
DB7~DB0: 8位的数据总线)
注:LM016L在EN的下降沿将执行指令
*/
#define RW (1<<9)
#define RS (1<<8)
#define EN (1<<10) /*检测忙时要用到数据位的最高位,此为掩码*/
/*00000000 00000000 00000000 00000001
*00000000 00000000 00000000 10000000
*/
#define BUSY (1<<7) /*the max length of one line*/
/*16 characters x 2 lines*/
#define MAX_X 16 /*instruction macro defination*/
#define CLEARDISPLAY 0x01 //显示清零
#define TWOLINE_5x7_8BIT 0x38 //16*2显示,5*7点阵,8位数据
#define DISPEN_CURSORSET 0x0e //显示使能及光标设置 /*check busy
*IODIR:0=INPUT,1=OUTPUT
*为了读取LCD的状态信号,必须将数据口置为输入方向,将状态口置为输出方向
*/
void checkBusy(void){
/*111 0000 0000,输入状态*/
IO0DIR=0x700; //GPIO P0 input
while(1){
IO0CLR=RS; //指令
IO0SET=RW; //读
IO0SET=EN; //有效
if(!(IO0PIN & BUSY)) break; //如果LCD空闲则跳出
IO0CLR=EN; //无效
}
/*111 1111 1111,输出状态*/
IO0DIR=0x7FF; //GPIO P0 output
} /*write command*/
void writeComm(unsigned char comm){
checkBusy(); //检查忙状态
IO0CLR=RS; //指令
IO0CLR=RW; //写
IO0CLR=0xFF; //清零
IO0SET=comm; //送出指令码
IO0SET=EN; //有效,构造EN的下降沿
IO0CLR=EN; //无效
} /*write data*/
void writeData(unsigned char data){
checkBusy(); //查忙
IO0SET=RS; //数据
IO0CLR=RW; //写
IO0CLR=0xFF; //清零
IO0SET=data; //送出数据
IO0SET=EN; //构造EN的下降沿
IO0CLR=EN;
}
/*lcd initial
*写入连续的指令使LCD初始化
*/
void lcdInitial(void){
writeComm(CLEARDISPLAY);
writeComm(0x38);
writeComm(0x0f);
writeComm(0x06);
writeComm(0x0c);
} /*display a char at the specified position*/
void dispChar(unsigned char x,unsigned char y,unsigned char dat){ /*横坐标为0x0-0xF,一行只能显示16个字符*/
/*纵坐标为0x0-0x1,只能两列*/
x &= 0x0F;/*restrict the vlaue of x,the max value is 15*/
y &= 0x01; /*restrict the value of y ,the max value is 1*/
if(y==1) //when display on the second line,x value plus 0x40
x |= 0x40;
x |= 0x80;
writeComm(x); //送出地址
writeData(dat); //送出数据
}
/*display a string at the specified position*/
void dispString(unsigned char x,unsigned char y,unsigned char *ptr){
unsigned char len;
len=0;
x &= 0x0F; //lm016l display mode is 16x2
y &= 0x01;
while(ptr[len]!='\0'){
if(x<=0x0F){
dispChar(x,y,ptr[len]);
len++;
x++;
}
}
}
/*return the length of a string*/
int strlen(unsigned char *ptr){
unsigned int len;
len=0;
while(ptr[len]!='\0')
len++;
return len;
} #include "LPC2124.h"
#include "LM016L.h"
int main(void){
unsigned char msg_welcome[]={"MacrosoftCorpora"};
unsigned char msg_info[]={"[email protected]"};
unsigned char msg_toolong[]="StringTooLong";
lcdInitial();
IO0DIR=0x7FF; //GPIO P0 output
IO0CLR=0x7FF; //P0=0 /*begin to display on the lcd
*if the length greater that 16 then separate it
*/
if(strlen(msg_welcome)>=16 || strlen(msg_info)>=16)
dispString(0,0,msg_toolong);
else
{
dispString(0, 0, msg_welcome);
dispString(0, 1, msg_info);
}
while(1);
return 0;
}
500)this.width=500;" border=0> 留下的问题: 不知道为什么,每一行的字符显示缓冲区必须是小于等于17个长度,虽然只能显示16个字符,假如大于17就第一行能显示出来,第二行显示不出来?有哪位高人给讲讲?先前用了个unsigned char *p临时指针限制长度也不好使.
引脚说明:
VSS:接地 0V
VDD:+5V (Power supply)
VO:可调亮度
RS: 1:数据输入,0:指令输入
RW: 1:读,0:写
E: 1或1->(下降沿),使能信号
DB7~DB0: 8位的数据总线)
注:LM016L在EN的下降沿将执行指令
*/
#define RW (1<<9)
#define RS (1<<8)
#define EN (1<<10) /*检测忙时要用到数据位的最高位,此为掩码*/
/*00000000 00000000 00000000 00000001
*00000000 00000000 00000000 10000000
*/
#define BUSY (1<<7) /*the max length of one line*/
/*16 characters x 2 lines*/
#define MAX_X 16 /*instruction macro defination*/
#define CLEARDISPLAY 0x01 //显示清零
#define TWOLINE_5x7_8BIT 0x38 //16*2显示,5*7点阵,8位数据
#define DISPEN_CURSORSET 0x0e //显示使能及光标设置 /*check busy
*IODIR:0=INPUT,1=OUTPUT
*为了读取LCD的状态信号,必须将数据口置为输入方向,将状态口置为输出方向
*/
void checkBusy(void){
/*111 0000 0000,输入状态*/
IO0DIR=0x700; //GPIO P0 input
while(1){
IO0CLR=RS; //指令
IO0SET=RW; //读
IO0SET=EN; //有效
if(!(IO0PIN & BUSY)) break; //如果LCD空闲则跳出
IO0CLR=EN; //无效
}
/*111 1111 1111,输出状态*/
IO0DIR=0x7FF; //GPIO P0 output
} /*write command*/
void writeComm(unsigned char comm){
checkBusy(); //检查忙状态
IO0CLR=RS; //指令
IO0CLR=RW; //写
IO0CLR=0xFF; //清零
IO0SET=comm; //送出指令码
IO0SET=EN; //有效,构造EN的下降沿
IO0CLR=EN; //无效
} /*write data*/
void writeData(unsigned char data){
checkBusy(); //查忙
IO0SET=RS; //数据
IO0CLR=RW; //写
IO0CLR=0xFF; //清零
IO0SET=data; //送出数据
IO0SET=EN; //构造EN的下降沿
IO0CLR=EN;
}
/*lcd initial
*写入连续的指令使LCD初始化
*/
void lcdInitial(void){
writeComm(CLEARDISPLAY);
writeComm(0x38);
writeComm(0x0f);
writeComm(0x06);
writeComm(0x0c);
} /*display a char at the specified position*/
void dispChar(unsigned char x,unsigned char y,unsigned char dat){ /*横坐标为0x0-0xF,一行只能显示16个字符*/
/*纵坐标为0x0-0x1,只能两列*/
x &= 0x0F;/*restrict the vlaue of x,the max value is 15*/
y &= 0x01; /*restrict the value of y ,the max value is 1*/
if(y==1) //when display on the second line,x value plus 0x40
x |= 0x40;
x |= 0x80;
writeComm(x); //送出地址
writeData(dat); //送出数据
}
/*display a string at the specified position*/
void dispString(unsigned char x,unsigned char y,unsigned char *ptr){
unsigned char len;
len=0;
x &= 0x0F; //lm016l display mode is 16x2
y &= 0x01;
while(ptr[len]!='\0'){
if(x<=0x0F){
dispChar(x,y,ptr[len]);
len++;
x++;
}
}
}
/*return the length of a string*/
int strlen(unsigned char *ptr){
unsigned int len;
len=0;
while(ptr[len]!='\0')
len++;
return len;
} #include "LPC2124.h"
#include "LM016L.h"
int main(void){
unsigned char msg_welcome[]={"MacrosoftCorpora"};
unsigned char msg_info[]={"[email protected]"};
unsigned char msg_toolong[]="StringTooLong";
lcdInitial();
IO0DIR=0x7FF; //GPIO P0 output
IO0CLR=0x7FF; //P0=0 /*begin to display on the lcd
*if the length greater that 16 then separate it
*/
if(strlen(msg_welcome)>=16 || strlen(msg_info)>=16)
dispString(0,0,msg_toolong);
else
{
dispString(0, 0, msg_welcome);
dispString(0, 1, msg_info);
}
while(1);
return 0;
}
500)this.width=500;" border=0> 留下的问题: 不知道为什么,每一行的字符显示缓冲区必须是小于等于17个长度,虽然只能显示16个字符,假如大于17就第一行能显示出来,第二行显示不出来?有哪位高人给讲讲?先前用了个unsigned char *p临时指针限制长度也不好使.
相关阅读 更多 +