新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 51單片機的出租車計價器C51程序

51單片機的出租車計價器C51程序

作者: 時間:2016-11-30 來源:網(wǎng)絡(luò) 收藏
已通過模擬測試
#includeunsigned 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};//分別對應(yīng)相應(yīng)的數(shù)碼管點亮/************初始化定時器0******************/void init_timer0(){TMOD=0x01;TH0=0x3C;TL0=0xAF;//定時50MSEA=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內(nèi)圈數(shù)是否小于5{t++;if(t==300)//等待時間累加滿5分鐘里程加1temp=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);}}/**************延時函數(shù)**************************/void delay(unsigned int cnt){while(--cnt);}/*********主函數(shù)******************/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;//去執(zhí)行單程計價程序}if(!key_doubleway){flag=1;//去執(zhí)行往返計價程序}if(!key_cheak){wait_time=1;//顯示等待時間//dis_time();//等待時間顯示函數(shù)}}/*********數(shù)據(jù)處理程序*****************/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();//價格顯示函數(shù)break;case 1:time_display();break;}}/*********價格顯示函數(shù)***********/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;}/*********里程顯示函數(shù)***********/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;}


評論


技術(shù)專區(qū)

關(guān)閉