今天测试LCM12864液晶,调试挺长时间的程序液晶始终都没有显示,正在一筹莫展的时候,发现液晶的背光突然变暗,赶紧检查开发板,发现板子上的MAX232发烫,立即关掉开发板上的电源开关,推断是USB转串口线或者是MAX232坏了,现在只能先换一条备用的USB转串口线试试,换完之后MAX232不烫了,唉!好几十块的劣质USB转串口坑爹呐!不过还好,换完USB转串口线就找出液晶不显示的原因了,原来是延时函数的事,有点粗心了。。。
测试程序:
#include <STC12C5A60S2.H> //STC头文件
/*********************************************************************************************/
/*-- 调入了一幅图像:C:\Documents and Settings\地质\桌面\logo.bmp --*/
/*-- 宽度x高度=128x22 --*/
unsigned char code logo[] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF,0xFF,0xC7,0x81,0xE0,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF,0xFF,0xF3,0x83,0xE0,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF,0xFF,0xF9,0x87,0xF0,0x7C,0x03,0x00,0x80,0x80,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,0x87,0xF0,0xFC,0x06,0x03,0x41,0xC0,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F,0xE0,0x03,0x03,0x33,0x33,0x0C,0x0C,0xC0,
0xFF,0xFF,0xFF,0xFF,0xF8,0x01,0xFC,0x1F,0xE0,0x1B,0x9B,0x33,0x33,0x6E,0x6C,0xC0,
0xFF,0xFF,0xFF,0xFF,0xF8,0x00,0x70,0x07,0x80,0x19,0xF1,0xE3,0x33,0x67,0xCC,0xC0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/*****************************************************************************/
/****************************************************************************/
//LCD接口定义
sbit RS = P2^4; //模式位,为0输入指令,为1输入数据
sbit RW = P2^5; //读写位,为0读,为1写
sbit E = P2^6; //使能位
#define Lcd_Bus P0 //数据总线
sbit PSB = P2^1; //H:8位或4位并口方式,L:串口方式
sbit NC = P2^2; //空脚
sbit RES = P2^3; //复位端,低电平有效
/*****************************************************************************/
/*********************************************************************************************/
void DELAY_MS (unsigned int a){
unsigned int i;
while( a-- != 0){
for(i = 0; i < 600; i++);
}
}
/*********************************************************************************************/
/******************************************************************************/
//LCD驱动(DY12864CBL液晶显示屏并口驱动程序)
/******************************************************************************/
void chk_busy(){//检查忙位
RS=0;
RW=1;
E=1;
Lcd_Bus=0xff;
while((Lcd_Bus&0x80)==0x80);
E=0;
}
/******************************************************************************/
void write_com(unsigned char cmdcode){//写命令到LCD
chk_busy();
RS=0;
RW=0;
E=1;
Lcd_Bus=cmdcode;
E=0;
}
/******************************************************************************/
void write_data(unsigned char Dispdata){ //写数据到LCD
chk_busy();
RS=1;
RW=0;
E=1;
Lcd_Bus=Dispdata;
E=0;
}
/******************************************************************************/
void lcm_init(){//初始化LCD屏)
PSB = 1; //H:8位或4位并口方式,L:串口方式
NC = 0; //空脚
RES = 1; //复位端,低电平有效
write_com(0x30); //选择8bit数据流
write_com(0x0c); //开显示(无游标、不反白)
write_com(0x01); //清除显示,并且设定地址指针为00H
}
/*****************************************************************************/
void lcm_w_word(unsigned char *s){ //向LCM发送一个字符串,长度64字符之内。
while(*s>0){ //
write_data(*s);
s++;
}
}
/******************************************************************************/
void lcm_w_test(bit i,unsigned char word){//写指令或数据
if(i == 0){
write_com(word); //写指令或数据(0,指令)
}else{
write_data(word);//写指令或数据(1,数据)
}
}
/******************************************************************************/
void lcm_clr(void){//清屏函数
lcm_w_test(0,0x01);
}
/******************************************************************************/
/******************************************************************************
//指定显示位置// (设置LCD显示的起始位置)
/******************************************************************************/
void LCD_set_xy( unsigned char x, //X为行(1~4)
unsigned char y ){ //Y为列(0~7)
unsigned char address;
switch(x){
case 0: address = 0x80 + y; break;
case 1: address = 0x80 + y; break;
case 2: address = 0x90 + y; break;
case 3: address = 0x88 + y; break;
case 4: address = 0x98 + y; break;
default:address = 0x80 + y; break;
}
lcm_w_test(0, address);
}
/******************************************************************************
//图形方式12864显示字模(横向8位左高位取模)
/******************************************************************************/
void Disp_img(unsigned char *img){
unsigned char i,j;
unsigned int k = 0;
lcm_w_test(0,0x36); //图形方式
for(i=0;i<32;i++){
lcm_w_test(0,0x80+i);
lcm_w_test(0,0x80);
for(j=0;j<16;j++) {
lcm_w_test(1,img[k++]);
}
}
for(i=0;i<32;i++){
lcm_w_test(0,0x80+i);
lcm_w_test(0,0x88);
for(j=0;j<16;j++) {
lcm_w_test(1,img[k++]);
}
}
}
/*****************************************************************************/
//---主程序---//
/*****************************************************************************/
main() {
lcm_init(); //初始化液晶显示器
Disp_img(logo);//显示图片测试内容
DELAY_MS (10000);
lcm_clr();
while(1){//主循环
lcm_init(); //初始化液晶显示器
LCD_set_xy(1,0); //写入字符的位置
lcm_w_word("预祝电子产品世界"); //写入字符内容
LCD_set_xy(2,0);
lcm_w_word(" MCU--DIY活动 ");
LCD_set_xy(3,0);
lcm_w_word(" 取得圆满成功 ");
LCD_set_xy(4,0);
lcm_w_word(" www.eepw.com.cn");
DELAY_MS (30000);
}
}