|
200黑币
#define INT8U unsigned char
#define INT16U unsigned int
#define INT32U unsigned long
#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
#include <math.h>
#include <ADC0832.C>
sbit DS =P3^5;
sbit SH_CP =P3^6;
sbit ST_CP =P3^7;
#define M_C 1388
#define B_C -5
#define K_C 2
#define M_F 3085.7
#define M_P -1.1962
code INT8U SEG_CODE[] =
{ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 };
INT8U Disp_Buff[] ={0,0,0};
extern INT8U GeT_CHx_AD_Value(INT8U ch);
void delay_ms(INT16U x){INT8U t;while(x--) for(t = 0; t<120; t++);}
void Serial_Input_595(INT8U dat)
{
INT8U i;
for(i = 0; i < 8; i++)
{ if (dat & 0x80) DS = 1; else DS = 0;
dat <<= 1;
SH_CP = 0; _nop_();_nop_();
SH_CP = 1; _nop_();_nop_();
}
SH_CP = 0;
}
void Parallel_output_595()
{
ST_CP= 0; _nop_();
ST_CP= 1; _nop_();
ST_CP= 0; _nop_();
}
float Get_Distance()
{
INT8U i; float AD_Sum = 0.0;
for (i = 0; i < 10; i++) AD_Sum+= Get_CHx_AD_Value(0);
return M_C / (AD_Sum / 10.0 + B_C) - K_C;
}
void DEC_TO_3DIGIT(INT16U d)
{
Disp_Buff[0] = 0; Disp_Buff[1] = 0; Disp_Buff[2] = 0;
while (d >= 100){ d -= 100;++Disp_Buff[2]; }
while (d >= 10) { d -= 10; ++Disp_Buff[1]; }
Disp_Buff[0] = d;
}
void main()
{
float d;
delay_ms(700);
while (1)
{ d = Get_Distance();
DEC_TO_3DIGIT((INT16U)(d*10));
Serial_Input_595(SEG_CODE[Disp_Buff[0]]);
Serial_Input_595(SEG_CODE[Disp_Buff[1]] & 0x7F);
Serial_Input_595(SEG_CODE[Disp_Buff[2]]);
Parallel_Output_595();
}
}
|
|