一、stm32cubemx配置如下图
二、spi读写代码`
/*
*
*MAX31856 GET TEMP
*/
uint16_t Max31856_GetTemp(uint8_t ch)
{
uint8_t temp_reg[5];
uint32_t tt;
// float temp=0.0;
SPI_ReadWrite(ch,12,temp_reg,5);
tt=(temp_reg[0]<<16)|(temp_reg[1]<<8)|temp_reg[2];
if (tt & 0x800000) {
tt |= 0xFF000000; // fix sign bit
}
tt >>= 5; // bottom 5 bits are unused
float temp = tt;
temp *= 0.0078125;
temp = temp*10;
return floor(temp);
}`
static void SPI_ReadWrite(uint8_t ch,uint8_t addr,uint8_t *dat,uint8_t size)
{
switch(ch)
{
case 1: CS1(0); CS2(1);CS3(1);CS4(1);break;
case 2: CS1(1); CS2(0);CS3(1);CS4(1); break;
case 3: CS1(1); CS2(1);CS3(0);CS4(1);break;
case 4: CS1(1); CS2(1);CS3(1);CS4(0); break;
default:break;
}
HAL_Delay(1);
HAL_SPI_Transmit(&hspi1,&addr,1,100);
if((addr&0x80)==0x80)
HAL_SPI_Transmit(&hspi1,dat,size,100);
else
HAL_SPI_Receive(&hspi1,dat,size,100);
HAL_Delay(1);
CS1(1); CS2(1);CS3(1);CS4(1);
}
三、实验结果
四、参考资料