51開發(fā)板測電壓,就是外部0-5V能被開發(fā)板顯示在數(shù)碼管上

#include "reg52.h"
#include "iic.h"
#define TSMG 500
#define uchar unsigned char?
#define?uint unsigned int
#define PCF8591 0x90
uchar dat=0;
sbit S4=P3^3;
sbit P2_0=P2^0;
sbit P2_1=P2^1;
uchar D[32];
double dianya;
uchar code smg1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc0,0xc6,0x86,0x8e,0xbf,0x7f};
uchar code smg2[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
uint smg_vlot=0;
uchar adc_value=0;
float adc_vlot=0;
uchar mode=1;
uchar stat_smg=1;
uchar control;
uchar shi,ge,bai;
void Hc573(uchar channel,uchar dat)
{
P0=dat;
switch(channel)
{
case 0:
P2=P2&0x1f|0x00;
break;
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;
}
P2=P2&0x1f|0x00;
}
void delay(uint t)
{
while(t--);
}
void smgsm(uchar pos,uchar date)
{
Hc573(6,0x01<<pos);
Hc573(7,date);
delay(TSMG);
Hc573(6,0x01<<pos);
Hc573(7,0xff);
}
void smgall(uchar date)
{
Hc573(6,0xff);
Hc573(7,date);
}
void smglook()
{
smgsm(7,smg1[ge]);
smgsm(6,smg1[shi]);
smgsm(5,smg2[bai]);
smgsm(4,0xff);
smgsm(3,0xff);
smgsm(2,0xbf);
smgsm(1,smg1[mode]);
smgsm(0,0xbf);
}
bit write(uchar sls,uchar c)
{
IIC_Start();
IIC_SendByte(sls);??
IIC_WaitAck();
IIC_SendByte(c);?
IIC_WaitAck();
IIC_Stop();?
return(1);
}
uchar read(uchar sls)
{
IIC_Start();
IIC_SendByte(sls+1);?
IIC_WaitAck();
dat=IIC_RecByte();?
IIC_SendAck(1);?
IIC_Stop();?
return dat;
}
void key()
{
if(S4==0)
{
delay(TSMG);
if(S4==0)
{
if(mode==1)
{
mode=2;
control=0x00;
Hc573(4,~(0x02));
}
else if(mode==2)
{
mode=3;
control=0x03;
Hc573(4,~(0x04));
}
else if(mode==3)
{
control=0x01;
mode=1;
Hc573(4,~(0x01));
}
while(S4==0)
{
smglook();
}
}
}
}
void Init_sys()
{
Hc573(0,0x00);
Hc573(4,0xfe);
Hc573(5,0x00);
smgall(0xff);
}
void main()
{
Init_sys();
while(1)
{
write(PCF8591,control);
D[0]=read(PCF8591);
dianya=(double)D[0];
dianya=(dianya*500/255);
ge=(int)dianya%10;
shi=(int)dianya/10%10;
bai=(int)dianya/100;
key();
smglook();
}
}