You are on page 1of 5

#include <REGX51.

H>
#define ADC_OUT1 P1_7
#define ADC_OUT2 P1_6
#define ADC_OUT3 P1_5
#define ADC_OUT4 P1_4
#define ADC_OUT5 P1_3
#define ADC_OUT6 P1_2
#define ADC_OUT7 P1_1
#define ADC_OUT8 P1_0
#define ADC_OE P3_0
#define ADC_EOC P3_1
#define ADC_START P3_2
#define ADC_ALE P3_3
#define ADC_ADDC P3_4
#define ADC_ADDB P3_5
#define ADC_ADDA P3_6
unsigned char M1[10]={0xc0,0xf9,0x24,0x30,0x19,0x12,0x02,0xf8,0x00,0x10};
unsigned char M2[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned int volt,volt2,volt3,y,x,z;
unsigned int data f[11]={7353,7204,7061,6928,6815,6690,6550,6444,6294,6156,5977};//mng gi tr ca tn
s.Tht ra th chnh xc ngi ta cn s dng mng gm 100 phn t,tng ng vi gi tr m t 0%
n 99%.
unsigned char doam,i;
unsigned int tanso;
void tre(int x)
{
int i;
for(i=0;i<=x;i++)
{;}
}

void dienap()
{
ADC_ADDA = 0;
ADC_ADDB = 0 ;
ADC_ADDC = 0;
ADC_ALE = 1;
ADC_START = 1;
tre(1);
ADC_ALE = 0;
ADC_START = 0;

while(ADC_EOC == 0)
{
;
}
ADC_OE = 1;
x = P1;
volt = x/10;
P3_7 = 0;
if (volt <= 18)
{
P3_7 = 1;
}
tre(1);
if (volt >= 24)
{
P3_7 = 1;
}
tre(1);
ADC_OE = 0;
}
void hienthidienap()
{
P2_6 = 1;
//led 1 sang
P0=M1[volt/10];tre(12);
P0=0xff;
P2_6 = 0;
P2_7 = 1;
//led 2 sang
P0=M1[volt%10];tre(12);
P0=0xff;
P2_7 = 0;
}

void nhietdo()
{
ADC_ADDA = 1;
ADC_ADDB = 0 ;
ADC_ADDC = 0;
ADC_ALE = 1;
ADC_START=1;
tre(1);

ADC_ALE = 0;
ADC_START=0;
while(ADC_EOC==0)
{
;
}
ADC_OE = 1;
y = P1;
P3_7 = 0;
if (y >= 30)
{
P3_7 = 1;
}
tre(1);
ADC_OE = 0;
}

void hienthinhietdo()
{
P2_4 = 1;
//led 1 sang
P0=M1[y/10];tre(12);
P0=0xff;
P2_4 = 0;
P2_5 = 1;
//led 2 sang
P0=M1[y%10];tre(12);
P0=0xff;
P2_5 = 0;
}
//--------------------------HAM TINH DOAM-----------------------unsigned char do_am(void)
{
TL1=0;
//Byte thap cua bo dinh thoi = 0 Gia tri ban dau bang 0
TH1=0;
//Byte cao cua bo dinh thoi = 0
TR1=1;
//cho phep bat dau dem su kien trong TIMER1
tre(1000);
nhiu ???

1s

//tr 1000ms = 1s.ci ny xme li nh.a ko bik hm tr em vit l tr bao

TR1=0;
tanso=(TH1*256+TL1)+150;

//ngung dem su kien trong TIMER1


//tan so bang voi so xung dem duoc trong thoi gian

if(tanso<5977) {doam=99;}
else if(tanso>7353) {doam=00;}
else
{
i=1;

//gioi han tren cua do am


//gioi han duoi cua do am

while(i<11)
{
if(tanso>f[i])//tm tn s trong mng gn vi tn s o c nht
{
//Ni suy tuyn tnh suy ra m
doam=10*i-10*(tanso-f[i])/(f[i-1]-f[i]);//tnh ton theo mu ngi ta a ra
cng thc thi,khi bn tm,bn tm th nghin cu datasheet
break;
}
else i++;
}
}
return doam;
}

//Nu vit hin th ng th k c hm tnh m sai th n cng s hin thi m =0%


void hienthidoam()
{
P2_2 = 1;
//led 1 sang
P0=M1[doam/10];tre(12);
P0=0xff;
P2_2 = 0;
P2_3 = 1;
//led 2 sang
P0=M1[doam];tre(12);
P0=0xff;
P2_3 = 0;
}

void main()
{
int i;
while(1)
{
TMOD = 0x51;
//0x51 = 0101 0001
//Xem ti liu v 8951
// Thanh ghi ch nh thi thit lp ch hot ng ca b inh thi
//thit lp nh trn th
// +Timer hot ng ch m s kin
// +Ch inh thi 16 bit
dienap();
for ( i =0;i<=200;i++)
{
hienthidienap();
hienthinhietdo();
hienthidoam();

}
nhietdo();
for
( i =0;i<=200;i++)
{
hienthidienap();
hienthinhietdo();
hienthidoam();
}
do_am();
for ( i =0;i<=200;i++)
{
hienthidienap();
hienthinhietdo();
hienthidoam();
}
}
}

You might also like