AD5663是ADI公司的一款DAC模块,用以实现两路模拟量信号输出。该芯片通过SPI通信来驱动。下面讲解使用STM32F7主控芯片来控制AD5663模拟量输出的流程。
配置STM32F7 SPI通信管脚
STM32CubeMX生成SPI驱动代码
/* SPI3 init function */
void MX_SPI3_Init(void)
{
/* USER CODE BEGIN SPI3_Init 0 */
/* USER CODE END SPI3_Init 0 */
/* USER CODE BEGIN SPI3_Init 1 */
/* USER CODE END SPI3_Init 1 */
hspi3.Instance = SPI3;
hspi3.Init.Mode = SPI_MODE_MASTER;
hspi3.Init.Direction = SPI_DIRECTION_2LINES;
hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi3.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi3.Init.NSS = SPI_NSS_SOFT;
hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi3.Init.CRCPolynomial = 7;
hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI3_Init 2 */
/* USER CODE END SPI3_Init 2 */
}
AD5663驱动代码开发
头文件io.h
#ifndef __IO_H__
#define __IO_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "gpio.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
/* USER CODE BEGIN Prototypes */
/*ÃüÁÒå*/
#define Write_to_Input_Register 0x000000
#define Update_DAC_Register 0x080000
#define Software_LDAC 0x100000
#define WriteTo_Update_DAC_Channel 0x180000
#define Power_Down 0x200000
#define RESET 0x280000
#define LDAC_Register_Setup 0x300000
#define Reference_Set 0x380000
/*µØַģʽ,Ñ¡ÔñDACͨµÀ*/
#define DAC_Channel_A 0x000000
#define DAC_Channel_B 0x010000
#define DAC_Channel_All 0X070000
//FOR POWER DOWN
#define NORMAL 0X00
#define K1Ohm 0X10
#define K100Ohm 0X20
#define ThreeState 0X30
#define PDACA 0X01
#define PDACB 0X02
typedef enum
{
AOUT1 = 0,
AOUT2 = 1,
} AOUT_NAME;
extern void IO_Initialize(void);
extern void Set_AOUT_Level(AOUT_NAME aout, uint16_t level);
extern void IO_Function_Test(void);
源文件io.c
#include <string.h>
#include "io.h"
#include "spi.h"
#include "usart.h"
/*Global variables*/
uint16_t Aout1, Aout2;
static void AD5663_PowerDown(void)
{
uint32_t inputShiftData = 0;
uint8_t SendSPIByte[3];
inputShiftData = Power_Down| NORMAL | PDACA | PDACB;
SendSPIByte[0] = inputShiftData>>16;
SendSPIByte[1] = inputShiftData>>8;
SendSPIByte[2] = inputShiftData;
HAL_GPIO_WritePin(DA_SYNC_GPIO_Port, DA_SYNC_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi3, SendSPIByte, sizeof(SendSPIByte), 100);
HAL_GPIO_WritePin(DA_SYNC_GPIO_Port, DA_SYNC_Pin, GPIO_PIN_SET);
}
void IO_Initialize(void)
{
AD5663_PowerDown();
}
void Set_AOUT_Level(AOUT_NAME aout, uint16_t level)
{
uint32_t inputShiftData = 0;
uint8_t SendSPIByte[3];
if(aout == AOUT1)
{
Aout1 = level;
inputShiftData = WriteTo_Update_DAC_Channel | DAC_Channel_A | level;
}
else if(aout == AOUT2)
{
Aout2 = level;
inputShiftData = WriteTo_Update_DAC_Channel | DAC_Channel_B | level;
}
SendSPIByte[0] = inputShiftData>>16;
SendSPIByte[1] = inputShiftData>>8;
SendSPIByte[2] = inputShiftData;
HAL_GPIO_WritePin(DA_SYNC_GPIO_Port, DA_SYNC_Pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi3, SendSPIByte, sizeof(SendSPIByte), 100);
HAL_GPIO_WritePin(DA_SYNC_GPIO_Port, DA_SYNC_Pin, GPIO_PIN_SET);
}
void IO_Function_Test(void)
{
Set_AOUT_Level(AOUT1, 0x8000);
Set_AOUT_Level(AOUT2, 0xEFFF);
}