0

For INA3221 current sensor, I found this library, however, I don't understand how to use the INA3221SetConfig() function as defined in the .cpp file link.

void SDL_Arduino_INA3221::INA3221SetConfig(void)
{

      // Set Config register to take into account the settings above
      uint16_t config =
          INA3221_CONFIG_ENABLE_CHAN1 |
          // INA3221_CONFIG_ENABLE_CHAN2 |
          // INA3221_CONFIG_ENABLE_CHAN3 |
          INA3221_CONFIG_RESET |
          INA3221_CONFIG_AVG1 |
          INA3221_CONFIG_VBUS_CT2 |
          INA3221_CONFIG_VSH_CT2 |
          INA3221_CONFIG_MODE_2 |
          INA3221_CONFIG_MODE_1 |
          INA3221_CONFIG_MODE_0;
      wireWriteRegister(INA3221_REG_CONFIG, config);
    }

It seems like the function is called automatically when the .begin() function is called and whatever is defined in the config function is written to the register. The use of | operator confuses me. I tried to comment out the some settings to see if it works but it makes no difference. There isn't any documentation so I am in a fix. Could you please tell how can I change the configuration settings e.g. instead of all the three channels or change the config_mode?

1
3

Without looking at the library, you can check yourself if this method (INA3221SetConfig) is called from the begin method (of the same class). Just trace back the methods manually until you find a call to this method.

About the | or 'bit-wise-or' operator (see https://en.wikipedia.org/wiki/Bitwise_operations_in_C), these are used to set bits in a byte/word/long.

Most registers have 8, 16 or 32 bits and with the | operator you can easily set multiple bits at the same time. Each value (e.g. INA3221_CONFIG_ENABLE_CHAN) has a value that is a power of 2 (mostly defined in the form 1 << ..something.. you can add them (with +) or | them together, but | is more clear what the purpose is and repeatedly calling | the same bit twice is not a problem (it still gives the same result).

2
  • 1
    I had to learn about bit -wise operations and manipulations and it becomes understandable :) Thanks for pointing me in the right direction
    – Zaffresky
    Feb 8 at 16:05
  • You're welcome, good luck with your project Feb 8 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.