Hello all i have bought the MCP3304 13b adc converter and having a problem with it i can not find a sketch that will work i did find something on the arduino forum page but didn't work can someone help me out please trying to program this chip it's a spi chip.
hello thanks for the reply not good at drawing but i did mange to figure out the wiring wasn't that hard i just can't find a sketch to it i did find this page Arduino Playground - MCP3208 my chip is the 3304 one not the 3308 ummm i can take a snapshot of what i did with my cellphone to show but that's all.
hello spycatcher2k yes i did the wiring is not the problem i can't not find a sketch that will work i looked online all over and only found the one in the arduino forum page and i tried it and nothing. I'm not a coder so i can't tell what is good or bad I'm trying to learn but it's a little over my head or i would make a sketch my self.
// read the MCP3304 in quad differential mode, non-bit-banging version
#include <Serial.h>
#include <SPI.h>
#include <HardwareSerial.h>
long ticks = 0;
// for Uno
#define SELPIN 10 // chip-select
#define DATAOUT 11 // MOSI
#define DATAIN 12 // MISO
#define SPICLOCK 13 // Clock
// for Mega2560 / Max32
//#define SELPIN 53 // chip-select
//#define DATAOUT 51 // MOSI
//#define DATAIN 50 // MISO
//#define SPICLOCK 52 // Clock
int readvalue;
void setup(){
//set pin modes
pinMode(SELPIN, OUTPUT);
// disable device to start with
digitalWrite(SELPIN, HIGH);
SPI.setClockDivider( SPI_CLOCK_DIV8 ); // slow the SPI bus down
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0); // SPI 0,0 as per MCP330x data sheet
SPI.begin();
Serial.begin(115200);
}
void loop() {
long ticks = millis();
int A = 0, B = 0;
A = read_adc(1);
B = read_adc(2);
// do whatever you want with these readings
long tcnv = millis() - ticks;
delay(100 - tcnv);
}
// non-bit-banging version
// channel ranges from 1.. 3 (not zero!)
// maximum clock frequency is 2.1 MHz @ 5V
//
// this is SPI_CLOCK_DIV8
// at DIV64, 512 in 60ms (8.5 ksps)
// at DIV32, 512 in 35ms (14.6 ksps)
// at DIV16, 512 in 22ms (23.3 ksps)
// at DIV8, 512 in 16ms (32 ksps)
int read_adc(int channel){
int adcvalue = 0;
int b1 = 0, b2 = 0;
int sign = 0;
// command bits for MCP3304
// 0000 = diff, ch0 = in+, ch1 = in-
// 0010 = diff, ch2 = in+, ch3 = in-
// 0100 = diff, ch4 = in+, ch5 = in-
digitalWrite (SELPIN, LOW); // Select adc
// first byte
// first byte will always be B000010xx where xx are the D2 and D1 channel bits
byte commandbits = B00001000;
commandbits |= (channel >> 1); // high bit of channel
SPI.transfer(commandbits); // send out first byte of command bits
// second byte; Bx0000000; leftmost bit is D0 channel bit
commandbits = B00000000;
commandbits |= (channel << 7); // if D0 is set it will be the leftmost bit now
b1 = SPI.transfer(commandbits); // send out second byte of command bits
// hi byte will have XX0SBBBB
// set the top 3 don't care bits so it will format nicely
b1 |= B11100000;
Serial.print(b1, BIN); Serial.print(" ");
sign = b1 & B00010000;
int hi = b1 & B00001111;
// read low byte
b2 = SPI.transfer(b2); // don't care what we send
Serial.print(b2, BIN); Serial.print("\r\n");
int lo = b2;
digitalWrite(SELPIN, HIGH); // turn off device
int reading = hi * 256 + lo;
if (sign) {
reading = (4096 - reading) * -1;
}
return (reading);
}
i reply with the answer on what I'm hooking up to the adc convertor chip i connected all the spi pins correctly i followed where the wiring is going and connected them to the uno also power as well. and i connected a lm35 temperature sensor to the channel 0 on the ADC converter chip to see if it will read from that and the last post i posted is what i got from the Serial monitor how is that not being serious if i wasn't serious why would i been here then if i didn't need help. makes no sense I'm very serious in needing help I'm not a coder or a programer and trying to get help shows I'm very serious in trying to make this work and need help in here.
// read the MCP3304 in quad differential mode, non-bit-banging version
#include <Serial.h>
#include <SPI.h>
#include <HardwareSerial.h>
long ticks = 0;
// for Uno
#define SELPIN 10 // chip-select
#define DATAOUT 11 // MOSI
#define DATAIN 12 // MISO
#define SPICLOCK 13 // Clock
// for Mega2560 / Max32
//#define SELPIN 53 // chip-select
//#define DATAOUT 51 // MOSI
//#define DATAIN 50 // MISO
//#define SPICLOCK 52 // Clock
int readvalue;
void setup(){
//set pin modes
pinMode(SELPIN, OUTPUT);
// disable device to start with
digitalWrite(SELPIN, HIGH);
SPI.setClockDivider( SPI_CLOCK_DIV8 ); // slow the SPI bus down
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0); // SPI 0,0 as per MCP330x data sheet
SPI.begin();
Serial.begin(115200);
}
void loop() {
long ticks = millis();
int A = 0, B = 0;
A = read_adc(1);
B = read_adc(2);
// do whatever you want with these readings
long tcnv = millis() - ticks;
delay(100 - tcnv);
}
// non-bit-banging version
// channel ranges from 1.. 3 (not zero!)
// maximum clock frequency is 2.1 MHz @ 5V
//
// this is SPI_CLOCK_DIV8
// at DIV64, 512 in 60ms (8.5 ksps)
// at DIV32, 512 in 35ms (14.6 ksps)
// at DIV16, 512 in 22ms (23.3 ksps)
// at DIV8, 512 in 16ms (32 ksps)
int read_adc(int channel){
int adcvalue = 0;
int b1 = 0, b2 = 0;
int sign = 0;
// command bits for MCP3304
// 0000 = diff, ch0 = in+, ch1 = in-
// 0010 = diff, ch2 = in+, ch3 = in-
// 0100 = diff, ch4 = in+, ch5 = in-
digitalWrite (SELPIN, LOW); // Select adc
// first byte
// first byte will always be B000010xx where xx are the D2 and D1 channel bits
byte commandbits = B00001000;
commandbits |= (channel >> 1); // high bit of channel
SPI.transfer(commandbits); // send out first byte of command bits
// second byte; Bx0000000; leftmost bit is D0 channel bit
commandbits = B00000000;
commandbits |= (channel << 7); // if D0 is set it will be the leftmost bit now
b1 = SPI.transfer(commandbits); // send out second byte of command bits
// hi byte will have XX0SBBBB
// set the top 3 don't care bits so it will format nicely
b1 |= B11100000;
Serial.print(b1, BIN); Serial.print(" ");
sign = b1 & B00010000;
int hi = b1 & B00001111;
// read low byte
b2 = SPI.transfer(b2); // don't care what we send
Serial.print(b2, BIN); Serial.print("\r\n");
int lo = b2;
digitalWrite(SELPIN, HIGH); // turn off device
int reading = hi * 256 + lo;
if (sign) {
reading = (4096 - reading) * -1;
}
return (reading);
}
That chip has no internal reference, it has a VREF pin. Therefore whatever is connected to that pin is critical, hence Mike's question which you did not address.
i connected that to the Aref on the uno board
Is AREF on the 328 an output by default in the Arduino setup, I think it is but that should be checked.