Hubo Library

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
AnalogInput1.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++ AnalogInput1.cpp -L../ -lhubo -lpthread -lrt -o AnalogInput1.out
11 Run:
12 sudo ./AnalogInput1.out
13 Purpose:
14 Simple definition and reading of the analog inputs.
15 First we define which channels to be read, then we read their value.
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 int channel = 0;
32 unsigned long adcCount;
33 double volt;
34 for (int i=0; i<20; i++)
35 {
36 // Read channel 0 and output its value.
37 if (Get_AI_Channel (channel, adcCount, volt))
38 {
39 printf ("Channel=%d ADC count=0x%02lX Volts=%lf\n", channel, adcCount, volt);
40 // Since ADC channels are buffered your'e referring to the buffered values instead of directly
41 // reading them from the ADC. The buffer might need some time to first get filled before you get a true
42 // result of a measuring value. Here the call to Initialize() is too close in time for a first
43 // usage of the Get_AI_Channel() call. That's why the first few samples will show "0".
44 }
45 else
46 {
47 printf ("Error: Get_AI_Channel\n");
48 }
49
50 usleep(1000);
51 }
52
53 // Free library resources.
54 Uninitialize();
55
56 return 0;
57 }
MAX_MCP3x08_CHANNELS
#define MAX_MCP3x08_CHANNELS
Definition: hubolib.h:39
main
int main(void)
Definition: AnalogInput1.cpp:18
HuboLib::Initialize
bool Initialize()
Initializes the library.
HuboLib::Get_AI_Channel
bool Get_AI_Channel(int channel, unsigned long &count, double &volt)
Get the buffered and oversampled data from the MCP3x08.
HuboLib::Set_MCP3x08_Oversampling
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.
HuboLib::Uninitialize
void Uninitialize()
Releases any resources bound to the library.
HuboLib
Definition: hubocfg.h:10