Set and check correct CS5530 registers

Hi there!

I am trying to play around with a CS5530 ADC board that I have made; I based my code on the library by Yasir Shahzad CS5530, I had to slightly modify the SPI configuration as I am using a 4809 with MegaCoreX which uses its own SPI library.

CS is not controlled by the library as it is tied to ground.

I am happy with how the system operates although I would like to fine-tune the settings to ensure that all the features of the chip are used correctly as well as using the right configurations.

I find myself in a bit of a struggle as I am very bad when it comes to registers and shifting therefore I would like to reach out to see if someone can help me.

By doing some tests I understood that calling CS5530.writeRegister(CMD_CONFIG_WRITE, *insert command*) multiple times does not work so I have tried to set all the commands of what I assume is the same register in the same function, seems to be working but I am not entirely sure it is the correct way.

Configuration register information is at p. 19 of the datasheet.

What I intend to do to start with is to set the configuration register:

  • Continuous conversion

  • Unipolar Signal

  • VRS (Voltage Reference Select) to 1 with the option to alternate between 0 and 1 for testing purposes.

  • FRS (Filter Rate Select) to 1 with the option to alternate between 0 and 1 for testing purposes.

  • Set Word Rate (Speed).

  • Read the register back to make sure that all the settings have the right configurations, I have tried to use the readRegister function but I am getting a value that I am not able to comprehend.

My code is as follows (library attached):

#include "SPI.h"
#include "5530_ADC.h"

CS5530 CS5530(CS5530_DEFAULT_SPI_FREQUENCY);

void setup() {
  CS5530.begin();
  CS5530.reset();
  CS5530.writeRegister(CMD_CONFIG_WRITE, WORD_RATE_6P25SPS | CS5530_UNIPOLAR | REG_CONFIG_VRS | REG_CONFIG_FRS);
  CS5530.write8(CMD_CONVERSION_CONTINUOUS);
}

void loop() {
CS5530.getCount();
}

How wrong is to do as follows:

CS5530.writeRegister(CMD_CONFIG_WRITE, 0x00000000001010000001000001000000);

Setting each bit to 0 or 1 according to what setting I want?

Any help is greatly appreciated, anything you see that looks or is wrong please point it out so that I can fix it!

Library:
5530_ADC.cpp (2.0 KB)
5530_ADC.h (2.2 KB)

Are you sure you want 0x and not 0b ? I think you want 0b where you have 0x.

Thanks for the input, makes sense as it is not hex, I'll give it a go now! To be honest I am just trying things, I do not understand much about registers! I need to make a function to read the registers to see what it is in there because like this it's just guessing :sob:

I suspect it does work, if you do it correctly. Post some code that does not work and hopefully we can explain why the way you did it was incorrect and how to do it correctly.

You want "0b", not "0x" as already pointed out. But also, while this could work in practice, it will be very error prone and unintelligible to anyone else reading your code, even yourself in a few weeks time.

This is a much better way to do it because it's less error prone and much more meaningful when you or others read the code later.

Why do you want to do it another way?

I also think maybe you have the bits in backwards order.
Bit number 0 should be on the right.
Bit number 31 should be on the left.

1 Like

You want "0b", not "0x" as already pointed out. But also, while this could work in practice, it will be very error prone and unintelligible to anyone else reading your code, even yourself in a few weeks time.

I know it is a very bad practice, but I wanted to physically turn the bits on or off to be sure that the register was configured the way I wanted as without a feedback is not very clear from the ADC count alone.

This is a much better way to do it because it's less error prone and much more meaningful when you or others read the code later.

Why do you want to do it another way?

I was not sure if I had done it correctly, if that is the correct way then I will leave it, I only came to that solution by searching around but it is the first time that I have tried such an approach.

How could I double-check that the bits are selected correctly?

Yes, I have noticed that I had the LSB on the left it is also very clear on the datasheet that the MSB is on the left, I will amend it and retry.

You shouldn't need to, but you could read back the contents of the register and check that the correct bits are set:

uint32_t reg = CS5530.writeRegister(CMD_CONFIG_READ);
Serial.println(reg, BIN);

NOTE: Serial.println() will suppress leading zeros when it prints the register contents in binary. The right-most digit printed will be bit 0.

Something you should check is whether you are writing the appropriate bits to the appropriate registers. The command above is writing to the configuration register. I think it probably is correct to write REG_CONFIG_VRS and REG_CONFIG_FRS to that register, but is it correct to write WORD_RATE_6P25SPS or CS5530_UNIPOLAR to that register? Maybe they should be written to a different register? I'm not sure. If you have been reading the sensor datasheet, you should know better than I do, but I think maybe you should double-check you are writing the appropriate bits to the appropriate registers.

Looking at the on-chip registers from the datasheet:

The 32-bit configuration register takes all the configurations.

While the 8-bit command register takes commands such as continuous conversion or single.
Screenshot 2024-05-05 at 20-17-14 CS5530_F3.pdf - CS5530_F3.pdf

I believe the register is addressed correctly although it was a matter of writing the right bits which seems like we did. I will try to read back from the configuration register as you suggested to see if I get a 32-bit binary value to verify.

I have made different configurations so I can now set the bit on or off accordingly.

#define INPUT_SHORT_ON 1UL << 27 //Input Short On
#define INPUT_SHORT_OFF 0UL << 27 //Input Short Off
#define VRS_2V5 1UL << 25 //Voltage Reference Select 2.5V
#define VRS_5V 0UL << 25 //Voltage Reference Select 5V
#define FRS_ON 1UL << 19 //Filter Rate Select On
#define FRS_OFF 0UL << 19 //Filter Rate Select Off

I believe this is how is done.

No, I don't think that's right. Zero is zero, no matter how many bits you shift it by!

If you want to set a bit off without disturbing the other bits, you could do it like this:

uint32_t reg = CS5530.readRegister(CMD_CONFIG_READ);
reg &= ~REG_CONFIG_VRS;
CS5530.writeRegister(CMD_CONFIG_READ, reg);

(Although it would be more efficient to keep the register value in a global variable so you don't need to read it each time.)

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.