Mcp3911 Register Errors

Hello friends,,

I am using 3911 library. But I have some errors. How can I fix them ?

Library link :

Software :

#include <Arduino.h>
#include <SPI.h>
#include <MCP3911.h>

#define ADC_CS_PIN 9
#define CLOCK_PIN 6

class myADC : public MCP3911::C {
public:
virtual void begin(void) {

Vref = 1.2023f;

pinMode(ADC_CS_PIN, OUTPUT);
digitalWrite(ADC_CS_PIN, HIGH);

SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
reset();
}
virtual void SPI_read(uint8_t addr, uint8_t *buffer, size_t count) {
digitalWrite(ADC_CS_PIN, LOW);
SPI.transfer(addr << 1 | 1);
while (count--)
*buffer++ = SPI.transfer(0xff);
digitalWrite(ADC_CS_PIN, HIGH);
}
virtual void SPI_write(uint8_t addr, uint8_t *buffer, size_t count) {
digitalWrite(ADC_CS_PIN, LOW);
SPI.transfer(addr << 1 | 0);
while (count--)
SPI.transfer(*buffer++);
digitalWrite(ADC_CS_PIN, HIGH);
}
};

MCP3911::_ConfRegMap c;
myADC adc;

void setup_timer(void) {

pinMode(CLOCK_PIN, OUTPUT);
TIMSK0 = 0;

// Timer 0, Mode 2, toggle a on compare, toggle b on compare
TCCR0A = bit (WGM01) | bit (COM0A0) | bit (COM0B0);
TCCR0B = 1;

OCR0A = 0;
OCR0B = 0;
}

void setup(void) {

Serial.begin(115200);

setup_timer();

adc.begin();

{ using namespace MCP3911;

// set register map ptr
adc._c = &c;

adc.reg_read(REG_STATUSCOM, REGISTER, 2);
c.status.read_reg_incr = ALL;
c.status.write_reg_incr = ALL;
adc.reg_write(REG_STATUSCOM, REGISTER, 2);

// read default values
adc.reg_read(REG_CHANNEL_0, ALL);

c.gain.ch0 = GAINX1;
c.gain.boost = BOOSTX2;

c.status.ch0_modout = 0;
c.status.dr_pull_up = 1;

c.config.az_on_hi_speed = 1;
c.config.clkext = 1;
c.config.vrefext = 0;
c.config.osr = O4096;
c.config.prescale = MCLK4;
c.config.dither = MAX;

c.status.read_reg_incr = ALL;
c.status.write_reg_incr = TYPE;

// vref adjustment
c.vrefcal += 0x11;

// write out all regs
adc.reg_write(REG_MOD, TYPE);

// set grouping for TYPE
c.status.read_reg_incr = TYPE;
adc.reg_write(REG_STATUSCOM, REGISTER, 2);
}
}

void loop(void) {

double v0,v1;

adc.reg_read(REG_CHANNEL_0, TYPE); // read 6 regs
v0 = adc.get_value(0);
v1 = adc.get_value(1);
Serial.print("CH0: ");Serial.print(v0, DEC);Serial.print(" ");
Serial.print("CH1: ");Serial.println(v1, DEC);

}

Errors:

Arduino:1.6.6 (Windows 8.1), Kart:"Arduino/Genuino Uno"

C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino: In function 'void loop()':

sketch_oct17a:108: error: 'REG_CHANNEL_0' was not declared in this scope

adc.reg_read(REG_CHANNEL_0, TYPE); // read 6 regs

^

C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:108:18: note: suggested alternative:

In file included from C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:3:0:

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:35:18: note: 'REG_CHANNEL_0'

typedef enum { REG_CHANNEL_0 = 0x00,

^

sketch_oct17a:108: error: 'TYPE' was not declared in this scope

adc.reg_read(REG_CHANNEL_0, TYPE); // read 6 regs

^

C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:108:33: note: suggested alternative:

In file included from C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:3:0:

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:61:37: note: 'TYPE'

typedef enum { REGISTER, GROUP, TYPE, ALL } Looping;

^

sketch_oct17a:109: error: no matching function for call to 'myADC::get_value(int)'

v0 = adc.get_value(0);

^

C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:109:25: note: candidate is:

In file included from C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:3:0:

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:173:14: note: void MCP3911::C::get_value(double*, uint8_t)

void get_value(double *, uint8_t channel);

^

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:173:14: note: candidate expects 2 arguments, 1 provided

sketch_oct17a:110: error: no matching function for call to 'myADC::get_value(int)'

v1 = adc.get_value(1);

^

C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:110:25: note: candidate is:

In file included from C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:3:0:

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:173:14: note: void MCP3911::C::get_value(double*, uint8_t)

void get_value(double *, uint8_t channel);

^

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:173:14: note: candidate expects 2 arguments, 1 provided

exit status 1
'REG_CHANNEL_0' was not declared in this scope

The MCP3911 library correctly uses a header file and source file. Why does your class not?

PaulS:
The MCP3911 library correctly uses a header file and source file. Why does your class not?

Already I added #include <MCP3911.h>.

Furkan:
Already I added #include <MCP3911.h>.

That does not answer my question.

PaulS:
That does not answer my question.

Sir, you can check from this link :
Library link :

The library include MCP3911.h and MCP3911.c files. I think , when I write as " #include <MCP3911.h> " , a header file and source file called.

The library include MCP3911.h and MCP3911.c files. I think , when I write as " #include <MCP3911.h> " , a header file and source file called.

Yes (except that the file is MCP3911.cpp). So, why is YOUR class not defined as a header and source file (myADC.h and myADC.cpp)?

I dont know why my class isnt defined. How can I define my class as a header and source file ?

if you look in the library you pointed to you will see this:

namespace MCP3911 {
 
 typedef enum {  REG_CHANNEL_0   = 0x00,
                    REG_CHANNEL_1   = 0x03,
                    REG_MOD         = 0x06,
                    REG_PHASE       = 0x07,
                    REG_GAIN        = 0x09,
                    REG_STATUSCOM   = 0x0a, 
                    REG_CONFIG      = 0x0c,
                    REG_CONFIG2     = 0x0c,
                    REG_OFFCAL_CH0  = 0x0e,
                    REG_GAINCAL_CH0 = 0x11,
                    REG_OFFCAL_CH1  = 0x14,
                    REG_GAINCAL_CH1 = 0x17,
                    REG_VREFCAL     = 0x1a } _Regs;

You can see in your setup() using namespace MCP3911; but not in your loop()

you can read about Namespaces here

I added " using namespace MCP3911; " in my loop. But I'm still getting some errors.

Arduino:1.6.6 (Windows 8.1), Kart:"Arduino/Genuino Uno"

C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino: In function 'void loop()':

sketch_oct17a:109: error: no matching function for call to 'myADC::get_value(int)'

     v0 = adc.get_value(0);

                         ^

C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:109:25: note: candidate is:

In file included from C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:3:0:

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:173:14: note: void MCP3911::C::get_value(double*, uint8_t)

         void get_value(double *, uint8_t channel);

              ^

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:173:14: note:   candidate expects 2 arguments, 1 provided

sketch_oct17a:110: error: no matching function for call to 'myADC::get_value(int)'

     v1 = adc.get_value(1);

                         ^

C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:110:25: note: candidate is:

In file included from C:\Users\FRKN\Documents\Arduino\sketch_oct17a\sketch_oct17a.ino:3:0:

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:173:14: note: void MCP3911::C::get_value(double*, uint8_t)

         void get_value(double *, uint8_t channel);

              ^

C:\Program Files (x86)\Arduino\libraries\MCP3911-master/MCP3911.h:173:14: note:   candidate expects 2 arguments, 1 provided

exit status 1
no matching function for call to 'myADC::get_value(int)'

The class has a method called get_value() that takes two arguments and returns nothing. You are trying to call it with one argument, and expecting it to return something. Why?

Does this line in the library creating this problem? Because, as you say, This line need 2 arguements and returns nothing, right ?

        void get_value(double *, uint8_t channel);

No this line creates the pb in your code

     v0 = adc.get_value([color=red]0[/color]);

because you give only one parameter and two are expected and you want to store the result in v0 but the call does not return anything...

probably

   adc.get_value(&v0, 0);
   adc.get_value(&v1, 1);

would already be a better way of handing this assuming the channel 0 and 1 are correct

The code in the mentioned repository doesn't work. As J-M-L pointed out, there are obvious mistakes in the examples. (As well as in the library itself).

I made a library that does work:

Greetings

Cramstyler