新人求助啊!LCD1602的编程错误!
#include<reg51.h>
#define LCD1602_DATAPINS P0;
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_E=P2^7;
unsigned char code DATA[]="I Want You" ;
void Delay1ms(unsigned int c)
{
unsigned char a,b;
for(;c>0;c--);
for(a=38;a>0;a--);
for(b=130;b>0;b--);
}
void lcdcom(unsigned char com)
{
LCD_E=0;
LCD_RS=0;
LCD_RW=0;
LCD1602_DATAPINS=com;
Delay1ms(10);
LCD_E=1;
Delay1ms(10);
LCD_E=0;
}
void lcddat(unsigned char dat)
{
LCD_E=0;
LCD_RS=1;
LCD_RW=0;
LCD1602_DATAPINS=dat;
Delay1ms(10);
LCD_E=1;
Delay1ms(10);
LCD_E=0;
}
void lctinit()
{
lcdcom(0x38);
lcdcom(0x0c);
lcdcom(0x06);
lcdcom(0x01);
}
void main()
{
lcdinit();
while(1)
{ unsigned char i;
lcdcom(0x80);
for(i=0;i<9;i++)
{
lcddat(DATA[i]);
}
}
}
Build target 'Target 1'
compiling lcd.c...
LCD.C(20): warning C275: expression with possibly no effect
LCD.C(20): error C141: syntax error near '='
LCD.C(51): error C141: syntax error near '='
Target not created
20行是“ LCD1602_DATAPINS=com;”,51行是“ LCD1602_DATAPINS=dat;”
第二行,define后面无需分号
#define LCD1602_DATAPINS P0;这个后面不需要分号的
同意“#define LCD1602_DATAPINS P0;”后面去掉分号 #define LCD1602_DATAPINS P0
目测了一下小编的延时函数很有个性,不知道为什么这么写!
提示错误主要是
20行是“ LCD1602_DATAPINS=com;”,51行是“ LCD1602_DATAPINS=dat;”都是因为有 LCD1602_DATAPINS,就是三楼所说的,除去宏定义上面的分号。
他的那个延时肯定有问题,多了分号了
你直接试试这个吧
# include <reg51.h>
# include <intrins.h>
typedef unsigned char BYTE;
typedef unsigned int WORD;
typedef bit BOOL;
sbit rs=P0^7;
sbit rw=P0^6;
sbit ep=P0^5;
BYTE code dis1 []={" yanlindianzi"};
BYTE code dis2 []={" 15001162199"};
void delay (int m)
{
unsigned char i,j;
for (i=0;i<m;i++)
for (j=0;j<253;j++);
}
BOOL lcd_bz()
{
BOOL result;
rs=0; // 读忙信号
rw=1;
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (BOOL)(P2&0x80);
ep=0;
result ;
}
void lcd_wcmd (BYTE cmd)
{
while (lcd_bz());
rs=0;
rw=0;
ep=0;
_nop_();
_nop_();
P2=cmd ;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
ep=0;
}
void lcd_pos (BYTE pos)
{
lcd_wcmd (pos|0x80);
}
void lcd_wdat (BYTE dat)
{
while (lcd_bz());
rs=1;
rw=0;
ep=0;
_nop_();
_nop_();
P2=dat ;
_nop_();
_nop_();
_nop_();
_nop_();
ep=1;
_nop_();
_nop_();
_nop_();
_nop_();
ep=0;
}
void lcd_init ()
{
lcd_wcmd (0x38);
delay (1);
lcd_wcmd (0x0c);
delay (1);
lcd_wcmd (0x06);
delay (1);
lcd_wcmd (0x01);
delay (1);
}
main ()
{
BYTE i;
lcd_init ();
delay (10);
lcd_pos (0);
i=0;
while (dis1 [i]!='\0')
{
lcd_wdat (dis1 [i]);
i++;
}
lcd_pos (0x41);
i=0;
while (dis2 [i]!='\0')
{
lcd_wdat (dis2 [i]);
i++;
}
while (1);
}
或者
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^6;
sbit lcden=P2^7;
sbit rw=P2^5;
uchar i;
uchar code table[]={"I Want You! "};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rs=0;
lcden=0;
P2=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
lcden=0;
P2=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void main()
{
init();
while(1)
{
write_com(0x80);
while(table[i]!='\0')
{
write_date(table[i]);
i++;
delay(5);
}
}
}
那你程序有三个问题:一是#define LCD1602_DATAPINS P0;不要就直接P0=det;而是延时那里最后一个要分号就好了,前面两个不要;第三个,也是最重要的错误就是
感谢,现已改正错误!
真的唉,我都没看到初始化那里打错了......谢谢你这么耐心地回答我的问题,改正后程序已经没问题了!
这么多回复,都是高手啊,好贴心的论坛~~
谢谢大家吧!有什么问题可以一起讨论
我试验了下,只出现I WANT YO,U没有了
改成
void main()
{
lcdinit();
while(1)
{ unsigned char i;
lcdcom(0x80);
for(i=0;i<10;i++)
{
lcddat(DATA[i]);
}
}
}