新聞中心

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

51單片機(jī)的出租車計(jì)價(jià)器C51程序

作者: 時(shí)間:2016-11-30 來(lái)源:網(wǎng)絡(luò) 收藏
已通過(guò)模擬測(cè)試
#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};//分別對(duì)應(yīng)相應(yīng)的數(shù)碼管點(diǎn)亮/************初始化定時(shí)器0******************/void init_timer0(){TMOD=0x01;TH0=0x3C;TL0=0xAF;//定時(shí)50MSEA=1;ET0=1;TR0=1;}/************初始化定時(shí)器1******************/void init_timer1(){TMOD=0x10;TH0=0x00;TL0=0x00;EA=1;                     ET1=1;                  TR1=1;}/************初始化定時(shí)器2******************/void init_timer2(){RCAP2H = (65536-50000)/256;// 50ms 自動(dòng)重載RCAP2L = (65536-50000)%256;ET2=1;EA=1;TR2=1;}/************定時(shí)器0中斷******************/void Timer0_isr(void) interrupt 1 using 1{TH0=0x3C;               TL0=0xAF;if(a==20)//定時(shí)1s{a=0;if( cir_num<5)//檢測(cè)1S內(nèi)圈數(shù)是否小于5{t++;if(t==300)//等待時(shí)間累加滿5分鐘里程加1temp=1;}}else cir_num++;}/************定時(shí)器1中斷******************/void Timer1_isr(void) interrupt 3 using 1{TH1=0x00;                /* Init value */TL1=0x00;distance=cir_num*28+temp;//里程cir_num++;//測(cè)速}/************定時(shí)器2中斷******************/void Timer2_isr(void) interrupt 5 using 1//定時(shí)器2中斷{TF2=0;while(1){pwm_mach=0;delay(200);pwm_mach=~pwm_mach;delay(400);}}/**************延時(shí)函數(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í)行單程計(jì)價(jià)程序}if(!key_doubleway){flag=1;//去執(zhí)行往返計(jì)價(jià)程序}if(!key_cheak){wait_time=1;//顯示等待時(shí)間//dis_time();//等待時(shí)間顯示函數(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();//價(jià)格顯示函數(shù)break;case 1:time_display();break;}}/*********價(jià)格顯示函數(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;}


評(píng)論


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

關(guān)閉