Analog In
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
Ticks | SC20xxx (Full/Half Speed) |
---|---|
0 | 23ns |
1 | 132ns |
2 | 257ns |
5 | 507ns |
10 | 1007ns |
60 | 6054ns |
126 | 12664ns |
Ticks | SC13xxx (Full Speed) |
---|---|
0 | 81ns |
1 | 156ns |
3 | 306ns |
5 | 593ns |
11 | 1156ns |
30 | 3094ns |
80 | 8006ns |
Ticks | SC13xxx (Half Speed) |
---|---|
1 | 162ns |
3 | 312ns |
6 | 612ns |
11 | 1187ns |
23 | 2312ns |
60 | 6187ns |
160 | 16012ns |
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);
}