Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: How to preserve custom ADC settings during analogRead on Teensy 3.6

  1. #1

    How to preserve custom ADC settings during analogRead on Teensy 3.6

    I have made several posts on topics relating to my project. I am essentially working on a BLDC motor controller that I designed and built. This particular part pertains to measuring the current through the motor via a current sense resistor, a MAX40201 current sense amplifier and a Teeny 3.6.

    To do this, I had to adjust the registers on the Teensy 3.6 to have the PWM trigger a PDB timer, which in turn activates the ADC at a specific interval so that current measurments are taken ONLY while the MOSFETs are "active", otherwise it will just measure zero amperes due to the MOSFETS isolating the current sense resistor from the motor.

    This is where it gets tricky and I could use some help. Paul's stock analogRead() stuff, housed in analog.c, overwrites ALL the ADC registers that I set, disabling the ADC interrupt registers that I need, as soon as an
    is called, whether or not I am reading the current sense pin or some other pin.

    How can I maintain the settings that I set for measuring current while still allowing the use of analogRead() without creating a custom version of the analogRead() functions? Is there some simple way to backup and restore the registers after a call to analogRead()?

    Separate, but related question:

    I noticed that Paul's analog.c code lumps the setting for ADC0 and ADC1 together. That is if you set ADC0 to 16bit, then ADC1 gets set to 16 bit also. Does the K66 controller require it to be that way? Can ADC0 and ADC1 have separate settings such that one reads 16 bit while the other reads 12 bit, for example?
    Last edited by RRdae; 05-21-2020 at 04:11 AM.

  2. #2
    Please consider this thread closed. The solution was obvious once I realized what the issue was.

    Basically, I was using settings (hardware triggering) for PWM sychronized ADC that disabled software triggering, which analogRead needs to function. So I wrote a quick wrap for analogRead that switches from hardware triggering to software triggering and vice versa, polling analogRead in the middle. It does have the effect of disabling current sensing during analogRead calls but they should be few and far between, so I can live with it.

    See code below:

    int MD_1v0::NBanalogRead(uint8_t MD_AR_pin){
        int MD_AR_val = 0;
        ADC0_SC2 ^= ADC_SC2_ADTRG; //enable software trigger
        MD_AR_val = analogRead(MD_AR_pin);
        ADC0_SC2 |= ADC_SC2_ADTRG; //enable hardware trigger
        ADC0_SC1A = ADC_SC1_AIEN | 0x8; //Reset channel and enable interrupt
        return MD_AR_val;

Posting Permissions

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