Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
AnalogInput4.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <unistd.h>
3 
4 #include "../hubolib.h"
5 
6 using namespace HuboLib;
7 
8 /*
9 Compile and link:
10  g++ AnalogInput4.cpp -L../ -lhubo -lpthread -lrt -o AnalogInput4.out
11 Run:
12  sudo ./AnalogInput4.out
13 Purpose:
14  Check and - if required - adjust the reference voltage of the MCP3208.
15  The reference voltage should only be adjusted once the TL431 reference voltage has been measured with a high
16  precision instrument.
17 */
18 
19 int main(void)
20 {
21  // Initialize the library once in your program.
22  if (!Initialize())
23  {
24  printf ("Error: Initialize\n");
25  return 1;
26  }
27 
28  // Define channel 0 to be oversampled 40 times (set to 40) all other channels (set to 0) are not read from the ADC.
29  unsigned short overSampling[MAX_MCP3x08_CHANNELS] = { 1, 0, 0, 0, 0, 0, 0, 0 };
30  Set_MCP3x08_Oversampling (overSampling);
31 
32  // Let the AD buffers get filled.
33  usleep(100000);
34 
35  // Check the current setting of the reference voltage of the TL431.
36  printf ("VRef=%lf\n", Get_VRef());
37 
38  int channel = 0;
39  unsigned long adcCount;
40  double volt;
41 
42  // Read channel 0 and output its count as well as it's voltage value.
43  Get_AI_Channel (channel, adcCount, volt);
44  printf ("Channel=%d ADC count=0x%02lX Volts=%lf\n", channel, adcCount, volt);
45 
46  // Latch some more values...
47  usleep(1000000);
48 
49  // Usually the reference voltage would be around 2.51V +-2%. So this value would be entered here.
50  // However, let's see what happens if we'd use a value that is twice as big as the actual reference voltage.
51  Set_VRef (2*Get_VRef());
52  printf ("VRef=%lf\n", Get_VRef());
53 
54  // Read channel 0 and output it's count as well as its voltage value.
55  Get_AI_Channel (channel, adcCount, volt);
56  printf ("Channel=%d ADC count=0x%02lX Volts=%lf\n", channel, adcCount, volt);
57 
58  // Conclusion: While adjusting the reference voltage directly impacts the output value of the calculated voltage it
59  // does of cause not impact the measured ADC count. Setting VRef also does not alter the buffered values.
60  // Since the setting refers to the TL431 it does affect all of the analog channels.
61 
62  // Free library resources.
63  Uninitialize();
64 
65  return 0;
66 }
#define MAX_MCP3x08_CHANNELS
Definition: hubolib.h:39
bool Initialize()
Initializes the library.
double Get_VRef()
Retrieves the reference voltage value used for ADC count convertion.
bool Get_AI_Channel(int channel, unsigned long &count, double &volt)
Get the buffered and oversampled data from the MCP3x08.
bool Set_MCP3x08_Oversampling(unsigned short overSampling[MAX_MCP3x08_CHANNELS])
Specifies the ADC channels to be sampled as well as the number they get oversampled.
void Uninitialize()
Releases any resources bound to the library.
void Set_VRef(double volt)
Set the reference voltage value for the ADC.
int main(void)