在我的KEIL(xx版,无2K限制)中, 在引脚定义和自定义的数组中, 若无前面的分号,编译时会出现syntax errors near 'sbit' , ' ; ' expected, 加上后,就无error和warning了, 另外写数据函数的传递参数变量名为data,就是编译不过,无奈只好换成lcmdata, 很是诡异啊~~~,上程序!!
//头文件包含,根据实际情况下INC文件夹中的库文件名进行改动
#include <REGX52.H>
//引脚定义
#define LCM2402_DB P0
sbit LCM2402_RS=P1^0
;sbit LCM2402_RW=P1^1
;sbit LCM2402_E=P1^2
;sbit LCM2402_BF=P0^7 //由2402指令集的"读BF/AC值"指令可知,忙标志在DB7
//定义LCM2402指令集
#define Clear 0x01//清屏,xxDDRAM和AC中的数据
#define Back 0x02//归位,AC=0,光标,画面回到xx
#define Decrease 0x04//读写后,AC减一,向左写
#define Increase 0x06//读写后,AC加一,向右写
#define Dis_on_Cursor_on_Blink_on 0x0f//开显示,开光标,开光标闪烁
#define Dis_on_Cursor_on_Blink_off 0x0e//开显示,开光标,关光标闪烁
#define Dis_on_Cursor_off_Blink_off 0x0c//开显示,关光标,关光标闪烁
#define Dis_off_Cursor_off_Blink_off 0x08//关显示,关光标,关光标闪烁
#define DB_8_Line_1_57DotMatrix 0x30//8位数据总线,1行显示,5*7点阵/字符
#define DB_8_Line_1_510DotMatrix 0x34//8位数据总线,1行显示,5*10点阵/字符
#define DB_8_Line_2_57DotMatrix 0x38//8位数据总线,2行显示,5*7点阵/字符
#define DB_8_Line_2_510DotMatrix 0x3c//8位数据总线,2行显示,5*10点阵/字符
#define DB_4_Line_1_57DotMatrix 0x20//4位数据总线,1行显示,5*7点阵/字符
#define DB_4_Line_1_510DotMatrix 0x24//4位数据总线,1行显示,5*10点阵/字符
#define DB_4_Line_2_57DotMatrix 0x28//4位数据总线,2行显示,5*7点阵/字符
#define DB_4_Line_2_510DotMatrix 0x2c//4位数据总线,2行显示,5*10点阵/字符
#define CGRAM_Start 0x40//CGRAM起始地址
//定义汉字,可写入8个自定义字符,写入后可用其CGRAM代码直接提取显示
;unsigned char Chinese_Character[]={
0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00,//摄氏度的符号,CGRAM中预留的代码为0x00
0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00,//一,CGRAM中预留的代码为0x01
0x00,0x00,0x00,0x0e,0x00,0xff,0x00,0x00,//二,CGRAM中预留的代码为0x02
0x00,0x00,0xff,0x00,0xf0e,0x00,0xff,0x00,//三,CGRAM中预留的代码为0x03
0x00,0x00,0xff,0xf5,0xfb,0xf1,0xff,0x00,//四,CGRAM中预留的代码为0x04
0x00,0xfe,0x08,0xfe,0x0a,0x0a,0xff,0x00,//五,CGRAM中预留的代码为0x05
0x00,0x04,0x00,0xff,0x00,0x0a,0x11,0x00,//六,CGRAM中预留的代码为0x06
0x00,0x1f,0x11,0x1f,0x11,0x11,0x1f,0x00//日,CGRAM中预留的代码为0x07
};
//函数声明
void LCM2402_Busy_Check(void);
void LCM2402_Write_Command(unsigned char command);
void LCM2402_Write_Data(unsigned char lcmdata);
void LCM2402_Write_CGRAM(void);
void LCM2402_Init(void);
void LCM2402_Display_String(unsigned char address, unsigned char *str);
void LCM2402_Display_Character(unsigned char a, unsigned char d);
void delay_ms(unsigned int n)
{ unsigned int i,j;
for(i=n;i>0;i--)
for(j=112;j>0;j--); }
void LCM2402_Busy_Check(void)
{ LCM2402_DB=0xff;
LCM2402_RS=0;
LCM2402_RW=1;
LCM2402_E=1;
while(LCM2402_BF);
LCM2402_E=0; }
void LCM2402_Write_Command(unsigned char command)
{ LCM2402_Busy_Check();
LCM2402_RS=0;
LCM2402_RW=0;
LCM2402_E=1;
LCM2402_DB=command;
LCM2402_E=0; }
void LCM2402_Write_Data(unsigned char lcmdata)
{ LCM2402_Busy_Check();
LCM2402_RS=1;
LCM2402_RW=0;
LCM2402_E=1;
LCM2402_DB=lcmdata;
LCM2402_E=0; }
void LCM2402_Write_CGRAM(void)
{ unsigned char i;
LCM2402_Write_Command(Increase);
LCM2402_Write_Command(CGRAM_Start);
for(i=0;i<64;i++)
{ LCM2402_Write_Data(Chinese_Character[i]); } }
void LCM2402_Init(void)
{ LCM2402_Write_Command(Clear);
LCM2402_Write_Command(Back);
LCM2402_Write_Command(Increase);
LCM2402_Write_Command(DB_8_Line_2_57DotMatrix);
LCM2402_Write_Command(Dis_on_Cursor_on_Blink_on);
LCM2402_Write_CGRAM( ); }
void LCM2402_Display_String(unsigned char address, unsigned char *str)
{ LCM2402_Write_Command(address);
while(*str!='\0')
{ LCM2402_Write_Data(*str++);}
*str=0; }
void LCM2402_Display_Character(unsigned char a, unsigned char d)
{ LCM2402_Write_Command(a);
LCM2402_Write_Data(d); }
void main(void)
{
while(1)
{ LCM2402_Init();
LCM2402_Display_String(0x80,"springvirus electronics ");
LCM2402_Display_String(0xc0,"battle robots studio");
delay_ms(2000);
LCM2402_Init();
LCM2402_Display_Character(0x80,0x05);
delay_ms(1000);
LCM2402_Display_Character(0x97,0x05);
delay_ms(2000); } }