Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
AnalogInput5.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++ AnalogInput5.cpp -L../ -lhubo -lpthread -lrt -o AnalogInput5.out
11 Run:
12  sudo ./AnalogInput5.out
13 Purpose:
14  Wait reading buffered samples until the reading buffer is filled.
15  First we define which channels to be read (channel 0), then we wait prior to reading its values.
16 */
17 
18 int main(void)
19 {
20  // Initialize the library once in your program.
21  if (!Initialize())
22  {
23  printf ("Error: Initialize\n");
24  return 1;
25  }
26 
27  // Define channel 0 to be read (set to 1) all other channels (set to 0) are not read from the ADC.
28  unsigned short overSampling[MAX_MCP3x08_CHANNELS] = { 1, 0, 0, 0, 0, 0, 0, 0 };
29  Set_MCP3x08_Oversampling (overSampling);
30 
31  // Wait as long until we have valid values in the buffer.
33 
34  int channel = 0;
35  unsigned long adcCount;
36  double volt;
37  for (int i=0; i<20; i++)
38  {
39  // Read channel 0 and output its value.
40  if (Get_AI_Channel (channel, adcCount, volt))
41  {
42  printf ("Channel=%d ADC count=0x%02lX Volts=%lf\n", channel, adcCount, volt);
43  // Since ADC channels are buffered your'e referring to the buffered values instead of directly
44  // reading them from the ADC. The buffer might need some time to first get filled before you get a true
45  // result of a measuring value. Here the call to Initialize() is too close in time for a first
46  // usage of the Get_AI_Channel() call. That's why the first few samples will show "0".
47  }
48  else
49  {
50  printf ("Error: Get_AI_Channel\n");
51  }
52 
53  usleep(1000);
54  }
55 
56  // Free library resources.
57  Uninitialize();
58 
59  return 0;
60 }
bool Wait_For_MCP3x08_Buffered_Values()
Waits until the input buffer of the MCP3x08 are initialized from the hardware.
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.