#includereg51.h#defineucharunsignedchar#defineuintunsignedint/*端口定义*/sbitin1=P1^0;sbitin2=P1^1;sbitjian=P2^0;sbitjia=P2^1;sbitdir=P2^2;sbitrun=P2^3;sbitinter_0=P3^2;sbitinter_1=P3^3;#defineLCD_dataP0//数据口sbitLCD_RS=P2^7;//寄存器选择输入sbitLCD_RW=P2^6;//液晶读/写控制sbitLCD_EN=P2^5;//液晶使能控制sbitLCD_PSB=P2^4;//串/并方式控制uchart10,t1,ce,cj,s1=0,pl=10;ucharzi[]={电机转速:};ucharnum[3];uintnums=0,speed,k=1200;voiddelay_1ms(ucharx){uchari,j;for(j=0;jx;j++)for(i=0;i110;i++);}/*******************************************************************//**//*写指令数据到LCD*//*RS=L,RW=L,E=高脉冲,D0-D7=指令码。*//**//*******************************************************************/voidwrite_cmd(ucharcmd){LCD_RS=0;LCD_RW=0;LCD_EN=0;P0=cmd;delay_1ms(5);LCD_EN=1;delay_1ms(5);LCD_EN=0;}/*******************************************************************//**//*写显示数据到LCD*//*RS=H,RW=L,E=高脉冲,D0-D7=数据。*//**//*******************************************************************/voidwrite_dat(uchardat){LCD_RS=1;LCD_RW=0;LCD_EN=0;P0=dat;delay_1ms(5);LCD_EN=1;delay_1ms(5);LCD_EN=0;}/*********************************************************//**//*设定显示位置*//**//*********************************************************/voidlcd_pos(ucharX,ucharY){ucharpos;if(X==0){X=0x80;}elseif(X==1){X=0x90;}elseif(X==2){X=0x88;}elseif(X==3){X=0x98;}pos=X+Y;write_cmd(pos);//显示地址}/*******************************************************************//**//*LCD初始化设定*//**//*******************************************************************/voidlcd_init(){LCD_PSB=1;//并口方式write_cmd(0x30);//基本指令操作delay_1ms(5);write_cmd(0x0C);//显示开,关光标delay_1ms(5);write_cmd(0x01);//清除LCD的显示内容delay_1ms(5);}voidmain(){TMOD=0x02;TH0=0X6;TL0=0X6;EA=1;ET0=1;TR0=1;IT0=1;EX0=1;IT1=1;EX1=1;PX1=1;PT0=1;t10=1;t1=10;delay_1ms(10);//延时lcd_init();lcd_pos(0,2);write_dat(zi[0]);write_dat(zi[1]);write_dat(zi[2]);write_dat(zi[3]);write_dat(zi[4]);write_dat(zi[5]);write_dat(zi[6]);write_dat(zi[7]);write_dat(zi[8]);while(1);}count0(void)interrupt1{pl--;if(pl==0){k--;if(k==0){k=1200;speed=nums/3;num[0]=speed%10+48;num[1]=speed/10%10+48;num[2]=speed/100+48;lcd_pos(1,3);write_dat(num[2]);write_dat(num[1]);write_dat(num[0]);nums=0;}pl=10;s1++;if(run==1){if(dir==1){if(s1=t10){in1=1;in2=0;}if((s1t10)&&(s1=t1)){in1=0;in2=0;}}else{if(s1=t10){in2=1;in1=0;}if((s1t10)&&(s1=t1)){in2=0;in1=0;}}}else{in1=0;in2=0;}if(s1=t1)s1=0;}}int_0(void)interrupt0{delay_1ms(15);if(inter_0==0){if(jian==0){if(t10=1)t10--;}if(jia==0){if(t1010)t10++;}}}int_1(void)interrupt2{nums++;}