基于HAL库的stm32f103rct6 MAX6675 .C和.H的完整HAL库程序
时间: 2024-06-09 10:11:28 浏览: 148
程序源码HAL版本参考
以下是基于HAL库的stm32f103rct6和MAX6675的完整程序,包含了对应的.C和.H文件。请注意,此程序仅供参考,具体实现可能需要根据您的硬件和软件环境进行修改。
MAX6675.h
```c
#ifndef __MAX6675_H
#define __MAX6675_H
#include "stm32f1xx_hal.h"
#define CS_PORT GPIOA
#define CS_PIN GPIO_PIN_1
#define SCK_PORT GPIOA
#define SCK_PIN GPIO_PIN_5
#define SO_PORT GPIOA
#define SO_PIN GPIO_PIN_6
void MAX6675_Init(void);
float MAX6675_Read_Temperature(void);
#endif
```
MAX6675.c
```c
#include "MAX6675.h"
/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
/* Private function prototypes -----------------------------------------------*/
void MAX6675_SPI_Init(void);
void MAX6675_Init(void)
{
MAX6675_SPI_Init();
HAL_GPIO_WritePin(CS_PORT, CS_PIN, GPIO_PIN_SET);
}
float MAX6675_Read_Temperature(void)
{
uint8_t i;
uint8_t buffer[2];
uint16_t value;
HAL_GPIO_WritePin(CS_PORT, CS_PIN, GPIO_PIN_RESET);
HAL_SPI_Receive(&hspi1, buffer, 2, HAL_MAX_DELAY);
HAL_GPIO_WritePin(CS_PORT, CS_PIN, GPIO_PIN_SET);
value = ((uint16_t)buffer[0] << 8) | buffer[1];
if (value & 0x4)
{
return -1000.0f;
}
value >>= 3;
return (float)value * 0.25f;
}
void MAX6675_SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* SPI1 GPIO Configuration */
/* PA5 ------> SPI1_SCK */
/* PA6 ------> SPI1_MISO */
/* PA7 ------> SPI1_MOSI */
GPIO_InitStruct.Pin = SCK_PIN | SO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(CS_PORT, &GPIO_InitStruct);
__HAL_RCC_SPI1_CLK_ENABLE();
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_0;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
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;
HAL_SPI_Init(&hspi1);
}
```
main.c
```c
#include "main.h"
#include "MAX6675.h"
#include <stdio.h>
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
int main(void)
{
float temperature;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
MAX6675_Init();
while (1)
{
temperature = MAX6675_Read_Temperature();
if (temperature == -1000.0f)
{
printf("Thermocouple is not connected!\r\n");
}
else
{
printf("Temperature: %.2f C\r\n", temperature);
}
HAL_Delay(1000);
}
}
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.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();
}
}
static void MX_SPI1_Init(void)
{
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
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();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(CS_PORT, CS_PIN, GPIO_PIN_SET);
/*Configure GPIO pin : CS_Pin */
GPIO_InitStruct.Pin = CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(CS_PORT, &GPIO_InitStruct);
}
```
阅读全文