/SPI configuration below * * * * * */
# define SPI1_CLK_PIN GPIO_PIN_3
# define SPI1_CLK_PORT GPIOB
# define SPI1_MISO_PIN GPIO_PIN_4
# define SPI1_MISO_PORT GPIOB
# define SPI1_MOSI_PIN GPIO_PIN_5
# define SPI1_MOSI_PORT GPIOB
Void SPI1_Init (void)
{
SPI1_Handler. Instance=SPI1;//SPI1
SPI1_Handler. Init. Mode=SPI_MODE_MASTER;//set the SPI work mode, set the main mode
SPI1_Handler. Init. Direction=SPI_DIRECTION_2LINES;//set the SPI one-way or two-way data model: the SPI is set to double mode
SPI1_Handler. Init. DataSize=SPI_DATASIZE_8BIT;//set the SPI data size: SPI to send and receive eight frame structure
SPI1_Handler. Init. CLKPolarity=SPI_POLARITY_LOW;//the idle state as the low level of serial synchronous clock
SPI1_Handler. Init. CLKPhase=SPI_PHASE_2EDGE;//the second jump along the serial synchronous clock (up or down) data by sampling
SPI1_Handler. Init. NSS=SPI_NSS_SOFT;//NSS signal by the hardware (NSS pin) or software (use SSI) management: internal NSS signal with SSI control
SPI1_Handler. Init. BaudRatePrescaler=SPI_BAUDRATEPRESCALER_256;//define the baud rate of the preassigned frequency value: baud rate preassigned frequency value of 256
SPI1_Handler. Init. FirstBit=SPI_FIRSTBIT_MSB;//specified data transmission from the MSB position or LSB: data transmission from the MSB start
SPI1_Handler. Init. TIMode=SPI_TIMODE_DISABLE;//close TI model
SPI1_Handler. Init. CRCCalculation=SPI_CRCCALCULATION_DISABLE;//close hardware CRC check
SPI1_Handler. Init. CRCPolynomial=7;//CRC value calculated polynomial
HAL_SPI_Init (& amp; SPI1_Handler);//initialize
__HAL_SPI_ENABLE (& amp; SPI1_Handler);//can make SPI1
LogsChar (" SPI1 initialized!" );
}
//SPI1 bottom drive, the clock can make, pin configuration
//this function will be HAL_SPI_Init () call
//hspi: SPI handle
Void HAL_SPI_MspInit (SPI_HandleTypeDef * hspi)
{
If (hspi - & gt; The Instance==SPI1)
{
GPIO_InitTypeDef GPIO_InitStruct.
__HAL_RCC_GPIOB_CLK_ENABLE ();//can make GPIOB clock
__HAL_RCC_SPI1_CLK_ENABLE ();//can make SPI1 clock
GPIO_InitStruct. Pin=SPI1_CLK_PIN;
GPIO_InitStruct. Mode=GPIO_MODE_AF_PP;
GPIO_InitStruct. Pull=GPIO_PULLUP;
GPIO_InitStruct. Speed=GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct. Alternate=GPIO_AF5_SPI1;
HAL_GPIO_Init (SPI1_CLK_PORT, & amp; GPIO_InitStruct);
GPIO_InitStruct. Pin=SPI1_MISO_PIN;
HAL_GPIO_Init (SPI1_MISO_PORT, & amp; GPIO_InitStruct);
GPIO_InitStruct. Pin=SPI1_MOSI_PIN;
HAL_GPIO_Init (SPI1_MOSI_PORT, & amp; GPIO_InitStruct);
LogsChar (" SPI1 GPIO initialized!" );
}
}
//SPI speed setting function
//SPI speed=fAPB1/frequency division coefficient
//@ ref SPI_BaudRate_Prescaler: SPI_BAUDRATEPRESCALER_2 ~ SPI_BAUDRATEPRESCALER_256
//fAPB1 45 MHZ clock is commonly:
Void SPI1_SetSpeed (uint8_t SPI_BaudRatePrescaler)
{
The assert_param (IS_SPI_BAUDRATE_PRESCALER (SPI_BaudRatePrescaler));//determine validity
__HAL_SPI_DISABLE (& amp; SPI1_Handler);//close the SPI
SPI1_Handler. The Instance - & gt; CR1 & amp;=0 xffc7;//a 3-5 reset, which is used to set the baud rate
SPI1_Handler. The Instance - & gt; CR1 |=SPI_BaudRatePrescaler;//set the speed of SPI
__HAL_SPI_ENABLE (& amp; SPI1_Handler);//can make SPI
}
//a byte
SPI1, speaking, reading and writing//TxData: to write bytes
//the return value: read bytes
Uint8_t SPI1_ReadWriteByte (uint8_t TxData)
{
Uint8_t Rxdata;
HAL_SPI_TransmitReceive (& amp; SPI1_Handler, & amp; TxData, & amp; Rxdata, 1, 1000);
return Rxdata;//returns the received data
}
/read a function of temperature below * * * * */
Float MAX31865_GetTemp_JT1 (void)
{
Float temp=0.0;//temperature data
Uint8_t a;
Uint32_t ADCCode=0;//ADC conversion data
//float Rt;//detected resistance
MAX31865_CS2_Set;//close JT2 area
MAX31865_CS1_Reset;//can make JT1 piece choose
A=SPI1_ReadWriteByte (0 x00);
LogsHex (" JT1 0 x00 read ", a);
A=SPI1_ReadWriteByte (0 x01);
LogsHex (" JT1 0 x01 read ", a);
A=SPI1_ReadWriteByte (0 x02);
LogsHex (" JT1 0 x02 read ", a);
A=SPI1_ReadWriteByte (0 x03);
LogsHex (" JT1 0 x03 read ", a);
A=SPI1_ReadWriteByte x04 (0);
LogsHex (JT1 0 x04 "read", a);
A=SPI1_ReadWriteByte x05 (0);
LogsHex (" JT1 0 x05 read ", a);
A=SPI1_ReadWriteByte x06 (0);
LogsHex (" JT1 0 x06 read ", a);
A=SPI1_ReadWriteByte x07 (0);
LogsHex (" JT1 0 x07 read ", a);
A=SPI1_ReadWriteByte (0 x01);
LogsHex (" JT1 0 x01 read ", a);
A=SPI1_ReadWriteByte (0 x02);
LogsHex (" JT1 0 x02 read ", a);
A=SPI1_ReadWriteByte (0 x01);
LogsHex (" JT1 0 x01 read ", a);
A=SPI1_ReadWriteByte (0 x02);
LogsHex (" JT1 0 x02 read ", a);
SPI1_ReadWriteByte (0 x00);
ADCCode=SPI1_ReadWriteByte (0 x01) & lt; <8;//read the temperature data high
SPI1_ReadWriteByte (0 x00);
ADCCode |=SPI1_ReadWriteByte (0 x02);//read the temperature data low
ADCCode & gt;>=1;//the left one, remove D0 error flags, the resulting value is the data value
//
//Rt=(float) (ADCCode * Rt0/32768.0);//value
Temp=(float) (ADCCode/32-256.0);//the approximate temperature
MAX31865_CS1_Set;//cancel can give a choice, stop to read and write
LogsFloat (" The temp of JT1 is ", temp);//send log
return temp;
}
/* -- - the following result -- -- -- -- -- -- -- -- -- - */
Always returns 0 XFF reads the results, unable to read to the correct temperature,
Below the data read printed the address information:
SPI1 GPIO initialized!
SPI1 initialized!
MAX31865 JT1 configured!
JT1 0 x00 read: 0 XFF
JT1 0 x01 read: 0 xc1
JT1 0 x02 read: 0 x00
JT1 0 x03 read: 0 x01
JT1 0 x04 read: 0 xc0
JT1 0 x05 read: 0 x00
JT1 0 x06 read: 0 x20
nullnullnullnullnullnullnullnullnull