12-27-2013 05:01 PM
hello everyone,
I'm trying to read values from the MCP3204 but I'm stuck ... I appreciate any help
below is the code that I'm trying to ported in LabView
the thing that bothers me is that the MCP3204 only responds in MODE1 although documentation says it supports MODE0 and MODE1.
I successfully connected the MCP4921 DAC and it works perfectly in MODE0
Is it possible to connect two SPI device with different MODEs and how?
idea is to connect DAC and ADC 12bit and comunicate via SPI.
the code below works great when uploading to arduino, problem is how to ported to labview.
working code:
#define SELPIN 10 //Selection Pin
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//Clock
int readvalue;
void setup(){
//set pin modes
pinMode(SELPIN, OUTPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK, OUTPUT);
//disable device to start with
digitalWrite(SELPIN,HIGH);
digitalWrite(DATAOUT,LOW);
digitalWrite(SPICLOCK,LOW);
Serial.begin(9600);
}
int read_adc(int channel){
int adcvalue = 0;
byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)
//allow channel selection
commandbits|=((channel-1)<<3);
digitalWrite(SELPIN,LOW); //Select adc
// setup bits to be written
for (int i=7; i>=3; i--){
digitalWrite(DATAOUT,commandbits&1<<i);
//cycle clock
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}
digitalWrite(SPICLOCK,HIGH); //ignores 2 null bits
digitalWrite(SPICLOCK,LOW);
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
//read bits from adc
for (int i=11; i>=0; i--){
adcvalue+=digitalRead(DATAIN)<<i;
//cycle clock
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}
digitalWrite(SELPIN, HIGH); //turn off device
return adcvalue;
}
void loop() {
readvalue = read_adc(1);
Serial.println(readvalue,DEC);
readvalue = read_adc(2);
Serial.println(readvalue,DEC);
Serial.println(" ");
delay(250);
}
is attacment is one of my unsuccessful attempt
thank in advance
12-28-2013 05:27 AM
The first thing that I notice is that the datasheet says that it supports "modes 0,0 and 1,1" which, I believe, refers to LIFA SPI modes "Mode 0" and "Mode 3" respectively. The other thing that I notice is that you are not using the same chip select pin. The Arduino code shows pin 10 but in LabVIEW you are using pin 9.
I haven't had time to look at much else more in depth but I hope this helps.
12-28-2013 08:55 AM
Thank you Nathan_B,
You're right, that's what I saw in the documentation regarding MODE, but i have some respond only if i set MODE1 on LIFA SPI (that is not 0,0 or 1,1)
Mode | Clock Polarity (CPOL) | Clock Phase (CPHA) |
SPI_MODE0 | 0 | 0 |
SPI_MODE1 | 0 | 1 |
SPI_MODE2 | 1 | 0 |
SPI_MODE3 | 1 | 1 |
Regarding selection of pins...
I connect CS pin from ADC MCP3204 to arduino pin 9 because the pin 10 is already in use with the MCP4921 DAC and work properly with LIFA MODE0
other pin from ADC are connected to 12 SDO, 11 SDI, 13 SCK, 9 CS
this works with MCP4921 DAC only MODE0 LIFA is different.
I'll try with logic analyser to see what's going on.
i have no idea for now what to do
12-28-2013 04:21 PM
I notice that the MCP3204 (on page 17) has a special note that the MCP4921 doesn't, referring to the datasheets. I don't entirely understand it so I can't be for sure but it might be something to consider.