CPU:STC89C54RD+
晶震:11.0592M
A0 A1 A2 均接地 地址设置为0 OS脚和INT0相连并通过10K上拉到VCC;当转换温度超过极限温度,输出中断蜂鸣器报警;刷新数码管显示温度 共5位前一位表示正负温度后三位为温度125 - -55 最后一位为0.1度 最高可精确到0.125 数码管图略定时器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};//共阴带小数点
//unsigned char tabdp[]={ 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//共阳
void delay_xms(unsigned int count) //延时函数
{
register unsigned char j;
while (count --)
{
for (j=0;j<125;j++) //约1MS
{
}
}
}
void Led_Display(unsigned int i,bit mode)
{
}
void delay(void) //延时函数
{
_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) //如果数据为高 置位非应答标志FO
F0=1;
SCL_LM75=0;
}
void Ack(void) //发响应信号
{
SDA_LM75=0;
delay();
SCL_LM75=1;
delay();
SCL_LM75=0;
}
void no_Ack(void) //发非响应信号
{
SDA_LM75=1;
SCL_LM75=0;
delay();
SCL_LM75=1; //迫使数据传输结束
delay();
}
void send_byte(unsigned char temp) //发送一字节数据
{
unsigned char i=8;
while(i--)
{
}
SCL_LM75=0;
delay();
SDA_LM75=1; //释放SDA数据线
}
unsigned char read_byte(void) //读一字节数据
{
unsigned char i=8;
unsigned char temp;
while(i--)
{
}
SCL_LM75=0;
delay();
SDA_LM75=1; //释放SDA数据线
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中断模式,温度转换正常模式
Write_intdata(ALARM,0x3300); //100度极限 由高9位有效位组成 BIT15为1表示为负为0表示为正超过100度 触发外部中断0
Write_intdata(DELAY,0X4A80); // 95度后不报警
}
void int0_int() interrupt 0 using 1 // 外部中断0服务函数
{
BP=0;
delay_xms(1000);
BP=1;
delay_xms(1000);
}
void t0_int() interrupt 1 using 1 //定时器0中断服务函数
{
}
void main() // 入口函数
{
IP=0X01;
TMOD=0X01;
TH0=0xdc;
TL0=0xff;
IT0=0;
ET0=1;
TR0=1;
EX0=1;
EA=1;
initial();
while(1)
{
}
}
上一篇:一种简单51单片机电子密码锁设计
下一篇:TEA5767的简单收音机设计
推荐阅读最新更新时间:2024-03-16 14:49