//Example : Analog to Digital Converter on RoBoard(AD7918)
#include <stdio.h>
#include <roboard.h>
int main()
{
int* val;
int c,i;
//choose 0 ~ 7 channels that you want to use
unsigned int usedchannels = AD7918_USECHANNEL0 +
AD7918_USECHANNEL1 +
AD7918_USECHANNEL2 +
AD7918_USECHANNEL3 +
AD7918_USECHANNEL4 +
AD7918_USECHANNEL5 +
AD7918_USECHANNEL6 +
AD7918_USECHANNEL7;
printf("Start ADC\n");
//SPI interface should be initialized first
if(spi_Initialize(SPICLK_21400KHZ) == false)
{
printf("SPI Initialize Failure : %s",roboio_GetErrMsg());
spi_Close();
return -1;
}
//Set AD7918 Input-voltage range: 0V ~ 5V , value range : -512 ~ 511
if (ad7918_InitializeMCH(usedchannels,AD7918MODE_RANGE_2VREF,AD7918MODE_CODING_511) == false)
{
printf("AD7918 Initialize Failure : %s",roboio_GetErrMsg());
spi_Close();
return -1;
}
while((c = getchar()) != 'q')//Press enter to read AD value or 'q' to exit
{
if(c == '\n')
{
val = ad7918_ReadMCH();
for(i = 0;i < 8;i++)
printf("AD7918 channel %d = %d\n",i,val[i]);
}
}
ad7918_CloseMCH();
spi_Close();
return 0;
}
wahooburger wrote:Hello,
Many thanks for the example.
Does that source code implement a single reading of one A/D value that I can repeat when pressing the enter key? If so, can it be changed do continuous acquisition ( see example below)
I am attempting to do a continuous acquisition at 1 kHz sampling rate over a period of 1 second, so I would want 1 second * 1 KSamples / second = 1000 samples each time I hit the enter key.
Many thanks for your kind assistance.
Regards,
Andy
ea4ps wrote:Sorry for my ignorance but, the values returned (-512 if pin is null) are in units of milivolts (mV)?
...
For test, I connected one batterie (AA 1,5V) to ADC and the value returned was 32700, but I still don't know the equivalent of return value for ad7918_ReadMCH() and voltage.
roboard wrote:Hi,
If you initialize ad7918_InitializeMCH() with AD7918MODE_RANGE_2VREF option, the unit of the returned value is 5V/1024 (~5mV); and if with AD7918MODE_RANGE_VREF option, the unit is 2.5V/1024 (~2.5mV).
Also, with AD7918MODE_CODING_1023 option, the returned value 0 corresponds to 0V, and value 1023 corresponds to the selected maximum voltage limit (2.5V or 5V);
with AD7918MODE_CODING_511 option, the returned value -512 corresponds to 0V, and value 511 corresponds to the selected maximum voltage limit (2.5V or 5V).
The returned value 32767 indicates an ADC fail or that the corresponding channel is not selected when calling ad7918_InitializeMCH().
ea4ps wrote:Thanks for help me! but something goes wrong. Im executing this program and if Im wrong in this code are initialized all channels, however, I get out this:
AD7918 channel 0 = 32767
AD7918 channel 1 = 496
AD7918 channel 2 = 32767
AD7918 channel 3 = 32767
AD7918 channel 4 = 32767
AD7918 channel 5 = 32767
AD7918 channel 6 = 32767
AD7918 channel 7 = 1023
and I have nothing connected to any pin. If I connected any voltage exit of program is the same. ADC chip could be broken?
ea4ps wrote:My code is exactly the same as written here, the second message of this thread.
#include <stdio.h>
#include <roboard.h>
int main()
{
int* val;
int c,i;
//choose 0 ~ 7 channels that you want to use
unsigned char usedchannels = AD7918_USECHANNEL0 +
AD7918_USECHANNEL1 +
AD7918_USECHANNEL2 +
AD7918_USECHANNEL3 +
AD7918_USECHANNEL4 +
AD7918_USECHANNEL5 +
AD7918_USECHANNEL6 +
AD7918_USECHANNEL7;
printf("Start ADC\n");
roboio_SetRBVer(RB-110);
//SPI interface should be initialized first
if(spi_Initialize(SPICLK_21400KHZ) == false)
{
printf("SPI Initialize Failure : %s",roboio_GetErrMsg());
spi_Close();
return -1;
}
//Set AD7918 Input-voltage range: 0V ~ 5V , value range : -512 ~ 511
if (ad7918_InitializeMCH(usedchannels, AD7918MODE_RANGE_2VREF, AD7918MODE_CODING_511) == false)
{
printf("AD7918 Initialize Failure : %s",roboio_GetErrMsg());
spi_Close();
return -1;
}
while((c = getchar()) != 'q')//Press enter to read AD value or 'q' to exit
{
if(c == '\n')
{
val = ad7918_ReadMCH();
for(i = 0;i < 8;i++)
printf("AD7918 channel %d = %d\n",i,val[i]);
}
}
ad7918_CloseMCH();
spi_Close();
return 0;
}