出租车计费仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg51.h>
- #include <intrins.h>
- unsigned code num[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
- unsigned char data x_pos, y_pos;
- sbit rs=P2^0;
- sbit rw=P2^1;
- sbit e=P2^2;
- sbit Control=P3^5;
- bit swit_flg=0;
- unsigned char time,minte,second;
- unsigned char result;
- void delay_ms(unsigned char ms)
- { unsigned int i,j;
- for(i=ms;i>0;i--)
- {
- for(j=200;j>0;j--);
- for(j=102;j>0;j--);
- }
- }
- void busy(void)
- {
- unsigned char i;
- P0=0xff;
- rs=0;
- rw=1;
- e=1;
- for(i=0;i<20;i++)
- if((P0&0x80)==0) break;
- e=0;
- }
- void LcdWD(unsigned char d)
- {
- busy();
- rs=1;
- rw=0;
- P0=d;
- e=1;
- _nop_();
- e=0;
- }
- void LcdWC(unsigned char c)
- {
- busy();
- rs=0;
- rw=0;
- P0=c;
- e=1;
- _nop_();
- e=0;
- }
- void LcdInit(void)
- {
- LcdWC(0x38); //显示模式设置
- delay_ms(3); //延时3ms
- LcdWC(0x38);
- delay_ms(3);
- LcdWC(0x38);
- delay_ms(3);
- LcdWC(0x38);
- delay_ms(3);
- LcdWC(0x08); //显示关闭
- LcdWC(0x01); //清屏
- delay_ms(3);
- LcdWC(0x06); //显示光标移动设置
- LcdWC(0x0c); //开显示及 光标设置
- }
- void charlcdpos(unsigned char x,unsigned char y)
- {
- x_pos=x;
- y_pos=y;
- x_pos&=0x0f;
- y_pos&=0x01;
- if(y_pos==0) LcdWC(x_pos|0x80); // 写第一行命令
- else LcdWC(x_pos|0xc0); // 写第二行命令
- }
- void putchar(unsigned char x,unsigned char y,unsigned char c)
- {
- charlcdpos(x,y);
- LcdWD(c);
- }
- /*
- void charcursornext(void)
- {
- x_pos++;
- if(x_pos>15)
- {
- x_pos=0;
- y_pos++;
- y_pos&=0x1;
- }
- }
- */
- void displayInit(void)
- { unsigned char tmp;
- tmp=time/10;putchar(4,0,num[tmp]);
- tmp=time%10;putchar(5,0,num[tmp]); putchar(6,0,0x3a);
- tmp=minte/10;putchar(7,0,num[tmp]);
- tmp=minte%10;putchar(8,0,num[tmp]); putchar(9,0,0x3a);
- tmp=second/10;putchar(10,0,num[tmp]);
- tmp=second%10;putchar(11,0,num[tmp]);
- tmp=result/10; putchar(7,1,num[tmp]);
- tmp=result%10;putchar(8,1,num[tmp]);
-
- }
- void IRS_EX0() interrupt 0 using 1
- {
- if(swit_flg==0)
- { time=0;
- minte=0;
- second=0;
- result=0;
- swit_flg=1;
- ET0=1;
- ET1=1;
- TR0=1;
- TR1=1;
- }
- else if(swit_flg==1)
- { result=(minte*60+second)*2;
- swit_flg=0;
- ET0=0;
- ET1=0;
- }
- }
- void IRS_T0() interrupt 1 using 2
- {
- Control=1;
- TH0=0XD8;
- TL0=0XF0;
- Control=0;
- }
- void ISR_T1() interrupt 3 using 2
- {
- second++;
- if(second>=60)
- { second=0;
- minte++;
- }
- if(minte>=60)
- { minte=0;
- time++;
- }
- if(time>=24) time=0;
- }
- void init_TC()
- {
- TMOD=0X61;
- TH0=0Xd8; //
- TL0=0Xf0;
- TL1=0XbC;//TL1=0X9C;
- TH1=0XbC;
- //IE=0X8A;
- Control=0;
- // TR0=1;
- // TR1=1;
- }
- void init_EX0()
- {
- IT0=1;
- EX0=1;
- EA=1;
- }
- void main()
- { unsigned char tmp;
-
- LcdInit();
- displayInit();
- delay_ms(100);
- init_TC();
- init_EX0();
- while(1)
- {
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
出租车.rar
(62.14 KB, 下载次数: 116)
|