你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

max6675测温不准!以下关于max6675的设置有问题吗?

[复制链接]
18523353044 提问时间:2023-5-11 17:46 / 未解决

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 */ static 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_8BIT; hspi2.Init.CLKPolarity = SPI_POLARITY_LOW; hspi2.Init.CLKPhase = SPI_PHASE_1EDGE; hspi2.Init.NSS = SPI_NSS_SOFT; hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi2.Init.TIMode = SPI_TIMODE_DISABLE; hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi2.Init.CRCPolynomial = 7; hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; 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_14|GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 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); } }

收藏 评论0 发布时间:2023-5-11 17:46

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版