Home > Back-end >  Microcontroller program measuring temperature, completely according to writing code, the result of m
Microcontroller program measuring temperature, completely according to writing code, the result of m

Time:11-01

# include
# include
# define uchar unsigned char
# define uint unsigned int
Sbit DQ=P1 ^ 7;
Bit DS18B20_IS_OK=1;
Bit Plus_Minus=0;
Bit Point=0;
Uchar time;
Uchar Number, Decimal;
Uchar Temp_Value []={0, 0};
Uchar Display_Digit []={0,0,0,0 x0c};
Uchar Error []={0 x86, 0 xaf, 0 xaf, 0 x0ff};
Uchar code Num_Tab []={xa4 xc0 0, 0 xf9, 0, 0, xb0 x99 0, 0 x92, 0 x82, 0 xf8, 0 x80, 0 x90, 0 XFF, 0 XBF, 0 x70};
Void delaynms (int x)
{
Uchar I;
While (x -)
for(i=0; i<123; I++);
}
Bit Init_DS18B20 ()
{
Bit status;
DQ=1;
For (time=0; Time<3; Time++);
DQ=0;
For (time=0; Time<150; Time++);
DQ=1;
For (time=0; Time<15. Time++);
The status=DQ;
For (time=0; Time<100; Time++);
DQ=1;
Return the status;
}
Unsigned char ReadOnebyte ()
{
Unsigned char I, dat.
for(i=0; i<8; I++)
{DQ=1;
_nop_();
DQ=0;
Dat & gt;>=1;
_nop_();
DQ=1;
For (time=0; Time<2; Time++);
If (DQ==1)
Dat |=0 x80;
The else
Dat |=0 x00;
For (time=0; Time<15. Time++);
}
Return dat.
}
Uchar WriteOnebyte uchar (dat)
{
Unsigned char I=0;
for(i=0; i<8; I++)
{
DQ=1;
_nop_();
DQ=0;
DQ=dat& 0 x01;
For (time=0; Time<10; Time++);
DQ=1;
For (time=0; Time<2; Time++);
Dat & gt;>=1;
}
For (time=0; Time<15. Time++);
}
Void ReadyReadTemp (void)
{
Init_DS18B20 ()
WriteOnebyte (0 XCC);
X44 WriteOnebyte (0);
For (time=0; Time<100; Time++);
Init_DS18B20 ();
WriteOnebyte (0 XCC);
The xbe WriteOnebyte (0);
}
Void Read_Temperature ()
{
If (Init_DS18B20 ()==1)
DS18B20_IS_OK=0;
The else
{
DS18B20_IS_OK=1;
ReadyReadTemp ();
Temp_Value [0]=ReadOnebyte ();
Temp_Value [1]=ReadOnebyte ();
Plus_Minus=0;
If ((Temp_Value [1] & amp; 0 xf8)==0 xf8)
{
Plus_Minus=1;
Temp_Value [1]=~ Temp_Value [1].
Temp_Value [0]=(~ Temp_Value [0]) + 1;
If (Temp_Value [0]==0 x00)
Temp_Value [1] + +;
}
Number=Temp_Value [1] * 16 + Temp_Value [0]/16;
A Decimal=(Temp_Value [0] % 16) * 10/16;
}
}
Void Data_Process ()
{
If (Plus_Minus==1)
{
Display_Digit [0]=0 x0b;
If (Number> 9)
{
Display_Digit [1]=Number/10;
Display_Digit [2]=% Number 10;
Point=0;
}
The else
{
Display_Digit [1]=Number;
Display_Digit [2]=Decimal;
Point=1;
}
}
The else
{
If (Number> 99)
{
Display_Digit [0]=Number/100;
Display_Digit [1]=(Number/10) % 10;
Display_Digit [2]=% Number 10;
Point=0;
}
Else if (Number> 9)
{
Display_Digit [0]=Number/10;
Display_Digit [1]=% Number 10;
Display_Digit [2]=Decimal;
Point=1;
}
The else
{
Display_Digit [0]=0 x0a;
Display_Digit [1]=Number;
Display_Digit [2]=Decimal;
Point=1;
}
}
}
Void Data_Display (void)
{
The P2=0 xf7;
P0=Num_Tab [Display_Digit [0]];
Delaynms (2);
P0=0 XFF;
The P2=0 XFB;
If (Point==1)
{
P0=Num_Tab [Display_Digit [1]] & amp; 0 x7f;
}
The else
{
P0=Num_Tab [Display_Digit [1]];
}
Delaynms (2);
P0=0 XFF;
The P2=0 XFD;
P0=Num_Tab [Display_Digit [2]];
Delaynms (2);
P0=0 XFF;
The P2=0 xfe;
P0=Num_Tab [Display_Digit [3]];
Delaynms (2);
P0=0 XFF;
The P2=0 XFF;
}
Void Error_Display (void)
{
Uchar I;
The P2=0 xf7;
for(i=0; i<4. I++)
{
P0=Error [I];
Delaynms [3].
The P2=P2 & gt;> 1;
}
}
Void main (void)
{
Delaynms (5)
While (1)
{
Read_Temperature ();
If (DS18B20_IS_OK==1)
{
Read_Temperature ();
Data_Process ();
Data_Display ();
}
The else
{
Error_Display ()
}
}
}

CodePudding user response:

Estimates that you are using keil compiler, I did not install keil, nor do compile testing, hope you to stick the wrong information, we help you to analyze,
In addition, a rough look at the code, also did not feel where there is a serious problem, problem of estimate on the compiler USES probability is bigger,
Best post error have a look at it
  • Related