#includereg51.h#includeintrins.hunsignedcharcodecdis1[]={MOTORSPEED};unsignedcharcodecdis2[]={R/S};unsignedcharcodecdis3[]={FRICTIONPOWER};sbitLCD_RS=P2^6;sbitLCD_RW=P2^5;sbitLCD_EN=P2^7;unsignedcharHdata=0,Ldata=0,Count=0;unsignedcharmsec;unsignedlongtemp=0;unsignedchardatadisplay[]={0x00,0x00,0x00,0x00,0x00,0x00};charcodereserve[3]_at_0x3b;//保留0x3b开始的3个字节voiddelayNOP(unsignedintdwTime){dwTime=250;while(dwTime--);}voiddelayms(unsignedintms){unsignedintx,y;for(x=0;xms;x++)for(y=0;y300;y++);}bitlcd_busy(){bitresult;LCD_RS=0;LCD_RW=1;LCD_EN=1;delayNOP();result=(bit)(P0&0x80);LCD_EN=0;return(result);}voidlcd_wcmd(unsignedcharcmd){while(lcd_busy());LCD_RS=0;LCD_RW=0;LCD_EN=1;P0=cmd;delayNOP();LCD_EN=0;}voidlcd_wdat(unsignedchardat){while(lcd_busy());LCD_RS=1;LCD_RW=0;LCD_EN=1;P0=dat;delayNOP();LCD_EN=0;}voidlcd_init(){//delayms(15);lcd_wcmd(0x38);//16*2显示,5*7点阵,8位数据//delayms(5);lcd_wcmd(0x38);///delayms(5);lcd_wcmd(0x38);//delayms(5);lcd_wcmd(0x0c);//开显示,不显示光标//delayms(5);lcd_wcmd(0x06);////delayms(5);lcd_wcmd(0x01);//清除LCD的显示内容//delayms(5);}voidlcd_pos(unsignedcharpos){lcd_wcmd(pos|0x80);//数据指针=80+地址变量}voidplay(){unsignedcharn;for(n=0;n=4;n++)//数据转换{display[n]=temp%10+0x30;temp=temp/10;}display[5]=temp+0x30;for(n=5;n0;n--)//高位为0不显示{if(display[n]==0x30)display[n]=0x20;elsebreak;}lcd_pos(0x46);//显示实际频率值for(n=5;n!=0xff;n--)lcd_wdat(display[n]);}/************************************************************主函数************************************************************/voidmain(){unsignedcharm;unsignedlongfrq_num;P3=0xff;lcd_init();lcd_pos(0x00);//设置显示位置为第一行for(m=0;m16;m++)lcd_wdat(cdis1[m]);//显示字符lcd_pos(0x40);//设置显示位置为第二行for(m=0;m16;m++)lcd_wdat(cdis2[m]);//显示字符TMOD=0x51;//定时器0工作在定时方式,定时器1工作在计数方式TH0=0x3c;//50ms定时TL0=0xb0;TH1=0x00;//计数初值TL1=0x00;ET1=1;//使能TIMER1中断PT1=1;//定义TIMER1中断优先TR0=1;TR1=1;ET0=1;//使能TIMER0中断EA=1;//允许中断while(1){if(msec==20){TR0=0;//关闭TIMER0TR1=0;//关闭TIMER1msec=0;Hdata=TH1;//取计数值Ldata=TL1;frq_num=Count*65535+Hdata*256+Ldata;//frq_num=Hdata*256+Ldata;TH1=0;TL1=0;Count=0;TR1=1;TR0=1;temp=frq_num/6;play();}}}voidTime0()interrupt1{TF0=0;TH0=0x3c;//50ms定时TL0=0xb0;msec++;}voidTime1()interrupt3{Count++;}