新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > LM75A的數(shù)字溫度計(jì)的設(shè)計(jì)

LM75A的數(shù)字溫度計(jì)的設(shè)計(jì)

作者: 時(shí)間:2016-11-21 來源:網(wǎng)絡(luò) 收藏
LM75A是PHILPS公司生產(chǎn)的數(shù)字溫度傳感器及看門狗。通過片內(nèi)集成的帶隙溫度傳感器和∑-△數(shù)摸轉(zhuǎn)換器來實(shí)現(xiàn)數(shù)字溫度的測(cè)量,并通過該溫度傳感器為用戶提供溫度超限報(bào)警輸出。LM75A內(nèi)部包含多個(gè)數(shù)據(jù)寄存器:(1)配置寄器(Conf):用來存儲(chǔ)器件的配置,如:器件工作模式、OS工作模式、OS極性和OS故障隊(duì)列;(2)溫度寄存器(Temp):用于存儲(chǔ)讀取的數(shù)字溫度;(3)設(shè)定點(diǎn)寄存器(Tos&Thyst):用來存儲(chǔ)可編程的過熱關(guān)斷和滯后限制。器件通過2線的串行I2C總線接口與控制器通信。LM75A包含一個(gè)開漏輸出(OS),當(dāng)溫度超過編程限制的值時(shí)該輸出有效。LM75A有3個(gè)可選的邏輯地址管腳,使得同一總線上可同時(shí)掛8個(gè)LM75A而無需其他硬件的支持

CPU:STC89C54RD+
晶震:11.0592M
A0 A1 A2 均接地 地址設(shè)置為0 OS腳和INT0相連并通過10K上拉到VCC;當(dāng)轉(zhuǎn)換溫度超過極限溫度,輸出中斷蜂鳴器報(bào)警;刷新數(shù)碼管顯示溫度 共5位前一位表示正負(fù)溫度后三位為溫度125 - -55 最后一位為0.1度 最高可精確到0.125 數(shù)碼管圖略定時(shí)器100MS采集溫度一次

本文引用地址:http://butianyuan.cn/article/201611/319373.htm

#include
#include
#include

sbit xiaoshu=P2^0; //COM口
sbit ge=P2^1;
sbit shi=P2^3;
sbit bai=P2^4;
sbit fh=P2^5;
sbit SDA_LM75=P1^0; //數(shù)據(jù)
sbit SCL_LM75=P1^1;
sbit BP=P2^1;
bit dp=0;
unsigned char JS=0;


#define LED P0 //8位數(shù)碼管 動(dòng)態(tài)時(shí)時(shí)刷新

#define CONF 0X01 //配置寄存器
#define TEMP 0X00 //溫度寄存器 只讀
#define ALARM 0X03 //超溫關(guān)閉極限寄存器默認(rèn)5000H
#define DELAY 0X02 //滯后寄存器默認(rèn)4B00H
#define WADDR 0X90 //寫地址
#define RADDR 0X91 //讀地址

unsigned int tempdata=0; //全局變量申明溫度

unsigned char code tab[]={ 0x3f,0x30,0x6d,0x79,0x72,0x5b,0x5f,0x31,0x7f,0x7b,0x40};//共陰
//0, 1, 2 3 4 5 6 7 8 9
//unsigned char tab[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共陽
//0, 1, 2 3 4 5 6 7 8 9

unsigned char code tabdp[]={ 0xbf,0xb0,0xed,0xf9,0xf2,0xdb,0xdf,0xb1,0xff,0xfb,0xc0};//共陰帶小數(shù)點(diǎn)
//0, 1, 2 3 4 5 6 7 8 9
//unsigned char tabdp[]={ 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//共陽


void delay_xms(unsigned int count) //延時(shí)函數(shù)
{
register unsigned char j;
while (count --)
{
for (j=0;j<125;j++) //約1MS
{
}
}
}


void Led_Display(unsigned int i,bit mode) //顯示函數(shù)
{
if(mode)
LED = 0x40; //共陰負(fù)
//LED=0xBF;
else
LED = 0x00;
//LED= 0XFF;
fh = 1;
bai = 0;
shi = 0;
ge = 0;
xiaoshu=0;
delay_xms(1);
LED = tab[i/1000];
i%=1000;
fh = 0;
bai = 1;
shi = 0;
ge = 0;
xiaoshu=0;
delay_xms(1);
LED = tab[i/100];
i%=100;
fh=0;
bai = 0;
shi = 1;
ge = 0;
xiaoshu=0;
delay_xms(1);
LED = tabdp[i/10]; //帶小數(shù)點(diǎn)
fh=0;
bai = 0;
shi = 0;
ge = 1;
xiaoshu=0;
delay_xms(1);
LED = tab[i%10];
fh=0;
bai = 0;
shi = 0;
ge = 0;
xiaoshu=1;
delay_xms(1);
}

void delay(void) //延時(shí)函數(shù)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}

void start_lm75(void)//起始
{
SDA_LM75=1;
SCL_LM75=1;
delay();
SDA_LM75=0;
delay();
SCL_LM75=0;
}

void stop_lm75(void)//停止
{
SDA_LM75=0;
SCL_LM75=1;
delay();
SDA_LM75=1;
delay();
SCL_LM75=0;
}

void Check_Ack(void) //檢查應(yīng)答信號(hào)
{
SDA_LM75=1;
SCL_LM75=1;
F0=0;
delay();
if(SDA_LM75) //如果數(shù)據(jù)為高 置位非應(yīng)答標(biāo)志FO
F0=1; //通用標(biāo)志位 PSW狀態(tài)寄存器
SCL_LM75=0; //準(zhǔn)備下一變化數(shù)據(jù)
}

void Ack(void) //發(fā)響應(yīng)信號(hào)
{
SDA_LM75=0;
delay();
SCL_LM75=1;
delay();
SCL_LM75=0;
}

void no_Ack(void) //發(fā)非響應(yīng)信號(hào)
{
SDA_LM75=1;
SCL_LM75=0;
delay();
SCL_LM75=1; //迫使數(shù)據(jù)傳輸結(jié)束
delay();
}

void send_byte(unsigned char temp) //發(fā)送一字節(jié)數(shù)據(jù)
{
unsigned char i=8;
while(i--)
{
SDA_LM75=(bit)(temp&0x80);
SCL_LM75=1;
delay();
SCL_LM75=0;
temp<<=1;
}
SCL_LM75=0;
delay();
SDA_LM75=1; //釋放SDA數(shù)據(jù)線
}

unsigned char read_byte(void) //讀一字節(jié)數(shù)據(jù)
{
unsigned char i=8;
unsigned char temp;
while(i--)
{
temp<<=1;
if(SDA_LM75)
temp++;
SCL_LM75=1;
delay();
SCL_LM75=0;
}
SCL_LM75=0;
delay();
SDA_LM75=1; //釋放SDA數(shù)據(jù)線
return (temp);
}

void Write_chardata(unsigned char addr,unsigned char tempdata)//寫配置寄存器
{
start_lm75();
send_byte(WADDR);
Check_Ack();
if(F0)
{
no_Ack();
return;
}
Ack();
send_byte(addr);
Check_Ack();
if(F0)
{
no_Ack();
return;
}
Ack();
send_byte(tempdata);
Check_Ack();
if(F0)
{
no_Ack();
return;
}
Ack();
delay();
stop_lm75();
}

void Write_intdata(unsigned char addr,unsigned int tempdata)//寫16位寄存器
{
unsigned char datahigh;
unsigned char datalow;
datahigh=(unsigned char)(tempdata>>8);
datalow=(unsigned char)(tempdata&0x00FF);
start_lm75();
send_byte(WADDR);
Check_Ack();
if(F0)
{
no_Ack();
return;
}
Ack();
send_byte(addr);
Check_Ack();
if(F0)
{
no_Ack();
return;
}
Ack();
send_byte(datahigh);
send_byte(datalow);
Check_Ack();
if(F0)
{
no_Ack();
return;
}
Ack();
delay();
stop_lm75();
}

unsigned char read_chardata(unsigned char addr) //讀8位寄存器
{
unsigned char temp;
start_lm75();
send_byte(WADDR);
Check_Ack();
if(F0)
{
no_Ack();
return 0;
}
Ack();
send_byte(addr);
Check_Ack();
if(F0)
{
no_Ack();
return 0;
}
Ack();
send_byte(RADDR);
Check_Ack();
if(F0)
{
no_Ack();
return 0;
}
Ack();
temp=read_byte();
stop_lm75();
return (temp);
}

unsigned int read_intdata(unsigned char addr) //讀16位寄存器
{
unsigned char temphigh,templow;
start_lm75();
send_byte(WADDR);
Check_Ack();
if(F0)
{
no_Ack();
return 0;
}
Ack();
send_byte(addr);
Check_Ack();
if(F0)
{
no_Ack();
return 0;
}
Ack();
send_byte(RADDR);
Check_Ack();
if(F0)
{
no_Ack();
return 0;
}
Ack();
temphigh=read_byte();
templow=(read_byte())&0xe0;
stop_lm75();
if(!(temphigh&0x80))
{
dp=0;
return (((temphigh<<8)|templow)>>5);
}
else
{
dp=1;
return (0x800-((temphigh<<8)|templow)>>5);
}
}

void initial() // 初始化LM75A
{
Write_chardata(CONF,0X20); //0S中斷模式,溫度轉(zhuǎn)換正常模式
Write_intdata(ALARM,0x3300); //100度極限 由高9位有效位組成 BIT15為1表示為負(fù)為0表示為正超過100度 觸發(fā)外部中斷0
Write_intdata(DELAY,0X4A80); // 95度后不報(bào)警
}


void int0_int() interrupt 0 using 1 // 外部中斷0服務(wù)函數(shù)
{
BP=0;
delay_xms(1000);
BP=1;
delay_xms(1000);
}
void t0_int() interrupt 1 using 1 //定時(shí)器0中斷服務(wù)函數(shù)
{
TR0=0;
TH0=0xdc; // 10MS中斷初值
TL0=0xff;
TR0=1;
JS++;
if(JS>10) //加到10為100MS
{
JS=0;
tempdata=read_intdata(TEMP); //采集數(shù)據(jù)
tempdata=(tempdata/8)*10; //轉(zhuǎn)換為溫度
}
}

void main() // 入口函數(shù)
{
IP=0X01;
TMOD=0X01;
TH0=0xdc; // 10MS中斷初值
TL0=0xff;
IT0=0; //外部中斷0采用電平觸發(fā)

ET0=1;
TR0=1; //開放定時(shí)器0中斷
EX0=1;
EA=1;
initial();
while(1)
{
if(dp)
Led_Display(tempdata,1);
else
Led_Display(tempdata,0);
}
}




關(guān)鍵詞: LM75A數(shù)字溫度

評(píng)論


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

關(guān)閉