LM75A的數(shù)字溫度計的設計
CPU:STC89C54RD+
晶震:11.0592M
A0 A1 A2 均接地 地址設置為0 OS腳和INT0相連并通過10K上拉到VCC;當轉(zhuǎn)換溫度超過極限溫度,輸出中斷蜂鳴器報警;刷新數(shù)碼管顯示溫度 共5位前一位表示正負溫度后三位為溫度125 - -55 最后一位為0.1度 最高可精確到0.125 數(shù)碼管圖略定時器100MS采集溫度一次
#include
#include
#include
sbit xiaoshu=P2^0;
sbit ge=P2^1;
sbit shi=P2^3;
sbit bai=P2^4;
sbit fh=P2^5;
sbit SDA_LM75=P1^0;
sbit SCL_LM75=P1^1;
sbit BP=P2^1;
bit dp=0;
unsigned char JS=0;
#define
#define CONF
#define TEMP
#define ALARM
#define DELAY
#define WADDR
#define RADDR
unsigned int tempdata=0; //全局變量申明溫度
unsigned char code tab[]={ 0x3f,0x30,0x6d,0x79,0x72,0x5b,0x5f,0x31,0x7f,0x7b,0x40};//共陰
//unsigned char tab[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共陽
unsigned char code tabdp[]={ 0xbf,0xb0,0xed,0xf9,0xf2,0xdb,0xdf,0xb1,0xff,0xfb,0xc0};//共陰帶小數(shù)點
//unsigned char tabdp[]={ 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//共陽
void delay_xms(unsigned int count) //延時函數(shù)
{
register unsigned char j;
while (count --)
{
for (j=0;j<125;j++) //約1MS
{
}
}
}
void Led_Display(unsigned int i,bit mode)
{
}
void delay(void) //延時函數(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) //檢查應答信號
{
SDA_LM75=1;
SCL_LM75=1;
F0=0;
delay();
if(SDA_LM75) //如果數(shù)據(jù)為高 置位非應答標志FO
F0=1;
SCL_LM75=0;
}
void Ack(void) //發(fā)響應信號
{
SDA_LM75=0;
delay();
SCL_LM75=1;
delay();
SCL_LM75=0;
}
void no_Ack(void) //發(fā)非響應信號
{
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--)
{
}
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--)
{
}
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)
{
}
Ack();
send_byte(addr);
Check_Ack();
if(F0)
{
}
Ack();
send_byte(tempdata);
Check_Ack();
if(F0)
{
}
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)
{
}
Ack();
send_byte(addr);
Check_Ack();
if(F0)
{
}
Ack();
send_byte(datahigh);
send_byte(datalow);
Check_Ack();
if(F0)
{
}
Ack();
delay();
stop_lm75();
}
unsigned char read_chardata(unsigned char addr) //讀8位寄存器
{
unsigned char temp;
start_lm75();
send_byte(WADDR);
Check_Ack();
if(F0)
{
}
Ack();
send_byte(addr);
Check_Ack();
if(F0)
{
}
Ack();
send_byte(RADDR);
Check_Ack();
if(F0)
{
}
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)
{
}
Ack();
send_byte(addr);
Check_Ack();
if(F0)
{
}
Ack();
send_byte(RADDR);
Check_Ack();
if(F0)
{
}
Ack();
temphigh=read_byte();
templow=(read_byte())&0xe0;
stop_lm75();
if(!(temphigh&0x80))
{
return (((temphigh<<8)|templow)>>5);
}
else
{
return (0x800-((temphigh<<8)|templow)>>5);
}
}
void initial() // 初始化LM75A
{
Write_chardata(CONF,0X20); //0S中斷模式,溫度轉(zhuǎn)換正常模式
Write_intdata(ALARM,0x3300); //100度極限 由高9位有效位組成 BIT15為1表示為負為0表示為正超過100度 觸發(fā)外部中斷0
Write_intdata(DELAY,0X4A80); // 95度后不報警
}
void int0_int() interrupt 0 using 1 // 外部中斷0服務函數(shù)
{
BP=0;
delay_xms(1000);
BP=1;
delay_xms(1000);
}
void t0_int() interrupt 1 using 1 //定時器0中斷服務函數(shù)
{
}
void main() // 入口函數(shù)
{
IP=0X01;
TMOD=0X01;
TH0=0xdc;
TL0=0xff;
IT0=0;
ET0=1;
TR0=1;
EX0=1;
EA=1;
initial();
while(1)
{
}
}
評論