2023-04-13 8:39 PM
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?
Solved! Go to Solution.
2023-05-10 6:34 PM
@Rene Lenerve
OK,I have declared the variable K_Temperature,and and I'm using float.
#include "main.h"
#include"stm32l4xx_hal_spi.h"
#define MAX6675_CS_Pin GPIO_PIN_12
#define MAX6675_CS_GPIO_Port GPIOB
SPI_HandleTypeDef hspi2;
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN Define */
/* USER CODE END Define */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN Macro */
/* USER CODE END Macro */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* External functions --------------------------------------------------------*/
/* USER CODE BEGIN ExternalFunctions */
/* USER CODE END ExternalFunctions */
/* USER CODE BEGIN 0 */
/**
* @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;
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();
}
}
/* USER CODE END 0 */
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
/* System interrupt init*/
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
/**
* @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
PB15 ------> SPI2_MOSI*/
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15|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 */
}
}
/**
* @brief SPI MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hspi: SPI handle pointer
* @retval None
*/
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{
if(spiHandle->Instance==SPI2)
{
/* USER CODE BEGIN SPI2_MspDeInit 0 */
/* USER CODE END SPI2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_SPI2_CLK_DISABLE();
/**SPI2 GPIO Configuration
PB13 ------> SPI2_SCK
PB14 ------> SPI2_MISO
PB15 ------> SPI2_MOSI
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
/* USER CODE BEGIN SPI2_MspDeInit 1 */
/* USER CODE END SPI2_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
//MAX6675 片选引脚�?始化
void GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOB, MAX6675_CS_Pin, GPIO_PIN_SET);
/*引脚�?置 */
GPIO_InitStruct.Pin = MAX6675_CS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/**
* @brief max66675模�?��?始化
* @param None
* @retval None
*/
void MAX6675_Init(void)
{
GPIO_Init(); //片选引脚�?始化
MX_SPI2_Init(); //spi总线�?始化
}
/**
* @brief max6675模�?�读写一个字节的数�?�
* @param txData:�?�?��?的数�?�
* @retval 接收到的数�?�
*/
uint8_t MAX6675_ReadWriteByte(uint8_t txData)
{
unsigned char txdata,rxdata;
txdata = txData;
HAL_SPI_TransmitReceive(&hspi2,&txdata,&rxdata,1,1000);
return rxdata;
}
/**
* @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=tmp&0x0FFF; //12bit
return tmp;
}
/**
* @brief max6675模�?�读�?�温度
* @param None
* @retval 温度值(�?��?:℃)
*/
float MAX6675_ReadTemperature(void)
{
return (MAX6675_ReadRawValue() * 1024.0 / 4096);
}
//MAX6675 片选控制
void MAX6675_CS(unsigned char choose)
{
if(choose == 1)
{
HAL_GPIO_WritePin(GPIOB, MAX6675_CS_Pin, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOB, MAX6675_CS_Pin, GPIO_PIN_RESET);
}
}
/* USER CODE END 1 */
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 );
MAX6675_Init(); //K型热电�?��?始化
/* write text message to EEPROM */
HAL_Delay(500);
K_Temperature= MAX6675_ReadTemperature(); //读�?�热电�?�温度值
sprintf(&temperaturebuffer[7], "Temperature = %d", K_Temperature);
NfcTag_WriteNDEF(sizeof(temperaturebuffer) + 1, (uint8_t *)temperaturebuffer);
/* Set the LED3 on to indicate Programing done */
NFC07A1_LED_On( YELLOW_LED );
}
2023-05-10 11:21 PM
Hi @帅 罗 ,
in the code above i think you didn't define temperaturebuffer with the NDEF Text header, maybe it was lost in the copy paste:
char temperaturebuffer[30] = { 0xD1, 0x01, 0x1B, 0x54, 0x02, 0x65, 0x6E };
If you still have Temperature = 0, you need to enter a debug session and see if the return raw value of the sensor is correct or not to determine where is the issue.
Hope this will help you.
Kind Regards.
2023-05-11 2:30 AM
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.
2023-05-11 5:01 AM
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.
2023-05-12 2:08 AM - edited 2023-11-20 8:15 AM
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 );
2023-05-12 3:33 AM
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.
2023-05-13 1:47 AM
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);
2023-05-14 11:42 PM
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.
2023-05-15 3:36 AM - edited 2023-11-20 8:16 AM
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);
}
2023-05-15 3:55 AM
Hi @帅 罗,
Could you give the raw value returned by the SPI, I mean the value stored in rxdata when calling MAX6675_ReadByte.
Kind Regards.