STM32管脚模拟协议驱动双路16位DAC芯片TM8211

STM32管脚模拟协议驱动双路16位DAC芯片TM8211

TM8211是一款国产的低成本双路16位DAC驱动芯片,可以应用于普通数模转换领域及音频转换领域等。这里介绍STM32 HAL库驱动TM8211的逻辑,时序和代码。

TM8211的功能特性为:
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第1张图片
TM8211的内部电路功能框图为:
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第2张图片

TM8211驱动逻辑

TM8211支持典型的3.3V供电和5V供电,在驱动后进行电压输出时,需要注意,如以3.3V供电为例,并非是驱动输出0~3.3V的范围,而是半范围,即驱动输出的电压范围为0.825V – 2.475V (1/4VDD-- 3/4VDD)。

TM8211的管脚定义为:
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第3张图片
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第4张图片
其中控制管脚为WS, BCK和DIN,LCH和RCH为两个输出通道,介绍如下:
WS: 高电平指示对LCH通道进行配置,低电平指示对RCH通道进行配置
BCK:配置过程的时钟线,TM8211在时钟上升沿锁存数据,在WS的某个电平状态,前16个时钟锁存的数据有效,后面的忽略
DIN: 配置过程的数据线

TM8211典型应用

TM8211可应用于双通道音频输出,如:
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第5张图片
也可以控制输出交流波形,通过隔直电容去掉直流电压部分。

工程配置

TM8211的配置时钟最大可达18.4MHz,在不需要配置MHz级信号输出控制时,可以采用GPIO管脚模拟协议对TM8211输出电压进行控制。这里介绍STM32CUBEIDE开发平台,以STM32G030F6P6为例,GPIO管脚模拟协议配置TM8211的范例。需要进行更高速配置时则需要调整为SPI硬件接口驱动。

首先建立基本工程并配置时钟:
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第6张图片
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第7张图片
配置3个GPIO作为驱动接口:
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第8张图片
保存并生成初始工程代码:
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第9张图片

工程代码:

工程代码实现都在main.c文件里,代码里用到的微秒延时函数参考: STM32 HAL us delay(微秒延时)的指令延时实现方式及优化

两个通道都实现为类似正弦波的连续波形输出:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define TM8211_WS_L HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET)
#define TM8211_WS_H HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET)
#define TM8211_BCK_L HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET)
#define TM8211_BCK_H HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET)
#define TM8211_DIN_L HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET)
#define TM8211_DIN_H HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET)
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
__IO float usDelayBase;
void PY_usDelayTest(void)
{
  __IO uint32_t firstms, secondms;
  __IO uint32_t counter = 0;

  firstms = HAL_GetTick()+1;
  secondms = firstms+1;

  while(uwTick!=firstms) ;

  while(uwTick!=secondms) counter++;

  usDelayBase = ((float)counter)/1000;
}

void PY_Delay_us_t(uint32_t Delay)
{
  __IO uint32_t delayReg;
  __IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}

void PY_usDelayOptimize(void)
{
  __IO uint32_t firstms, secondms;
  __IO float coe = 1.0;

  firstms = HAL_GetTick();
  PY_Delay_us_t(1000000) ;
  secondms = HAL_GetTick();

  coe = ((float)1000)/(secondms-firstms);
  usDelayBase = coe*usDelayBase;
}


void PY_Delay_us(uint32_t Delay)
{
  __IO uint32_t delayReg;

  __IO uint32_t msNum = Delay/1000;
  __IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);

  if(msNum>0) HAL_Delay(msNum);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define us_num 2

void TM8211_OUTPUT_CONFIG(uint16_t LCH, uint16_t RCH)
{
	uint8_t i;
	TM8211_WS_L;
	PY_Delay_us_t(us_num);

	for(i=0; i<16; i++)
	{
		TM8211_BCK_L;
		if( (RCH>>(15-i))&0x0001 ) TM8211_DIN_H;
		else TM8211_DIN_L;
		PY_Delay_us_t(us_num);
		TM8211_BCK_H;
		PY_Delay_us_t(us_num);
	}

	PY_Delay_us_t(us_num);

	TM8211_WS_H;
	PY_Delay_us_t(us_num);
	for(i=0; i<16; i++)
	{
		TM8211_BCK_L;
		if( (LCH>>(15-i))&0x0001 ) TM8211_DIN_H;
		else TM8211_DIN_L;
		PY_Delay_us_t(us_num);
		TM8211_BCK_H;
		PY_Delay_us_t(us_num);
	}

	PY_Delay_us_t(us_num);
	TM8211_WS_L;
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  uint16_t C1 = 0, C2 = 0;

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */
  PY_usDelayTest();
  PY_usDelayOptimize();

  TM8211_OUTPUT_CONFIG(0x0000, 0x0000);
  PY_Delay_us_t(5000000);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	for(uint16_t i=0x8000; i<0xffff; i++)
	{
		C1=C2=i;
		TM8211_OUTPUT_CONFIG(C1, C2);
	}

	for(uint16_t j=0x0000; j<0x7fff; j++)
	{
		C1=C2=j;
		TM8211_OUTPUT_CONFIG(C1, C2);
	}

	for(uint16_t j=0x7fff; j>0x0000; j--)
	{
		C1=C2=j;
		TM8211_OUTPUT_CONFIG(C1, C2);
	}

	for(uint16_t i=0xffff; i>0x8000; i--)
	{
		C1=C2=i;
		TM8211_OUTPUT_CONFIG(C1, C2);
	}
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
  RCC_OscInitStruct.PLL.PLLN = 8;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7, GPIO_PIN_RESET);

  /*Configure GPIO pins : PA4 PA5 PA7 */
  GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

其中,配置值和输出电压的关系参考如下测试结果(3.3V供电)了解:
STM32管脚模拟协议驱动双路16位DAC芯片TM8211_第10张图片
如果要应用供电参考电压全范围输出的双路16位DAC,则参考:STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出

例程下载

STM32G030F6P6管脚模拟协议驱动双路16位DAC芯片TM8211例程

–End–

你可能感兴趣的:(STM32,stm32,TM8211,DAC,16位,双路)