stm32f103rct6 MAX6675 HAL库
时间: 2023-10-14 12:05:44 浏览: 251
基于STM32F的MAX6675驱动
4星 · 用户满意度95%
以下是基于STM32 HAL库的STM32F103RCT6与MAX6675的代码示例:
```c
#include "stm32f1xx_hal.h"
SPI_HandleTypeDef hspi1;
/* MAX6675 CS Pin */
#define MAX6675_CS_GPIO_PORT GPIOB
#define MAX6675_CS_GPIO_PIN GPIO_PIN_12
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
/* Initialize SPI */
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* Initialize MAX6675 CS Pin */
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = MAX6675_CS_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(MAX6675_CS_GPIO_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(MAX6675_CS_GPIO_PORT, MAX6675_CS_GPIO_PIN, GPIO_PIN_SET);
uint16_t data;
float temperature;
while (1)
{
/* Read temperature from MAX6675 */
HAL_GPIO_WritePin(MAX6675_CS_GPIO_PORT, MAX6675_CS_GPIO_PIN, GPIO_PIN_RESET);
HAL_SPI_Receive(&hspi1, &data, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(MAX6675_CS_GPIO_PORT, MAX6675_CS_GPIO_PIN, GPIO_PIN_SET);
/* Convert data to temperature */
temperature = (float)data * 0.25f;
/* Do something with temperature */
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
```
在此代码示例中,使用SPI1与MAX6675进行通信。首先,初始化了SPI1,并设置了CS引脚为输出模式。然后在主循环中,通过SPI1从MAX6675读取温度数据,并转换成温度值。最后可以将温度值用于其他操作。
需要注意的是,此代码示例中的时钟配置适用于8MHz的外部晶振,并使用PLL倍频到72MHz。如果使用其他时钟配置,需要相应更改代码。另外,此代码示例中的SPI时钟分频系数为256,这意味着SPI时钟为72MHz/256=281.25kHz。如果需要更高的SPI时钟速度,可以将分频系数更改为较小的值。
阅读全文