LabVIEW Interface for Arduino Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

Revise LIFA for MCP 4822

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:

QQ截图20120614214405.bmp

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!

0 Kudos
Message 1 of 11
(7,164 Views)

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.

Message 2 of 11
(3,558 Views)

So I should follow that. Are there any other wrongs?

0 Kudos
Message 3 of 11
(3,558 Views)

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.

0 Kudos
Message 4 of 11
(3,558 Views)

Thank you!

0 Kudos
Message 5 of 11
(3,558 Views)

HI, Nathan, it doesn't work

0 Kudos
Message 6 of 11
(3,558 Views)

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.

0 Kudos
Message 7 of 11
(3,558 Views)

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?

0 Kudos
Message 8 of 11
(3,558 Views)

Did you try it?  At what point does it not work? E.g.  Does it compile?  Do you get any errors?

0 Kudos
Message 9 of 11
(3,558 Views)

Yeah, i tried tha. It compiled, no errors but not work

0 Kudos
Message 10 of 11
(3,558 Views)