/*LCD12864顯示子程序*、 #include<util/delay.h> #define uchar unsigned char #define uint unsigned int #define SETLCD12864RS PORTA|=(1<<PA4) #define SETLCD12864RW PORTA|=(1<<PA5) #define SETLCD12864EN PORTA|=(1<<PA6) #define CLLCD12864RS PORTA&=~(1<<PA4) #define CLLCD12864RW PORTA&=~(1<<PA5) #define CLTLCD12864EN PORTA&=~(1<<PA6) #define PSB PA3 void initLCD12864_PORTX(void) { PORTB =0xff; DDRB =0xff; PORTA =0x00; DDRA =(1<<PA4)|(1<<PA5)|(1<<PA6); } void delay_1ms(void) { unsigned int i=0; for(i=0;i<1140;i++); } void delay_nms(unsigned int n) { unsigned int i=0; for(i=0;i<n;i++) { delay_1ms(); } } void LCM12864_TestBusy(void)//忙檢測子程序// { unsigned int uiTemp; uiTemp=3000; DDRB =0x00; CLLCD12864RS; //RS =0;讀命令// SETLCD12864RW; //RW=1;讀指令// SETLCD12864EN; // E=1;使能// while((0x80&PINB)&&(uiTemp--)); //忙信號檢測,直到忙信號為0,才能讀寫操作// CLTLCD12864EN; //E=0;// DDRB =0xff; //PB為輸出口,準(zhǔn)備向端口發(fā)送數(shù)據(jù)// } void write_cmd_12864(unsigned char LCM12864_command)//寫操作子程序// { LCM12864_TestBusy(); CLLCD12864RS;//RS=0;// CLLCD12864RW;//RW=0;// SETLCD12864EN;//E=0;// _delay_ms(2); PORTB = LCM12864_command;//向PB端口寫一個指令// CLTLCD12864EN; //E=0;// LCM12864_TestBusy(); } void LCD12864Init(void) { write_cmd_12864(0x01); //清屏 _delay_ms(15); write_cmd_12864(0x38); //顯示模式 _delay_ms(5); //LCD12864WriteCommand(0x0f); //顯示開關(guān)控制,開顯示,光標(biāo)顯示,光標(biāo)閃爍 write_cmd_12864(0x0c); //顯示開關(guān)控制,開顯示,光標(biāo)不顯示,光標(biāo)不閃爍 _delay_ms(5); write_cmd_12864(0x06); //光標(biāo)設(shè)置,讀或?qū)懸粋字符后,地址指針加一,光標(biāo)加一,整屏不移動 _delay_ms(5); } void write_data_12864(unsigned char LCM12864_data)//寫涵數(shù)// { LCM12864_TestBusy(); SETLCD12864RS; CLLCD12864RW; SETLCD12864EN;//E=0;// _delay_ms(2); PORTB = LCM12864_data;//向PB口寫一個涵數(shù) CLTLCD12864EN; //E=0;// LCM12864_TestBusy(); } void LCD_set_xy( unsigned char x, unsigned char y )//初始化液晶行和例// { unsigned char address; switch(y){ case 0: address=0x80+x;break;//第一例// case 1: address=0x90+x;break;//第二例// case 2: address=0x88+x;break;//第三例// case 3: address=0x98+x;break;//第四例// default:address=0x80+x;//默認(rèn)第一例// } write_cmd_12864(address); } void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)//X列,Y行,*S字符串// { LCD_set_xy( X, Y ); while (*s) { write_data_12864(*s ); s ++; delay_nms(1); } } void LCD12864_write_char(unsigned char X,unsigned char Y,unsigned char data)//X列,Y行,涵數(shù)// { LCD_set_xy( X,Y ); write_data_12864(data); }