main.c
#include<stc15f2k60s2.h>
#include<iic.h>
unsigned char code smgduan[]={~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,(~0x3F)&0x7f,(~0x06)&0x7f,(~0x5B)&0x7f,(~0x4F)&0x7f,(~0x66)&0x7f,(~0x6D)&0x7f,(~0x7D)&0x7f,(~0x07)&0x7f,(~0x7F)&0x7f,(~0x6F)&0x7f,0xff};
unsigned char smgbuffer[8];
unsigned char smgcom,keytt,ADtt;
unsigned char guang;
unsigned int chushui;
float chushuiliang,jiage;
int jiagee;
bit mode;
void display();
void keyscan();
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
TL1 = 0x20;
TH1 = 0xD1;
TF1 = 0;
TR1 = 1;
ET1=1;
EA=1;
}
void Timer1() interrupt 3
{
unsigned char P0_keeper, P2_keeper;
long tt;
P0_keeper=P0; P2_keeper=P2; P2=P2&0x1f;
display();
keytt++; if(keytt==10) {keytt=0; keyscan();}
ADtt++;
if(mode==1){tt++; if(tt==100){tt=0; chushui++;}}
else if(mode==0) {tt=0;}
P0=P0_keeper; P2=P2_keeper;
}
void display()
{
P0=0xff; P2=P2&0x1f|0xc0; P2=P2&0x1f;
P0=0xff; P2=P2&0x1f|0xe0; P2=P2&0x1f;
P0=1<<smgcom; P2=P2&0x1f|0xc0; P2=P2&0x1f;
P0=smgduan[smgbuffer[smgcom++]]; P2=P2&0x1f|0xe0; P2=P2&0x1f;
if(smgcom==8) smgcom=0;
}
void keyscan()
{
unsigned char dat,trg,cnt;
dat=P3^0xff;
trg=dat&(dat^cnt);
cnt=dat;
if(trg&0x01) {chushui=0; mode=1; P0=0x10; P2=P2&0x1f|0xa0; P2=P2&0x1f;}
else if(trg&0x02){mode=0; P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;}
}
void ADC()
{
if(ADtt==200){ADtt=0; guang=AD_read(0x01);}
if(guang<63){P00=0; P2=P2&0x1f|0x80; P2=P2&0x1f;}
else if(guang>63){P00=1; P2=P2&0x1f|0x80; P2=P2&0x1f;}
}
void xianshi()
{
if(mode==0)
{
smgbuffer[0]=20;
smgbuffer[1]=10;smgbuffer[2]=5;smgbuffer[3]=0;
chushuiliang=chushui/100+(chushui%100)*0.01;
jiage=chushuiliang*0.5;
jiagee=jiage*100;
smgbuffer[4]=jiagee/1000;
smgbuffer[5]=jiagee%1000/100+10;
smgbuffer[6]=jiagee%1000%100/10;
smgbuffer[7]=jiagee%10;
}
else if(mode==1)
{
smgbuffer[0]=20;
smgbuffer[1]=10;smgbuffer[2]=5;smgbuffer[3]=0;
smgbuffer[4]=chushui/1000;
smgbuffer[5]=chushui%1000/100+10;
smgbuffer[6]=chushui%1000%100/10;
smgbuffer[7]=chushui%10;
if(chushui==9999){mode=0;}
}
}
void main()
{
P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
Timer1Init();
while(1)
{
ADC();
xianshi();
}
}
iic.c
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
IIC_WaitAck();
}
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
IIC_WaitAck();
return da;
}
void AD_write(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_SendByte(add);
IIC_SendByte(dat);
IIC_Stop();
}
unsigned char AD_read(unsigned char add)
{
unsigned char AD_temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_SendByte(add);
IIC_Start();
IIC_SendByte(0x91);
AD_temp=IIC_RecByte();
return AD_temp;
}