main.c
#include"stc15f2k60s2.h"
#include"iic.h"
unsigned char code smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,~0x3e,~0x73,0xff};
unsigned char smgbuffer[8];
unsigned char smgcom,keytt,ADtt;
unsigned char Rb2;
bit mode;
unsigned int flag,NE555tt,zheng,fan,zheng_1,fan_1,T,f;
void display();
void keyscan();
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xC4;
TH0 = 0xFF;
TF0 = 0;
TR0 = 0;
}
void Timer0() interrupt 1
{
if(P34==0)
{
fan++;
if(flag==0){flag=1; zheng_1=zheng; zheng=0;}
}
if(P34==1)
{
zheng++;
if(flag==1){flag=0; fan_1=fan; fan=0;}
}
}
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_kepper=0,P2_kepper=0;
P0_kepper=P0; P2_kepper=P2; P2=P2&0x1f;
display();
keytt++; if(keytt==10) {keytt=0; keyscan();}
ADtt++;
NE555tt++;
{
if(NE555tt==969){TR0=1; ET0=1;}
if(NE555tt==999){NE555tt=0; TR0=0; ET0=0; T=(zheng_1+fan_1)*5; f=1000000/T;}
}
P0=P0_kepper; P2=P2_kepper;
}
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){P0=0; P2=P2&0x1f|0x80; P2=P2&0x1f;}
else if(trg&0x02){P0=0x55; P2=P2&0x1f|0x80; P2=P2&0x1f;}
else if(trg&0x04){P0=0xaa; P2=P2&0x1f|0x80; P2=P2&0x1f;}
else if(trg&0x08){mode=~mode;}
}
void ADC()
{
if(ADtt==200)
{
ADtt=0;
Rb2=AD_read(0x03);
}
}
void xianshi()
{
if(mode==0)
{
smgbuffer[0]=10; smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=smgbuffer[4]=12;
smgbuffer[5]=(int)(Rb2*1.96+0.5)/100; smgbuffer[6]=(int)(Rb2*1.96+0.5)%100/10; smgbuffer[7]=(int)(Rb2*1.96+0.5)%10;
}
else if(mode==1)
{
smgbuffer[0]=11;
smgbuffer[3]=f/10000; smgbuffer[4]=f%10000/1000; smgbuffer[5]=f%10000%1000/100; smgbuffer[6]=f%10000%1000%100/10; smgbuffer[7]=f%10;
}
}
void main()
{
P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
Timer0Init();
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;
}
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);
}
return da;
}
unsigned char AD_read(unsigned char add)
{
unsigned char AD_temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
AD_temp=IIC_RecByte();
IIC_Stop();
return AD_temp;
}
void AD_write(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
unsigned char EEPROM_read(unsigned char add)
{
unsigned char EEPROM_temp;
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xA1);
IIC_WaitAck();
EEPROM_temp=IIC_RecByte();
IIC_Stop();
return EEPROM_temp;
}
void EEPROM_write(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}