Pages: [1]   Go Down
Author Topic: Getting mcp4822 and mcp4621 libraries to work in Arduino 1.0 - RESOLVED  (Read 1150 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

I've been using the mcp4822 from here:
https://bitbucket.org/iorodeo/iorodeo_arduino_libs/src

and 4621 (McpDigitalPot) from here:
https://github.com/teabot/McpDigitalPot

they both work fine in Arduino 022, but not in IDE 1.0, even after adding (for 4822 library)

#include "Arduino.h"
#else
#include "WProgram.h"
#endif

to the .cpp file.

Can anyone point me in the right direction what else needs to be changed?
In Arduino 1.0, it seems like the CS pin is not pulled low and so there is no communication with the chip, and I assume it has got something to do with the library, but I have idea what.
Any help or pointers in the right direction would be appreciated, I was thinking it might be something to do with how bytes are written in IDE 1.0?

« Last Edit: February 09, 2012, 08:35:05 am by ICPK » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50500
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any help or pointers in the right direction would be appreciated, I was thinking it might be something to do with how bytes are written in IDE 1.0?
Bytes are written exactly the same way in 1.0 as they were written in previous versions.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul, so we now know that bytes are not the problem.

If someone else has a look at the libraries and figures out what may stop them from working in IDE 1.0, could you please let me know.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50500
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In Arduino 1.0, it seems like the CS pin is not pulled low and so there is no communication with the chip
Have you tried adding code to explicitly set the CS pin? A lot of libraries now do this automatically.

Code:
MCP4822::MCP4822(int csPin, int ldacPin) {
    // Configure chip select and latch pins
    cs = csPin;
    ldac = ldacPin;
    pinMode(cs,OUTPUT);
    pinMode(ldac,OUTPUT);
    digitalWrite(cs,HIGH);
    digitalWrite(ldac,HIGH);
    // Set to default configuration
    setGain2X_AB();
}
The calls to pinMode() and digitalWrite() have no place in the constructor. The constructor will be called before init() does it's thing, so when init() runs, stuff done in the constructor will be undone.

The library really needs a begin method where the pinMode() and digitalWrite() calls are made (and probable the setGain2S_AB() call, too.

The begin() method, when you add it, should be called in setup().
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS,

Thanks for your reply and the suggestions. In order to figure out what you were talking about exactly , I've looked at the arduino library tutorial (http://arduino.cc/it/Hacking/LibraryTutorial) and a library that has a begin() call (the built in wire.cpp and wire.h).

In the tutorial, pinMode is called in the constructor but I followed you suggestion and changed the code inside mcp4822.cpp to
Code:
--
MCP4822::MCP4822(int csPin, int ldacPin)
{
    // Configure chip select and latch pins
    cs = csPin;
    ldac = ldacPin;
}

//---------------------------------------------------------------------------
void MCP4822::begin()
{
    pinMode(cs,OUTPUT);
    pinMode(ldac,OUTPUT);
    digitalWrite(cs,HIGH);
    digitalWrite(ldac,HIGH);
    // Set to default configuration
    setGain2X_AB();
}


In mcp4822.h, I added

Code:
public:
   void begin

and in Keyword.txt I added "begin" as a "KEYWORD2".

after saving the files, I restart Arduino 1.0, open the setValue_AB example sketch and add MCP4822.begin(); after the SPI.begin() call in setup().

I'm clearly doing something wrong, the result on compiling is the following error message:
In file included from set_value_AB.cpp:2:
/Users/Arduino Sketches/libraries/mcp4822/mcp4822.h:25: error: variable or field 'begin' declared void
/Users/Arduino Sketches/libraries/mcp4822/mcp4822.h:26: error: expected ';' before 'void'

I've been looking for missing curly brackets or semicolons, but can't see what I should change now.


Secondly, you were asking
Quote
Have you tried adding code to explicitly set the CS pin? A lot of libraries now do this automatically.

I don't know what you mean by explicitly setting the CS pin. I have tried using the same pins for other SPI stuff under IDE 1.0 (e.g. using the SD library) and that works fine.



Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50500
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In mcp4822.h, I added

Code:
public:
   void begin
begin is a function, isn't it? Functions always have parentheses. Statements always end with ;.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul,

Apologies for the late reply, been without internet for a few days.
Should have really spotted that mistake.... I fixed it and get one more error message:

Code:
set_value_AB.cpp: In function 'void setup()':
set_value_AB.pde:-1: error: expected unqualified-id before '.' token

This occurs when i add the MCP4822.begin(); to setup() in the example code:

Code:
#include <SPI.h>
#include "mcp4822.h"

#define AOUT_CS A1
#define AOUT_LDAC A3

MCP4822 analogOut = MCP4822(AOUT_CS,AOUT_LDAC);

void setup() {
    // Setup serial and SPI communications
    Serial.begin(115200);
    SPI.setDataMode(SPI_MODE0);
    SPI.setBitOrder(MSBFIRST);
    SPI.setClockDivider(SPI_CLOCK_DIV8);
    SPI.begin();
    MCP4822.begin();
    // Configure analog outputs
    analogOut.setGain2X_AB();
}


void loop() {
    static int cnt=0;
    analogOut.setValue_AB(cnt,4095-cnt);
    cnt += 20;
    if (cnt > 4095) {
        cnt = 0;
    }
}
do I need to tell the library that MCP4822.begin(); is related to void MCP4822::begin() in mcp4822.cpp?

Thanks again for all your help.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, the command in setup() should have been analogOut.begin();, not MCP4822.begin();

Will at iorodeo very kindly updated both the macp4822 and their mcp4621 libraries yesterday - both are now working under Arduino IDE 1.0.

Thanks to both of you for your help!
Logged

Pages: [1]   Go Up
Jump to: