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
AnalogInput3.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++ AnalogInput3.cpp -L../ -lhubo -lpthread -lrt -o AnalogInput3.out
11 Run:
12 sudo ./AnalogInput3.out
13 Purpose:
14 Lets see how much we can use oversampling in order to increase the resolution of our 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 40 times (set to 40) all other channels (set to 0) are not read from the ADC.
27 unsigned short overSampling[MAX_MCP3x08_CHANNELS] = { 40, 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 }
40 else
41 {
42 printf ("Error: Get_AI_Channel\n");
43 }
44
45 usleep(1000);
46 }
47
48 // Let's try 41 oversamples for channel 0.
49 unsigned short overSampling2[MAX_MCP3x08_CHANNELS] = { 41, 0, 0, 0, 0, 0, 0, 0 };
50 if (!Set_MCP3x08_Oversampling (overSampling2))
51 printf ("41 oversamples for channel 0 failed.\n");
52
53 // Ok, let's then try to oversample channel 0 and channel 1 for 40 times.
54 unsigned short overSampling3[MAX_MCP3x08_CHANNELS] = { 40, 40, 0, 0, 0, 0, 0, 0 };
55 if (!Set_MCP3x08_Oversampling (overSampling3))
56 printf ("40 oversamples for channel 0 and 40 oversamples for channel 1 failed.\n");
57
58 // Well, how many times can I the oversample a channel?
59 // This depends on the following things:
60 // - the number of channels to be oversampled,
61 // - the number of oversamples for each channel and
62 // - the cycle time which should be kept.
63 // Of cause it also depends on the CPU load of the Raspi...
64
65 // Test 30 oversamples for channel 0 and 10 for channel 1.
66 unsigned short overSampling4[MAX_MCP3x08_CHANNELS] = { 30, 10, 0, 0, 0, 0, 0, 0 };
67 if (Set_MCP3x08_Oversampling (overSampling4))
68 printf ("30 oversamples for channel 0 and 10 oversamples for channel 1 worked.\n");
69
70 // Test 8 oversamples for channel 0, 12 for channel 1, 10 for channel 5 and 10 for channel 7.
71 unsigned short overSampling5[MAX_MCP3x08_CHANNELS] = { 8, 12, 0, 0, 0, 10, 0, 10 };
72 if (Set_MCP3x08_Oversampling (overSampling5))
73 printf ("Oversampling 8/12/10/10 for several channels worked.\n");
74
75 // Conclusion: For the given cycle time we can have a total of 40 oversamples shared amongst
76 // all of our 8 analog input channels.
77
78 // Free library resources.
79 Uninitialize();
80
81 return 0;
82 }
MAX_MCP3x08_CHANNELS
#define MAX_MCP3x08_CHANNELS
Definition: hubolib.h:39
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.
main
int main(void)
Definition: AnalogInput3.cpp:17
HuboLib
Definition: hubocfg.h:10