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 15, 2023

    Hi,@Rene Lenerve​ 

    Note that for the MAX6675, its temperature data is represented in 12-bit twos complement, with the top 4 bits being identification bits and discarded. Therefore, when reading the MAX6675 temperature data using this code, proper bit crunching and processing according to the MAX6675 specifications is required to obtain the correct temperature value.

    • rxdata[0]
    • : The most significant byte (MSB) is saved.
    • rxdata[1]
    • : The least significant byte (LSB) is saved.

    So I changed my code to get low 12 bits of data, Is the following code correct?

    uint16_t MAX6675_ReadByte(void)
    {
     unsigned char rxdata[2]; // 警告:读�?�2个字节,因为 hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
     uint16_t readtemperature;
     HAL_SPI_Receive(&hspi2, rxdata, 2, 1000); // 接收两个字节的数�?�
     readtemperature = ((uint16_t)rxdata[1] << 8) | rxdata[0]; // 将两个字节进行组�?�,注�?顺�?
     readtemperature >>= 3; // 丢弃最高的 4 �?标识�?,�?留低12�?数�?�
     return readtemperature;
    }

    ST Employee
    May 15, 2023

    Hi @帅 罗​​​​,

    If the MSB is in rxdata[0], so this the High part of your uint16_t, this imply:

    readtemperature = ((uint16_t)rxdata[0] << 8) | rxdata[1];

    If you don't know how to debug your code and get the value of rxdata, you can use a console and display with a printf in this console the value of any data, there is plenty examples on how to do that and you can use the UARTConsolePrint function to do it available in the NDEF_URI application, it uses the UART available with ST-Link (No need to connected other wires).

    Hope this will help you.

    Kind Regards.

    SchuylerAuthor
    Explorer
    May 15, 2023

    Hi,@Rene Lenerve 

    I'm going to write the code into MX_NFC7_NDEF_URI_Process,but the mobile phone NFC reading did not respond,I don't know what caused it. Please help me check why NFC can't read ST25?

    /**
     ******************************************************************************
     * File Name : app_nfc7.c
     * Description : This file provides code for the configuration
     * of the STMicroelectronics.X-CUBE-NFC7.1.0.0 instances.
     ******************************************************************************
     * @attention
     *
     * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
     * All rights reserved.</center></h2>
     *
     * This software component is licensed by ST under SLA0094 license,
     * the "License"; You may not use this file except in compliance with the
     * License. You may obtain a copy of the License at:
     * https://www.st.com/SLA0094
     *
     ******************************************************************************
     */
     
    #ifdef __cplusplus
     extern "C" {
    #endif
     
    /* Includes ------------------------------------------------------------------*/
    #include "app_nfc7.h"
    #include "main.h"
     
    /* Includes ------------------------------------------------------------------*/
    #include "common.h"
    #include "tagtype5_wrapper.h"
    #include "lib_NDEF_URI.h"
    #include<stdio.h>
    #include"MAX6675.h"
    float K_Temperature ; //K型热电�?� 温度值
     char temperaturebuffer[30] = { 0xD1, 0x01, 0x1B, 0x54, 0x02, 0x65, 0x6E };
     
    /** @defgroup ST25_Nucleo
     * @{
     */
     
    /** @defgroup Main
     * @{
     */
     
    /* Private typedef -----------------------------------------------------------*/
    /* Private define ------------------------------------------------------------*/
    /* Private macro -------------------------------------------------------------*/
    /* Private variables ---------------------------------------------------------*/
    /* Global variables ----------------------------------------------------------*/
    sURI_Info URI;
     
    extern sCCFileInfo CCFileStruct;
     
    /* Private functions ---------------------------------------------------------*/
    void MX_NFC7_NDEF_URI_Init(void);
    void MX_NFC7_NDEF_URI_Process(void);
     
    void MX_NFC7_Init(void)
    {
     /* USER CODE BEGIN SV */
     
     /* USER CODE END SV */
     
     /* USER CODE BEGIN NFC7_Library_Init_PreTreatment */
     
     /* USER CODE END NFC7_Library_Init_PreTreatment */
     
     /* Initialize the peripherals and the NFC7 components */
     MX_NFC7_NDEF_URI_Init();
     
     /* USER CODE BEGIN SV */
     
     /* USER CODE END SV */
     
     /* USER CODE BEGIN NFC7_Library_Init_PostTreatment */
     
     /* USER CODE END NFC7_Library_Init_PostTreatment */
    }
    /*
     * LM background task
     */
    void MX_NFC7_Process(void)
    {
     /* USER CODE BEGIN NFC7_Library_Process */
     
     /* USER CODE END NFC7_Library_Process */
     
     MX_NFC7_NDEF_URI_Process();
     
    }
     
    void MX_NFC7_NDEF_URI_Init(void)
    {
    	 /******************************************************************************/
     /* Configuration of X-NUCLEO-NFC02A1 */
     /******************************************************************************/
     /* Init of the Leds on X-NUCLEO-NFC07A1 board */
     NFC07A1_LED_Init(GREEN_LED );
     NFC07A1_LED_Init(BLUE_LED );
     NFC07A1_LED_Init(YELLOW_LED );
     NFC07A1_LED_On( GREEN_LED );
     HAL_Delay( 300 );
     NFC07A1_LED_On( BLUE_LED );
     HAL_Delay( 300 );
     NFC07A1_LED_On( YELLOW_LED );
     HAL_Delay( 300 );
     
     /* Init ST25DVXXKC driver */
     while( NFC07A1_NFCTAG_Init(NFC07A1_NFCTAG_INSTANCE) != NFCTAG_OK );
     
     /* Reset Mailbox enable to allow write to EEPROM */
     NFC07A1_NFCTAG_ResetMBEN_Dyn(NFC07A1_NFCTAG_INSTANCE);
     
     NfcTag_SelectProtocol(NFCTAG_TYPE5);
     
     /* Check if no NDEF detected, init mem in Tag Type 5 */
     if( NfcType5_NDEFDetection( ) != NDEF_OK )
     {
     CCFileStruct.MagicNumber = NFCT5_MAGICNUMBER_E1_CCFILE;
     CCFileStruct.Version = NFCT5_VERSION_V1_0;
     CCFileStruct.MemorySize = ( ST25DVXXKC_MAX_SIZE / 8 ) & 0xFF;
     CCFileStruct.TT5Tag = 0x05;
     /* Init of the Type Tag 5 component (M24LR) */
     while( NfcType5_TT5Init( ) != NFCTAG_OK );
     }
     
     /* Init done */
     NFC07A1_LED_Off( GREEN_LED );
     HAL_Delay( 300 );
     NFC07A1_LED_Off( BLUE_LED );
     HAL_Delay( 300 );
     NFC07A1_LED_Off( YELLOW_LED );
     HAL_Delay( 300 );
     
     
     
    }
     
    /**
     * @brief Process of the NDEF_URI application
     * @retval None
     */
    void MX_NFC7_NDEF_URI_Process(void)
    {
    	 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 );
    }
     
    #ifdef __cplusplus
    }
    #endif
     
    /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

    SchuylerAuthor
    Explorer
    May 17, 2023

    Hi,@Rene Lenerve​ 

    If my data collection is normal, will the following conversion methods affect the accuracy of data results?

    float K_Temperature ; //K型热电�?� 温度值
    char temperaturebuffer[30] = { 0xD1, 0x01, 0x1B, 0x54, 0x02, 0x65, 0x6E };
     
    void MX_NFC7_NDEF_URI_Process(void)
    {
    	 /* Write NDEF to EEPROM */
    	 HAL_Delay(1000);
    	 K_Temperature=MAX6675_ReadTemperature(); //读�?�热电�?�温度值
    	 sprintf(&temperaturebuffer[7], "Temperature = %.2f C", K_Temperature);
    	 NfcTag_WriteNDEF(sizeof(temperaturebuffer) + 1, (uint8_t *)temperaturebuffer);
    }

    ST Employee
    May 17, 2023

    Hi @帅 罗​​​​​,

    Does the raw value read from the sensor is the expected value related to the temperature applied to the sensor?

    Kind Regards.

    SchuylerAuthor
    Explorer
    May 17, 2023

    Hi,@Rene Lenerve​ 

    That's not true,My sensor measures the air temperature, but the phone is reading several hundred degrees,so I think there is an error in data conversion or NFC reading the conversion string affects the data

    ST Employee
    May 17, 2023

    Hi @帅 罗​,

    Can you give us this value read from the sensor?

    Kind Regards.

    SchuylerAuthor
    Explorer
    May 17, 2023

    Hi,@Rene Lenerve​ 

    The data is read by the phone app and fluctuates in a three-digit range, with the correct number in the 20s.
    _legacyfs_online_stmicro_images_0693W00000bkGAcQAM.png
    _legacyfs_online_stmicro_images_0693W00000bkGC8QAM.png

    ST Employee
    May 17, 2023

    Hi @帅罗.1 (Community Member)​,

    Can you give us the RAW value read from the sensor?

    Kind Regards.

    SchuylerAuthor
    Explorer
    May 17, 2023

    Hi,@Rene Lenerve​ 

    The value read from the sensor is about 25 degrees