I try to use ChibiOS presetted ADCs. Available channels following the specification:
- PC1, ADC1 IN11
- PC2 ADC1 IN12
- PA6 ADC2 IN6
- PA7 ADC2 IN7
- PB0 ADC2 IN8
- Temp Sensor ADC1
- VrefInt ADC1
- Vbatt ADC1
Presetted ADCs from ChibiOS' build source (target_system_device_adc_config.cpp):
#include <sys_dev_adc_native_target.h>
const NF_PAL_ADC_PORT_PIN_CHANNEL AdcPortPinConfig[] = {
// ADC1
{1, GPIOC, 1, ADC_CHANNEL_IN11},
{1, GPIOC, 2, ADC_CHANNEL_IN12},
// ADC2
{2, GPIOA, 6, ADC_CHANNEL_IN6},
{2, GPIOA, 7, ADC_CHANNEL_IN7},
{2, GPIOB, 0, ADC_CHANNEL_IN8},
// these are the internal sources, available only at ADC1
{1, NULL, 0, ADC_CHANNEL_SENSOR},
{1, NULL, 0, ADC_CHANNEL_VREFINT},
{1, NULL, 0, ADC_CHANNEL_VBAT},
};
const int AdcChannelCount = ARRAYSIZE(AdcPortPinConfig);
How to use ADCs? Is there some way to configure ADC in C# code? I would appreciate any help. Thank you
I wrote this code:
using System;
using System.Diagnostics;
using System.Threading;
using System.Device.Adc;
namespace ADCMeasure
{
public class Program
{
private static int adcValue;
public static void Main()
{
AdcController adcController = new AdcController();
AdcChannel pa6Channel = adcController.OpenChannel(6);
while (true)
{
adcValue = pa6Channel.ReadValue();
double ratio = pa6Channel.ReadRatio();
Debug.WriteLine($"{nameof(adcValue)}: {adcValue}; {nameof(ratio)}: {ratio}; MaxValue: {adcController.MaxValue}; MinValue: {adcController.MinValue}");
Thread.Sleep( 1000 );
}
}
}
}
and have debug console output: adcValue: 1695; ratio: 0.41343101; MaxValue: 4095; MinValue: 0
It corresponds real 2.7V on PA6.
If I change the channel to 7th (adcController.OpenChannel(7)
),
I have adcValue: 4; ratio: 0; MaxValue: 4095; MinValue: 0
and 0V on PA7.
If I connect PA7 (7th channel) to external 2V I have no changes in debug console but I see this 2V on PA7 by voltmeter, it would be about adcValue: 1260
and ratio: 0.30769
The index that you use when calling
adcController.OpenChannel(6)
is the index of theAdcPortPinConfig[]
array. By using 7 you're reading the ADC_CHANNEL_VBAT. (mind that's 0 based)