購(gòu)買了一個(gè)ADC0831就試著編寫程序,讀取電壓值正確。使用NTC(3950 10K)顯示溫度,但是在轉(zhuǎn)換阻值與查表卻無法編寫出正確的程序,經(jīng)多次實(shí)驗(yàn)還是無法寫出程序,實(shí)屬無奈還請(qǐng)大神不吝賜教,給予文獻(xiàn)作為參考不勝感激!!!單片機(jī)使用;STC89C52;
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit SCK = P1^0; //11腳數(shù)據(jù)時(shí)鐘
sbit RCK = P1^1; //12腳輸出時(shí)鐘
sbit SI = P1^2; //14腳數(shù)據(jù)
sbit CS=P1^3; //ADC片選信號(hào)
sbit CLK=P1^4; //ADC時(shí)鐘信號(hào)
sbit DO=P1^5; //ADC數(shù)據(jù)接口
unsigned int NTC_R;//定義熱敏電阻阻值變量
unsigned char code segmcode[]={
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 //共陰極數(shù)碼管段碼0-9
};
unsigned int code Ttable[106][2]={//溫度與電阻阻值對(duì)應(yīng)關(guān)系表格
0,3274,//0度對(duì)應(yīng)阻值32.74k
1,3111,//1度對(duì)應(yīng)阻值31.11k
2,2957,//2度對(duì)應(yīng)阻值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度對(duì)應(yīng)阻值10k
26,957,//26度對(duì)應(yīng)阻值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度對(duì)應(yīng)阻值1.02k
87, 99,//87度對(duì)應(yīng)阻值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度對(duì)應(yīng)阻值0.58k
};
unsigned char data bitcode[4]={0,0,0,0}; //數(shù)碼管顯示位置 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置低,啟動(dòng)轉(zhuǎn)換
CLK=1; _nop_(); _nop_();
CLK=0; _nop_(); _nop_();//第一個(gè)下降沿,準(zhǔn)備輸出數(shù)據(jù)
CLK=1; _nop_(); _nop_();
for(i=8;i>0;i--){
CLK=1; //第二至九個(gè)下降沿
_nop_();
_nop_();
com<<=1;//左移,先采最高位
if(DO)com++;//采當(dāng)前數(shù)據(jù)
CLK=0;
_nop_();
_nop_();
}
CS=1;
_nop_();
_nop_();
return com;
}
//數(shù)碼管顯:
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(); //顯示
}
}
|