21ic问答首页 - 为什么AD5328没有输出电压
为什么AD5328没有输出电压
伊小于2024-06-17
#ifndef __SPI1_H
#define __SPI1_H
#include "gd32f4xx.h"
/* 时钟使能宏定义 */
#define SPIx SPI1
#define SPIx_CLK_ENABLE() rcu_periph_clock_enable(RCU_SPI1)
#define SPIx_SCK_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
#define SPIx_MISO_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
#define SPIx_MOSI_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
#define SPIx_CS_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
#define LDAC_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
/* SPIx 引脚定义 */
#define SPIx_SCK_PIN GPIO_PIN_13
#define SPIx_SCK_GPIO_PORT GPIOB
#define SPIx_MOSI_PIN GPIO_PIN_15
#define SPIx_MOSI_GPIO_PORT GPIOB
#define AD5328_CS_PIN GPIO_PIN_12
#define AD5328_CS_GPIO_PORT GPIOB
// AD5328 的 LDAC 引脚
#define AD5328_LDAC_PIN GPIO_PIN_14
#define AD5328_LDAC_GPIO_PORT GPIOB
#define AD5328_LDAC_PIN GPIO_PIN_14
#define AD5328_LDAC_GPIO_PORT GPIOB
#define digitalHi(p,i) {gpio_bit_set(p, i);} // 设置为高电平
#define digitalLo(p,i) {gpio_bit_reset(p, i);} // 设置为低电平
#define SPI_AD5328_CS_LOW() digitalLo(AD5328_CS_GPIO_PORT, AD5328_CS_PIN)
#define SPI_AD5328_CS_HIGH() digitalHi(AD5328_CS_GPIO_PORT, AD5328_CS_PIN)
#define AD5328_LDAC_LOW() digitalLo(AD5328_LDAC_GPIO_PORT, AD5328_LDAC_PIN)
#define AD5328_LDAC_HIGH() digitalHi(AD5328_LDAC_GPIO_PORT, AD5328_LDAC_PIN)
void SPI1_Init(void);//SPI1初始化
void Write_Byte(unsigned int Data);//SPI1写数据
void SPI1_Write_Data(unsigned char Channel,unsigned int Tdata);//写数据
void AD5328_Init(); //AD5328初始化
void SPWM_Out_DAValue(unsigned char Channel,unsigned int Dim_Vlaue);
#endif /* __SPI1_H */
#include "gd32f4xx.h"
#include "main.h"
#include "systick.h"
unsigned int RST = 0xE000;
unsigned int Buff_SET = 0x8000;
unsigned int POWER_DOWN = 0xC000;
unsigned int LOCK_MODE_Updata = 0xA001;
void AD5328_Init(void)//AD5328初始化
{
Write_Byte(RST);//复位芯片
delay_1ms(1);//延时1ms
Write_Byte(Buff_SET); //设置缓冲模式
Write_Byte(POWER_DOWN);//关闭功耗模式
Write_Byte(LOCK_MODE_Updata);//单次更新模式
}
void SPI1_Init(void)//SPI1初始化
{
spi_parameter_struct spi_init_struct;//SPI参数结构体
/* 使能外设和GPIO时钟 */
SPIx_SCK_GPIO_CLK_ENABLE();
SPIx_MOSI_GPIO_CLK_ENABLE();
SPIx_CS_GPIO_CLK_ENABLE();
SPIx_CLK_ENABLE();
LDAC_GPIO_CLK_ENABLE();
/* 配置 SPI SCK GPIO 引脚 */
gpio_mode_set(SPIx_SCK_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, SPIx_SCK_PIN);
gpio_output_options_set(SPIx_SCK_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SPIx_SCK_PIN);
gpio_af_set(SPIx_SCK_GPIO_PORT, GPIO_AF_5, SPIx_SCK_PIN); // 设置为 AF5 用于 SPI1
/* 配置 SPI MOSI GPIO 引脚 */
gpio_mode_set(SPIx_MOSI_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, SPIx_MOSI_PIN);
gpio_output_options_set(SPIx_MOSI_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SPIx_MOSI_PIN);
gpio_af_set(SPIx_MOSI_GPIO_PORT, GPIO_AF_5, SPIx_MOSI_PIN); // 设置为 AF5 用于 SPI1
/* 配置 CS 引脚 */
gpio_mode_set(AD5328_CS_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, AD5328_CS_PIN);
gpio_output_options_set(AD5328_CS_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, AD5328_CS_PIN);
//gpio_af_set(AD5328_CS_GPIO_PORT, GPIO_AF_5, AD5328_CS_PIN); // 设置为 AF5 用于 SPI1
/* 配置 LDAC 引脚 */
gpio_mode_set(AD5328_LDAC_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, AD5328_LDAC_PIN);
gpio_output_options_set(AD5328_LDAC_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, AD5328_LDAC_PIN);
/* SPI 配置 */
spi_init_struct.trans_mode = SPI_TRANSMODE_BDTRANSMIT;//半双工模式
spi_init_struct.device_mode = SPI_MASTER;//主机模式
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;//8位帧
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;//时钟极性低,第二个边沿采样
spi_init_struct.nss = SPI_NSS_SOFT;//软件控制NSS
spi_init_struct.prescale = SPI_PSC_4;//预分频256
spi_init_struct.endian = SPI_ENDIAN_MSB;//高位在前
spi_init(SPIx, &spi_init_struct);
// spi_bidirectional_line_config()
spi_enable(SPIx);
}
unsigned char SPI_Buff[4] = {0};
void Write_Byte(unsigned int Data)//SPI1写数据
{
SPI_AD5328_CS_LOW();//NSS拉低
SPI_Buff[0] = (unsigned char)(Data>>8); //高位
SPI_Buff[1] = (unsigned char)(Data);//低位
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_TBE));// 等待发送缓冲区空闲
spi_i2s_data_transmit(SPIx, SPI_Buff[0]);
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_TBE));// 等待发送缓冲区空闲
spi_i2s_data_transmit(SPIx, SPI_Buff[1]);
SPI_AD5328_CS_HIGH();//NSS拉高
}
unsigned int sdata =0;
void SPI1_Write_Data(unsigned char Channel,unsigned int Tdata)//写入数据
{
SPI_AD5328_CS_LOW();//NSS拉低
sdata = (Channel<<12) | (Tdata & 0x0FFF);//设置数据位
sdata = sdata & 0x7FFF;//去掉最高位的1
SPI_Buff[0] = (unsigned char)(sdata>>8); //高位
SPI_Buff[1] = (unsigned char)(sdata);//低位
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_TBE));//等待空闲位
spi_i2s_data_transmit(SPIx, SPI_Buff[0]);
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_TBE));//等待空闲位
spi_i2s_data_transmit(SPIx, SPI_Buff[1]);
SPI_AD5328_CS_HIGH();//NSS拉高
}
unsigned int REF_AD_Value = 0;//AD采样值
unsigned int DA_Value = 0;//DAC输出值
double FDA_Value = 0;//DAC输出值(浮点数)
/////////////////////////////////
//Channel:通道选择,0~7
//Dim_Vlaue:亮度值,0~5000
////////////////////////////////
void SPWM_Out_DAValue(unsigned char Channel,unsigned int Dim_Vlaue)//SPWM输出DAC值
{
if(Dim_Vlaue <= 4998)//0-5V
{
FDA_Value = (double)Dim_Vlaue/5000;//DAC输出值(浮点数)
DA_Value = FDA_Value*4096;//DAC输出值(整数)
}else
{
DA_Value = 4095;//最大值
}
SPI1_Write_Data(Channel,Dim_Vlaue);//写入数据
}
#define __SPI1_H
#include "gd32f4xx.h"
/* 时钟使能宏定义 */
#define SPIx SPI1
#define SPIx_CLK_ENABLE() rcu_periph_clock_enable(RCU_SPI1)
#define SPIx_SCK_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
#define SPIx_MISO_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
#define SPIx_MOSI_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
#define SPIx_CS_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
#define LDAC_GPIO_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB)
/* SPIx 引脚定义 */
#define SPIx_SCK_PIN GPIO_PIN_13
#define SPIx_SCK_GPIO_PORT GPIOB
#define SPIx_MOSI_PIN GPIO_PIN_15
#define SPIx_MOSI_GPIO_PORT GPIOB
#define AD5328_CS_PIN GPIO_PIN_12
#define AD5328_CS_GPIO_PORT GPIOB
// AD5328 的 LDAC 引脚
#define AD5328_LDAC_PIN GPIO_PIN_14
#define AD5328_LDAC_GPIO_PORT GPIOB
#define AD5328_LDAC_PIN GPIO_PIN_14
#define AD5328_LDAC_GPIO_PORT GPIOB
#define digitalHi(p,i) {gpio_bit_set(p, i);} // 设置为高电平
#define digitalLo(p,i) {gpio_bit_reset(p, i);} // 设置为低电平
#define SPI_AD5328_CS_LOW() digitalLo(AD5328_CS_GPIO_PORT, AD5328_CS_PIN)
#define SPI_AD5328_CS_HIGH() digitalHi(AD5328_CS_GPIO_PORT, AD5328_CS_PIN)
#define AD5328_LDAC_LOW() digitalLo(AD5328_LDAC_GPIO_PORT, AD5328_LDAC_PIN)
#define AD5328_LDAC_HIGH() digitalHi(AD5328_LDAC_GPIO_PORT, AD5328_LDAC_PIN)
void SPI1_Init(void);//SPI1初始化
void Write_Byte(unsigned int Data);//SPI1写数据
void SPI1_Write_Data(unsigned char Channel,unsigned int Tdata);//写数据
void AD5328_Init(); //AD5328初始化
void SPWM_Out_DAValue(unsigned char Channel,unsigned int Dim_Vlaue);
#endif /* __SPI1_H */
#include "gd32f4xx.h"
#include "main.h"
#include "systick.h"
unsigned int RST = 0xE000;
unsigned int Buff_SET = 0x8000;
unsigned int POWER_DOWN = 0xC000;
unsigned int LOCK_MODE_Updata = 0xA001;
void AD5328_Init(void)//AD5328初始化
{
Write_Byte(RST);//复位芯片
delay_1ms(1);//延时1ms
Write_Byte(Buff_SET); //设置缓冲模式
Write_Byte(POWER_DOWN);//关闭功耗模式
Write_Byte(LOCK_MODE_Updata);//单次更新模式
}
void SPI1_Init(void)//SPI1初始化
{
spi_parameter_struct spi_init_struct;//SPI参数结构体
/* 使能外设和GPIO时钟 */
SPIx_SCK_GPIO_CLK_ENABLE();
SPIx_MOSI_GPIO_CLK_ENABLE();
SPIx_CS_GPIO_CLK_ENABLE();
SPIx_CLK_ENABLE();
LDAC_GPIO_CLK_ENABLE();
/* 配置 SPI SCK GPIO 引脚 */
gpio_mode_set(SPIx_SCK_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, SPIx_SCK_PIN);
gpio_output_options_set(SPIx_SCK_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SPIx_SCK_PIN);
gpio_af_set(SPIx_SCK_GPIO_PORT, GPIO_AF_5, SPIx_SCK_PIN); // 设置为 AF5 用于 SPI1
/* 配置 SPI MOSI GPIO 引脚 */
gpio_mode_set(SPIx_MOSI_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, SPIx_MOSI_PIN);
gpio_output_options_set(SPIx_MOSI_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, SPIx_MOSI_PIN);
gpio_af_set(SPIx_MOSI_GPIO_PORT, GPIO_AF_5, SPIx_MOSI_PIN); // 设置为 AF5 用于 SPI1
/* 配置 CS 引脚 */
gpio_mode_set(AD5328_CS_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, AD5328_CS_PIN);
gpio_output_options_set(AD5328_CS_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, AD5328_CS_PIN);
//gpio_af_set(AD5328_CS_GPIO_PORT, GPIO_AF_5, AD5328_CS_PIN); // 设置为 AF5 用于 SPI1
/* 配置 LDAC 引脚 */
gpio_mode_set(AD5328_LDAC_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, AD5328_LDAC_PIN);
gpio_output_options_set(AD5328_LDAC_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, AD5328_LDAC_PIN);
/* SPI 配置 */
spi_init_struct.trans_mode = SPI_TRANSMODE_BDTRANSMIT;//半双工模式
spi_init_struct.device_mode = SPI_MASTER;//主机模式
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;//8位帧
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;//时钟极性低,第二个边沿采样
spi_init_struct.nss = SPI_NSS_SOFT;//软件控制NSS
spi_init_struct.prescale = SPI_PSC_4;//预分频256
spi_init_struct.endian = SPI_ENDIAN_MSB;//高位在前
spi_init(SPIx, &spi_init_struct);
// spi_bidirectional_line_config()
spi_enable(SPIx);
}
unsigned char SPI_Buff[4] = {0};
void Write_Byte(unsigned int Data)//SPI1写数据
{
SPI_AD5328_CS_LOW();//NSS拉低
SPI_Buff[0] = (unsigned char)(Data>>8); //高位
SPI_Buff[1] = (unsigned char)(Data);//低位
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_TBE));// 等待发送缓冲区空闲
spi_i2s_data_transmit(SPIx, SPI_Buff[0]);
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_TBE));// 等待发送缓冲区空闲
spi_i2s_data_transmit(SPIx, SPI_Buff[1]);
SPI_AD5328_CS_HIGH();//NSS拉高
}
unsigned int sdata =0;
void SPI1_Write_Data(unsigned char Channel,unsigned int Tdata)//写入数据
{
SPI_AD5328_CS_LOW();//NSS拉低
sdata = (Channel<<12) | (Tdata & 0x0FFF);//设置数据位
sdata = sdata & 0x7FFF;//去掉最高位的1
SPI_Buff[0] = (unsigned char)(sdata>>8); //高位
SPI_Buff[1] = (unsigned char)(sdata);//低位
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_TBE));//等待空闲位
spi_i2s_data_transmit(SPIx, SPI_Buff[0]);
while (RESET == spi_i2s_flag_get(SPIx, SPI_FLAG_TBE));//等待空闲位
spi_i2s_data_transmit(SPIx, SPI_Buff[1]);
SPI_AD5328_CS_HIGH();//NSS拉高
}
unsigned int REF_AD_Value = 0;//AD采样值
unsigned int DA_Value = 0;//DAC输出值
double FDA_Value = 0;//DAC输出值(浮点数)
/////////////////////////////////
//Channel:通道选择,0~7
//Dim_Vlaue:亮度值,0~5000
////////////////////////////////
void SPWM_Out_DAValue(unsigned char Channel,unsigned int Dim_Vlaue)//SPWM输出DAC值
{
if(Dim_Vlaue <= 4998)//0-5V
{
FDA_Value = (double)Dim_Vlaue/5000;//DAC输出值(浮点数)
DA_Value = FDA_Value*4096;//DAC输出值(整数)
}else
{
DA_Value = 4095;//最大值
}
SPI1_Write_Data(Channel,Dim_Vlaue);//写入数据
}
赞1
评论
2024-06-21
硬件也没有把mosl硬件下拉,然后我读文档也没有注意,所以出问题 加一句: gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, GPIO_PIN_14);
我已经调试出来了,MOSI要下拉,不下拉的话,就无法通讯
您需要登录后才可以回复 登录 | 注册