#include<intrins.h> #include<reg51.h> #define uint unsigned int #define uchar unsigned char uint i,j,m,n,flag,t; signed int a,b,c,d; double tt; uchar code LEDData[]="0123456789"; uchar TX[6]={0,}; uchar run[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; 控制口定义 sbit EnA =P2^6; sbit SA1 =P2^2; sbit SA2 =P2^3; sbit EnB =P2^7; sbit SB1 =P2^4; sbit SB2 =P2^5; sbit en =P3^3; sbit rw =P3^1; sbit rs =P3^0; sbit start=P1^4; sbit vh =P1^5; sbit vl =P1^6; sbit stop =P1^7; 延时1ms程序 void delay0(int t) {int w; for(;t>0;t--) for(w=110;w>0;w--); } 键盘扫描程序 void key() {if(start==0) {delay0(10); while(start==0); {j++; n=1; } } if(stop==0) {delay0(10); while(stop==0); {n--; j=0; } } if(vh==0) {delay0(10); while(vh==0); {m++; a++;} } if(vl==0) {delay0(10); while(vl==0); {m--; b--;} } } 写指令程序 void write_com(uchar com) { rs=0; P0=com; delay0(5); en=1; delay0(5); en=0; } 写数据程序 void write_data(uchar date) { rs=1; P0=date; delay0(5); en=1; delay0(5); en=0; } PID程序 void PID() {uint e1,e2,e3; e1=TX[5]-TX[4]; e2=TX[3]-TX[2]; e3=TX[1]-TX[0]; if(e1>e2>10) { t=10*e1-6*e2+4*e3; } if(2<e1<e2) { t=3*e1-5*e2+2*e3; } 中断0服务程序 void interrupt0() interrupt 1 using 2 {c++; } 定时器0服务程序 void timer0() interrupt 1 { uint e; uint f; TH0=(65536-(1250-t))/256; TL0=(65536-(1250-t))%256; if(f<7) {P2=run[f]; f++; } else {P2=run[f]; f=0; } d++; if(d==m) {if(e<6) {e++; TX[e]=c; } else {e=0; PID(); } c=0; d=0; } } 内部资源初始化 void Init(void) { TMOD=0x11; TH0=(65536-(1250-t))/256; TL0=(65536-(1250-t))%256; TH1=(65536-50000)/256; TL1=(65536-50000)%256; IT0=1; EX0=1; IP=0x03; EA=1; ET0=1; TR0=1; en=0; rw=0; write_com(0x38); write_com(0x0c); write_com(0x04); write_com(0x01); write_com(0x80); write_data('S'); write_data('S'); write_data('P'); write_data('E'); write_data('E'); write_data('D'); write_data(':'); write_com(0x80+0x0B); write_data('R'); write_data('/'); write_data('S'); write_com(0x80+0x40); write_data('R'); write_data('S'); write_data('P'); write_data('E'); write_data('E'); write_data('D'); write_data(':'); write_com(0x80+0x4B); write_data('R'); write_data('/'); write_data('S'); } 速度显示 void speeddisplay() { write_com(0x80+0x07); write_data('0'); write_data(LEDData[m/100]); write_data(LEDData[m%100/10]); write_data(LEDData[m%10]); if((m<600)&(m>950)) {write_com(0x80+0x48); write_data(LEDData[(m+1)/100]); write_data(LEDData[(m+1)%100/10]); write_data(LEDData[(m+1)%10]);} else {write_com(0x80+0x47); write_data('0'); write_data(LEDData[m/100]); write_data(LEDData[m%100/10]); write_data(LEDData[m%10]);}; } void main(void) { a=b=c=d=i=0; j=0; m=800; n=1; flag=0; while(1) {key(); if(j==1) {init(); } while(j&&n) {key(); PID(); tt=(1.3*(a+b)); t=tt; speeddisplay(); } if(n==0) {write_com(0x38); write_com(0x0c); write_com(0x04); write_com(0x01); } } }
|