锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

单片机驱动1602液晶电路图及c51程序

时间:2024-04-17 16:07:10

原理图:


1602液晶电路图
51单片机驱动程序:
#include #include #define uint unsigned int #define uchar unsigned char //这三个引脚参考资料 sbit E=P2^7; //1602使能引脚 sbit RW=P2^6; //1602读写引脚 sbit RS=P2^5; //1602数据/命令选择引脚 void init() { TMOD=0X00; TH0=0X03; TL0=0X32; IE=0X82; TR0=1; } /******************************************************************** * 名称 : Delay_1ms() * 功能 : 延时子程序,延时时间为 1ms * x * 输入 : x (延时一毫秒的个数) * 输出 : 无 ***********************************************************************/ void Delay_1ms(uint i)//1ms延时 { uchar x,j; for(j=0;j for(x=0;x<=148;x++); } void delay() { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } bit Busy(void) { bit busy_flag = 0; RS = 0; RW = 1; E = 1; delay(); busy_flag = (bit)(P0 & 0x80); E = 0; return busy_flag; } void wcmd(uchar del) { while(Busy()); RS = 0; RW = 0; E = 0; delay(); P0 = del; delay(); E = 1; delay(); E = 0; } void wdata(uchar del) { while(Busy()); RS = 1; RW = 0; E = 0; delay(); P0 = del; delay(); E = 1; delay(); E = 0; } void L1602_init(void) { wcmd(0x38); Delay_1ms(5); wcmd(0x38); Delay_1ms(5); wcmd(0x38); Delay_1ms(5); wcmd(0x38); wcmd(0x08); wcmd(0x0c); wcmd(0x06); wcmd(0x01); } void L1602_char(uchar hang,uchar lie,char sign) { uchar a; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; wcmd(a); wdata(sign); } void L1602_string(uchar hang,uchar lie,uchar *p) { uchar a,b=0; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; while(1) { wcmd(a++); if((*p == '\0')||(b==16)) break; b++; wdata(*p); p++; } } uchar Keyscan(void) { uchar i,j, temp, Buffer[4] = {0xfe, 0xfd, 0xfb, 0xf7}; for(j=0; j<4; j++) { P1 = Buffer[j]; temp = 0x10; for(i=0; i<4; i++) { if(!(P1 & temp)) { return (i+j*4); } temp <<= 1; } } } void Main(void) { uchar Key_Value; //读出的键值 L1602_init(); init(); L1602_string(1,16," 4*4 KeyBoard "); L1602_string(2,16,"You Press The "); for(Key_Value=15;Key_Value>0;Key_Value--) { wcmd(0x18); Delay_1ms(250); Delay_1ms(250); } while(1) { P1 = 0xf0; if(P1 != 0xf0) { Delay_1ms(20); //按键消抖 if(P1 != 0xf0) { Delay_1ms(20); //按键消抖 if(P1 != 0xf0) { Key_Value = Keyscan(); } } } L1602_char(2,30,Key_Value / 10 + 48); L1602_char(2,31,Key_Value % 10 + 48); } } void timer0() interrupt 1 { }


-电子元器件采购网(www.ruidan.com)是本土元器件目录分销商,采用“小批量、现货、样品”销售模式,致力于满足客户多型号、高质量、快速交付的采购需求。 自建高效智能仓储,拥有自营库存超过50,000种,提供一站式正品现货采购、个性化解决方案、选型替代等多元化服务。
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章