购买了一个ADC0831就试着编写程序,读取电压值正确。使用NTC(3950 10K)显示温度,但是在转换阻值与查表却无法编写出正确的程序,经多次实验还是无法写出程序,实属无奈还请大神不吝赐教,给予文献作为参考不胜感激!!!单片机使用;STC89C52;
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit SCK = P1^0; //11脚数据时钟
sbit RCK = P1^1; //12脚输出时钟
sbit SI = P1^2; //14脚数据
sbit CS=P1^3; //ADC片选信号
sbit CLK=P1^4; //ADC时钟信号
sbit DO=P1^5; //ADC数据接口
unsigned int NTC_R;//定义热敏电阻阻值变量
unsigned char code segmcode[]={
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 //共阴极数码管段码0-9
};
unsigned int code Ttable[106][2]={//温度与电阻阻值对应关系表格
0,3274,//0度对应阻值32.74k
1,3111,//1度对应阻值31.11k
2,2957,//2度对应阻值29.57k
3,2812,//
4,2674,//
5,2545,
6,2422,
7,2306,
8,2196,
9,2092,
10,1993,
11,1900,
12,1811,
13,1728,
14,1648,
15,1573,
16,1501,
17,1433,
18,1369,
19,1308,
20,1250,
21,1194,
22,1142,
23,1092,
24,1045,
25,1000,//25度对应阻值10k
26,957,//26度对应阻值9.57k
27,916,
28,877,
29,840,
30,805,
31,771,
32,739,
33,709,
34,679,
35,652,
36,625,
37,600,
38,576,
39,553,
40,531,
41,510,
42,490,
43,471,
44,453,
45,435,
46,418,
47,402,
48,387,
49,372,
50,358,
51,345,
52,332,
53,320,
54,308,
55,297,
56,286,
57,276,
58,266,
59,256,
60,247,
61,238,
62,230,
63,222,
64,214,
65,207,
66,199,
67,193,
68,186,
69,180,
70,174,
71,168,
72,162,
73,157,
74, 152,
75, 147,
76, 142,
77, 137,
78, 133,
79, 128,
80, 124,
81, 120,
82, 116,
83, 113,
84, 109,
85, 106,
86, 102,//86度对应阻值1.02k
87, 99,//87度对应阻值0.99k
88, 96,
89, 93,
90, 90,
91, 88,
92, 85,
93, 82,
94, 80,
95, 78,
96, 75,
97, 73,
98, 71,
99, 69,
100,67,
101,65,
102,63,
103,61,
104,59,
105,58//105度对应阻值0.58k
};
unsigned char data bitcode[4]={0,0,0,0}; //数码管显示位置 1-4
unsigned int date,i;
void hc595(uchar num)
{
uchar i;
for(i=0;i<8;i++)
{
SI=(num<<i)&0x80;
SCK=0;
_nop_();
SCK=1;
SCK=0;
}
RCK=0;
_nop_();
RCK=1;
RCK=0;
}
unsigned char ad_conv(void){
unsigned char i,com;
CS=1;
CLK=0; _nop_(); _nop_();
CS=0; _nop_(); _nop_();//CS置低,启动转换
CLK=1; _nop_(); _nop_();
CLK=0; _nop_(); _nop_();//第一个下降沿,准备输出数据
CLK=1; _nop_(); _nop_();
for(i=8;i>0;i--){
CLK=1; //第二至九个下降沿
_nop_();
_nop_();
com<<=1;//左移,先采最高位
if(DO)com++;//采当前数据
CLK=0;
_nop_();
_nop_();
}
CS=1;
_nop_();
_nop_();
return com;
}
//数码管显:
void display()
{
static uchar i=0;
bitcode[0]=segmcode[date/100] ;
bitcode[1]=segmcode[date%100/10];
bitcode[2]=segmcode[date%10];
P0=0xFF;
hc595(0x01<<i);
P0=bitcode[i];
i++;
i%=3;
}
void main(void)
{
uchar i = 0;
while(1)
{
i++;
if(i==100)
{
i=0;
date=ad_conv()*100;
date=date/51;
}
display(); //显示
}
}
|