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?
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.
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().
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
--
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
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
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.
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:
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: