STM32F103 使用并口通讯AD9851输出正弦波,方波,三角波,脉冲波
时间: 2023-07-11 10:25:27 浏览: 227
AD9851 是一款数字频率合成器,能够通过并口通讯控制输出正弦波、方波、三角波和脉冲波等多种波形。下面是使用 STM32F103 控制 AD9851 输出这些波形的示例程序:
```c
#include "stm32f10x.h"
#define AD9851_W_CLK_GPIO GPIOB
#define AD9851_W_CLK_PIN GPIO_Pin_12
#define AD9851_FQ_UD_GPIO GPIOB
#define AD9851_FQ_UD_PIN GPIO_Pin_13
#define AD9851_DATA_GPIO GPIOB
#define AD9851_DATA_PIN GPIO_Pin_15
#define AD9851_RESET_GPIO GPIOB
#define AD9851_RESET_PIN GPIO_Pin_14
#define AD9851_W_CLK_SET() GPIO_SetBits(AD9851_W_CLK_GPIO, AD9851_W_CLK_PIN)
#define AD9851_W_CLK_CLR() GPIO_ResetBits(AD9851_W_CLK_GPIO, AD9851_W_CLK_PIN)
#define AD9851_FQ_UD_SET() GPIO_SetBits(AD9851_FQ_UD_GPIO, AD9851_FQ_UD_PIN)
#define AD9851_FQ_UD_CLR() GPIO_ResetBits(AD9851_FQ_UD_GPIO, AD9851_FQ_UD_PIN)
#define AD9851_DATA_SET() GPIO_SetBits(AD9851_DATA_GPIO, AD9851_DATA_PIN)
#define AD9851_DATA_CLR() GPIO_ResetBits(AD9851_DATA_GPIO, AD9851_DATA_PIN)
#define AD9851_RESET_SET() GPIO_SetBits(AD9851_RESET_GPIO, AD9851_RESET_PIN)
#define AD9851_RESET_CLR() GPIO_ResetBits(AD9851_RESET_GPIO, AD9851_RESET_PIN)
// AD9851 输出波形类型
typedef enum {
AD9851_WAVE_SINE = 0x00,
AD9851_WAVE_TRIANGLE= 0x02,
AD9851_WAVE_SQUARE = 0x04,
AD9851_WAVE_PULSE = 0x06
} AD9851_WaveType;
// 向 AD9851 发送一帧数据
void AD9851_Send(uint32_t data) {
uint8_t i;
for (i = 0; i < 32; i++) {
if (data & 0x00000001) {
AD9851_DATA_SET();
} else {
AD9851_DATA_CLR();
}
AD9851_W_CLK_SET();
AD9851_W_CLK_CLR();
data >>= 1;
}
}
// 初始化 AD9851
void AD9851_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = AD9851_W_CLK_PIN | AD9851_FQ_UD_PIN | AD9851_DATA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = AD9851_RESET_PIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
AD9851_W_CLK_CLR();
AD9851_FQ_UD_CLR();
AD9851_DATA_CLR();
AD9851_RESET_SET();
AD9851_RESET_CLR();
}
// 设置 AD9851 输出频率
void AD9851_SetFreq(uint32_t freq) {
uint32_t phase = 0;
uint64_t data = 0;
// 计算相位步进值
phase = (uint32_t)((double)0xFFFFFFFF * ((double)freq / 125000000.0));
data = ((uint64_t)phase << 2) | 0x20000000; // 0x20000000 表示幅度为满幅度
// 发送频率设置帧
AD9851_FQ_UD_CLR();
AD9851_Send(data >> 24);
AD9851_Send(data >> 16);
AD9851_Send(data >> 8);
AD9851_Send(data >> 0);
AD9851_FQ_UD_SET();
}
// 设置 AD9851 输出波形
void AD9851_SetWave(AD9851_WaveType wave) {
uint64_t data = 0;
// 发送波形设置帧
AD9851_FQ_UD_CLR();
data = ((uint64_t)wave << 8) | 0x40000000; // 0x40000000 表示频率不变
AD9851_Send(data >> 24);
AD9851_Send(data >> 16);
AD9851_Send(data >> 8);
AD9851_Send(data >> 0);
AD9851_FQ_UD_SET();
}
int main(void) {
// 初始化系统时钟和 IO 端口
SystemInit();
AD9851_Init();
// 设置输出频率和波形
AD9851_SetFreq(1000000); // 设置输出频率为 1MHz
AD9851_SetWave(AD9851_WAVE_SINE); // 设置输出波形为正弦波
while (1) {
// 循环执行
}
}
```
注意,在实际使用中需要根据自己的硬件电路和 AD9851 的特性进行适当调整。例如,AD9851 的时钟信号需要在 30MHz 左右,需要使用外部晶振或者 PLL 使系统时钟提高到这个频率。同时,也需要根据 AD9851 的特性设置相应的输出幅度和增益等参数。
阅读全文