Helo
I've connected the MCP3208 ADC to Arduino using instructions from h_t_t_p_://_w_w_w.arduino.cc/playground/Code/MCP3208. I'm reading four inputs from this ADC at channels 0,2,4,6 of the ADC. The output Serial.println's should send messages with speed of exactly 12Hz, and with this code it does. I'm using the code:
#define bitSamples0 20 // 1x1 - 12bit; 16x1-16bit; 16x16-20bit; 32x1-17bit; 32x32-22bit
#define bitSamples1 3 // 13x10x12Hz=1560sampli/sek; 14x10x12Hz=1680sampli/sek ; 40x40x12 = 19200sampli/sek ;
#define SELPIN 10 //Selection Pin
#define DATAOUT 11 //MOSI
#define DATAIN 12 //MISO
#define SPICLOCK 13 //Clock
void setup() {
Serial.begin(115200);
//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);
}//end setup()
void loop(){
unsigned long total0 = 0;
float total1[4] = { 0.0000000,0.0000000,0.0000000,0.0000000 };
if ( Serial.available () > 0 ){
char xxx = Serial.read ();
for ( byte j=0; j<bitSamples1; j++){
for ( byte z=0; z<4; z++ ){
for ( byte i=0; i<bitSamples0; i++ ){
total0 += lb_analogRead ( z*2 ); // input channels 0, 2, 4, 6 [0-7]
}//i
total1[z]+= total0/bitSamples0;
total0 = 0;
}//z
}//j
//-- Serial.println -----------------------
if ( xxx == 'T' ){
Serial.print( "T " );
Serial.print ( (((total1[0]/bitSamples1) / 4 ) * 100), 2 );
Serial.print(";");
Serial.print ( (((total1[1]/bitSamples1) / 4 ) * 100), 2 );
Serial.print(";");
Serial.print ( (((total1[2]/bitSamples1) / 4 ) * 100), 2 );
Serial.print(";");
Serial.println( (((total1[3]/bitSamples1) / 4 ) * 100), 2 );
delay(1);
}//T
}// end... serial.availble()
}// end... loop()
I'm using 13x10 not 130x1 because this gives me better quality of the readings (teoreticly it doesn't matter but empirycaly it does).
Using code from the link above I reached up to 20x3x12=720samples per channel. It makes 2880samples/s generaly. Then I've changed the code from the link above into:
int lb_analogRead(int channel){
int adcvalue = 0;
byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)
//allow channel selection
commandbits|=(channel<<3);
//Select adc
PORTB ^= (1 << 2);
// setup bits to be written
for (int i=7; i>=3; i--){
digitalWrite(DATAOUT,commandbits&1<<i);
//cycle clock
PORTB ^= (1 << 5);
PORTB ^= (1 << 5);
}
//ignores 1 null bit
PORTB ^= (1 << 5);
PORTB ^= (1 << 5);
//digitalRead(DATAIN) //it should check if the bit is 0, but I don't care it. It works
//ignores 1 null bit
PORTB ^= (1 << 5);
PORTB ^= (1 << 5);
//read bits from adc
for (int i=11; i>=0; i--){
adcvalue+=digitalRead(DATAIN)<<i;
//cycle clock
PORTB ^= (1 << 5);
PORTB ^= (1 << 5);
}
//turn off device
PORTB ^= (1 << 2);
return adcvalue;
}
It works fine, but still too slow. It reaches 13x10x12/channes/s (6240Hz). I need to have at leaset 20x10x12/cannel/s (2400Hz/channel; 9600Hz). The perfect value would be 50x10x12/channel/s (6000Hz/channel; 24000Hz) (or similar 25x20x12/channel/s) .
I'm writig for help. I've tried to use the SPI Library h_t_t_p_://w_w_w.arduino.cc/playground/Code/Spi to speed this up, but I dot know how to change the reedings into a integer/long value especialy, if the ADC first reads 5 bits then (after one SELPIN low/high state) sends one startbit and 12bits of data. The ADC datasheet is here h_t_t_p://_w_w_1.microchip.com/downloads/en/DeviceDoc/21298D.pdf.
If You have better solutions please write it.
(Sorry for my language. I'm Polish. Sorry for h_t_t_p_://. This is my first post and I cant sent URL's)