新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > PIC16F877A-SPI-TC77(溫度傳感器)

PIC16F877A-SPI-TC77(溫度傳感器)

作者: 時(shí)間:2016-11-13 來(lái)源:網(wǎng)絡(luò) 收藏
---------------------------------借鑒,TCK(0-1024℃)分辨率:0.25℃
TC77:分辨率0.0625℃

#include
#define CS RC2
void initPORT();
void initMSSP();
void delay(unsigned char time);
void display(float data);
float ReadTC77();
const unsigned char disp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void main()
{
float temp;
initPORT();
initMSSP();
while(1)
{
temp=ReadTC77();
display(temp);
}
}
/*初始化PIC單片機(jī)端口*/
void initPORT()
{
TRISB=0x00; //控制段選
PORTB=0x00;
TRISC=0xf0; //與溫度傳感器連接
PORTC=0x00;
TRISA=0x00; //控制位選
PORTA=0xff;
}
/*初始化MSSP模塊的SPI模式*/
void initMSSP()
{
SSPSTAT=0x00;
SSPCON=0x30;
SSPIF=0;
}
/*讀取溫度傳感器的13位數(shù)值并且轉(zhuǎn)換成浮點(diǎn)類型*/
float ReadTC77()
{
float result,PowerTwo=1;
unsigned char count,HighByte,LowByte;
CS=0;
SSPBUF=0;
while(!SSPIF){}
HighByte=SSPBUF; //獲取溫度寄存器的高字節(jié)
SSPIF=0;
SSPBUF=0;
while(!SSPIF){}
LowByte=SSPBUF; //獲取溫度寄存器的低字節(jié)
SSPIF=0;
CS=1;
for(count=0;count<8;count++) //轉(zhuǎn)換整數(shù)部分
{
PowerTwo*=2;
if(HighByte&(0x01<
}
PowerTwo=1;
for(count=0;count<5;count++) //轉(zhuǎn)換個(gè)位及小數(shù)部分
{
if(LowByte&(0x80>>count)) result+=PowerTwo;
PowerTwo=PowerTwo/2.; //注意,不能換成.2,否則會(huì)出錯(cuò)
}
return result; //返回轉(zhuǎn)換浮點(diǎn)結(jié)果
}
/*液晶顯示*/
void display(float data)
{
unsigned int conver,First,Second,Third,Fourth;
conver=data*100;
First=conver/1000;
Second=conver/100%10;
Third=conver/10%10;
Fourth=conver%10;
PORTA=0xfe; //顯示十位
PORTB=disp[First];
delay(2);
PORTB=0x00;
PORTA=0xfd; //顯示個(gè)位
PORTB=0x80+disp[Second]; //加上0x80即可帶點(diǎn)
delay(2);
PORTB=0x00;
PORTA=0xfb; //顯示十分位
PORTB=disp[Third];
delay(2);
PORTB=0x00;
PORTA=0xf7; //顯示百分位
PORTB=disp[Fourth];
delay(2);
PORTB=0x00;
}
void delay(unsigned char time)
{
unsigned char i,j;
for(i=time;i>0;i--)
for(j=255;j>0;j--);
}



關(guān)鍵詞: PIC16F877ASPITC77溫度傳感

評(píng)論


技術(shù)專區(qū)

關(guān)閉