per ADCH hai ragione, se ti bastano 8 bit
per il free running, funziona in modo completamente diverso.
Stai settando i registri sbagliati: ADMUX lo modifichi solo una volta per settare la configurazione, poi da li in poi cambi solo gli ultimi 4 bit per selezionare il pin da leggere e il bit ADSC per avviare una lettura.
Ti posto il codice che ho scritto, anche se per ora non funziona, ma son troppo stanco per capire cosa ho sbagliato, spero in un'anima pia, altrimenti domani provo a sbatterci ancora la testa:
/*
Fast Analog Input
it read 3 analog pin with a 10bit precision. (until 200kHz according to datasheet, but it will work at 8MHz)
it will store the 3 read of 30bit into one unsigned long (32bit) and then print it to serial.
That way it use 4 byte instead of 6 to store the data, and because it will write 5 byte instead of 7 into serial, it is 1,4 times faster to write
If in 8bit mode, it will store the 3 read of 24bit + 8bit of '\n' into one unsigned long (32bit) and then print it to serial.
That way it use 3 byte instead of 6 to store the data, and because it will write 4 byte instead of 7 into serial, it is 1,75 times faster to write
End of String value is \n
it will use analog pin
A0 = 14
A1 = 15
A2 = 16
This code should work on all arduino IDE version, but faster on version > 1.0 because of asincronous serial communication.
That mean that serial will write data while you are reading the next one, but this also mean we cannot use the ADC noise canceller mode.
This code is tested on Atmega328P, but should work for:
Atmega48A
Atmega48PA
Atmega88A
Atmega88PA
Atmega168A
Atmega168PA
Atmega328
Atmega328P
Created by lesto for http://www.arduino.cc
12 June 2012
This example code is LPGL licence Version 3, 29 June 2007
you can find it on http://www.gnu.org/licenses/lgpl.html
*/
#define USE10BIT 1
void setup() {
// start serial (it is slow, change this to higher value)
Serial.begin(9600);
Serial.flush();
//just a bit of delay
delay(2000);
#if USE10BIT
Serial.println("10BIT");
#else
Serial.println("8BIT");
#endif
delay(2000);
//ADCSRA: with this register we set the prescaler to 2, on 16MHz clock this mean 8MHz reading
//ADEN to 1: ADC enable
//ADSC to 0: do not start ADC read now
//ADATE to 0: single conversion mode
//ADIF to 0: we don't have reading done yet
//ADIE to 0: we don't want ADC interrupt
//ADPS[2:0] to 000: prescaler = 2
//ADCSRA |= B00000111;
Serial.println(ADCSRA, DEC);
}
int i;
unsigned long ris;
void loop() {
//reset ris
ris=0;
for (int pin = A0; pin < A3; pin++){
#if USE10BIT
ris = ris << 10;
ris += analogRead(pin);
#else
ris = ris << 8;
ris += analogRead(pin)>>2; //remove 2 lower bit
#endif
}
#if USE10BIT
ris = ris<<2;
Serial.write(ris>>24);
Serial.write(ris>>16);
Serial.write(ris>>8);
Serial.write(ris);
Serial.print('\n');
#else
ris = ris<<8;
ris |='\n';
Serial.write(ris>>24);
Serial.write(ris>>16);
Serial.write(ris>>8);
Serial.write(ris);
#endif
Serial.flush();
delay(100);
}
per leggere i dati, usa processing:
/**
* Simple Read Fast
*
* from lesto http://www.arduino.cc
*/
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
println(portName);
myPort = new Serial(this, portName, 9600);
}
void draw()
{
byte[] inBuffer = new byte[30];
int l = myPort.readBytesUntil('\n', inBuffer);
if (l != 0)
println("readed "+l);
if (inBuffer != null && l == 5) {
int a=0, b=0, c=0;
for (int i = 0; i < 4; i++){
println(inBuffer[i]& 0xFF);
}
a = ( inBuffer[0] & 0xFF)<<2; //all 0
println(a);
a += ( inBuffer[1]& 0xFF) >>6; //and 2 first 2 bit of 1
b = ( (inBuffer[1]& 0xFF) & 63)<<4; //last 6bit of 1
println(b);
b += ( (inBuffer[2]& 0xFF) >>4); //and first 4 bit of 2
c = ( (inBuffer[2])& 0xFF & 15)<<6; //last 4bit of if 2
println(c);
c += ( inBuffer[3] & 0xFF) >> 2; //fisrt 6 bit of 3
println("a:"+a+" b:"+b+" c:"+c);
}
if (inBuffer != null && l == 4) {
int a=0, b=0, c=0;
a = inBuffer[0] & 0xFF;
b = inBuffer[1] & 0xFF;
c = inBuffer[2] & 0xFF;
println("a:"+a+" b:"+b+" c:"+c);
}
}