基于正点原子HAL库的stm32f103rct6 MAX6675 .C和.H整体程序
时间: 2024-05-08 19:22:29 浏览: 103
3,标准例程-HAL库版本_hal库_HAL_stm32f103rct6正点原子例程_STM32hal例程_
5星 · 资源好评率100%
很抱歉,我并没有现成的基于正点原子HAL库的stm32f103rct6 MAX6675整体程序。不过您可以参考以下代码进行开发。
MAX6675.c
```
#include "MAX6675.h"
//SPI接口初始化函数
void MAX6675_SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
//打开SPI1时钟
RCC_APB2PeriphClockCmd(MAX6675_SPI_CLK, ENABLE);
//打开SPI1引脚时钟
RCC_APB2PeriphClockCmd(MAX6675_SPI_GPIO_CLK, ENABLE);
//配置SPI1引脚
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStruct.GPIO_Pin = MAX6675_SCK_PIN | MAX6675_MOSI_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MAX6675_SPI_GPIO, &GPIO_InitStruct);
//配置SPI1 NSS 引脚
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Pin = MAX6675_CS_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MAX6675_CS_GPIO, &GPIO_InitStruct);
//配置SPI1 MISO 引脚
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_InitStruct.GPIO_Pin = MAX6675_MISO_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MAX6675_SPI_GPIO, &GPIO_InitStruct);
//配置SPI1
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //时钟分频
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; //第一个边沿采样
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; //时钟极性,空闲低
SPI_InitStruct.SPI_CRCPolynomial = 7; //CRC多项式
SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b; //数据位长度
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双向全双工
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; //高位在前
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; //主机模式
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; //软件控制 NSS
SPI_Init(MAX6675_SPI, &SPI_InitStruct);
SPI_Cmd(MAX6675_SPI, ENABLE); //使能 SPI
}
//读取温度函数
float MAX6675_Read_Temp(void)
{
uint16_t temp;
float temperature;
GPIO_ResetBits(MAX6675_CS_GPIO, MAX6675_CS_PIN); //使能片选
//发送读取命令
SPI_I2S_SendData(MAX6675_SPI, 0x0000);
while (SPI_I2S_GetFlagStatus(MAX6675_SPI, SPI_I2S_FLAG_BSY) == SET)
;
temp = SPI_I2S_ReceiveData(MAX6675_SPI);
//读取温度数据
temp = 0x0000;
SPI_I2S_SendData(MAX6675_SPI, 0x0000);
while (SPI_I2S_GetFlagStatus(MAX6675_SPI, SPI_I2S_FLAG_BSY) == SET)
;
temp = SPI_I2S_ReceiveData(MAX6675_SPI);
GPIO_SetBits(MAX6675_CS_GPIO, MAX6675_CS_PIN); //禁止片选
//计算温度
temperature = (float)temp * 0.25;
return temperature;
}
```
MAX6675.h
```
#ifndef __MAX6675_H
#define __MAX6675_H
#include "stm32f10x.h"
#define MAX6675_SPI_CLK RCC_APB2Periph_SPI1
#define MAX6675_SPI_GPIO_CLK RCC_APB2Periph_GPIOA
#define MAX6675_SPI GPIO1
#define MAX6675_SPI_GPIO GPIOA
#define MAX6675_CS_GPIO GPIOA
#define MAX6675_CS_PIN GPIO_Pin_4
#define MAX6675_SCK_PIN GPIO_Pin_5
#define MAX6675_MOSI_PIN GPIO_Pin_7
#define MAX6675_MISO_PIN GPIO_Pin_6
void MAX6675_SPI_Init(void);
float MAX6675_Read_Temp(void);
#endif /* __MAX6675_H */
```
main.c
```
#include "stm32f10x.h"
#include "MAX6675.h"
int main(void)
{
float temperature;
MAX6675_SPI_Init(); //SPI接口初始化
while (1)
{
temperature = MAX6675_Read_Temp(); //读取温度
//处理温度数据
}
}
```
阅读全文