基于51单片机的出租车计价器,有两个独立按键一个是运行/停止按键另一个是停止时清零的按键
单片机源程序如下:
- #include<reg52.h>
- #include"nokia5110.h"
- #include"ds1302.h"
- unsigned int distance=0;
- unsigned char S1_flag=0;
- unsigned int price=0;
- sbit S1=P3^2;//定义按键
- sbit S2=P3^3;
- void delay_ms(unsigned char ms)//延时k ms
- {
- unsigned char i,j;
- for(i=0;i<ms;i++)
- for(j=0;j<110;j++);
- }
- void Int_Init(void)//外部中断初始化
- {
- IT0=1;
- EX0=1;
-
- IT1=1;
- EX1=1;
- PT1=1;
- EA=1;
- }
- void Timer0_Init(void) //定时器0初始化
- {
- TMOD|=0X05;
- TH0=(65536-10)/256;
- TL0=(65536-10)%256;
- ET0=1;
- TR0=1;
- EA=1;
- }
- void Price_caculate(void)//价格计算 根据距离和起步价
- { if(distance==0)price=0;
- else if(distance<300)price=50;
- else if(distance<1000){price=(distance-300)*0.5+50;}
- else if(distance<2000){price=190+(distance-1000)*0.3;}
- else {price=340+(distance-2000)*0.1;}
- }
- void Key_Handler(void)//按键处理
- {
- if(S1_flag==0){TR0=0;}
- else if(S1_flag==1){TR0=1;S1_flag++;distance=0;}
- }
- void nokia5110_defaultshow(void)//显示屏固定显示
- {
- LCD_write_hanzi(0,0,0);
- LCD_write_hanzi(2,0,1);
- LCD_write_char(4,0,':');
- LCD_write_shu(7,0,10);
- LCD_write_char(10,0,'K');
- LCD_write_char(12,0,'M');
-
- LCD_write_hanzi(0,2,2);
- LCD_write_hanzi(2,2,3);
- LCD_write_char(4,2,':');
- LCD_write_shu(8,2,10);
- );
- }
- void show_time(void)//显示时间及计价器运行状态
- {
- LCD_write_shu(0,4,TIME[2]/16);
- LCD_write_shu(1,4,TIME[2]%16);
- LCD_write_char(2,4,':');
- LCD_write_shu(3,4,TIME[1]/16);
- LCD_write_shu(4,4,TIME[1]%16);
- LCD_write_char(5,4,':');
- LCD_write_shu(6,4,TIME[0]/16);
- LCD_write_shu(7,4,TIME[0]%16);
- if(S1_flag==0)
- {
- LCD_write_char(10,4,'O');
- LCD_write_char(11,4,'F');
- LCD_write_char(12,4,'F');
- }else
- {
- LCD_write_char(10,4,'O');
- LCD_write_char(11,4,'N');
- LCD_write_char(12,4,' ');
- }
- }
- void main()
- {
- LCD_init();
- //Ds1302Init();
- LCD_clear();
- Timer0_Init();
- Int_Init();
- //
- nokia5110_defaultshow();
- while(1)
- {
- LCD_write_shu(5,0,distance/1000%10);//显示距离
- LCD_write_shu(6,0,distance/100%10);
- LCD_write_shu(8,0,distance/10%10);
- LCD_write_shu(9,0,distance%10);
-
- LCD_write_shu(5,2,price/1000%10);//显示价格
- LCD_write_shu(6,2,price/100%10);
- LCD_write_shu(7,2,price/10%10);
- LCD_write_shu(9,2,price%10);
- Key_Handler();//按键处理
- Price_caculate();//价格计算
- Ds1302ReadTime();//读取ds1302时间 存放在数组TIME[];
- show_time();//显示时间及计价器状态
- }
- }
- void Timer0(void) interrupt 1//里程中断 计数器模式
- {
- TH0=(65536-10)/256;
- TL0=(65536-10)%256;
- distance++;
-
- }
- void Int0() interrupt 0//按键中断
- {
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
出租车计价器.rar
(127.09 KB, 下载次数: 139)
|