
//温度检测芯片DS18B20, 处理器无关代码,需要加载底层驱动

unit ds18b20
{
	public const uint16 ID = 0;
	
	public link unit DS {}
	
	#include "$run$.txt"
	
	bool Integer_isOK;
	int8 Integer;
	
	bool Float_isOK;
	int8 Float;
	
	//---------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		port_init();
		
		//加上以下语句导致系统无法和电脑控件同步(直接连接运行)
		//2014.1.9 22:53 刚才听音乐时忽然想到为什么不行了, 因为在底层读写变量执行了中断屏蔽和打开...
		
		//触发下一次测量
		//start_sd();
		//跳过序列号
		//write_byte( 0xcc );
		//启动温度转换
		//write_byte( 0x44 );
	}
	//---------------------------------------------------
	//[i] function_int32 get_integer;
	public int32 get_integer()
	{
		if( !Integer_isOK ) {
			Refresh();
		}
		Integer_isOK = false;
		return Integer;
	}
	//---------------------------------------------------
	//[i] function_int32 get_float;
	public int32 get_float()
	{
		if( !Float_isOK ) {
			Refresh();
		}
		Float_isOK = false;
		return Float;
	}
	//---------------------------------------------------
	//[i] function_int32 get_temperature;
	
	//...
	
	void Refresh()
	{
		int32 data = get_temperature();
		if( data < 0 ) {
			Integer = (int8)(int16)(data/16);
			Float = (int8)(int16) ((-data%16)*10/16);
		}
		else {
			Integer = (int8)(int16)(data/16);
			Float = (int8)(int16) ((data%16)*10/16);
		}
		Integer_isOK = true;
		Float_isOK = true;
	}
}























