Exemple #1
0
void main()
{
    LED = 1;

    while(1)
    {
        DS18B20_Reset();                //设备复位
        DS18B20_WriteByte(0xCC);        //跳过ROM命令
        DS18B20_WriteByte(0x44);        //开始转换命令
        while (!DQ);                    //等待转换完成
        DS18B20_Reset();                //设备复位
        DS18B20_WriteByte(0xCC);        //跳过ROM命令
        DS18B20_WriteByte(0xBE);        //读暂存存储器命令
        a = DS18B20_ReadByte();       //读温度低字节
        b = DS18B20_ReadByte();       //读温度高字节
        t = b;
        t <<= 8;
        t = t | a;
        tt = t * 0.0625;
        t = tt * 10 + 0.5;
        if((t % 100) > 32) LED = 0;
        else LED = 1;
        PrintString("温度: ");		//提示用户码
        //Tx1Send('0' + t / 1000);	//用户码高字节的高半字节
        Tx1Send('0' + (t / 100) % 10);		//用户码高字节的低半字节
        Tx1Send('0' + (t / 10) % 10);	//用户码低字节的高半字节
        Tx1Send('.');
        Tx1Send('0' + t % 10);		//用户码低字节的低半字节
        PrintString("℃");		//提示用户码
        Tx1Send(10);

    }
}
Exemple #2
0
/*
*********************************************************************************************************
*	函 数 名: DS18B20_ReadTempByID
*	功能说明: 读指定ID的温度寄存器的值(原始数据)
*	形    参: 无
*	返 回 值: 温度寄存器数据 (除以16得到 1摄氏度单位, 也就是小数点前面的数字)
*********************************************************************************************************
*/
int16_t DS18B20_ReadTempByID(uint8_t *_id)
{
	uint8_t temp1, temp2;
	uint8_t i;

	DS18B20_Reset();		/* 总线复位 */

	DS18B20_WriteByte(0x55);	/* 发命令 */
	for (i = 0; i < 8; i++)
	{
		DS18B20_WriteByte(_id[i]);
	}
	DS18B20_WriteByte(0x44);	/* 发转换命令 */

	DS18B20_Reset();		/* 总线复位 */

	DS18B20_WriteByte(0x55);	/* 发命令 */
	for (i = 0; i < 8; i++)
	{
		DS18B20_WriteByte(_id[i]);
	}	
	DS18B20_WriteByte(0xbe);

	temp1 = DS18B20_ReadByte();	/* 读温度值低字节 */
	temp2 = DS18B20_ReadByte();	/* 读温度值高字节 */

	return ((temp2 << 8) | temp1);	/* 返回16位寄存器值 */
}
Exemple #3
0
void main()
{
    DS18B20_Reset();                //设备复位
    DS18B20_WriteByte(0xCC);        //跳过ROM命令
    DS18B20_WriteByte(0x44);        //开始转换命令
    while (!DQ);                    //等待转换完成

    DS18B20_Reset();                //设备复位
    DS18B20_WriteByte(0xCC);        //跳过ROM命令
    DS18B20_WriteByte(0xBE);        //读暂存存储器命令
    TPL = DS18B20_ReadByte();       //读温度低字节
    TPH = DS18B20_ReadByte();       //读温度高字节

    while (1);
}
Exemple #4
0
float DS18B20_Temperature(void)
{
    BYTE flag = 0,i = 0;
    BYTE a[10];
    float temp = 0.0f;

    flag = Init_DS18B20();
    if (flag == 0x00)
        return -9999;
    DS18B20_WriteByte(DS18B20_SkipROM);
    DS18B20_WriteByte(DS18B20_ConvertTemp);

    flag = Init_DS18B20();
    if (flag == 0x00)
        return -9999;

    DS18B20_WriteByte(DS18B20_SkipROM);
    DS18B20_WriteByte(DS18B20_ReadSCR);
    
    for(i = 0;i < 9;i++)
        a[i]  = DS18B20_ReadByte();
 
    if(CRC(a,9) == 0)
	{
		if(a[1]&0xf0)
	        temp= 0-(0xffff-(unsigned short)((a[1]<<8)+a[0])+1)*0.0625;
	    else
	        temp=((unsigned short)(a[1]<<8)+a[0])*0.0625;
 	}
	else
		temp = 9999;
    return temp;
}
// 读取DS18B20温度,放大100倍。id为温度传感器编号(目前可以支持2个,主缸和底缸各一个)
BaseType_t GetTemperature(uint8_t id, int16_t* pData)
{
	uint8_t		i;
	uint16_t	temp;
	int32_t		temperature;
	uint8_t		crc;

	assert_param(pData);

	if ( !(DS18B20_Reset(id)) )
	{
		return pdFALSE;
	}
	DS18B20_WriteByte(id, DS18B20_COMMAND_SKIPROM);
	DS18B20_WriteByte(id, DS18B20_COMMAND_CONVERTT);

	// todo:需要更换为FreeRTOS的延迟函数,以便可以进行任务切换
	for (i = 0; i < 9; ++i)
	{
		Delay_us(50 * 1000);
		Delay_us(50 * 1000);
	}

	if ( !(DS18B20_Reset(id)) )
	{
		return pdFALSE;
	}
	DS18B20_WriteByte(id, DS18B20_COMMAND_SKIPROM);
	DS18B20_WriteByte(id, DS18B20_COMMAND_READSCRATCHPAD);

	for (i = 0; i < sizeof(s_Scratchpad) / sizeof(s_Scratchpad[0]); ++i)
	{
		s_Scratchpad[i] = DS18B20_ReadByte(id);
	}

	// CRC校验
	crc = crc8(s_Scratchpad, 9);
	if (crc)
	{
		return pdFALSE;
	}

	temp = s_Scratchpad[1] << 8;
	temp += s_Scratchpad[0];

	// 转换为摄氏度,并放大100倍
	temperature = (int16_t)temp;		// 先进行符号位扩展
	temperature = temperature * 625 / 100;

	// todo:看看是否需要进行四舍五入的处理?

	*pData = (int16_t)temperature;
	return pdTRUE;
}
Exemple #6
0
/*
*********************************************************************************************************
*	函 数 名: DS18B20_ReadTempReg
*	功能说明: 读温度寄存器的值(原始数据)
*	形    参: 无
*	返 回 值: 温度寄存器数据 (除以16得到 1摄氏度单位, 也就是小数点前面的数字)
*********************************************************************************************************
*/
int16_t DS18B20_ReadTempReg(void)
{
	uint8_t temp1, temp2;

	/* 总线复位 */
	if (DS18B20_Reset() == 0)
	{
		return 0;
	}		

	DS18B20_WriteByte(0xcc);	/* 发命令 */
	DS18B20_WriteByte(0x44);	/* 发转换命令 */

	DS18B20_Reset();		/* 总线复位 */

	DS18B20_WriteByte(0xcc);	/* 发命令 */
	DS18B20_WriteByte(0xbe);

	temp1 = DS18B20_ReadByte();	/* 读温度值低字节 */
	temp2 = DS18B20_ReadByte();	/* 读温度值高字节 */

	return ((temp2 << 8) | temp1);	/* 返回16位寄存器值 */
}
Exemple #7
0
/*
*********************************************************************************************************
*	函 数 名: DS18B20_ReadID
*	功能说明: 读DS18B20的ROM ID, 总线上必须只有1个芯片
*	形    参: _id 存储ID
*	返 回 值: 0 表示失败, 1表示检测到正确ID
*********************************************************************************************************
*/
uint8_t DS18B20_ReadID(uint8_t *_id)
{
	uint8_t i;

	/* 总线复位 */
	if (DS18B20_Reset() == 0)
	{
		return 0;
	}

	DS18B20_WriteByte(0x33);	/* 发命令 */
	for (i = 0; i < 8; i++)
	{
		_id[i] = DS18B20_ReadByte();
	}

	DS18B20_Reset();		/* 总线复位 */
	
	return 1;
}