赞
踩
链接: ad9833_亚德诺半导体网站.(链接中有官方驱动代码)
控制方式:SPI
SPI_HandleTypeDef SPI5_Handler; // SPI句柄 /*spi5读写函数*/ u8 SPI5_ReadWriteByte(u8 dat) { u8 Rxdata; HAL_SPI_TransmitReceive(&SPI5_Handler, &dat, &Rxdata, 1, 1000); return Rxdata; // 返回收到的数据 } void SPI5_Init(void) { SPI5_Handler.Instance = SPI5; // SP5 SPI5_Handler.Init.Mode = SPI_MODE_MASTER; // 设置SPI工作模式,设置为主模式 SPI5_Handler.Init.Direction = SPI_DIRECTION_2LINES; // 设置SPI单向或者双向的数据模式:SPI设置为双线模式 SPI5_Handler.Init.DataSize = SPI_DATASIZE_8BIT; // 设置SPI的数据大小:SPI发送接收8位帧结构 SPI5_Handler.Init.CLKPolarity = SPI_POLARITY_HIGH; // 串行同步时钟的空闲状态为高电平 SPI5_Handler.Init.CLKPhase = SPI_PHASE_1EDGE; // 串行同步时钟的第1个跳变沿(上升或下降)数据被采样 SPI5_Handler.Init.NSS = SPI_NSS_SOFT; // NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制 SPI5_Handler.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128; // 定义波特率预分频的值:波特率预分频值为256 SPI5_Handler.Init.FirstBit = SPI_FIRSTBIT_MSB; // 指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始 SPI5_Handler.Init.CRCPolynomial = 7; // CRC值计算的多项式 HAL_SPI_Init(&SPI5_Handler); // 初始化 __HAL_SPI_ENABLE(&SPI5_Handler); // 使能SPI5 SPI5_ReadWriteByte(0xff); } //----------------------------------------------------------------- // // 函数功能: SPI5底层驱动,时钟使能,引脚配置 // 入口参数: SPI_HandleTypeDef *hspi:SPI句柄 // 返 回 值: 无 // 注意事项: 此函数会被HAL_SPI_Init()调用 // //----------------------------------------------------------------- void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) { GPIO_InitTypeDef GPIO_Initure; __HAL_RCC_GPIOF_CLK_ENABLE(); // 使能GPIOF时钟 __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_SPI5_CLK_ENABLE(); // 使能SPI5时钟 // PF7->SCLK, PF8->MISO, PF9->MOSI GPIO_Initure.Pin = GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9; GPIO_Initure.Mode = GPIO_MODE_AF_PP; // 复用推挽输出 GPIO_Initure.Pull = GPIO_PULLUP; // 上拉 GPIO_Initure.Speed = GPIO_SPEED_HIGH; // 高速 GPIO_Initure.Alternate = GPIO_AF5_SPI5; // 复用为SPI5 HAL_GPIO_Init(GPIOF, &GPIO_Initure); // 初始化 GPIO_Initure.Pin = GPIO_PIN_15; //SPI_NSS GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP; //推挽输出 GPIO_Initure.Pull = GPIO_PULLUP; // 上拉 GPIO_Initure.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_Initure); } //----------------------------------------------------------------- // void SPI5_SetSpeed(u8 SPI_BaudRatePrescaler) //----------------------------------------------------------------- // // 函数功能: SPI速度设置函数 // 入口参数: u8 SPI_BaudRatePrescaler:SPI_BAUDRATEPRESCALER_2~SPI_BAUDRATEPRESCALER_2 256 // 返 回 值: 无 // 注意事项: SPI速度=fAPB1/分频系数,fAPB1时钟一般为45Mhz // //----------------------------------------------------------------- void SPI5_SetSpeed(u8 SPI_BaudRatePrescaler) { assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler)); // 判断有效性 __HAL_SPI_DISABLE(&SPI5_Handler); // 关闭SPI SPI5_Handler.Instance->CR1 &= 0XFFC7; // 位3-5清零,用来设置波特率 SPI5_Handler.Instance->CR1 |= SPI_BaudRatePrescaler; // 设置SPI速度 __HAL_SPI_ENABLE(&SPI5_Handler); // 使能SPI }
说明:
PF7->SCLK,PF8->MISO,PF9->MOSI,PB15->SPI_NSS。
MISO是主机输入从机输出(AD9833没有用到),MOSI是主机输出从机输入(AD9833的data口)
ad9833头文件
#ifndef _AD9833_H_ #define _AD9833_H_ /******************************************************************************/ /* Include Files */ /******************************************************************************/ #include "common.h" //公共头文件 #define AD9833_FSYNC PBout(15)//这个是NSS管脚 /******************************************************************************/ /* AD9833 */ /******************************************************************************/ /* Registers */ #define AD9833_REG_CMD (0 << 14) #define AD9833_REG_FREQ0 (1 << 14) #define AD9833_REG_FREQ1 (2 << 14) #define AD9833_REG_PHASE0 (6 << 13) #define AD9833_REG_PHASE1 (7 << 13) /* Command Control Bits */ #define AD9833_B28 (1 << 13) #define AD9833_HLB (1 << 12) #define AD9833_FSEL0 (0 << 11) #define AD9833_FSEL1 (1 << 11) #define AD9833_PSEL0 (0 << 10) #define AD9833_PSEL1 (1 << 10) #define AD9833_PIN_SW (1 << 9) #define AD9833_RESET (1 << 8) #define AD9833_SLEEP1 (1 << 7) #define AD9833_SLEEP12 (1 << 6) #define AD9833_OPBITEN (1 << 5) #define AD9833_SIGN_PIB (1 << 4) #define AD9833_DIV2 (1 << 3) #define AD9833_MODE (1 << 1) #define AD9833_OUT_SINUS ((0 << 5) | (0 << 1) | (0 << 3)) #define AD9833_OUT_TRIANGLE ((0 << 5) | (1 << 1) | (0 << 3)) #define AD9833_OUT_MSB ((1 << 5) | (0 << 1) | (1 << 3)) #define AD9833_OUT_MSB2 ((1 << 5) | (0 << 1) | (0 << 3)) /******************************************************************************/ /* Functions Prototypes */ /******************************************************************************/ /* Initializes the SPI communication peripheral and resets the part. */ unsigned char AD9833_Init(void); /* Sets the Reset bit of the AD9833. */ void AD9833_Reset(void); /* Clears the Reset bit of the AD9833. */ void AD9833_ClearReset(void); /* Writes the value to a register. */ void AD9833_SetRegisterValue(u16 regValue); /* Writes to the frequency registers. */ void AD9833_SetFrequency(u16 reg,float fout); /* Writes to the phase registers. */ void AD9833_SetPhase(unsigned short reg, unsigned short val); /* Selects the Frequency,Phase and Waveform type. */ void AD9833_Setup(unsigned short freq,unsigned short phase,unsigned short type); void AD9833_SetFrequencyQuick(float fout,unsigned short type); unsigned char AD9833_SPI_Write(unsigned char* data,unsigned char bytesNumber); #endif
AD9833驱动代码
#include "ad9833.h" #include "spi.h" #include "delay.h" #define FCLK 25000000 //设置晶振频率 #define RealFreDat (268435456.0/FCLK) //F=FCLK/2^28; /********往ad9833写******************************/ unsigned char AD9833_SPI_Write(unsigned char* data,unsigned char bytesNumber) { unsigned char i =0; unsigned char writeData[5] = {0,0, 0, 0, 0}; AD9833_FSYNC =0; for(i = 0;i < bytesNumber;i ++) { writeData[i] = data[i + 1]; } for(i=0 ;i<bytesNumber ;i++) { SPI5_ReadWriteByte(writeData[i]); } AD9833_FSYNC = 1; delay_us(5); return i; } //----------------------------------------------------- // void AD9833_SetRegisterValue(unsigned short regValue) // 寄存器写入函数 // regValue:16位数据 //----------------------------------------------------- void AD9833_SetRegisterValue(unsigned short regValue) { unsigned char data[5] = {0x03, 0x00, 0x00}; data[1] = (unsigned char)((regValue & 0xFF00) >> 8); data[2] = (unsigned char)((regValue & 0x00FF) >> 0); AD9833_SPI_Write(data,2); } //----------------------------------------------------- // void AD9833_SetFrequency(unsigned short reg, float fout) // 频率寄存器写入函数 // reg:寄存器选项 // fout:频率 //----------------------------------------------------- void AD9833_SetFrequency(unsigned short reg, float fout) { unsigned short freqHi = reg; unsigned short freqLo = reg; unsigned long val=RealFreDat*fout;//F寄存器的值 freqHi |= (val & 0xFFFC000) >> 14 ; freqLo |= (val & 0x3FFF); AD9833_SetRegisterValue(AD9833_B28); AD9833_SetRegisterValue(freqLo); AD9833_SetRegisterValue(freqHi); } //----------------------------------------------------- // void AD9833_SetFrequency(unsigned short reg, float fout) // 相位寄存器写入函数 // reg:寄存器选项 // val:16位的相位数据,P=2π/2^16*val //----------------------------------------------------- void AD9833_SetPhase(unsigned short reg, unsigned short val) { unsigned short phase = reg; phase |= val; AD9833_SetRegisterValue(phase); } //----------------------------------------------------- // void AD9833_SetFrequencyQuick(float fout,unsigned short type) // 应用函数 // fout:频率 // type:输出模式选项 //----------------------------------------------------- void AD9833_SetFrequencyQuick(float fout,unsigned short type) { AD9833_SetFrequency(AD9833_REG_FREQ0, fout); AD9833_Setup(AD9833_FSEL0, AD9833_PSEL0, type); } unsigned char AD9833_Init(void) { SPI5_Init(); AD9833_SetRegisterValue(AD9833_REG_CMD | AD9833_RESET); return (1); } //----------------------------------------------------- // void AD9833_Setup(unsigned short freq,unsigned short phase,unsigned short type) // 功能函数 // freq:输出所需频率寄存器 // phase:输出所需相位寄存器 // type:输出模式 //----------------------------------------------------- void AD9833_Setup(unsigned short freq,unsigned short phase,unsigned short type) { unsigned short val = 0; val = freq | phase | type; AD9833_SetRegisterValue(val); }
说明:
宏定义的FCLK是AD9833模块上所提共的频率(一般是模块上晶振的频率),读者应该根据自己所用的模块进行修改。
main.c
#include "ad9833.h"
AD9833_Init();
AD9833_SetFrequencyQuick(500000,AD9833_OUT_SINUS);//频率,波形
参考文章:
链接: https://blog.csdn.net/weixin_43141547/article/details/106452650.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。