Skip to main content
Explorer
April 14, 2023
Solved

NDEF_WriteText function read a temperature signal

  • April 14, 2023
  • 39 replies
  • 4627 views

I want to read the sensor signal with NDEF_WriteText function,How do you read a temperature signal instead of a temperature text?

NDEF_WriteText funtion is printf funtion,so what should I do to read signal?

    This topic has been closed for replies.
    Best answer by Schuyler

    Hi,@Rene Lenerve​ 

    I found that the problem is caused by incorrect SPI initialization settings, which causes the data to be garbled.

    39 replies

    SchuylerAuthor
    Explorer
    May 11, 2023

    Hi,@Rene Lenerve​ 

    OK,I get the Temperature ,and I need real-time temperature data,so how can I make the temperature data change in real time.

    ST Employee
    May 11, 2023

    Hi @帅 罗​ ​,

    This is an issue with the MAX6675, which is not an ST product. Please refer to the DataSheet of this component to see how to get the correct value, or ask on MAXIM for support on their product. And check also connection of the component to the board.

    Kind Regards.

    SchuylerAuthor
    Explorer
    May 12, 2023

    Hi,@Rene Lenerve​ 

    I have checked my code and the data I read is 1023.75. According to the code, the output data is because the sensor is not detected, so I think there may be a hardware connection problem. Please help me check whether STM32L476 should be connected in this way?

    #include"MAX6675.h"
    SPI_HandleTypeDef hspi2;
    #define MAX6675_CS_Pin GPIO_PIN_12
    #define MAX6675_CS_GPIO_Port GPIOB
     float K_Temperature ; //K型热电�?� 温度值
     char temperaturebuffer[30] = { 0xD1, 0x01, 0x1B, 0x54, 0x02, 0x65, 0x6E };
    // ------------------- Functions ----------------
    uint8_t MAX6675_ReadWriteByte(uint8_t txData)
    {
     unsigned char txdata,rxdata;
     txdata = txData;
     HAL_SPI_TransmitReceive(&hspi2,&txdata,&rxdata,2,1000);
     return rxdata;
    }
    //MAX6675 片选控制
    void MAX6675_CS(unsigned char choose)
    {
     if(choose == 1)
     {
     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
     }
     else
     {
     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
     }
    }
     
    /**
     * @brief max6675模�?�读�?�测得的原始数�?�
     * @param None
     * @retval 温度的原始数�?�
     */
    uint16_t MAX6675_ReadRawValue(void)
    {
     uint16_t tmp=0;
     MAX6675_CS(0);
     tmp = MAX6675_ReadWriteByte(0XFF); //read MSB
     tmp <<= 8;
     tmp |= MAX6675_ReadWriteByte(0XFF); //read LSB
     MAX6675_CS(1);
     
     if (tmp & 4)
     {
     // thermocouple open
     tmp = 4095; //未检测到热电�?�
     }
     else
     {
     tmp = tmp >> 3;
     tmp &= 0x0FFF; // �?留低 12 �?数�?�
     }
     return tmp;
    }
     
    /**
     * @brief max6675模�?�读�?�温度
     * @param None
     * @retval 温度值(�?��?:℃)
     */
    float MAX6675_ReadTemperature(void)
    {
     return (MAX6675_ReadRawValue() * 1024.0 / 4096);
    }
     
     /* write text message to EEPROM */
     HAL_Delay(500);
     K_Temperature=MAX6675_ReadTemperature(); //读�?�热电�?�温度值
     sprintf(&temperaturebuffer[7], "Temperature = %.2f", K_Temperature);
     NfcTag_WriteNDEF(sizeof(temperaturebuffer) + 1, (uint8_t *)temperaturebuffer);
     /* Set the LED3 on to indicate Programing done */
     NFC07A1_LED_On( YELLOW_LED );


    _legacyfs_online_stmicro_images_0693W00000bjwsJQAQ.png
    _legacyfs_online_stmicro_images_0693W00000bjwsEQAQ.png
    _legacyfs_online_stmicro_images_0693W00000bjwrpQAA.png

    ST Employee
    May 12, 2023

    Hi @帅 罗​,

    Hardware connection seems to be ok.

    You said that read data is 1023.75, so that means you can read data from the sensor. If this is the raw data this imply that the temperature read is around 256? (you should read this value in the NDEF Text record and not 0).

    Only Miso is connected and needed to the sensor so you don't need to use the transmitreceive function.

    You can change SPI configuration to this one (I don't have a MAX6675 sensor to test this code so consider it as example and adapt it if any error):

    /**
     * @brief SPI2 Initialization Function
     * @param None
     * @retval None
     */
    void MX_SPI2_Init(void)
    {
     
     /* USER CODE BEGIN SPI2_Init 0 */
     
     /* USER CODE END SPI2_Init 0 */
     
     /* USER CODE BEGIN SPI2_Init 1 */
     
     /* USER CODE END SPI2_Init 1 */
     /* SPI2 parameter configuration*/
     hspi2.Instance = SPI2;
     hspi2.Init.Mode = SPI_MODE_MASTER;
     hspi2.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
     hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
     hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
     hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
     hspi2.Init.NSS = SPI_NSS_SOFT;
     hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
     hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
     hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
     hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
     hspi2.Init.CRCPolynomial = 10;
     if (HAL_SPI_Init(&hspi2) != HAL_OK)
     {
     Error_Handler();
     }
    }
     
    /**
    * @brief SPI MSP Initialization
    * This function configures the hardware resources used in this example
    * @param hspi: SPI handle pointer
    * @retval None
    */
    void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
    {
     GPIO_InitTypeDef GPIO_InitStruct = {0};
     if(spiHandle->Instance==SPI2)
     {
     /* USER CODE BEGIN SPI2_MspInit 0 */
     
     /* USER CODE END SPI2_MspInit 0 */
     /* Peripheral clock enable */
     __HAL_RCC_SPI2_CLK_ENABLE();
     
     __HAL_RCC_GPIOB_CLK_ENABLE();
     /**SPI2 GPIO Configuration
     PB13 ------> SPI2_SCK
     PB14 ------> SPI2_MISO*/
     GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
     GPIO_InitStruct.Pull=GPIO_NOPULL;
     GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
     /* USER CODE BEGIN SPI2_MspInit 1 */
     
     /* USER CODE END SPI2_MspInit 1 */
     }
     
    }
     
    uint16_t MAX6675_ReadByte(void)
    {
     unsigned char rxdata[2]; //Warning: 2 bytes are read because hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
     uint16_t readtemperature;
     HAL_SPI_Receive(&hspi2,rxdata,1,1000);
     readtemperature = rxdata[0];
     readtemperature = (readtemperature << 8) | rxdata[1]
     return readtemperature ;
    }
     
    /**
     * @brief max6675模�?�读�?�测得的原始数�?�
     * @param None
     * @retval 温度的原始数�?�
     */
    uint16_t MAX6675_ReadRawValue(void)
    {
     uint16_t tmp=0;
     MAX6675_CS(0);
     tmp = MAX6675_ReadByte();
     MAX6675_CS(1);
     
     if (tmp & 4)
     {
     // thermocouple open
     tmp = 4095; //未检测到热电�?�
     }
     else
     {
     tmp = tmp >> 3;
     tmp &= 0x0FFF; // �?留低 12 �?数�?�
     }
     return tmp;
    }

    The SPI speed of the sensor seems to be 4.3MHz max, have a look on your clock configuration and check that SPI_BAUDRATEPRESCALER_128 is ok to get the SPI speed needed.

    Hope this will help you.

    Kind Regards.

    SchuylerAuthor
    Explorer
    May 13, 2023

    Hi,@Rene Lenerve​ 

    I have noticed a change in the MAX6675_ReadWriteByte function, is it the way the original data is measured? And the CS definition is the following code?

     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
     /*引脚�?置 */
     GPIO_InitStruct.Pin = GPIO_PIN_12;
     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    ST Employee
    May 15, 2023

    Hi @帅 罗​,

    Yes, I changed the MAX6675_ReadWriteByte function in my example code, because your example provided was not clear on the sizes of the data in my opinion. SPI is configured with SPI_DATASIZE_16BIT and data sent by the MAX6675 are 16 bits (this is fine). Your function MAX6675_ReadWriteByte was getting 16bit (2bytes) in an unsigned char which is only 1 byte (this seems like a problem to me). That's why I proposed this update on the read function, but as I said previously I don't have a MAX6675 to test it, so it may need to be updated for it to work properly.

    The Chip Select pin configuration is aligned with your hardware connection, so it seems fine.

    Hope this will help you.

    Kind Regards.

    SchuylerAuthor
    Explorer
    May 15, 2023

    Hi,@Rene Lenerve​ 

    I changed my code as you instructed,but I still failed. Here are my codes,I think the temperature is not measured and the 12-digit data is 1, and the current data should be caused by incorrect reading of digits. Please help me to check it,I don't know why  I got 969.

    static void MX_SPI2_Init(void)
    {
     /* SPI2 parameter configuration*/
     hspi2.Instance = SPI2;
     hspi2.Init.Mode = SPI_MODE_MASTER;
     hspi2.Init.Direction = SPI_DIRECTION_2LINES;
     hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
     hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
     hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
     hspi2.Init.NSS = SPI_NSS_SOFT;
     hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
     hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
     hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
     hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
     hspi2.Init.CRCPolynomial = 10;
     if (HAL_SPI_Init(&hspi2) != HAL_OK)
     {
     Error_Handler();
     }
    }
     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
     /*引脚�?置 */
     GPIO_InitStruct.Pin = GPIO_PIN_12;
     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    #include"MAX6675.h"
    SPI_HandleTypeDef hspi2;
    #define MAX6675_CS_Pin GPIO_PIN_12
    #define MAX6675_CS_GPIO_Port GPIOB
    // ------------------- Functions ----------------
    uint16_t MAX6675_ReadByte(void)
    {
     unsigned char rxdata[2]; //Warning: 2 bytes are read because hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
     uint16_t readtemperature;
     HAL_SPI_Receive(&hspi2,rxdata,1,1000);
     readtemperature = rxdata[0];
     readtemperature = (readtemperature << 8) | rxdata[1];
     return readtemperature ;
    }
    //MAX6675 片选控制
    void MAX6675_CS(unsigned char choose)
    {
     if(choose == 1)
     {
     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
     }
     else
     {
     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
     }
    }
    /**
     * @brief max6675模�?�读�?�测得的原始数�?�
     * @param None
     * @retval 温度的原始数�?�
     */
    uint16_t MAX6675_ReadRawValue(void)
    {
     uint16_t tmp=0;
     MAX6675_CS(0);
     tmp = MAX6675_ReadByte();
     MAX6675_CS(1);
     
     if (tmp & 4)
     {
     // thermocouple open
     tmp = 4095; //未检测到热电�?�
     
     }
     else
     {
     tmp = tmp >> 3;
     tmp &= 0x0FFF; // �?留低 12 �?数�?�
     }
     return tmp;
    }
    /**
     * @brief max6675模�?�读�?�温度
     * @param None
     * @retval 温度值(�?��?:℃)
     */
    float MAX6675_ReadTemperature(void)
    {
     return (MAX6675_ReadRawValue() * 1024.0 / 4096);
    }


    _legacyfs_online_stmicro_images_0693W00000bk5C7QAI.png

    ST Employee
    May 15, 2023

    Hi @帅 罗​​,

    Could you give the raw value returned by the SPI, I mean the value stored in rxdata when calling MAX6675_ReadByte.

    Kind Regards.

    SchuylerAuthor
    Explorer
    May 15, 2023

    Hi,@Rene Lenerve​ 

    I don't​ know the raw value returned by the SPI, I want get the temperature by max6675, this code can get 969 and not accurate temperature data.

    ST Employee
    May 15, 2023

    Hi @帅 罗​​​,

    It will not be possible to help you more if you don't provide enough information. Now you are able to read data with SPI and write it to the ST25DV tag (and able to read it on the smartphone). Your issue is now around getting correct value from the MAX6675 it is more an issue on this component and you will get support for MAXIMs components on the MAXIM forum.

    Kind Regards.