Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
AnalogInput2.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++ AnalogInput2.cpp -L../ -lhubo -lpthread -lrt -o AnalogInput2.out
11 Run:
12  sudo ./AnalogInput2.out
13 Purpose:
14  Using oversampling in order to increase the resolution of an ADC sample value.
15 */
16 
17 int main(void)
18 {
19  // Initialize the library once in your program.
20  if (!Initialize())
21  {
22  printf ("Error: Initialize\n");
23  return 1;
24  }
25 
26  // Define channel 0 to be oversampled 4 times (set to 4) all other channels (set to 0) are not read from the ADC.
27  unsigned short overSampling[MAX_MCP3x08_CHANNELS] = { 4, 0, 0, 0, 0, 0, 0, 0 };
28  Set_MCP3x08_Oversampling (overSampling);
29 
30  int channel = 0;
31  unsigned long adcCount;
32  double volt;
33  for (int i=0; i<20; i++)
34  {
35  // Read channel 0 and output its value.
36  if (Get_AI_Channel (channel, adcCount, volt))
37  {
38  printf ("Channel=%d ADC count=0x%02lX Volts=%lf\n", channel, adcCount, volt);
39  // Again - you'll see that you're getting the bufferd value a bit later.
40  // As it can be seen from the adcCount - the value is about 4 times as high while the
41  // voltage is still the same. Oversampled values are just added up, while the voltage
42  // is averaged over the 4 samples.
43  // Also you'll notice that the time to get the averaged value has not increased
44  // compared to the previouse example where no oversampling was used. This is why all
45  // oversampling will be done in one and the same cycle time slice.
46  }
47  else
48  {
49  printf ("Error: Get_AI_Channel\n");
50  }
51 
52  usleep(1000);
53  }
54 
55  // Free library resources.
56  Uninitialize();
57 
58  return 0;
59 }
int main(void)
#define MAX_MCP3x08_CHANNELS
Definition: hubolib.h:39
bool Initialize()
Initializes the library.
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.