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的连接如下图所示:
DAC8552控制协议
STM32通过发送24个位的SPI数据控制DAC8552的工作状态。其中前16个位为单路(通道A或通道B)的DAC配置值,后8个位为控制指令。
协议控制操作主要由两部分操作构成:
- 发送24位数据到某个通道的buffer
- 执行buffer里24位数据里的控制指令,即"load"某个通道,指令可能是通道关电,也可能是DAC电压根据配置数据值输出
在关电模式可以配置通道管脚处于1K下拉,100K下拉或高阻状态。操作方式比较多,如下图所示:
可以进行简化,对于通道配置只采用这几种指令:
对于通告关电只采用这几种:
STM32CUBEIDE工程配置
这里采用STM32CUBEIDE,对STM32F103C6T6芯片进行HAL库工程配置。
首先配置时钟系统:
然后配置三线的管脚:
保存并生成基本:
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–