AnsweredAssumed Answered

Using TLC2543 ADC chip on STM32L476RG

Question asked by paker earth on Feb 8, 2017
Latest reply on Feb 8, 2017 by waclawek.jan

Hi all

 

I followed the post below to setup tlc2543.

http://blog.csdn.net/haozi_1989/article/details/6111776 

datasheet http://www.ti.com/lit/ds/symlink/tlc2543-ep.pdf 

 

Because the STM32L4 series used HAL library not SPL, so I have to modify some codes.

But I still got random voltage values.

Could someone tell me if the modification is right, or how to solve it?

Thank you all in advance.

 

Here are related codes. SPI2 , PB1 use as CS

uint16_t Read_TLC2543(uint16_t chan)
{
   uint8_t data;
   uint16_t i,ADCdata;
   chan <<= 12;
   chan|= 0x0C00;

 

   //GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);

   for(i=0;i<2000;i++);//delay

   //while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==RESET);
   while(((hspi2.Instance->SR) & SPI_FLAG_TXE) != SPI_FLAG_TXE) ;

   //SPI_I2S_SendData(SPI1,chan);
   if(HAL_SPI_Transmit(&hspi2, &chan, 2, 1000) != HAL_OK)
      printf("error1\r\n");

   //while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET) ;
   while(((hspi2.Instance->SR) & SPI_FLAG_RXNE) != SPI_FLAG_RXNE) ;

   //ADCdata=SPI_I2S_ReceiveData(SPI1);
   if(HAL_SPI_Receive(&hspi2, &ADCdata, 2, 1000) != HAL_OK)
      printf("error2\r\n");

   for(i=0;i<2000;i++);//delay

   ADCdata >>= 4;
   //GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
   return ADCdata;
}

 

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{

   GPIO_InitTypeDef GPIO_InitStruct;
   if(hspi->Instance==SPI2)
   {
   /* USER CODE BEGIN SPI2_MspInit 0 */

   /* USER CODE END SPI2_MspInit 0 */
   /* Peripheral clock enable */
   __HAL_RCC_SPI2_CLK_ENABLE();

   /**SPI2 GPIO Configuration
   PB13 ------> SPI2_SCK
   PB14 ------> SPI2_MISO
   PB15 ------> SPI2_MOSI
   */
   /* USER CODE BEGIN SPI2_MspInit 1 */
   /* Configure SPI SCK */
   GPIO_InitStruct.Pin = GPIO_PIN_13;
   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_SPI2;
   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

   /* Configure SPI MISO and MOSI */
   GPIO_InitStruct.Pin = GPIO_PIN_15;
   GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
   GPIO_InitStruct.Pull = GPIO_PULLDOWN;
   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

   GPIO_InitStruct.Pin = GPIO_PIN_14;
   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
   /* USER CODE END SPI2_MspInit 1 */
   }

}

 

static void MX_SPI2_Init(void)
{

   hspi2.Instance = SPI2;
   hspi2.Init.Mode = SPI_MODE_MASTER;
   hspi2.Init.Direction = SPI_DIRECTION_2LINES;
   hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
   hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
   hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
   hspi2.Init.NSS = SPI_NSS_SOFT;
   hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
   hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
   hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
   hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
   hspi2.Init.CRCPolynomial = 7;
   hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
   hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
   if (HAL_SPI_Init(&hspi2) != HAL_OK)
   {
      Error_Handler();
   }

}

Outcomes