#include<reg51.h>
#include<type.h>
#include<intrins.h>
#include<lcddisp.h>
#include<ds1302.h>
uint count = 0;
uchar time[7] = {0x09,0x1,0x1,0x1,0x0,0x0,0x0};//保存时间的数组
uchar univalent[4] = {1,40,0,4};//保存单价的数组,[0]-里程单价整数位,[1]-里程单价小数位
//[2]-时间单价整数位,[3]-时间单价小数位
uchar code time_pos[7] = {4,-1,7,10,4,7,10}; //各时间在液晶上的显示位置
uchar code money_pos[4] = {4,7,4,7}; //计价器价格各位在液晶上的显示位置
uchar config_state; //当前配置状态,0-配置时间,1-配置里程单价,2-配置等待时间单价
bit config_ok = 0;
void INT0_init(void);
void INT1_init(void);
void Delay(uint);
void System_init(void);
void Time_Show(uchar time_data[7]);
void Univalent_Show(uchar univalent[4]);
void Run_Show_Time(uchar time_data[7]);
void Run_Show_Time(uchar time_data[7])
{
WriteChar('/',2,0);
WriteChar(':',10,0);
WriteChar(':',13,0);
WriteInt(BcdToInt(time_data[2]),1, 0,0);
WriteInt(BcdToInt(time_data[3]),1, 3,0);
WriteInt(BcdToInt(time_data[4]),1, 8,0);
WriteInt(BcdToInt(time_data[5]),1,11,0);
WriteInt(BcdToInt(time_data[6]),1,14,0);
}
void Univalent_Show(uchar univalent[4])
{
WriteChar('.',5,0);
WriteString("Y/km",9,0);
WriteChar('.',5,1);
WriteString("Y/min",9,1);
WriteDecimal(univalent[0],0,5,0);
WriteDecimal(univalent[1],1,5,0);
WriteDecimal(univalent[2],0,5,1);
WriteDecimal(univalent[3],1,5,1);
}
void Time_Show(uchar time_data[7])
{
WriteChar('/',6,0);
WriteChar('/',9,0);
WriteChar(':',6,1);
WriteChar(':',9,1);
WriteInt(BcdToInt(time_data[0]),1, 4,0);
WriteInt(BcdToInt(time_data[2]),1, 7,0);
WriteInt(BcdToInt(time_data[3]),1,10,0);
WriteInt(BcdToInt(time_data[4]),1, 4,1);
WriteInt(BcdToInt(time_data[5]),1, 7,1);
WriteInt(BcdToInt(time_data[6]),1,10,1);
}
void System_init(void)
{
RstLcd();
ClrLcd();
/* WriteString("welcome to ",3,0);
WriteString("use the product",0,1);
Delay(60000);
Delay(60000);
ClrLcd();
WriteString("for the 1st use",0,0);
WriteString("reset system...",0,1);
Delay(60000);
Delay(60000);
ClrLcd();
*/
config_state = 0; //配置时间
config_ok = 0;
Time_Show(time);
LcdPos(5,0);
SetCur(3);
INT1_init();
while(config_ok == 0); //等待配置时间结束标志
ClrLcd();
SetCur(1);
WriteString("set time over...",0,0);
Delay(60000);
ClrLcd();
config_state = 1;//配置单价
config_ok = 0;
Univalent_Show(univalent);
LcdPos(4,0);
SetCur(3);
while(config_ok == 0); //等待配置单价结束标志
ClrLcd();
SetCur(1);
WriteString("set syetem over",0,0);
Delay(60000);
ClrLcd();
INT0_init();
}
void Delay(uint dtime)
{
uint temp;
for(temp=0;temp<dtime;temp++);
}
void INT0_init(void)
{
EA = 0;
IT0 = 1;
EX0 = 1;
EA = 1;
}
void INT1_init(void)
{
EA = 0;
IT1 = 1;
EX1 = 1;
EA = 1;
}
void main()
{
uchar time_tmp[7],i;
System_init();
DS1302_GetData(time);
while(1)
{
// WriteInt(count,0,0,0);
DS1302_GetData(time_tmp);
if(time[6] != time_tmp[6])
{
Run_Show_Time(time_tmp);
for(i = 0;i < 7;i++)
time[i] = time_tmp[i];
}
}
}
static void INT0_ser(void) interrupt 0
{
count++;
}
static void INT1_ser(void) interrupt 2
{
uchar month_day[12]={31,0,31,30,31,30,31,31,30,31,30,31};
char result;
uchar i;
static uchar pos;//配置到哪一项
if(config_state == 0) //配置时间
{
if(!(P2 & 0x20)) //++
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x20))
{
result = BcdToInt(time[pos]) + 1;
if((pos == 0 && result == 50) || (pos == 4 && result == 24) || (pos == 5 && result == 60) || (pos == 6 && result == 60))
result = 0;
else if(pos == 2 && result == 13)
result = 1;
else if(pos == 3)
{
month_day[1] = (time[0] % 4 == 0 && time[0] % 100 != 0) || time[0] % 400 == 0 ? 29:28;//判断润平年
if(result == month_day[time[2] - 1] + 1)
result = 1;
}
time[pos] = IntToBcd(result);
}
else;
}
else if(!(P2 & 0x40)) //--
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x40))
{
result = BcdToInt(time[pos]) - 1;
if((pos == 0 && result == -1))
result = 49;
else if(pos == 2 && result == 0)
result = 12;
else if(pos == 3)
{
month_day[1]=(time[0] % 4 == 0 && time[0] % 100 != 0) || time[0] % 400 == 0 ? 29:28;//判断润平年
if(result == 0)
result = month_day[time[2] - 1];
}
else if(pos == 4 && result == -1)
result = 23;
else if((pos == 5 || pos == 6) && result == -1)
result = 59;
else
;
time[pos] = IntToBcd(result);
}
}
else if(!(P2 & 0x08)) //NEXT
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x08))
{
pos++;
pos %= 7;
if(pos == 1)
pos = 2;
}
}
else if(!(P2 & 0x10)) //PREVIOUS
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x10))
{
pos--;
if(pos == -1)
pos = 6;
if(pos == 1)
pos = 0;
}
}
else if(!(P2 & 0x80)) //ENTER
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x80))
{
config_ok = 1;
pos = 0;
DS1302_Reset();
DS1302_SettingData(time);
}
}
WriteInt(BcdToInt(time[pos]),1, time_pos[pos],pos > 3 ?1:0 );
LcdPos(time_pos[pos]+1,pos > 3 ?1:0);
}
else if(config_state == 1) //配置单价
{
if(!(P2 & 0x20)) //++
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x20))
{
result = univalent[pos] + 1;
if(((pos == 0 || pos == 2) && result == 10) || ((pos == 1 || pos == 3) && result == 100))
result = 0;
univalent[pos] = result;
}
else;
}
else if(!(P2 & 0x40)) //--
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x40))
{
result = univalent[pos] - 1;
if((pos == 0 || pos == 2) && result == -1)
result = 9;
else if((pos == 1 || pos == 3) && result == -1)
result = 99;
univalent[pos] = result;
}
}
else if(!(P2 & 0x08)) //NEXT
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x08))
{
pos++;
pos %= 4;
}
}
else if(!(P2 & 0x10)) //PREVIOUS
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x10))
{
pos--;
if(pos == -1)
pos = 3;
}
}
else if(!(P2 & 0x80)) //ENTER
{
for(i = 0;i < 100;i++);
if(!(P2 & 0x80))
{
config_ok = 1;
pos = 0;
}
}
WriteDecimal(univalent[pos],pos % 2,5,pos > 1?1:0);
LcdPos(money_pos[pos],pos > 1?1:0);
}
}
评论0