高手们:
帮我解决一下,我用C8051F020控制12864并口显示文字时,有的时候显示的是乱码,有时换一块电路板就好了。不知道是哪里的问题。是程序有问题,还是电路问题。大家看看我的程序有问题么?
#include <c8051f020.h>
#include <LCD.h>
//12864端口定义
#define LCD_DATA P5 //数据口
sbit RS = P3^0; //并行的指令/数据选择信号, H数据, L命令
sbit RW = P3^1; //并行读写选择信号, H读, L写
sbit E = P3^2; //并行使能端, H有效, L无效
sbit PSB = P3^3; //并/串接口选择, H并,L串
sbit RST = P3^4; //复位, L有效
void delayms(unsigned int time) //延时 time * 1ms
{
unsigned int i,j;
for(i = 0; i < time; i++)
for(j = 0; j < 300; j++);
}
bit checkBusy() //检测LCD是否处于忙状态, 若忙返回1, 空闲返回0
{
bit busy;
RS = 0;
RW = 1;
E = 1;
delayms(1);
busy = (bit)(LCD_DATA&0x80);
E = 0;
return busy;
}
void wait() //等待LCD到空闲
{
while(checkBusy());
}
void writeCmd(unsigned char cmd) //写命令
{
wait();
RS = 0;
RW = 0;
E = 0;
delayms(1);
LCD_DATA = cmd;
delayms(1);
E = 1;
delayms(1);
E = 0;
}
void writeData(unsigned char dat) //写数据
{
wait();
RS = 1;
RW = 0;
E = 0;
delayms(1);
LCD_DATA = dat;
delayms(1);
E = 1;
delayms(1);
E = 0;
}
void LCD_Init() //初始化LCD屏
{
RST = 0;
delayms(100);
RST = 1;
PSB = 1; //并口方式
writeCmd(0x30); //基本指令, 扩充指令为34H
delayms(10);
writeCmd(0x0c); //显示开, 关光标
delayms(10);
writeCmd(0x01); //清屏
delayms(10);
}
void setPosition(unsigned char x, unsigned char y)
{
unsigned char p;
switch(x%4)
{
case 0: p = 0x80; break; //第一行开始地址
case 1: p = 0x90; break; //第二行
case 2: p = 0x88; break; //第三行
case 3: p = 0x98; break; //第四行
}
p += y;
writeCmd(p);
}
void writeString(unsigned char * str)
{
unsigned char i = 0;
while(str[i] != '\0')
{
writeData(str[i++]);
}
}