Forum Rule: Always post complete source code & details to reproduce any issue!
Page 20 of 20 FirstFirst ... 10 18 19 20
Results 476 to 480 of 480

Thread: ADC library, with support for Teensy 4, 3.x, and LC

  1. #476
    Senior Member
    Join Date
    Jan 2020
    Posts
    106
    Hello!

    I'm trying to do calculations while waiting for conversions to finish.
    Is it guaranteed that ADC0 will read sensor A0 and sensor A2? And
    ADC1 will always read A1 and A3 in this code? If not, is there a way
    I can change the code to do that?



    Code:
    void loop()
    {
    
    adc->startSynchronizedSingleRead(A0, A1);
    
    //Calculations on previous sensor pair
    from second time through the loop (A2, A3);
    
    while (adc->adc0->isConverting() || adc->adc1->isConverting());
    g_sensorValue[0] = 1023 - (adc->adc0->readSingle());
    g_sensorValue[1] = 1023 - (adc->adc1->readSingle());
    adc->startSynchronizedSingleRead(A2, A3);
    
    //Calculations on previous sensor pair (A0, A1);
    
    while (adc->adc0->isConverting() || adc->adc1->isConverting());
    g_sensorValue[2] = 1023 - (adc->adc0->readSingle());
    g_sensorValue[3] = 1023 - (adc->adc1->readSingle());
    
    }

  2. #477
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    7,422
    Looking at the code, I think it is reasonably safe to assume that:
    Code:
    bool ADC::startSynchronizedSingleRead(uint8_t pin0, uint8_t pin1)
    {
        // check pins
        if (!adc0->checkPin(pin0))
        {
            adc0->fail_flag |= ADC_ERROR::WRONG_PIN;
            return false;
        }
        if (!adc1->checkPin(pin1))
        {
            adc1->fail_flag |= ADC_ERROR::WRONG_PIN;
            return false;
        }
    
        // check if we are interrupting a measurement, store setting if so.
        adc0->adcWasInUse = adc0->isConverting(); // is the ADC running now?
        if (adc0->adcWasInUse)
        { // this means we're interrupting a conversion
            // save the current conversion config, the adc isr will restore the adc
            __disable_irq();
            //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) );
            adc0->saveConfig(&adc0->adc_config);
            __enable_irq();
        }
        adc1->adcWasInUse = adc1->isConverting(); // is the ADC running now?
        if (adc1->adcWasInUse)
        { // this means we're interrupting a conversion
            // save the current conversion config, the adc isr will restore the adc
            __disable_irq();
            //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) );
            adc1->saveConfig(&adc1->adc_config);
            __enable_irq();
        }
    
        // no continuous mode
        adc0->singleMode();
        adc1->singleMode();
    
        // start both measurements
        adc0->startReadFast(pin0);
        adc1->startReadFast(pin1);
    
        //digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) );
        return true;
    }
    But what I have not looked at is if those pins are Valid for those ADCs on every Teensy... But you should be able to verify that for whichever teensy you are using by either looking at code, or Reference Manual or easier yet I believe he has a summary of which pins are valid for which ADC...

  3. #478
    Senior Member
    Join Date
    May 2015
    Location
    USA
    Posts
    561
    > trying to do calculations while waiting for conversions

    Yes, it makes sense to do calculations on A2,A3 values after starting a read on A0,A1. And vice versa.

    > ADC0 will read sensor A0 ...

    Yes, ADC0 is always assigned to the first pin in the parameter list.

  4. #479
    Senior Member
    Join Date
    Jan 2020
    Posts
    106
    Quote Originally Posted by KurtE View Post
    Looking at the code, I think it is reasonably safe to assume that.

    But what I have not looked at is if those pins are Valid for those ADCs on every Teensy... But you should be able to verify that for whichever teensy you are using by either looking at code, or Reference Manual or easier yet I believe he has a summary of which pins are valid for which ADC...
    Thanks, the pins are valid on the teensy 4 which Iím using and he did specify that.

    Iím getting some strange values ocassionally and itís because of my code but wanted to make sure it wasnít something else before I spend hours looking for a solution.

  5. #480
    Senior Member
    Join Date
    Jan 2020
    Posts
    106
    Quote Originally Posted by jonr View Post
    > trying to do calculations while waiting for conversions

    Yes, it makes sense to do calculations on A2,A3 values after starting a read on A0,A1. And vice versa.

    > ADC0 will read sensor A0 ...

    Yes, ADC0 is always assigned to the first pin in the parameter list.
    Thanks 🙏 Just needed to be sure

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •