已通过模拟测试 #include<reg52.h> unsigned char cir_num,pwm_mach,temp,distance,wait_time,price,distance; unsigned int a,t; sbit pwm_mach=P1^0; sbit key_clean=P3^0;//定义按键位置 sbit key_stop=P3^1; sbit key_cheak=P3^2; sbit key_oneway=P3^3; sbit key_doubleway=P3^4; unsigned char code led_buf[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; unsigned char code seg[]={0,1,2,3,4,5,6,7};//分别对应相应的数码管点亮 /************初始化定时器0******************/ void init_timer0() { TMOD=0x01; TH0=0x3C; TL0=0xAF;//定时50MS EA=1; ET0=1; TR0=1; } /************初始化定时器1******************/ void init_timer1() { TMOD=0x10; TH0=0x00; TL0=0x00; EA=1; ET1=1; TR1=1; } /************初始化定时器2******************/ void init_timer2() { RCAP2H = (65536-50000)/256;// 50ms 自动重载 RCAP2L = (65536-50000)%256; ET2=1; EA=1; TR2=1; } /************定时器0中断******************/ void Timer0_isr(void) interrupt 1 using 1 { TH0=0x3C; TL0=0xAF; if(a==20)//定时1s {a=0; if( cir_num<5)//检测1S内圈数是否小于5 {t++; if(t==300)//等待时间累加满5分钟里程加1 temp=1; } } else cir_num++; } /************定时器1中断******************/ void Timer1_isr(void) interrupt 3 using 1 { TH1=0x00; /* Init value */ TL1=0x00; distance=cir_num*28+temp;//里程 cir_num++;//测速 } /************定时器2中断******************/ void Timer2_isr(void) interrupt 5 using 1//定时器2中断 { TF2=0; while(1) { pwm_mach=0; delay(200); pwm_mach=~pwm_mach; delay(400); } } /**************延时函数**************************/ void delay(unsigned int cnt) { while(--cnt); } /*********主函数******************/ void main() { init_time0(); init_time1(); init_time2(); while(1) { key_order(); dsp_data(); } } /**********按键控制****************/ void key_order() { if(!key_clean) {distance=0; cir_num=0; t=0; } if(!key_stop) { ET0=0; ET1=0; TR0=0; TR1=0; ET2=0; TR2=0; } if(!key_oneway) { flag=0;//去执行单程计价程序 } if(!key_doubleway) { flag=1;//去执行往返计价程序 } if(!key_cheak) { wait_time=1;//显示等待时间 //dis_time();//等待时间显示函数 } } /*********数据处理程序*****************/ void dsp_data() { switch(flag) { case 0:price=(cir_num-10714)*20;break; case 1:price=(cir_num-10714)*15;break; default:price=8; break; } switch(wait_time) { case 0:distance=cir_num*28+temp;//里程 price_display();//价格显示函数 break; case 1:time_display(); break; } } /*********价格显示函数***********/ void price_display() { led_buf[4]=price/1000; led_buf[5]=price%1000/100; led_buf[6]=price%100/10; led_buf[7]=price%10; led_buf[6]=0x80; } /*********里程显示函数***********/ void distance_display() { led_buf[0]=distance/1000; led_buf[1]=distance%1000/100; led_buf[2]=distance%100/10; led_buf[3]=distance%10; led_buf[2]=0x80; } void time_display() { led_buf[0]=t/1000; led_buf[1]=t%1000/100; led_buf[2]=t%100/10; led_buf[3]=t%10; led_buf[4]=0x6d; }