一、DS1621介绍
DS1621是DALLAS公司生产的一种功能强大的数字式温度传感器和恒温控制器。 接口与IIC总线兼容,一片控制器控制可控制多达8片的DS1621,工作电压为2.7~5.5 V,适用于低功耗应用系统。 DS1621可作为恒温控制器单独使用,也可通过2线接口在ARM的控制下完成温度的测量及计算。
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/763f6bda6fb24eb7bd8e358a98def377.png)
二、IIC程序
#include "iic.h"
#include "intrins.h"
#define DELAY_TIME 5
sbit SDA = P2^4;
sbit SCL = P2^3;
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
void delaynms(unsigned int nms)
{
unsigned int i,j;
for(i=nms;i>0;i--)
for(j=174;j>0;j--);
}
void IIC_Start(void)
{
SDA = 1;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
}
void IIC_ACK(void)
{
unsigned char i=0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
while((SDA==1)&& (i<250))
{
i++;
}
SCL = 0;
IIC_Delay(DELAY_TIME);
}
void IIC_Stop(void)
{
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
void Write_one_date(unsigned char date)
{
unsigned char i,temp;
temp = date;
for(i=0;i<8;i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if((temp&0x80)==0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
temp = temp << 1;
}
SCL = 0;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
unsigned char Read_one_date(void)
{
unsigned char i,k;
SCL = 0;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
for(i=0;i<8;i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
k = ((k<<1)|((P2 & 0x10)>>4));
SCL = 0;
IIC_Delay(DELAY_TIME);
}
IIC_Delay(DELAY_TIME);
return k;
}
三、DS1621写/读程序
DS1621的写/读程序可以放在IIC.C文件下方
void Write_w_cmd(unsigned char w_cmd,unsigned char cmd)
{
IIC_Start();
Write_one_date(w_cmd);
IIC_ACK();
Write_one_date(cmd);
IIC_ACK();
}
unsigned char IIC_Read(unsigned char r_add_cmd)
{
unsigned char temp_h,temp_l;
IIC_Start();
Write_one_date(r_add_cmd);
IIC_ACK();
temp_h = Read_one_date();
delaynms(10);
IIC_ACK();
temp_l = Read_one_date();
delaynms(10);
IIC_ACK();
IIC_Stop();
return temp_h;
}
四、主函数处理
由于写/读数据是受芯片7、6、5三个引脚(地址)控制,下图U5写是0x90,读是0x91;U6写是0x92,读是0x93,依次往复。
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/43866e2912a44e2bac75925d1e86179a.png)
main.c程序如下
if((ucTemp&0x80) >= 0x80)成立代表检测到负温度,这时就要将原码取反加一得到负数的补码,表示负温度。
unsigned int ucTemp;
void main(void)
{
while(1)
{
Write_w_cmd(0x90,0xEE);
Delay4500us();
Write_w_cmd(0x90,0xAA);
ucTemp = IIC_Read(0x91);
if((ucTemp&0x80) >= 0x80)
{
ucTemp = ~(ucTemp) + 1;
}
Write_w_cmd(0x92,0xEE);
Delay4500us();
Write_w_cmd(0x92,0xAA);
ucTemp1 = IIC_Read(0x93);
if((ucTemp1&0x80) >= 0x80)
{
ucTemp1 = ~(ucTemp1) + 1;
}
}
}
五、实物展示
上面并没有LCD的相关程序,只有DS1621的使用程序。
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/9555a90355dc4ee2ad4a54fc7c133897.jpeg)