单片机LCD怎么显示
http://bbs.fishc.com/data/attachment/album/201408/23/170935zf9waejvovvhwee7.jpg<P> #include <reg51.h>
#define uchar unsignedchar
sbit rs=P0^5;
sbit rw=P0^6;
sbite=P0^7;
//延时
void time(int i)
{
int a,b;
for(a=0;a<i;a++)
{
for(b=0;b<100;b++)
{
}
}
}
//写指令
void w_c(uchar codee)
{
rs=0;
rw=0;
P2=codee;
time(5);
e=1;
time(5);
e=0;
}
//写数据
void w_d(uchar dataa)
{
rs=1;
rw=0;
P2=dataa;
time(5);
e=1;
time(5);
e=0;
}
//LCD初始化
void in()
{
w_c(0x38);
w_c(0x1);//清屏
w_c(0x06);
w_c(0x08);
w_c(0x0c);//光标
w_c(0x80);//显示地址
}
void main()
{
uchar c[]="wwwwww";
int i;
in();
//写入数据
for(i=0;i<6;i++)
{
w_d(c);
time(5);
}
while(1);
}</P>LCD 没有显示字符
请问 那里错了
本帖最后由 大黑鱼 于 2014-8-23 19:27 编辑
#include <reg52.h>
#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
#define DataPort P0
sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
bit LCD_Check_Busy(void) //判忙函数
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
}
void LCD_Write_Com(unsigned char com) //写入指令函数
{
//
DelayMs(5);
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
void LCD_Write_Data(unsigned char Data) //写入数据函数
{
//while(LCD_Check_Busy()); 判忙
DelayMs(5);
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
void LCD_Clear(void) //清屏函数
{
LCD_Write_Com(0x01);
DelayMs(5);
}
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_Write_Char(unsigned char x,unsigned char y,unsigned char Data) //写入字符函数X第几个小屏
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
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);
}
注:main函数没写
/*LCD1602液晶准备函数*/
void lcd1602_wait_ready()
{
unsigned char state = 0; //状态值
P0 = 0xff; //给高电平
LCD1602_RW = 1;//选择读取
LCD1602_RS = 0;//选择读取状态
do{
LCD1602_E = 1;
state = P0; //读取状态值
LCD1602_E = 0;
} while (state & 0x80); //bit7为1表示液晶正忙,重复检测直到其为0为止
}
/*LCD1602液晶写入命令函数,cmd-待写入命令值*/
void lcd1602_write_cmd(unsigned char cmd)
{
lcd1602_wait_ready();
LCD1602_RW = 0; //选择写入
LCD1602_RS = 0; //选择写入命令
P0 = cmd; //放入命令
LCD1602_E = 1;//高脉冲
LCD1602_E = 0;
}
/*LCD1602液晶写入数据函数,dat-待写入数据值*/
void lcd1602_write_dat(unsigned char dat)
{
lcd1602_wait_ready();
LCD1602_RW = 0;//选择写入
LCD1602_RS = 1;//选择写入数据
P0 = dat; //放入数据
LCD1602_E = 1; //高脉冲
LCD1602_E = 0;
}
/*LCD1602液晶初始化函数*/
void lcd1602_init()
{
lcd1602_write_cmd(0x38); //16*2显示,5*7点阵,8位数据接口
lcd1602_write_cmd(0x0c); //显示器开,光标关闭
lcd1602_write_cmd(0x06); //文字不动,地址自动+1
lcd1602_write_cmd(0x01); //清屏
}
/*LCD1602液晶设置光标位置,即显示RAM起始地址,(x, y)-对应屏幕上的字符坐标*/
void lcd1602_set_cursor(unsigned char x, unsigned char y)
{
unsigned char address = 0;
if (y) //由输入的屏幕坐标计算显示RAM的地址
{
address = 0x40 + x; //第二行字符地址从0x40起始
}
else
{
address = 0x00 + x; //第一行字符地址从0x00起始
}
lcd1602_write_cmd(0x80 + address);//设置RAM起始
}
页:
[1]