本人是刚学C语言的菜鸟,最近想用 51单片机+lcd1602 来显示几个字符,但是搞了半天也搞不了,就是下面就是程序。rs接P2.7,rw接P2地,使能端E接P2.5;D0-D7接P0口,VSS与VEE接地,VDD接电源。设有上啦电阻。望路过的各路高手帮忙看看了。#include<at89x52.h>#include<absacc.h>#define LCDCOM XBYTE[0x2000]#define LCDDATAW XBYTE[0xa000]sbit LCDEN=P2^5;/*---驱动部分函数说明----*/void delayl();void delays();void WriteCommand(unsigned char c); //命令子函数说明void WriteData(unsigned char c); //数据子函数说明void ShowChar(unsigned char pos,unsigned char c);//显示字符子函数说明void ShowString(unsigned char line,char *ptr);void InitLcd();/*--主函数--*/void main(void){ char code str1[]="u=218v=290k"; char code str2[]="new year!"; unsigned char i,j; SP=0x50; delayl(); InitLcd(); ShowString(0,str1); ShowString(1,str2); delayl(); delayl(); delayl(); delayl(); for(i=0;i<17;i++) { ShowString(0,str1+i); ShowString(1,str2+i); for(j=0;j<120;j++) delays(); } }/*----LCD驱动部分-----*/void delayl(){ unsigned char i,j; i=0xa0; j=0xff; while(i--) while(j--); }void delays(){ unsigned char i; i=0x1f; while(i--); }void writecommand(unsigned char c)//写命令,将字C写入LCD{ delays(); LCDEN=0; LCDCOM=c; }void writedate(unsigned char c)//写数据,使显示字符{ delays(); LCDEN=0; LCDDATAW=c; }void showchar(unsigned char pos,unsigned char c){unsigned char p; if(pos<0x10) p=pos+0x80; else p=pos+0xb0; writecommand (p); writedate(c); }void ShowString(unsigned char line,char *ptr){unsigned char l,i;l=line*0x10;for(i=0;i<16;i++)/*ShowChar(l++;*(ptr+i))*/; 错误点 就还有这个错误了 错误显示内容:( AA.C(72): warning C280: 'ptr': unreferenced local variable) } void initlcd(){delayl(); writecommand(0x38); writecommand(0x38); writecommand(0x06); writecommand(0x0c); writecommand(0x01); writecommand(0x80);}3\4楼的回答都正确了,可是还是不能显示。原因是没有检测忙信号。还有编程也有点错误,这是一条书上的编程,但是书是错误的。应该作以下改正才能显示:bit lcd_bz() //测试LCD忙碌状态{bit result;rs = 0;rw = 1;LCDEN = 1;delays();delays();result = (bit)(P0 & 0x80);LCDEN = 0;return result;}/*----LCD驱动部分-----*/void delayl(){ unsigned char i,j; i=0xa0; j=0xff; while(i--) while(j--); }void delays(){ unsigned char i; i=0x1f; while(i--); }void writecommand(unsigned char c)//写命令,将字C写入LCD{ while(lcd_bz()); rs=0; rw=0; LCDEN=0; P0=c; delays(); LCDEN=1; delays(); LCDEN=0; }void writedate(unsigned char c)//写数据,使显示字符{ while(lcd_bz()); rs=1; rw=0; LCDEN=0; P0=c; LCDEN=1; delays(); LCDEN=0; }
void ShowString(unsigned char line,unsigned char *ptr){unsigned char l,i;l=line*0x10;for(i=0;i<16;i++)showchar(l++,*(ptr+i)); }/*******ptr类型声明有问题吧,还有showchar两个参数之间要用逗号,要全小写,跟上面的保持一致,指针变量ptr要明确声明unsigned char *ptr,还有,如果你的字符串不够16个这样就不太好吧,改一下吧:*****/void ShowString(unsigned char line,unsigned char *ptr){unsigned char l,i;l=line*0x10;while(*ptr) {showchar(l,*ptr); l++;ptr++;} }
#include<reg52.h>#define uchar unsigned char#define uint unsigned intuchar code table[]="I LIKE MCU!";uchar code table1[]="www.TXMCU.COM";sbit lcden=P3^4;sbit lcdrs=P3^5;sbit dula=P2^6;sbit wela=P2^7;uchar num;void delay(uint z){ uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--);}void write_com(uchar com){ lcdrs=0; P0=com; delay(5); lcden=1; delay(5); lcden=0;}void write_data(uchar date){ lcdrs=1; P0=date; delay(5); lcden=1; delay(5); lcden=0;}void init(){ dula=0; wela=0; lcden=0; write_com(0x38); write_com(0x0e); write_com(0x06); write_com(0x01); write_com(0x80+0x10);}void main(){ init(); for(num=0;num<11;num++) { write_data(table[num]); delay(20); }// write_com(1); write_com(0x80+0x53); for(num=0;num<13;num++) { write_data(table1[num]); delay(20); } for(num=0;num<16;num++) { write_com(0x18); delay(20); } while(1);}这是郭天翔教程上的例程自己看下,用的也是1602,不知有用没。