来了个大佬666
是这样的程序的确不是完整的,我上面只发了有关AD转换的部分,数码管显示部分我想着关系不大就省略没发出来,那个阻值表是我用的NTC热敏电阻的温度—阻值对应表,1到100度对应的阻值,是问厂家那里拿的;p1口的状态我改了高阻态但是还是不行;还有测温的电路图我不太会分析,AD转换得到的Vout和Rt热敏间的关系怎样求,能麻烦大佬帮忙看下吗,谢谢大佬回复
完整程序:
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr ADC_CONTR = 0xBC;
sfr ADC_RES = 0xBD;
sfr ADC_RESL = 0xBE;
sfr P1ASF = 0x9D;
sfr CLK_DIV = 0x97;
sbit STB = P3^3;
sbit CLK = P3^6;
sbit DIO = P3^7;
sbit KEY = P1^5;
#define ADC_POWER 0x80
#define ADC_FLAG 0x10
#define ADC_START 0x08
#define ADC_SPEED 0x60
WORD num;
float n;
uchar u = 1;
void display(uchar K);
void delay(WORD n);
uchar code Duanma[10]={0xfa,0x82,0xb9,0xab,0xc3,0x6b,0x7b,0xa2,0xfb,0xe3};
uchar code zhuzhi[101]={65.83,62.54,59.43,56.49,53.72,51.10,48.62,46.28,
44.06,41.96,39.97,38.09,36.31,34.63,33.03,31.51,
30.07,28.71,27.41,26.18,25.02,23.91,22.86,21.86,
20.90,20.00,19.14,18.32,17.54,16.80,16.10,15.42,
14.78,14.17,13.59,13.04,12.51,12.00,11.52,11.06,
10.62,10.20,9.80,9.42,9.05,8.70,8.37,8.05,7.74,
7.45,7.17,6.90,6.65,6.40,6.17,5.94,5.73,5.52,5.32,5.13,
4.95,4.77,4.61,4.45,4.29,4.14,4.00,3.86,3.73,3.61,
3.48,3.37,3.25,3.15,3.04,2.94,2.85,2.75,2.67,2.58,
2.50,2.42,2.34,2.27,2.20,2.13,2.06,2.00,1.94,1.88,
1.82,1.77,1.71,1.66,1.61,1.56,1.52,1.47,1.43,1.39,
1.35};
void send_8bit(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
if(dat&0x01)
{
DIO = 1;
}
else
{
DIO = 0;
}
CLK = 0;
CLK = 1;
dat=dat>>1;
}
}
void command(uchar com)
{
STB = 1;
STB = 0;
send_8bit(com);
}
void display(uchar K)
{
uchar ge,shi,bai;
ge = K%10;
shi = (K/10)%10;
bai = (K/100)%10;
command(0x03);
command(0x40);
command(0xc6);
send_8bit(Duanma[bai]);
send_8bit(0x00);
send_8bit(0x00);
send_8bit(0x00);
send_8bit(Duanma[shi]);
send_8bit(0x00);
send_8bit(Duanma[ge]);
send_8bit(0x00);
command(0x8f);
STB = 1;
}
void init_ADC()
{
P1ASF = 0x06;
delay(2);
ADC_RES = 0;
ADC_RESL = 0;
}
void resetADC(BYTE ch)
{
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = ADC_SPEED | ADC_START | ADC_POWER | ch;
}
void delay(WORD n)
{
uint x;
while(n--)
{
x = 500;
while(x--);
}
}
void get_result()
{
while (!(ADC_CONTR & ADC_FLAG));
ADC_CONTR &= ~ADC_FLAG;
num = (ADC_RES<<2)|ADC_RESL;
}
WORD lvbo()
{
WORD count;
uchar i = 0;
for(i = 0;i<32;i++)
{
resetADC(u);
delay(2);
get_result();
count += num;
}
count = count>>5;
return count;
}
void chselect()
{
if(KEY == 0)
{
delay(5);
if(KEY == 0)
{
while(KEY == 0);
if(2<++u) u = 1;
}
}
}
void data_change()
{
WORD i;
i = lvbo();
n = i/(55-0.05*i);
}
void cha_biao()
{
uint low,high,middle;
low = 0;
high = 100;
middle = (high + low)/2;
if((n<=zhuzhi[low])&&(zhuzhi[high]<=n))
{
while((high - low) != 1)
{
if(n < zhuzhi[middle])
{
low = middle+1;
middle = (high + low)/2;
}
else if(zhuzhi[middle] < n)
{
high = middle-1;
middle = (high + low)/2;
}
else
{
if(n == zhuzhi[middle])
{
display(middle);
return;
}
}
}
if((zhuzhi[low]-n)<(n-zhuzhi[high]))
{
display(low);
return;
}
if((n-zhuzhi[high])<(zhuzhi[low]-n))
{
display(high);
return;
}
}
else
{
if(n<zhuzhi[high])
{
display(high);
return;
}
if(zhuzhi[low]<n)
{
display(low);
return;
}
}
}
void main()
{
P1M1 = 0x06;
P1M0 = 0x00;
KEY = 1;
init_ADC();
while(1)
{
chselect();
lvbo();
resetADC(u);
delay(2);
get_result();
data_change();
cha_biao();
delay(6);
}
}C:\Users\Administrator\Desktop\01 |