STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出

这篇具有很好参考价值的文章主要介绍了STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出

STM32部分芯片具有12位DAC输出能力,要实现16位及以上DAC输出需要外挂DAC转换ASIC。

DAC8552是双路16位DAC输出芯片,通过SPI三线总线进行配置控制输出。这里介绍通过GPIO管脚模拟时序进行控制的方式。

电路连接

DAC8552支持2.7V~5.5V的供电,根据需要提供电源电压,对于STM32可能面对不同供电电压的DAC8552, 因此STM32与DAC8552连接的三线,可以用10K电阻上拉到DAC8552的供电电压,而STM32选择支持FT(5V耐压)的三个管脚,并采用Open-drain无上下拉输出模式,从而可以兼容在各种供电条件下的DAC8552访问控制。对于STM32F103可以采用PB6, PB7和PB8进行连接,对于5V供电的DAC8552的连接如下图所示:

STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出

DAC8552控制协议

STM32通过发送24个位的SPI数据控制DAC8552的工作状态。其中前16个位为单路(通道A或通道B)的DAC配置值,后8个位为控制指令。
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出
协议控制操作主要由两部分操作构成:

  1. 发送24位数据到某个通道的buffer
  2. 执行buffer里24位数据里的控制指令,即"load"某个通道,指令可能是通道关电,也可能是DAC电压根据配置数据值输出

在关电模式可以配置通道管脚处于1K下拉,100K下拉或高阻状态。操作方式比较多,如下图所示:
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出
可以进行简化,对于通道配置只采用这几种指令:
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出
对于通告关电只采用这几种:
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出

STM32CUBEIDE工程配置

这里采用STM32CUBEIDE,对STM32F103C6T6芯片进行HAL库工程配置。
首先配置时钟系统:
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出
然后配置三线的管脚:
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出
保存并生成基本:
STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出

STM32工程代码

SPI三线只向DAC8552单向发送数据进行控制。在SYNC即片选低电平期间,每个SCLK时钟下降沿DAC8552采样输入数据。

这里采用的微秒级延时函数,参考 STM32 HAL us delay(微秒延时)的指令延时实现方式及优化 。

首先定义管脚输出态:

#define   DAC8552_SYNC_LOW    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
#define   DAC8552_SYNC_HIGH   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
#define   DAC8552_DIN_LOW     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);
#define   DAC8552_DIN_HIGH    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);
#define   DAC8552_SCLK_LOW    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
#define   DAC8552_SCLK_HIGH   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);

然后编写通道输出配置函数, 这里提供单通道和双通道配置的函数,以及单通道和双通道关电的函数:

void DAC8552_Set_Channel_A(uint16_t Data)
{
	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x10;
	WriteData = (CMD<<16) | Data;

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts
}

void DAC8552_Set_Channel_B(uint16_t Data)
{
	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x24;
	WriteData = (CMD<<16) | Data;

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts
}


void DAC8552_Set_Channel_AB(uint16_t Data)
{
	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x10;
	WriteData = (CMD<<16) | Data;

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;


    CMD = 0x24;
	WriteData = (CMD<<16) | Data;

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts
}

void DAC8552_Set_PowerDown_1K_A(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x11;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_1K_B(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x25;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_1K_AB(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x11;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;


    CMD = 0x25;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}


void DAC8552_Set_PowerDown_100K_A(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x12;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_100K_B(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x26;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_100K_AB(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x12;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;


    CMD = 0x26;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_Hz_A(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x13;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_Hz_B(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x27;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_Hz_AB(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x13;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;


    CMD = 0x27;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

就可以进行函数调用实现控制DAC8552双路的电压输出值。如控制A路为DAC8552参考电压的1/2, B路输出为DAC8552参考电压的3/4:

  DAC8552_Set_Channel_A(32768);
  DAC8552_Set_Channel_B(49152);

完整的main.c代码如下:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 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 */
__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 PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define   DAC8552_SYNC_LOW    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
#define   DAC8552_SYNC_HIGH   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
#define   DAC8552_DIN_LOW     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);
#define   DAC8552_DIN_HIGH    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);
#define   DAC8552_SCLK_LOW    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
#define   DAC8552_SCLK_HIGH   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);

void DAC8552_Set_Channel_A(uint16_t Data)
{
	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x10;
	WriteData = (CMD<<16) | Data;

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts
}

void DAC8552_Set_Channel_B(uint16_t Data)
{
	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x24;
	WriteData = (CMD<<16) | Data;

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts
}


void DAC8552_Set_Channel_AB(uint16_t Data)
{
	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x10;
	WriteData = (CMD<<16) | Data;

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;


    CMD = 0x24;
	WriteData = (CMD<<16) | Data;

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts
}

void DAC8552_Set_PowerDown_1K_A(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x11;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_1K_B(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x25;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_1K_AB(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x11;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;


    CMD = 0x25;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}


void DAC8552_Set_PowerDown_100K_A(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x12;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_100K_B(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x26;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_100K_AB(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x12;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;


    CMD = 0x26;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_Hz_A(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x13;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_Hz_B(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x27;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}

void DAC8552_Set_PowerDown_Hz_AB(void)
{

	uint8_t CMD = 0;
	uint32_t WriteData = 0;

	__disable_irq() ; //disable all interrupts

    CMD = 0x13;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;


    CMD = 0x27;
    WriteData = (CMD<<16);

	DAC8552_SYNC_HIGH;
	PY_Delay_us_t(1);
	DAC8552_SYNC_LOW;

	for(uint8_t i=0;i<24;i++)
	{
		if( (WriteData << i) & 0x800000 )
		{
			DAC8552_DIN_HIGH;
		}
		else
		{
			DAC8552_DIN_LOW;
		}

		DAC8552_SCLK_HIGH;
		PY_Delay_us_t(1);
		DAC8552_SCLK_LOW;
		PY_Delay_us_t(1);
	}

	DAC8552_SYNC_HIGH;

    __enable_irq() ;  //enable all interrupts

}
/* 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 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* 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();



  DAC8552_Set_Channel_A(32768);
  DAC8552_Set_Channel_B(49152);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  PY_Delay_us_t(1000000);
    /* 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};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  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_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = 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};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8, GPIO_PIN_SET);

  /*Configure GPIO pins : PB6 PB7 PB8 */
  GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* 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 */

STM32例程下载

STM32F103C6T6模拟SPI时序控制DAC8552电压输出例程

–End–文章来源地址https://www.toymoban.com/news/detail-408537.html

到了这里,关于STM32模拟SPI时序控制双路16位数模转换(16bit DAC)芯片DAC8552电压输出的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • STM32模拟SPI协议控制数字电位器MCP41010电阻值

    MCP41010是单路8位分辨率数字电位器,通过SPI接口可控制电位器阻值分配,相当于PW0端在PA0和PB0之间滑动。如下图所示: MCP41010是10K欧姆规格的数字电位器,即PA0和PB0之间的阻值恒定为10K,PW0与PA0或PW0与PB0之间的阻值互补可配。 另外有相关型号不同阻值规格和集成双路的MCP系列

    2024年02月16日
    浏览(42)
  • STM32模拟SPI协议获取24位模数转换(24bit ADC)芯片AD7791电压采样数据

    STM32大部分芯片只有12位的ADC采样性能,如果要实现更高精度的模数转换如24位ADC采样,则需要连接外部ADC实现。AD7791是亚德诺(ADI)半导体一款用于低功耗、24位Σ-Δ型模数转换器(ADC) ,适合低频测量应用,提供50 Hz/60 Hz同步抑制。 这里介绍基于AD7791的24位ADC采样实现。 AD7791的管脚

    2024年02月09日
    浏览(61)
  • STM32F103模拟SPI控制6针/7针0.96寸OLED显示屏

    OLED主要参数 1、高分辨率:128 64(和12864LCD相同分辨率,但该OLED屏的单位面积像素点多)。 2、广可视角度:大于160°。 3、低功耗:正常显示时0.04W。 4、宽供电范围:直流3.3V-5V。 5、工业级:工作温度范围-30°℃~70°℃。 6、体积小:28.8mm 28.5mm。 7、通信方式:lIC、SPI。 8、亮

    2024年02月04日
    浏览(160)
  • STM32配置读取双路24位模数转换(24bit ADC)芯片CS1238数据

    CS1238是一款国产双路24位ADC芯片,与CS1238对应的单路24位ADC芯片是CS1237,功能上相当于HX711和TM7711的组合。其功能如下所示: 市面上的模块: CS1238内部原理如下所示, VDD是DVDD和AVDD的合并: 有单独的参考电压输入设置管脚,以及内部输出与VDD同电压的参考输出电压,可选连接到

    2024年02月16日
    浏览(45)
  • STM32的HAL库SPI操作(master 模式)-根据时序图配置SPI

    SPI基本概念请自行百度,参考:百度百科SPI简介.我们讲重点和要注意的地方。 接线一一对应 也就是说主控的MISO,MOSI,SCLK,[CSn]分别和设备的MISO,MOSI,SCLK,[CSn]一一对应相连,不交叉,不交叉,不交叉…(重要的事情说三遍)。 这是无线模块CC2500的SPI接口时序,这里可以看到,从

    2024年02月06日
    浏览(42)
  • STM32开发_利用SPI协议读写SD卡、介绍SD卡SPI时序

    目录 一、​  SD卡引脚接口功能介绍 1.1 SD卡引脚接口图 1.2 SPI方式驱动SD卡介绍 1.3 开发板接口定义 二、MMC卡、SD卡介绍 2.1 SD卡和MMC两者间区别 2.2 SD卡版本说明 2.3 SD卡常用的指令表 三、向SD卡发送命令的步骤介绍(SendSDCardCmd) 3.1 取消选中SD卡(SDCardCancelCS) 3.2 选中SD卡(SDCardSele

    2024年02月16日
    浏览(51)
  • 【STM32学习】——SPI通信协议&SPI时序&W25Q64存储芯片&软件SPI读写

    目录 前言 一、SPI通信协议 1.概述​ 2.硬件电路  3.移位示意图 二、SPI时序 1.时序基本单元 2.完整时序波形 三、W25Q64存储芯片 1.芯片简介  2.硬件电路引脚定义  3.芯片框图 4.Flash操作注意事项 四、软件SPI读写W25Q64 五、SPI通信外设 总结 声明:学习笔记来自江科大自化协B站教

    2024年02月09日
    浏览(62)
  • STM32F407 SPI配置和时序图讲解(二)

    上节讲了SPI的基本配置,这节主要讲解 如何看时序图 ,SPI数据到底是如何传输的。 SPI初始化后,就可以开始向对象发送数据了,但是要发送数据给W25Q128模块,需要按照它的时序图来发送( 个人用的是W25Q128模块 ) W25Q128模块简介 W25Q128是一款常见的串行闪存存储器模块,属

    2024年02月06日
    浏览(38)
  • STM32 软件模拟SPI

    STM32库:标准函数库 测试环境:STM32F103系列 SPI(Serial Peripheral Interface,串行外设接口)是由摩托罗拉(Motorola)在1980前后提出的一种全双工同步串行通信接口,它用于MCU与各种外围设备以串行方式进行通信以交换信息,通信速度最高可达25MHz以上。 SPI接口主要应用在EEPROM、

    2024年02月09日
    浏览(65)
  • STM32模拟SPI的程序

    最近完成的一个项目因为SPI管脚被别的资源占用了,只能通过模拟SPI对存储器进行操作。最后测试发现模拟SPI的速度也很快,和硬件SPI相比感觉不到太大的差异。现将模拟SPI的程序分享如下: 管脚定义,大家可以根据需要自行修改,任何一个GPIO都可以模拟SPI 模拟SPI MOSI MOS

    2024年02月12日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包