Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting unexpected value when reading SCAN register #15

Open
riccardobastoni97 opened this issue Jul 9, 2024 · 5 comments
Open

Getting unexpected value when reading SCAN register #15

riccardobastoni97 opened this issue Jul 9, 2024 · 5 comments

Comments

@riccardobastoni97
Copy link

Hi everyone,
I'm trying to read what is written in SCAN register after doing the reset and the init. Then I'm sending the value to a Python program that displays it. I'm getting the right exadecimal value for the first 3 bytes I read, but the last byte I receive is a weird 0x17 and I don't understand why. This happens just if I read the SCAN register and the timer register... other registers have the right values.

uint8_t cmd [5] = {0,0,0,0,0};
cmd[0] = MCP3562_SCAN_SREAD;
uint8_t resp [5] = {0,0,0,0,0};
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, cmd, resp, 4, MCP3562_HAL_TIMEOUT);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
HAL_UART_Transmit(&huart2, resp, 4, HAL_MAX_DELAY);

Thanks for your help!

@paul-thelu
Copy link
Contributor

Hi,
I personally don't use the SCAN feature nor the *MCP3561_PrintRegisters(SPI_HandleTypeDef hspi) function to verify the registers content.

However:

  1. What value have you have defined for MCP3561_USERCONF_SCAN_REG?
  2. The cmd and resp buffers are 5 bytes long, but in both the HAL_SPI_TransmitReceive(&hspi1, cmd, resp, 4, MCP3562_HAL_TIMEOUT); and HAL_UART_Transmit(&huart2, resp, 4, HAL_MAX_DELAY); 4 is written instead of 5. (might not solve the problem though)
  3. When you say "the last byte is 0x17", do we speak of of the value contained in resp[3]? or resp[4]?
  4. Does your ADC project run as expected otherwise?

@riccardobastoni97
Copy link
Author

  1. #define MCP3562_USERCONF_SCAN_REG (MCP3562_SCAN_DLY_NONE | MCP3562_SCAN_CH0 | MCP3562_SCAN_CH1 | MCP3562_SCAN_CH2) so, it should be 0b00000111 -> 0x07 in hexadecimal

  2. You're right. I edited like this:

     				uint8_t cmd [4] = {0,0,0,0};
     				cmd[0] = MCP3562_SCAN_SREAD;
     				uint8_t resp [4] = {0,0,0,0};
     				HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
     				HAL_SPI_TransmitReceive(&hspi1, cmd, resp, 4, MCP3562_HAL_TIMEOUT);
     				HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
     				HAL_UART_Transmit(&huart2, resp, 4, HAL_MAX_DELAY);
    
  3. After editing, I display the following result: b'\x17\x00\x00\x07'
    So the unexpected byte is the resp[3]

  4. No, I don't get any interrupt and that's why I was trying to read the registers content... maybe there is sothing wrong there...

@paul-thelu
Copy link
Contributor

paul-thelu commented Jul 10, 2024

  1. Hmm are you sure it's resp[3]? What is written in your "printf" that displays b'\x17\x00\x00\x07'? I think it's resp[0] as we read MSB first, could you confirm?

  2. Did you check your IRQ register to make sure IRQ output is properly set?

  3. Maybe the best for now is to provide the content of your mcp356x_conf.h, so I could check if you set everything correctly.

@riccardobastoni97
Copy link
Author

  1. Yes, I confirm. 0x17 could be the STATUS byte maybe. If that's the case 0x17 is 00010111 so:

    • STAT[0] = 1 -> POR has not occurred since the last reading (default)
    • STAT[1] = 1 -> CRC error has not occurred for the Configuration registers (default)
    • STAT[2] = 1 -> ADCDATA has not been updated since last reading or last reset (default)
  2. Yes, it seems correct (you can see it in point 4.). On CubeMX I have configured a the IRQ pin as GPIO_EXTI with GPIO Mode: External Interrupt Mode with Falling edge trigger detection

      #ifndef INC_MCP3562_CONF_H_
      #define INC_MCP3562_CONF_H_


      // 3-channel, auto conversion in SCAN mode
      // 32 bit data format with CHannel ID
      #define MCP3562_USERCONF_REG0 (MCP3562_CONFIG0_VREF_SEL_EXT | MCP3562_CONFIG0_CLK_SEL_INT | MCP3562_CONFIG0_ADC_MODE_CONV | MCP3562_CONFIG0_CS_SEL_NONE)  // CONFIG0_REGISTER -> 00000011 (0x03)

      #define MCP3562_USERCONF_REG1 (MCP3562_CONFIG1_OSR_64 | MCP3562_CONFIG1_AMCLK_DIV0)  // CONFIG1_REGISTER -> 00000100 (0x04)

      #define MCP3562_USERCONF_REG2 (MCP3562_CONFIG2_BOOST_x1 | MCP3562_CONFIG2_GAIN_x1 | MCP3562_CONFIG2_AZ_MUX_OFF | MCP3562_CONFIG2_AZ_REF_OFF)  // CONFIG2_REGISTER -> 10001000 (0x8b -> in mcp3562.c poi si vanno a mettere a 1 il bit 1 e 0)

      #define MCP3562_USERCONF_REG3 (MCP3562_CONFIG3_CONV_MODE_CONTINUOUS | MCP3562_CONFIG3_DATA_FORMAT_32BIT_CHID_SGN | MCP3562_CONFIG3_CRCCOM_OFF | MCP3562_CONFIG3_GAINCAL_OFF | MCP3562_CONFIG3_OFFCAL_OFF)  // CONFIG3_REGISTER -> 11110000 (0xf0)

      #define MCP3562_USERCONF_IRQ_REG (MCP3562_IRQ_MODE_IRQ_HIGH | MCP3562_IRQ_FASTCMD_ON | MCP3562_IRQ_STP_OFF)  // IRQ_REGISTER -> 00000110

      #define MCP3562_USERCONF_SCAN_REG (MCP3562_SCAN_DLY_NONE | MCP3562_SCAN_CH0 | MCP3562_SCAN_CH1 | MCP3562_SCAN_CH2)  // SCAN_REGISTER -> 000000000000000000000111  (0x07)

      #define MCP3562_USERCONF_TIMER_VAL (138)  // (0x8a)

      #endif /* INC_MCP3562_CONF_H_ */

@mozinitu
Copy link
Contributor

mozinitu commented Aug 1, 2024

Hey, 0x17 is status.

In the Code:

int32_t * MCP3561_ReadADCData_32Bit_Scan(SPI_HandleTypeDef *hspi){
uint8_t val[5] = {0,0,0,0,0};
uint8_t cmd[5] = {0,0,0,0,0};
cmd[0] = MCP3561_SREAD_DATA_COMMAND;
HAL_GPIO_WritePin(MCP3561_CHIP_SELECT_GPIO_Port, MCP3561_CHIP_SELECT_GPIO_Pin, 0);
HAL_SPI_TransmitReceive(hspi, cmd, val, 5, MCP3561_HAL_TIMEOUT);
HAL_GPIO_WritePin(MCP3561_CHIP_SELECT_GPIO_Port, MCP3561_CHIP_SELECT_GPIO_Pin, 1);
int32_t value[3] = { val[0] , <---------------- STATUS
( (val[1] & 0xF0) >> 4 ) , <---------------- Channel
( (val[1] & 0x01) << 31) | (val[2] << 16) | (val[3] << 8) | val[4]}; <---------------- Value
int32_t * ptr = &value[0];
return ptr;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants