cancel
Showing results for 
Search instead for 
Did you mean: 

Reading temperature sensor MAX31855 by SPI1 of STM32F070xx

Md Mubdiul Hasan
Associate III
Posted on October 21, 2016 at 08:53

Dear  all,

I want to read MAX31855 by stm32F070xx in degree celsius.

Kindly take a look in this code, how can I implement it to 32f0xx?

#define SPI_PORT      SPI2

#define SPI_SCK_PIN   GPIO_Pin_13    // PB13

#define SPI_MISO_PIN  GPIO_Pin_14    // PB14

#define SPI_CS_PIN    GPIO_Pin_12    // PB12

#define SPI_GPIO_PORT GPIOB

void max31855_init() {

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

    GPIO_InitTypeDef PORT;

    PORT.GPIO_Speed = GPIO_Speed_50MHz;

    PORT.GPIO_Pin = SPI_MISO_PIN | SPI_SCK_PIN;

    PORT.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init(GPIOB,&PORT);

    PORT.GPIO_Speed = GPIO_Speed_50MHz;

    PORT.GPIO_Pin =  SPI_CS_PIN;

    PORT.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_Init(GPIOB,&PORT);

    SPI_InitTypeDef SPI;

    SPI.SPI_Mode = SPI_Mode_Master;

    SPI.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;

    SPI.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

    SPI.SPI_CPOL = SPI_CPOL_Low;

    SPI.SPI_CPHA = SPI_CPHA_2Edge;

    SPI.SPI_DataSize = SPI_DataSize_16b;

    SPI.SPI_FirstBit = SPI_FirstBit_MSB;

    SPI.SPI_NSS = SPI_NSS_Soft;

    SPI_Init(SPI_PORT,&SPI);

    SPI_NSSInternalSoftwareConfig(SPI_PORT,SPI_NSSInternalSoft_Set);

    SPI_Cmd(SPI_PORT,ENABLE);

    GPIO_SetBits(SPI_GPIO_PORT,SPI_CS_PIN);

}

uint16_t max31855_ReadData() {

    while (SPI_I2S_GetFlagStatus(SPI_PORT,SPI_I2S_FLAG_TXE) == RESET); // Wait while DR register is not empty

    SPI_I2S_SendData(SPI_PORT,0x0F); // Send byte to SPI

    while (SPI_I2S_GetFlagStatus(SPI_PORT,SPI_I2S_FLAG_RXNE) == RESET); // Wait to receive byte

    return SPI_I2S_ReceiveData(SPI_PORT); // Read byte from SPI bus

}

double max31855_readCelsius() {

  uint32_t v = 0;

  GPIO_ResetBits(SPI_GPIO_PORT,SPI_CS_PIN);

  v = (uint32_t)max31855_ReadData() << 16 | max31855_ReadData(); // получаем две по�?ылки по 16 байт

  GPIO_SetBits(SPI_GPIO_PORT,SPI_CS_PIN);

  float internal = (v >> 4) & 0x7FF;

  internal *= 0.0625;

  if ((v >> 4) & 0x800)

    internal *= -1;

  if (v & 0x7) {

    return -333; // чето не так

  }

  if (v & 0x80000000) {

    v = 0xFFFFC000 | ((v >> 18) & 0x00003FFFF);

  }

  else {

    v >>= 18;

  }

  double centigrade = v;

  centigrade *= 0.25;

  return centigrade;

People used in this way also, 

void MAX31855_chipUnselect()

  {

    HAL_GPIO_WritePin(GPIOB, GPIO_TEMP_SO, GPIO_TEMP_/CS);

  }

  void MAX31855_chipSelect()

  {

    HAL_GPIO_WritePin(GPIOB,GPIO_TEMP_/CS, GPIO_PIN_RESET);

  }

  uint32_t MAX31855_readData()

  {

    uint8_t pDataRx[4] = { };

    MAX31855_chipSelect();

    HAL_SPI_Receive(&hspi1, (uint8_t*) pDataRx, 4, 1000);

    MAX31855_chipUnselect();

    return (pDataRx[i] << 24) | (pDataRx[i] << 16) | (pDataRx[i] << 8) | pDataRx[i];

  }

1. How could I change first code to second format for celsius ?

2.MAX31855 is not bidirectional,can I follow this idea, http://www.ba0sh1.com/2014/05/31/howto-use-stm32-spi-half-duplex-mode/ 

Help me please.

Hasan
0 REPLIES 0