⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds1620.c

📁 单片机C语言教程
💻 C
字号:
/****************************************************************
/* 这是直接用C51高级语言编写的DS1620转换程序-没有调用汇编语言。*/
/* 文件名DS1620C.C                                              */
/* 功能:本程序主要是读出DS1620芯片的温度转换值,转换值先放      */
/*       在变量temp1中。转换值范围-55℃~125℃。 若flag0=0为     */
/*       正温度,若flag0=1为负温度值,最终结果在浮点变量cc中。    */
/*       本程序适合DS1620 和51系列单片机的连接,晶振为12MHZ左右。*/
/****************************************************************/

#include "REG52.h"
#include "intrins.h"

#define uchar unsigned char
#define uint unsigned int

sbit	DQ        = P1^0;
sbit	CLK_CONV  = P1^1;
sbit	RST       = P1^2;

uint idata temp1;
uchar idata val,flag,temp2;
float idata cc;

void write_byte(uchar val)  //写字节子程序
{
	uchar i;
	uchar b;
	b=1;
	for(i=0;i<8;i++)
	{
		CLK_CONV=0;
		DQ=(val&b);
		CLK_CONV=1;
		b<<=1;
	}
}
uchar read_byte(void)     //读字节子程序
{
	uchar i;
	uchar value,b;
	value=0;
	b=1;
	for(i=0;i<8;i++)
	{
		DQ=1;
		CLK_CONV=0;
		if (DQ)
			value|=b;
		CLK_CONV=1;
		b<<=1;
	}
	return(value);
}
uchar DS1620startConv(void) //DS1620 开始转换
{
	RST=1;
	write_byte(0xEE);
	RST=0;
	return 0x00;
}

uchar DS1620ReadConf(void)  //DS1620读配置 返回值为配置寄存器内容
{
	uchar tmp;
	RST=1;
	write_byte(0xAC);
	tmp=read_byte();
	RST=0;
	return tmp;
}
//DS1620 写配置,入回参数为配置寄存器新配置内容
uchar DS1620SetConf(uchar val)
{
	uchar tmp;
	RST=1;
	write_byte(0x0C);
	write_byte(val);
	RST=0;
	return tmp;
}
//DS1620 读温度转换数据,在返回值的低9位
uint DS1620read(void)
{
	uchar hbyte,lbyte;
	uint temp;
	RST=1;
	write_byte(0xAA);
	lbyte=read_byte();
	hbyte=read_byte();
	RST=0;
	temp=hbyte;
	temp<<=8;
	temp|=lbyte;
	return temp;
}

void main()                    //主程序
{
      SP=0xcf;
      EA=0;
      flag=0;
      val=0x0a;
      DS1620SetConf(val);
      val=DS1620ReadConf();
      DS1620startConv();
      temp1=DS1620read();       //把温度值放入变量temp1中。
      temp1=temp1&0x01ff;       //保留数值有用部分
      if  (temp1>0xff) {
      	  flag=1;
          temp2=temp1-256;
      	  temp2=~temp2+1;
          temp1=temp2;
      	  }
      cc=(float)temp1/2.0;      //计算出温度值
      while(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -