Go Down

Topic: HardwareSerial and analogRead suggestions (Read 1 time) previous topic - next topic


I would like to have som new functions/methods in HardwareSerial and analog reading.

I called them:

void analogRBegin(pin)  

This starts the analog conversion only.

int analogRRead()

Returns -1 if the conversion is not ready, otherwise the analog reading from the conversion started by analogRBegin();

And I would like to have a similar solution for writing to the hardware serial port.  Absolutely no need for buffering or so, it would be enough if You could ask if the TX is available like you can ask if the RX port is available. In some applications maybe you don't want to send some data with lower priority if the port is busy as one example.

Now I must add a couple of rows to a couple of files every time I download a new release of Arduino. These simple functions would not affect the others and is a must in some applications where you can't afford to waiste clock cycles waiting for an analog read or the serial port.


Why don't you share you code as an example how you think it should be done?

Check out my experiments http://blog.blinkenlight.net


Good idea!

void analogRBegin(uint8_t pin)
     // set the analog reference (high two bits of ADMUX) and select the
     // channel (low 4 bits).  this also sets ADLAR (left-adjust result)
     // to 0 (the default).
     ADMUX = (analog_reference << 6) | (pin & 0x07);
#if defined(__AVR_ATmega1280__)
     // the MUX5 bit of ADCSRB selects whether we're reading from channels
     // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
     ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);

     // without a delay, we seem to read from the wrong channel

     // start the conversion
     sbi(ADCSRA, ADSC);

int analogRRead()
     uint8_t low, high;

     // ADSC is cleared when the conversion finishes
     if (bit_is_set(ADCSRA, ADSC)) return -1;

     // we have to read ADCL first; doing so locks both ADCL
     // and ADCH until ADCH is read.  reading ADCL second would
     // cause the results of each conversion to be discarded,
     // as ADCL and ADCH would be locked when it completed.
     low = ADCL;
     high = ADCH;

     // combine the two bytes
     return (high << 8) | low;

Done in 2 minutes, there might be errors ^^


hmm analogRBegin should probable test bit_is_set(ADCSRA, ADSC) to see if there's an ongoing conversion and return an error code accordingly...

Go Up