51单片机驱动1602程序
扫描二维码
随时随地手机看文章
sbitLCD_RS=P3^4;//定义IO口,具体1602的读控制线sbitLCD_RW=P3^3;//定义IO口,具体1602的写控制线sbitLCD_EN=P2^0;//定义IO口,具体1602的使能控制线sbitLight=P3^7;//定义IO口,具体1602的背光控制线(0:开通,1关闭)sbitD0=P1^0;//定义IO口,P1.0数据口1602LCD的D0口sbitD1=P1^1;//定义IO口,P1.1数据口1602LCD的D1口sbitD2=P1^2;//定义IO口,P1.2数据口1602LCD的D2口sbitD3=P1^3;//定义IO口,P1.3数据口1602LCD的D3口sbitD4=P1^4;//定义IO口,P1.4数据口1602LCD的D4口sbitD5=P1^5;//定义IO口,P1.5数据口1602LCD的D5口sbitD6=P1^6;//定义IO口,P1.6数据口1602LCD的D6口sbitD7=P2^1;//定义IO口,P2.1数据口1602LCD的D7口sbitB_0=B^0;//定义变量B的位,方便位操作sbitB_1=B^1;sbitB_2=B^2;sbitB_3=B^3;sbitB_4=B^4;sbitB_5=B^5;sbitB_6=B^6;sbitB_7=B^7;
voidmain(void){P1M0=0x00;P1M1=0x00;//配置P1口为准双向口P2M0=0x00;P2M1=0x00;//配置P2口为准双向口P3M0=0x00;P3M1=0x80;//配置P3.7口为强推挽输出(控制背光),其它IO口默认为准双向口(详细配置见STC12C56XXAD数据手册)Light=0;//开1602LCD的背光lcd_init();//LCD初始化lcd_write_char(0,0,'A');//在坐标(0,0)处写入Alcd_write_char(1,0,'B');//在坐标(1,0)处写入Blcd_write_char(2,0,'C');//在坐标(2,0)处写入Clcd_write_char(3,0,'D');//在坐标(3,0)处写入Dlcd_write_char(4,0,'E');//在坐标(4,0)处写入Elcd_write_char(5,0,'F');//在坐标(5,0)处写入Flcd_write_char(6,0,'G');//在坐标(6,0)处写入Glcd_write_char(7,0,'H');//在坐标(7,0)处写入Hlcd_write_char(8,0,'I');//在坐标(8,0)处写入Ilcd_write_char(9,0,'J');//在坐标(9,0)处写入Jlcd_write_char(10,0,'K');//在坐标(10,0)处写入Klcd_write_char(11,0,'L');//在坐标(11,0)处写入Llcd_write_char(12,0,'M');//在坐标(12,0)处写入Mlcd_write_char(13,0,'N');//在坐标(13,0)处写入Nlcd_write_char(14,0,'O');//在坐标(14,0)处写入Olcd_write_char(15,0,'P');//在坐标(15,0)处写入Plcd_write_char(0,1,'Q');//在坐标(0,1)处写入Qlcd_write_char(1,1,'R');//在坐标(1,1)处写入Rlcd_write_char(2,1,'S');//在坐标(2,1)处写入Slcd_write_char(3,1,'T');//在坐标(3,1)处写入Tlcd_write_char(4,1,'U');//在坐标(4,1)处写入Ulcd_write_char(5,1,'V');//在坐标(5,1)处写入Vlcd_write_char(6,1,'W');//在坐标(6,1)处写入Wlcd_write_char(7,1,'X');//在坐标(7,1)处写入Xlcd_write_char(8,1,'Y');//在坐标(8,1)处写入Ylcd_write_char(9,1,'Z');//在坐标(9,1)处写入Zwhile(1)//进入程序主循环{}}voiddelay1(intms)//延时子函数,用于模拟时序中高低电平的保持时间,非精确的定时{unsignedchary;while(ms--){for(y=0;y<100;y++){_nop_();_nop_();_nop_();_nop_();}}}//================================*///1602检测判忙函数*///================================*/bitlcd_busy()//读写判断数据的D7读写位,用于判断1602是否忙{bitresult;D7=1;//数据口D7置1,为读状态做准备LCD_RS=0;//选择指令寄存LCD_RW=1;//选择读控制线LCD_EN=1;//开使能控制线delayNOP();//时序延时result=D7;//读D7的电平LCD_EN=0;//关使能return(result);//返回值1:忙,0可以执行操作}//===============================*///1602写命令函数*///===============================*/voidlcd_wcmd(ucharcmd){while(lcd_busy());//判断等待LCD_RS=0;//选择指令寄存LCD_RW=0;//选择写LCD_EN=0;//开使能控制线_nop_();//时序延时_nop_();//时序延时B=cmd;//把命令字送入BD0=B_0;//把cmd的0位写入D0D1=B_1;//把cmd的1位写入D1D2=B_2;//把cmd的2位写入D2D3=B_3;//把cmd的3位写入D3D4=B_4;//把cmd的4位写入D4D5=B_5;//把cmd的5位写入D5D6=B_6;//把cmd的6位写入D6D7=B_7;//把cmd的7位写入D7delayNOP();//时序延时LCD_EN=1;//使能控制线delayNOP();//时序延时LCD_EN=0;//关闭使能控制线}//===========================*///1602写数据函数*///===========================*/voidlcd_wdat(uchardat){while(lcd_busy());//判断等待LCD_RS=1;//选择数据寄存器LCD_RW=0;//选择写LCD_EN=0;//使能控制线B=dat;//把databuf送入BD0=B_0;//把dat的0位写入D0D1=B_1;//把dat的1位写入D1D2=B_2;//把dat的2位写入D2D3=B_3;//把dat的3位写入D3D4=B_4;//把dat的4位写入D4D5=B_5;//把dat的5位写入D5D6=B_6;//把dat的6位写入D6D7=B_7;//把dat的7位写入D7delayNOP();//时序延时LCD_EN=1;//开使能控制线delayNOP();//时序延时LCD_EN=0;//关使能控制线}//=========================*///1602LCD初始化*///=========================*/voidlcd_init(){delay1(15);//时序延时lcd_wcmd(0x38);//16*2显示,5*7点阵,8位数据delay1(5);//时序延时lcd_wcmd(0x38);delay1(5);//时序延时lcd_wcmd(0x38);delay1(5);//时序延时lcd_wcmd(0x0c);//显示开,关光标delay1(5);lcd_wcmd(0x06);//移动光标delay1(5);lcd_wcmd(0x01);//清除LCD的显示内容delay1(5);}//====================*///设置显示地址*///===================*/voidlcd_write_address(unsignedcharx,unsignedchary){x&=0x0f;//列地址限制在0-15y&=0x01;//行地址限制在0-1if(y==0x00)lcd_wcmd(x|0x80);else//需要自行查看1602写地址的指令格式lcd_wcmd((x+0x40)|0x80);//第二行的列地址写入}//========================*///指定地址写入函数*///=====================*/voidlcd_write_char(unsignedcharx,unsignedchary,unsignedcharbuf){lcd_write_address(x,y);//写入地址lcd_wdat(buf);//写入显示数据}