Skip to main content

Analog In

Docs under migration

These docs are under active migration and updates. The original docs will remain available at docs.ghielectronics.com for future reference.

Unlike digital input pins, which can only read high or low, analog pins can read a range of voltage levels. Microcontrollers based on 3.3V can typically read voltages anywhere between zero and 3.3V. Analog inputs connect internally to an Analog to Digital Converter (ADC) that converts the analog voltage level on the pin to a digital value.

tip

Note that the analog channel number is not the pin number. You need to determine the channel number of a specific pin using your system's documentation.

The resolution of the ADC determines its accuracy. An 8bit ADC has 256 steps to work with, 3.3V/256=0.013V. This means an increase of 0.013V will increase the ADC value by one. In other words, a voltage change of less than 0.013V has no effect.

var adc = AdcController.FromName(SC20100.Adc.Controller1.Id);
var analog = adc.OpenChannel(SC20100.Adc.Controller1.PA0);

while (true) {
double d = analog.ReadRatio();
Debug.WriteLine("An-> " + d.ToString("N2"));
Thread.Sleep(100);
}

Support Sampling timing

Changing sampling time can be configured as follows:

adcChannel.SamplingTime = TimeSpan.FromTicks(10); // Set to 1us (a tick is 100ns)

SITCore allows these sampling times. If the SamplingTime is between two values above, the higher values will be used.

note

SITCore ADC resolution by device

SC20xxx = 16 bit

SC13xxx = 12 bit

TicksSC20xxx (Full/Half Speed)
023ns
1132ns
2257ns
5507ns
101007ns
606054ns
12612664ns
TicksSC13xxx (Full Speed)
081ns
1156ns
3306ns
5593ns
111156ns
303094ns
808006ns
TicksSC13xxx (Half Speed)
1162ns
3312ns
6612ns
111187ns
232312ns
606187ns
16016012ns
tip

Default is 1 tick.

Temperature sensor

SITCore support reading temperature sensor.

var sc20xxx = DeviceInformation.DeviceName.IndexOf("SC20") >= 0 ? true : false;

var ts_reg1 = sc20xxx ? (IntPtr)0x1FF1E820 : (IntPtr)0x1FFF75A8;
var ts_reg2 = sc20xxx ? (IntPtr)0x1FF1E840 : (IntPtr)0x1FFF75CA;
var enable_reg = sc20xxx ? (IntPtr)((0x40000000U + 0x18020000 + 0x6300 + 8)) : (IntPtr)(0x40000000U + 0x08000000U + 0x08040300U + 8);

var controller = sc20xxx ? AdcController.FromName(SC20100.Adc.Controller3.Id) : AdcController.FromName(SC13048.Adc.Controller1.Id);
var channel = sc20xxx ? controller.OpenChannel(SC20100.Adc.Controller3.InternalTemperatureSensor) : controller.OpenChannel(SC13048.Adc.Controller1.InternalTemperatureSensor);
var enable_val = Marshal.ReadInt32(enable_reg);

enable_val |= (1 << 23);

Marshal.WriteInt32(enable_reg, enable_val);

while (true)
{
var v = channel.ReadValue() * 1.0;


var ts1 = Marshal.ReadInt32(ts_reg1);
var ts2 = Marshal.ReadInt32(ts_reg2);

if (!sc20xxx)
{
ts1 &= 0xFFFF;
ts2 &= 0xFFFF;
v *= 1.1;
}

var t1 = (110 - 30) * 1.0;
var t2 = (ts2 - ts1) * 1.0;
var t3 = (v - ts1) * 1.0;

var temperature = t1 / t2 * t3 + 30;

Debug.WriteLine("T = " + temperature + " Celsius");

Thread.Sleep(1000);
}