//FileName:12864lcds.h /************************************* ** start51 study board test software ** test 12864LCD ** using serial interface ** author:bluehacker<kernelpaniccrash AT gmail.com> ** QQ:282074921 ** Date:2007-11-18 **************************************/ #ifndef _LZP_START51_12864LCDS_H #define _LZP_START51_12864LCDS_H #include "reg52.h" //12864串行口定义 sbit LCD_CS=P2^0; sbit LCD_CLK=P2^2; sbit LCD_SID=P2^1; //12864LCD指令定义 #define CMD_CLEAR 0x01 #define CMD_RESET 0x02 //设置波特率 #define OSC_FREQ 11059200L #define BAUD_115200 256 - (OSC_FREQ/192L)/115200L // 255 #define BAUD_57600 256 - (OSC_FREQ/192L)/57600L // 254 #define BAUD_38400 256 - (OSC_FREQ/192L)/38400L // 253 #define BAUD_28800 256 - (OSC_FREQ/192L)/28800L // 252 #define BAUD_19200 256 - (OSC_FREQ/192L)/19200L // 250 #define BAUD_14400 256 - (OSC_FREQ/192L)/14400L // 248 #define BAUD_9600 256 - (OSC_FREQ/192L)/9600L // 244 void Delay_us(unsigned char nUs); void Delay_ms(unsigned int nMs); bit lcd_busy(void); void write_command(unsigned char cmd); unsigned char read_data(void); void write_data(unsigned char dat); void clear_lcd(); void lcd_init(); void lcd_setxy(unsigned char x,unsigned char y); void lcd_display_str(unsigned char x, unsigned char y,unsigned char *str); #endif //FileName:12864lcds.c /************************************* ** start51 study board test software ** test 12864LCD ** using serial interface ** author:bluehacker<kernelpaniccrash AT gmail.com> ** QQ:282074921 ** Date:2007-11-18 **************************************/ #include "12864lcds.h" bit lcd_busy() { unsigned char i; bit dat; //先产生连续5个'1' LCD_CS=1; for(i=0;i<5;i++) { LCD_CLK=0; LCD_SID=1; LCD_CLK=1; } //再发送一个'1',表示读操作 LCD_CLK=0; LCD_SID=1; LCD_CLK=1; //再发送一个'0',表示写命令操作 LCD_CLK=0; LCD_SID=0; LCD_CLK=1; //再发送一个'0',这是表示第一个字节结束 LCD_CLK=0; LCD_SID=0; LCD_CLK=1; //Delay_us(1); dat="0"; LCD_CLK=0; dat="LCD"_SID; LCD_CLK=1; LCD_CS=0; return dat; } void write_command(unsigned char cmd) { unsigned char i; while(lcd_busy()); //先产生连续5个'1' LCD_CS=1; for(i=0;i<5;i++) { LCD_CLK=0; LCD_SID=1; LCD_CLK=1; } //再发送一个'0',表示写操作 LCD_CLK=0; LCD_SID=0; LCD_CLK=1; //再发送一个'0',表示写命令操作 LCD_CLK=0; LCD_SID=0; LCD_CLK=1; //再发送一个'0',这是表示第一个字节结束 LCD_CLK=0; LCD_SID=0; LCD_CLK=1; //下面发送具体指令,先发送高4位 for(i=0;i<4;i++) { LCD_CLK=0; if(cmd&0x80) LCD_SID=1; else LCD_SID=0; LCD_CLK=1; cmd<<=1; } //发送连续4个'0' for(i=0;i<4;i++) { LCD_CLK=0; LCD_SID=0; LCD_CLK=1; } //下面发送指令的低4位 for(i=0;i<4;i++) { LCD_CLK=0; if(cmd&0x80) LCD_SID=1; else LCD_SID=0; LCD_CLK=1; cmd<<=1; } //发送连续4个'0' for(i=0;i<4;i++) { LCD_CLK=0; LCD_SID=0; LCD_CLK=1; } LCD_CS=0;//结束 } void write_data(unsigned char dat) { unsigned char i; while(lcd_busy()); //先产生连续5个'1' LCD_CS=1; for(i=0;i<5;i++) { LCD_CLK=0; LCD_SID=1; LCD_CLK=1; } //再发送一个'0',表示写操作 LCD_CLK=0; LCD_SID=0; LCD_CLK=1; //再发送一个'1',表示写数据操作 LCD_CLK=0; LCD_SID=1; LCD_CLK=1; //再发送一个'0',这是表示第一个字节结束 LCD_CLK=0; LCD_SID=0; LCD_CLK=1; //下面发送具体数据,先发送高4位 for(i=0;i<4;i++) { LCD_CLK=0; if(dat&0x80) LCD_SID=1; else LCD_SID=0; LCD_CLK=1; dat<<=1; } //发送连续4个'0' for(i=0;i<4;i++) { LCD_CLK=0; LCD_SID=0; LCD_CLK=1; } //下面发送数据的低4位 for(i=0;i<4;i++) { LCD_CLK=0; if(dat&0x80) LCD_SID=1; else LCD_SID=0; LCD_CLK=1; dat<<=1; } //发送连续4个'0' for(i=0;i<4;i++) { LCD_CLK=0; LCD_SID=0; LCD_CLK=1; } LCD_CS=0;//结束 } unsigned char read_data() { unsigned char i,dat; while(lcd_busy()); //先产生连续5个'1' LCD_CS=1; for(i=0;i<5;i++) { LCD_CLK=0; LCD_SID=1; LCD_CLK=1; } //再发送一个'1',表示读操作 LCD_CLK=0; LCD_SID=0; LCD_CLK=1; //再发送一个'1',表示数据操作 LCD_CLK=0; LCD_SID=1; LCD_CLK=1; //再发送一个'0',这是表示第一个字节结束 LCD_CLK=0; LCD_SID=0; LCD_CLK=1; //下面读具体数据 //先读高4位 dat="0"; for(i=0;i<4;i++) { LCD_CLK=0; dat<<=1; LCD_CLK=1; dat|=LCD_SID; } //再接受4个连续'0',没什么意义,直接丢掉 for(i=0;i<4;i++) { LCD_CLK=0; Delay_us(1); LCD_CLK=1; Delay_us(1); } //接受低4位 for(i=0;i<4;i++) { LCD_CLK=0; dat<<=1; LCD_CLK=1; dat|=LCD_SID; } //再接受4个连续'0',没什么意义,直接丢掉 for(i=0;i<4;i++) { LCD_CLK=0; Delay_us(1); LCD_CLK=1; Delay_us(1); } LCD_CS=0; return dat; } void clear_lcd() { write_command(CMD_CLEAR); } void lcd_setxy(unsigned char x,unsigned char y) { unsigned char addr; switch(y) { case 0: addr="0x80"+x; break; case 1: addr="0x90"+x; break; case 2: addr="0x88"+x; break; case 3: addr="0x98"+x; break; } write_command(addr); } void lcd_display_str(unsigned char x, unsigned char y,unsigned char *str) { unsigned char i; lcd_setxy(x,y); for(i=0;str!='\0';i++) { write_data(str); } } void lcd_init() { write_command(CMD_RESET); write_command(0x0d);//设置显示状态,整体显示ON,游标与游标位置ON write_command(0x30);//功能设置,基本指令 write_command(0x18);//游标与显示移位控制 clear_lcd(); //write_command(0x80);//设置DDRAM位址,并不必须 } void main() { TCON="0"; SCON="0x50";/*mode 1,1 start bit ,8 data bit ,1 stop bit,enable receive*/ PCON="0x80";/*SMOD=1, Baud Rate twice*/ TMOD="0x21";/*timer 0--mode 1 and timer 1 ---mode2*/ //set baud rate,use timer 1 as baud rate generator TH1=BAUD_19200; TL1=TH1; TR1=1; // ET1=0;//disable timer 1 interrupt EA="0"; lcd_init(); Delay_ms(100); //显示武林外传 lcd_setxy(1,2); write_data(0xce); write_data(0xe4); lcd_setxy(2,2); write_data(0xc1); write_data(0xd6); lcd_setxy(3,2); write_data(0xcd); write_data(0xe2); lcd_setxy(4,2); write_data(0xb4); write_data(0xab); Delay_ms(5000); clear_lcd(); write_command(CMD_RESET); Delay_ms(5000); lcd_display_str(2,0,"bluehacker"); lcd_display_str(1,1,"QQ:282074921"); lcd_display_str(3,2,"李正平"); while(1) { } }
文章评论(0条评论)
登录后参与讨论