
unit ADbasic
{
	public link unit PORT {}
	const uint8 channel = PORT.AD_INDEX;
	
	public const int32 MinValue = 0;
	public const int32 MaxValue = 0;
	
	//---------------------------------------------------
	//[i] function_void OS_init;
	public void OS_init()
	{
		PORT.D0_DIR = 0;
		PORT.D0_OUT = 0;
		#.MEGA32.ADCSRA = 0b1000_0011;  //8·ÖÆµ
	}
	//---------------------------------------------------
	//[i] function_int32 get_data;
	public int32 get_data()
	{
		#.MEGA32.ADMUX = 0b0100_0000 + channel;
		#.MEGA32.ADCSRA.6(bit) = 1;
		while( #.MEGA32.ADCSRA.6(bit) == 1 ) {}
		
		uint16 data;
		data.0(uint8) = #.MEGA32.ADCL;
		data.8(uint8) = #.MEGA32.ADCH;
		
		uint32 d = data;
		
		return MinValue + (int)d * (MaxValue - MinValue) / 1024;
	}
}





