06-14-2012 09:46 PM
I just revise LIFA code for MCP 4822 following this post.I will test it tomorrow morning in the morning. Now I just post it out and I am not sure whether it is correct or not.
Someone has already created MCP 4822 arduino library which is pretty good. You can download it from here.
The Example code is as below
#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);
analogOut.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV8);
SPI.begin();
// Configure analog outputs
analogOut.setGain2X_AB();
}
void loop() {
static int cnt=0;
analogOut.setValue_A(cnt);
cnt += 20;
if (cnt > 4095) {
cnt = 0;
}
}
The example is straightforward. I could get an output voltage from DACa through setValue_A method. So, I would like to create a VI with one parameter which is voltage(0-4095).
1. Revise LabVIEW base
I added serveral code:
// Variables
unsigned int retVal;
int sevenSegmentPins[8];
int currentMode;
unsigned int freq;
unsigned long duration;
int i2cReadTimeouts = 0;
char spiBytesToSend = 0;
char spiBytesSent = 0;
char spiCSPin = 0;
char spiWordSize = 0;
Servo *servos;
byte customChar[8];
LiquidCrystal lcd(0,0,0,0,0,0,0);
MCP4822 analogOut(A3,A0);
Create on MCP4822 object first. A3 is CS pin, A0 is LDAC pin
case 0x2E: // MCP4822 support
analogOut.setGain2X_AB();
analogOut.setValue_A(command[2]);
break;
I added serveral line here as Example does. command[2] is the set voltage.
And then,I create a new VI:
The number for MCP4822Write is 46, which equals to 0x2E.
I am not sure this is correct, so please help me if you find something wrong with it. Thank You!
06-14-2012 10:07 PM
You didn't initilize the object correctly (i.e. Your method doesn't match that of the example).
Also, this would be more appropriate in your original thread.
06-14-2012 10:08 PM
So I should follow that. Are there any other wrongs?
06-14-2012 10:10 PM
Nothing that is obvious as far as I can tell. The best thing to do is just test it, IMO. If it works then great. If not then it's time to try and debug it.
06-14-2012 10:13 PM
Thank you!
06-15-2012 02:20 PM
HI, Nathan, it doesn't work
06-15-2012 05:54 PM
What didn't work? Does the code compile for Arduino? Does the code run on Arduino? Do other functions via LabVIEW work with the code that contains the new additions?
Otherwise, I can't help you without your code.
06-16-2012 11:51 AM
Hi, Nathan, this is my whole 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);
analogOut.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV8);
SPI.begin();
// Configure analog outputs
analogOut.setGain2X_AB();
}
void loop() {
static int cnt=0;
analogOut.setValue_A(cnt);
cnt += 20;
if (cnt > 4095) {
cnt = 0;
}
}
This is the original MCP4822 example.
Based on that, I added extra codes to LABVIEWInterface.pde as follows
1. Define CS pin and LDAC pin, also included the library.
#include <Wire.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#define AOUT_CS 10
#define AOUT_LDAC A3
2. Create one MCP4822 object at variable area
// Variables
unsigned int retVal;
int sevenSegmentPins[8];
int currentMode;
unsigned int freq;
unsigned long duration;
int i2cReadTimeouts = 0;
char spiBytesToSend = 0;
char spiBytesSent = 0;
char spiCSPin = 0;
char spiWordSize = 0;
Servo *servos;
byte customChar[8];
LiquidCrystal lcd(0,0,0,0,0,0,0);
MCP4822 analogOut = MCP4822(AOUT_CS,AOUT_LDAC);
3. create serveral case for begin(), setGain2X_AB(), and setValue_A();
case 0x2E:
analogOut.begin();
Serial.write('0');
break;
case 0x2F:
analogOut.setGain2X_AB();
Serial.write('0');
break;
case 0x34:
analogOut.setValue_A(command[2]);
Serial.write('0');
break;
This what I have done for firmware. Can you find something wrong?
06-16-2012 03:12 PM
Did you try it? At what point does it not work? E.g. Does it compile? Do you get any errors?
06-16-2012 03:14 PM
Yeah, i tried tha. It compiled, no errors but not work