#include #include #include #include #define E (1 << 1) // PA1----E #define D_I (1 << 5) // PA5----D/I #define W_R (1 << 3) // PA3----R/W #define set_e() (PORTA |= E) // 位置位,输出1 #define set_di() (PORTA |= D_I) #define set_wr() (PORTA |= W_R) #define clear_e() (PORTA &= ~E) // 位清零,输出0 #define clear_di() (PORTA &= ~D_I) #define clear_wr() (PORTA &= ~W_R) #define datalcd PORTD //定义数据接口 #define Busy 0x80 #pragma data:code const char e1[]="LCD test 001 !"; const char e2[]="这是我的first example"; const char e3[]="今天天气真好"; const char e4[]="液晶测试程序"; const char e5[]="mymach@etang.com"; const char sh[]="按键测试"; const char me1[]= "测试——1"; const char me2[]= "测试——2"; const char me3[]= "测试——3"; const char me4[]= "测试——4"; const char pd[32]= { 0xc0,0x00,//1: 1100 0000 0000 0000 0x70,0x00,//2: 0111 0000 0000 0000 0x3c,0x00,//3: 0011 1100 0000 0000 0x1b,0x00,//4: 0001 1011 0000 0000 0x0c,0xc0,//5: 0000 1100 1100 0000 0x06,0x30,//6: 0000 0110 0011 0000 0x03,0x0c,//7: 0000 0011 0000 1100 0x01,0x83,//8: 0000 0001 1000 0011 0x01,0x83,//9: 0000 0001 1000 0011 0x03,0x0c,//10: 0000 0011 0000 1100 0x06,0x30,//11: 0000 0110 0011 0000 0x0c,0xc0,//12: 0000 1100 1100 0000 0x1b,0x00,//13: 0001 1011 0000 0000 0x3c,0x00,//14: 0011 1100 0000 0000 0x70,0x00,//15: 0111 0000 0000 0000 0xc0,0x00//16: 1100 0000 0000 0000 }; #pragma data:data void WaitForEnable( void ); //判断忙标志 void Lcd_Write_Data( char x );//向LCD写数据 void Lcd_Write_Command( unsigned char x,unsigned char AttribC );//向LCD写指令 void lcd_init( void );//LCD初始化 void DispOneChar(unsigned char x,unsigned char y,unsigned char Wdata);//显示一个字符 void ePutstr(unsigned char x,unsigned char y, const char *ptr);//显示一字符串 void Display_Chinese_String(char x,char y, char *s1);//显示中文字符串 void Display_Chinese_Word(unsigned char Word_High,unsigned char Word_Low);//显示中文字 void watchdog_init(void);//看门狗初始化 void delay_1ms(void);//延时1ms void delay_nms(unsigned int n);//延时nms void display_string(const char *ptr,unsigned char ddram) ;//显示字符串(包含中文和英文) void LCD_Fill(void);//向CGRAM写自定义字符 void Disp_XY(char posx,char posy) ;//光标定位 void LCD_DispOneMap(char x,char y,char dat);//显示自定义字符 unsigned char colum,rows; /**************************************************************************************/ //函数名 :main //属性 : //功能 :主函数 //输入参数 : //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ //unsigned char l=0; void main(void) { unsigned char temp,j=0; char disp; char wei; DDRB=0x00; DDRA=0xff; watchdog_init(); lcd_init(); temp = 32; delay_nms(500); LCD_Fill(); LCD_DispOneMap(16,0,0); display_string(e1,128); delay_nms(1000); display_string(e3,0x90); display_string(e4,0x88); display_string(e5,0x98); delay_nms(1000); while((PINB&0x02)==0x02); Lcd_Write_Command( 0x01, 1); display_string(me1,0x80); display_string(me2,0x90); display_string(me3,0x88); display_string(me4,0x98); } /**************************************************************************************/ //函数名 :LCD_Fill //属性 :公有 //功能 :对自定义字型设定 //输入参数 : //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void LCD_Fill(void) { char i; Lcd_Write_Command( 0x34, 1); Lcd_Write_Command( 0x34, 1); Lcd_Write_Command( 0x02, 1); Lcd_Write_Command( 0x30, 1); Lcd_Write_Command( 0x30, 1); Lcd_Write_Command( 0x40, 0); for(i=0;i<32;i++) { Lcd_Write_Data(pd[i]); } } /**************************************************************************************/ //函数名 :LCD_DispOneMap //属性 :公有 //功能 :在设定位置显示一个自定义字形 //输入参数 :x水平位置 y竖直位置 dat要显示的字型 0 2 4 6 //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void LCD_DispOneMap(char x,char y,char dat) { Disp_XY( x, y ); Lcd_Write_Data(0x00); Lcd_Write_Data(dat); } /**************************************************************************************/ //函数名 :Display_Chinese_String //属性 : //功能 :显示中文字符串 //输入参数 :显示位置坐标x,y;显示的字符串s1 //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void Display_Chinese_String(char x,char y, char *s1) { unsigned char s,l=0,i; y = 0; l = strlen(s1)/2; for(i=0;i>8); Lcd_Write_Data(Word_High); delay_nms(5); Lcd_Write_Data(Word_Low); delay_nms(5); } /**************************************************************************************/ //函数名 :Display_Chinese_String //属性 : //功能 :显示中文字符串 //输入参数 :显示位置坐标x,y;显示的字符串s1 //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void ePutstr(unsigned char x,unsigned char y, const char *ptr) { unsigned char i,k=0; while (ptr[k] >31) k++; for (i=0;i<=k;i++) { DispOneChar(x++,y,ptr[i]); if ( x == 16 ) { x = 0; y ^= 1; } } } /**************************************************************************************/ //函数名 :Disp_XY //属性 : //功能 :将光标定位 //输入参数 :显示位置坐标x,y //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void Disp_XY(char posx,char posy) { unsigned char temp; temp = posx & 0x0f; posy &= 0x01; if(posy) { temp |=0x90; } else { temp |=0x80; } _NOP(); Lcd_Write_Command(temp,1);//XG } /**************************************************************************************/ //函数名 :DisplayoneChar //属性 : //功能 :显示一个字符 //输入参数 :显示位置坐标x,y;显示的字符Wdata //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void DispOneChar(unsigned char x,unsigned char y,char Wdata) { Disp_XY( x, y ); // 定位显示地址 Lcd_Write_Data( Wdata ); // 写字符 } /**************************************************************************************/ //函数名 :Lcd_Write_Command //属性 : //功能 :向LCD写命令 //输入参数 :指令代码x;是否判断忙标志AttribC //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void Lcd_Write_Command(unsigned char x,unsigned char AttribC) { if(AttribC) WaitForEnable(); clear_di(); clear_wr(); NOP(); datalcd = x; set_e(); NOP(); clear_e(); delay_nms(5); } /**************************************************************************************/ //函数名 :LCD_Write_Data //属性 : //功能 :向液晶里面写入数据 //输入参数 : 写入的数据x //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void Lcd_Write_Data( char x) { WaitForEnable(); set_di(); clear_wr(); NOP(); datalcd = x; set_e(); NOP(); clear_e(); //delay_nms(100); //此处的延时很有必要,否则很容易产生乱码即干扰。 } /**************************************************************************************/ //函数名 :WaitForEnable //属性 : //功能 :判断忙信号 //输入参数 : //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void WaitForEnable( void ) { DDRD = 0x00; clear_di(); set_wr(); datalcd= 0xff; set_e(); NOP(); while( PIND & Busy ); clear_e(); DDRD=0xff; PORTD=0Xff; } /**************************************************************************************/ //函数名 :lcd_init //属性 : //功能 :液晶初始化 //输入参数 : //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void lcd_init( void ) { Lcd_Write_Command( 0x38, 0); // 显示模式设置(不检测忙信号) delay_nms(5); Lcd_Write_Command( 0x38, 0); // 共三次 delay_nms(5); Lcd_Write_Command( 0x38, 0); //八位控制界面 delay_nms(5); Lcd_Write_Command( 0x38, 1); // 显示模式设置(以后均检测忙信号) Lcd_Write_Command( 0x08, 1); // 显示关闭 Lcd_Write_Command( 0x01, 1); // 显示清屏 Lcd_Write_Command( 0x06, 1); // 显示光标移动设置 Lcd_Write_Command( 0x0c, 1); // 显示开及光标设置 } /**************************************************************************************/ //函数名 :delay_1ms //属性 : //功能 :延时1ms函数 //输入参数 : //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void delay_1ms(void) { int i; for(i=0;i<150;i++); } /**************************************************************************************/ //函数名 :delay_nms //属性 : //功能 :延时n毫秒 //输入参数 : 延时时间n //输出参数 : //返回值 : //修改日期 : //修改人 : /**************************************************************************************/ void delay_nms(unsigned int n) { int i; for(i=0;i0x07) { col=col-0x08;} x=col*2; Lcd_Write_Command(ddram,1); // 定位显示起始地址 while ((ptr[l])!='\0') {l++;} for (i1=0;i13) { Lcd_Write_Command(0x80,1); row=0; } } } }