
//时钟芯片底层驱动类
unit basic
{
	public link unit CLK {}
	public link unit DAT {}
	public link unit RST {}
	
	link bit RESET_DIR = RST.D0_DIR;
	link bit RESET_OUT = RST.D0_OUT;
	link bit CP_DIR = CLK.D0_DIR;
	link bit CP_OUT = CLK.D0_OUT;
	link bit DATA_DIR = DAT.D0_DIR;
	link bit DATA_OUT = DAT.D0_OUT;
	link bit DATA_IN = DAT.D0_IN;
	
	//---------------------------------------------------
	//初始化
	//[i] function OS_init;
	public void OS_init()
	{
		RESET_DIR = 1;
		RESET_OUT = 0;
		CP_DIR = 1;
		CP_OUT = 0;
		DATA_DIR = 1;
		DATA_OUT = 0;
	}
	//---------------------------------------------------
	//[i] function set_N8,
	//写入数据接口
	public void set_uint8( uint8 comand, uint8 data )
	{
		RESET_OUT = 1;
		write( comand );  //地址定义时低位为零,正好对应写数据,所以不必清零
		write( data );
		RESET_OUT = 0;
	}
	//---------------------------------------------------
	//读取数据接口
	//[i] function get_N8;
	public uint8 get_uint8( uint8 comand )
	{
		RESET_OUT = 1;
		write( comand | 0b0000_0001 );    //地址定义时低位为零,读出数据需要低位置1
		uint8 data = read();
		RESET_OUT = 0;
		return data;
	}
	//=================================================================================
	//私有函数
	
	//写一个字节
	void write( uint8 data )
	{
		loop( 8 ) {
			DATA_OUT = data.0(bit);
			data >> 1;
			CP_OUT = 1;
			CP_OUT = 0;
		}
	}
	//---------------------------------------------------
	//读一个字节
	uint8 read()
	{
		DATA_DIR = 0;
		uint8 data;
		loop( 8 ) {
			data >> 1;
			data.7(bit) = DATA_IN;
			CP_OUT = 1;
			CP_OUT = 0;
		}
		DATA_DIR = 1;
		return data;
	}
}


