89C52與LCD1602
難點(diǎn)1:時序,難點(diǎn)2:LCD1602的11條指令;
本文引用地址:http://www.ex-cimer.com/article/201611/323375.htm時序?qū)α?LCD1602基本上就能顯示了:(LCD1602的老是忙等待,所以把忙等待改成10MS延時程序了);
下面是程序:
//------------------------------------------- #include#include #define dataport P0#define uchar unsigned char#define uint unsigned int// sbit RS =P2^4;// sbit RW =P2^3;// sbit En =P2^2;sbit RS=P2^0;sbit RW=P2^1;sbit En=P2^2;void delay_ms(uchar time){uchar i,j;for(i=time;i>0;i--){for(j=85;j>0;j--){;}}}/*/----------------------------------------- 忙等void read_bf(void){En=0;RS=0;RW=1;dataport=0xff;_nop_();_nop_();_nop_();En=1;while(dataport&0x80) ;En=0;} */bit lcd_bf(){bit result;RS=0;RW=1;En=1;_nop_();_nop_();_nop_();result=(bit)(P0&0x80);En=0;return result;}//-----------------------------------------寫命令void write_command(uchar command){// read_bf();// while(lcd_bz());delay_ms(10);RW=0;RS=0;En=0;_nop_();_nop_(); dataport=command;_nop_();_nop_();_nop_();En=1;_nop_();_nop_();En=0;}//------------------------------------------寫數(shù)據(jù)void write_data(uchar data_){// read_bf();// while(lcd_bz());delay_ms(10);RW=0;RS=1;En=0;_nop_();_nop_();dataport=data_;_nop_(); _nop_();_nop_();En=1;_nop_();_nop_();En=0;}//-------------------------------------------初始化void init_lcd(void){ delay_ms(15);write_command(0x38);delay_ms(5);write_command(0x38);delay_ms(5);write_command(0x38);while(lcd_bf());write_command(0x38); //8位數(shù)據(jù)雙行57// while(lcd_bf());// write_command(0x08); //關(guān)顯示while(lcd_bf());write_command(0x01); //清顯示while(lcd_bf());write_command(0x06);while(lcd_bf());write_command(0x0c);}void lcd_disp(uchar addr,uchar a_data){write_command(addr);write_data(a_data);} //-------------------------------------------主函數(shù)void main(){// uchar ch[8]= {0x53,0x54,0x43,0x38,0x39,0x43,0x35,0x32};uchar ch[6]={"hello!"} ;uchar ch_1[16]={" 1234567890abcde"} ;uchar i,j,k;init_lcd();for(i=0;i<16;i++)lcd_disp(0x80+i,ch[i]) ;for(k=0;k<16;k++)lcd_disp(0xc0+k,ch_1[k]);for (i=0;i<16;i++)write_command(0x07) ;while(1){for(j=0;j<10;j++){write_command(0x1c); //右移delay_ms(500);}delay_ms(500);for(k=0;k<10;k++){ write_command(0x18); //左移delay_ms(500);}delay_ms(500);}}//-------------------------------------------
評論