|
#include<reg52.h>
#define u8 unsigned char
sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;
u8 flag=0;
u8 TRH0=0,TRL0=0;
u8 code LedChar[18] =
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
extern void I2C_Start();
extern void I2C_Stop();
extern unsigned char I2CReadNAK();
extern bit I2C_Write(u8 dat);
extern u8 I2C_ReadAck();
void Delay(unsigned int t)//延時(shí)函數(shù)
{
while(t)
t--;
}
void Chosse573(u8 i)//573選擇使能
{
switch(i)
{
case 4: P2=(P2&0X1F)|0X80;break;
case 5: P2=(P2&0X1F)|0XA0;break;
case 6: P2=(P2&0X1F)|0XC0;break;
case 7: P2=(P2&0X1F)|0XE0;break;
}
}
void SMG_XZ(u8 pos,u8 value)//數(shù)碼管段選和位選
{
Chosse573(6);
P0=0X01<<pos;
Chosse573(7);
P0=value;
}
void StopLed()//關(guān)閉Led
{
Chosse573(4);
P0=0xff;
}
void InitSystem()//關(guān)閉蜂鳴器
{
Chosse573(5);
P0=0X00;
}
u8 ADC_Value(u8 channel)//讀取PCF8951數(shù)據(jù)
{
u8 value;
I2C_Start();
if(!I2C_Write(0x48<<1))
{
I2C_Stop();
return 0;
}
I2C_Write(0x40|channel);
I2C_Start();
I2C_Write(0x48<<1);
I2C_ReadAck();
value=I2CReadNAK();
I2C_Stop();
return value;
}
void ADTrans(u8 dat)//數(shù)據(jù)轉(zhuǎn)換
{
dat=(dat*25)/255;
SMG_XZ(0,LedChar[ dat / 10 ]);
Delay(500);
SMG_XZ(1,0x7f);
Delay(500);
SMG_XZ(2,LedChar[ dat % 10 ]);
Delay(500);
}
void KeyDriver()//按鍵函數(shù)
{
u8 value;
if(S7==0)
{
Delay(500);
if(S7==0)
{
value=ADC_Value(0);
ADTrans(value);
}
}
if(S6==0)
{
Delay(500);
if(S6==0)
{
value=ADC_Value(3);
ADTrans(value);
}
}
}
void main()
{
EA=1;
StopLed();
InitSystem();
while(1)
{
KeyDriver();
}
}
|
|