?? main一.c
字號:
//////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////出租車計價器///////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
#include <AT89X55.h>
//#include <reg52.h>
#include <intrins.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define TIME1H 0x9C //0.1ms
//uchar tt;//調試用
sbit KEY_1= P1^0;
sbit KEY_2= P1^1;
sbit KEY_3= P1^2;
sbit KEY_4= P1^3;
uchar KEY;
uchar tpKEY;
//////////////////////////////////////////Menu//////////////////////////////////////////////
uchar setPriceMod=0;//0設置白天,1設置晚上
uchar priceMod=0;//0按白天計價,1按晚上計價
///////////////////////////////////計算用全局變量///////////////////////////////////////////
uchar isBilling=0;
float dayPrice=5.5, nightPrice=6.5; //價格
float distance;//里程
float waitDistance=0;
uint wheelPerimeter=100;//車輪周長cm
uint circle=0; //圈數
//uint kCircle=0;//千圈數
uint hCircle=0;//百圈數
uint totalCost=0;//總價格
//uint waitPrice=0;//
uint waitTime=0;//等待時間
uint tpCircle=0;
uint _testWait=0;
uint timeMs=0;
//uint timeS=0;
uint lastAddr;
//uchar isBillingCut; //是否在計費模式下斷電 0不是 1是 IsBillingCut
///////////////////////////////////////顯示/////////////////////////////////////////////////
uchar data arr_dayPrice[4],arr_nightPrice[4];
//uchar data arr_totalCost[3];
uchar data arr_distance[5];
uchar data arr_wheelPerimeter[4];
uchar data arr_kCircle[4]; //千圈
uchar data arr_totalCost[4];
//-----------------------------------24c02------------------------------
sbit scl=P2^6; //24c02 SCL
sbit sda=P2^5; //24c02 SDA
//sbit DOG=P1^7; //狗
uchar x24c02_read(uchar address); //從24c02的地址address中讀取一個字節數據
void x24c02_write(uchar address,uchar info); //向24c02的address地址中寫入一字節數據info
void x24c02_init(); //24c02初始化子程序
void delay1(uchar x);
void flash();
void x24c01_init();
void start();
void stop();
void writex(uchar j);
uchar readx();
void clock();
/**************顯示程序*********/
sbit sda1=P3^0;
sbit clock1=P3^1;
uchar num[6];
uchar sdata1,datxp[16];
/********************************************************/
/**名稱:delay1(); **/
/**功能:延時程序 **/
/********************************************************/
void delay1(uchar x)
{
uint i;
for(i=0;i<x;i++);
// DOG=~DOG;
}
/********************************************************/
/**名稱:flash(); **/
/**功能:閃爍程序 **/
/********************************************************/
void flash()
{
// uchar u;
// DOG=~DOG;
}
/********************************************************/
/**名稱:x24c02_init() **/
/**功能:x24c02初始化 **/
/********************************************************/
void x24c02_init()
{
scl=1; flash(); sda=1; flash();
}
/********************************************************/
/**名稱:start() **/
/**功能:I2C開始 **/
/********************************************************/
void start()
{
sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();
}
/********************************************************/
/**名稱:stop() **/
/**功能:I2C停止 **/
/********************************************************/
void stop()
{
sda=0; flash(); scl=1; flash(); sda=1; flash();
}
/********************************************************/
/**名稱:writex(uchar j) **/
/**功能:I2C寫 **/
/********************************************************/
void writex(uchar j)
{
uchar i,temp;
temp=j;
for (i=0;i<8;i++){
temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();
}
scl=0; flash(); sda=1; flash();
}
/********************************************************/
/**名稱:readx() **/
/**功能:I2C讀 **/
/********************************************************/
uchar readx()
{
uchar i,j,k=0;
scl=0; flash(); sda=1;
for (i=0;i<8;i++){
flash(); scl=1; flash();
if (sda==1) j=1;
else j=0;
k=(k<<1)|j; scl=0;
}
flash(); return(k);
}
/********************************************************/
/**名稱:clock() **/
/**功能:I2C時鐘 **/
/********************************************************/
void clock()
{
uchar i=0;
scl=1; flash();
while ((sda==1)&&(i<255))i++;
scl=0; flash();
}
/********************************************************/
/**名稱:x24c02_read(uchar address) **/
/**功能:讀x24c02 **/
/********************************************************/
uchar x24c02_read(uchar address)
{
uchar i;
start(); writex(0xa0);
clock(); writex(address);
clock(); start();
writex(0xa1); clock();
i=readx(); stop();
delay1(100);
return(i);
}
/********************************************************/
/**名稱:x24c02_write(uchar address,uchar info) **/
/**功能:寫x24c02 **/
/********************************************************/
void x24c02_write(uchar address,uchar info)
{
EA=0;
start(); writex(0xa0);
clock(); writex(address);
clock(); writex(info);
clock(); stop();
EA=1;
delay1(500);
}
//-----------------------------------24c02----------------------------
//-----------------------------------LCD------------------------------
#define LCD12864_IO P0
#define LINE1 0
#define LINE2 1
#define HIGH 1
#define LOW 0
#define CLERADISPLAY LCD12864_command(0x01);
#define uchar unsigned char
#define uint unsigned int
sbit xssjj=P2^5;
//uchar temp=33;
//uchar code weiB_a[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
//uchar code weiB_b[8]={0x80,0xa0,0x50,0x28,0x14,0x0a,0x05,0x01};
//uchar weiB_c[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
// int a=16;
//uchar data juli[3];
/********************************************************************/
sbit LCD12864_EN=P2^2; //en
sbit LCD12864_DIN=P2^0; //rs
sbit LCD12864_CLK=P2^1; //rw
sbit cs1=P2^3;
sbit cs2=P2^4;
/********************************************************************/
//void lcd_gps_wei(unsigned char wei_x,unsigned char wei_y);
void LCD12864_DELAY(unsigned int a)
{
for(;a>0;a--)
_nop_();
}
/*******************************************************************/
void LCD12864_busy(void)
{
bit BF = 0;
LCD12864_EN=0;
LCD12864_DIN=0;
LCD12864_CLK=1;
LCD12864_IO=0xff; //單片機讀數據之前必須先置高位
do
{
LCD12864_EN=1;
BF=LCD12864_IO&0x80;
LCD12864_EN=0;
} while(BF);
}
/*******************************************************************/
// 寫入命令
/*******************************************************************/
void LCD12864_command(unsigned char command)
{
LCD12864_busy();
LCD12864_EN=0;
LCD12864_DIN=0;
LCD12864_CLK=0;
LCD12864_EN=1;
LCD12864_IO=command;
LCD12864_EN=0;
}
/*******************************************************************/
// 寫入一字節數據
/*******************************************************************/
void LCD12864_data(unsigned char dat)
{
LCD12864_busy();
LCD12864_EN=0;
LCD12864_DIN=1;
LCD12864_CLK=0;
LCD12864_EN=1;
LCD12864_IO=dat;
LCD12864_EN=0;
}
void LCD12864_char(unsigned char dat)
{
if((dat>=0)&&(dat<=9))
dat|=0x30;
LCD12864_data(dat);
}
/*******************************************************************/
// 設置顯示位置 X(1~8),Y(1~4)
/*******************************************************************/
void LCD12864_address(unsigned char line,unsigned char row)
{
switch(row)
{
case 1:LCD12864_command(0x7f + line);
break;
case 2:LCD12864_command(0x8f + line);
break;
case 3:LCD12864_command(0x87 + line);
break;
case 4:LCD12864_command(0x97 + line);
default:
break;
}
}
/*******************************************************************/
// 在指定位置顯示字符串
/*******************************************************************/
void LCD12864_string(unsigned char row,unsigned char line,unsigned char *s)
{
unsigned char LCD12864_temp;
LCD12864_address(row,line);
LCD12864_temp=*s;
while(LCD12864_temp != 0x00)
{
LCD12864_data(LCD12864_temp);
LCD12864_temp=*(++s);
}
}
/********************************************************************/
void Delay_100xuS(uint x) //
{
unsigned int i,j;
for( i =0;i < x;i++ )
{
for( j =0;j<9;j++ );
}
}
/*******************************************************************/
// 初始化設置
/*******************************************************************/
void LCD12864_init(void)
{
CLERADISPLAY // clear DDRAM
LCD12864_command(0x30); // 8 bits unsigned interface,basic instrument
LCD12864_command(0x02); // cursor return
LCD12864_command(0x0c); // display,cursor on
LCD12864_command(0x03);
LCD12864_command(0x06);
CLERADISPLAY // clear DDRAM
}
void Mcu_init(void)
{
LCD12864_init();
CLERADISPLAY
CLERADISPLAY
LCD12864_init();
}
//----------------------------------------------------LCD-------------------------------------
//--------------------------------------------------Menu----------------------------------
void MainMenu()
{
CLERADISPLAY
LCD12864_string(1,1,"1.菜單");
if(priceMod==0)
LCD12864_string(7,4,"白天");
if(priceMod==1)
LCD12864_string(7,4,"夜晚");
LCD12864_string(1,2,"2.開始計費");
LCD12864_string(1,3,"3.停止計費");
LCD12864_string(1,4,"4.收費模式");
}
void ListMenu()
{
CLERADISPLAY
LCD12864_string(1,1,"1.設置價格");
//if(feeMode==0)
LCD12864_string(1,2,"2.設置輪胎周長");
//if(feeMode==1)
LCD12864_string(1,3,"3.重置計價器");
LCD12864_string(1,4,"4.返回");
}
void SetPriceMenu()
{
CLERADISPLAY
if(setPriceMod==0)
LCD12864_string(1,1,"1.白天: 元");
if(setPriceMod==1)
LCD12864_string(1,1,"1.晚上: 元");
LCD12864_string(1,2,"2.加 1元");
LCD12864_string(1,3,"3.減 1元");
LCD12864_string(1,4,"4.完成");
}
void BillingMenu()
{
CLERADISPLAY
LCD12864_string(1,1,"行駛: 千米");
LCD12864_string(1,2,"單價: 元");
LCD12864_string(1,3,"總價: 元");
}
void SetWheelMenu()
{
CLERADISPLAY
LCD12864_string(1,1,"周長: 厘米");
LCD12864_string(1,2,"加 1厘米");
LCD12864_string(1,3,"減 1厘米");
LCD12864_string(1,4,"完成");
}
void BillingData()
{
LCD12864_string(4,1,arr_distance);//路程
if(priceMod==0)
{
LCD12864_string(6,2,arr_dayPrice);//價格
}
else if(priceMod==1)
{
LCD12864_string(6,2,arr_nightPrice);
}
LCD12864_string(6,3,arr_totalCost);//總價格
//LCD12864_string(4,1,"10.9");//定位
// LCD12864_string(6,2,arr_dayPrice);//定位
//LCD12864_string(6,3,"550");//定位
}
//--------------------------------------------------Menu----------------------------------
//--------------------------------------------------數據轉換------------------------------
void ChangeDayPrice()
{
uint tpDayPrice=dayPrice*10;
arr_dayPrice[0]=tpDayPrice/10+'0';
arr_dayPrice[1]='.';
arr_dayPrice[2]=tpDayPrice%10+'0';
arr_dayPrice[3]='\0';
}
void ChangeNightPrice()
{
uint tpNightPrice=nightPrice*10;
arr_nightPrice[0]=tpNightPrice/10+'0';
arr_nightPrice[1]='.';
arr_nightPrice[2]=tpNightPrice%10+'0';
arr_dayPrice[3]='\0';
}
void ChangeWheelPerimeter()
{
uint tpWheelPerimeter=wheelPerimeter;
arr_wheelPerimeter[0]=wheelPerimeter/100+'0';
arr_wheelPerimeter[1]=wheelPerimeter/10%10+'0';
arr_wheelPerimeter[2]=wheelPerimeter%10+'0';
arr_dayPrice[3]='\0';
}
/*void ChangeKCircle()
{
uint tpCircle=kCircle;
arr_kCircle[0]=tpCircle/100+'0';
arr_kCircle[1]=tpCircle/10%10+'0';
arr_kCircle[2]=tpCircle%10+'0';
arr_dayPrice[3]='\0';
} */
void ChangeDistance()
{
uint tpDistance=(unsigned int)(distance*10);
if(tpDistance>=100)
arr_distance[0]=tpDistance/100+'0';
else
arr_distance[0]=' ';
arr_distance[1]=tpDistance/10%10+'0';
arr_distance[2]='.';
arr_distance[3]=tpDistance%10+'0';
arr_dayPrice[4]='\0';
}
void ChangeTotalCost()
{
uint tpTotalCost=totalCost;
arr_totalCost[0]=tpTotalCost/100+'0';
arr_totalCost[1]=tpTotalCost/10%10+'0';
arr_totalCost[2]=tpTotalCost%10+'0';
arr_dayPrice[3]='\0';
if(tpTotalCost>=10&&tpTotalCost<100)
arr_totalCost[0]=' ';
if(tpTotalCost<10&&tpTotalCost>=0)
{arr_totalCost[0]=' ';arr_totalCost[1]=' ';}
}
//---------------------------------------------------------------------------------------------
void Delay(void)
{
unsigned char i;
unsigned int j;
for(i=0;i<200;i++)
for(j=1000;j>0;j--);
}
void DelayM(unsigned int a)
{//延時函數 1MS/次
unsigned char i;
while( --a != 0)
{
for(i = 0; i < 125; i++); //一個 ; 表示空語句,CPU空轉。
} //i 從0加到125,CPU大概就耗時1毫秒
}
void ShowSetPrice()
{
uchar tpKEY=0;
SetPriceMenu();
ChangeDayPrice();
ChangeNightPrice();
if(setPriceMod==0)
{
LCD12864_string(6,1,arr_dayPrice);
}
else if(setPriceMod==1)
{
LCD12864_string(6,1,arr_nightPrice);
}
while(1)
{
if(KEY_1==0)
{
DelayM(20);
tpKEY=1;
while(KEY_1==0);
DelayM(20);
if(tpKEY==1)
{
if(setPriceMod==0)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -