Pages: [1]   Go Down
Author Topic: HardwareSerial and analogRead suggestions  (Read 747 times)
0 Members and 1 Guest are viewing this topic.
Sweden
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 24
Posts: 3499
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Udo
Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
#endif

      // without a delay, we seem to read from the wrong channel
      //delay(1);

      // 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 << smiley-cool | low;
}


Done in 2 minutes, there might be errors ^^
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 21
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: