stm32单片机max31855程序
时间: 2023-08-08 21:07:57 浏览: 97
以下是基于STM32单片机的max31855程序:
```
#include "stm32f10x.h"
#include "stdio.h"
#define CS_PIN GPIO_Pin_4
#define CS_GPIO GPIOA
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
void SPI_Configuration(void)
{
SPI_InitTypeDef spi;
GPIO_InitTypeDef gpio;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_StructInit(&gpio);
gpio.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
gpio.GPIO_Speed = GPIO_Speed_10MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpio);
GPIO_StructInit(&gpio);
gpio.GPIO_Pin = CS_PIN;
gpio.GPIO_Speed = GPIO_Speed_10MHz;
gpio.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(CS_GPIO, &gpio);
SPI_StructInit(&spi);
spi.SPI_Mode = SPI_Mode_Master;
spi.SPI_NSS = SPI_NSS_Soft;
spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
spi.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
spi.SPI_DataSize = SPI_DataSize_16b;
spi.SPI_CPOL = SPI_CPOL_Low;
spi.SPI_CPHA = SPI_CPHA_1Edge;
spi.SPI_FirstBit = SPI_FirstBit_MSB;
spi.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &spi);
SPI_Cmd(SPI1, ENABLE);
}
uint16_t SPI_ReadWrite(uint16_t data)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, data);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
float MAX31855_ReadTemp(void)
{
uint16_t data;
float temp;
GPIO_ResetBits(CS_GPIO, CS_PIN);
Delay(10);
data = SPI_ReadWrite(0);
Delay(10);
data = SPI_ReadWrite(0);
GPIO_SetBits(CS_GPIO, CS_PIN);
if(data & 0x7) // error bit is set
{
return 0.0f;
}
data >>= 18; // only the 14 MSBs are used for temperature data
temp = data & 0x3fff;
temp /= 4.0f;
if(data & 0x8000) // negative temperature
{
temp -= 8192.0f; // 2^13
}
return temp;
}
int main(void)
{
float temp;
char str[16];
SPI_Configuration();
while(1)
{
temp = MAX31855_ReadTemp();
sprintf(str, "Temperature: %.2f C\r\n", temp);
printf("%s", str);
Delay(1000);
}
}
```
注意,这个程序是基于STM32F10x系列单片机的,如果您使用的是其他型号的STM32单片机,可能需要对代码进行一些修改才能正常运行。同时,需要把程序中的CS_PIN和CS_GPIO改为您使用的引脚。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)