FPAG IP核调用小练习

一、调用步骤

1、打开Quartus

右上角搜索ROM,如图所示

2、点击后会弹出如图所示

其中文件路径需要选择你自己的

3、点击OK弹出如图所示

图中红色改为12与1024

4、然后一直点NEXT,直到下图

这里要选择后缀为 .mif的文件

5、用C语言生成 .mif文件

c 复制代码
// 生成1024点正弦波表(C程序)
#include <stdio.h>
#include <math.h>

#define PI 3.14159265358979323846
#define DEPTH 1024  // ROM深度
#define WIDTH 12    // 数据宽度(12位)

int main() {
    FILE *fp = fopen("sine_rom.mif", "w");
    if (!fp) return -1;
    fprintf(fp, "WIDTH=%d;\nDEPTH=%d;\nADDRESS_RADIX=HEX;\nDATA_RADIX=HEX;\nCONTENT BEGIN\n", WIDTH, DEPTH);
    for (int i = 0; i < DEPTH; i++) {
        float phase = (float)i / DEPTH * 2 * PI;
        short value = (short)(sin(phase) * (pow(2, WIDTH-1) - 1));
        fprintf(fp, "%03x : %03x;\n", i, value & 0xFFF);
    }
    fprintf(fp, "END;\n");
    fclose(fp);
    return 0;
}

然后运行.c文件,会在文件所在目录下生成一个.mif文件,然后你就在第4步中选择它就行。

然后一直next,直到finish

二 、核心模块

1、相位累加器

bash 复制代码
module phase_accumulator (
    input wire clk,          // 50MHz系统时钟
    input wire [23:0] K,     // 频率控制字(24位)
    output reg [23:0] phase  // 相位累加值
);
    always @(posedge clk) begin
        phase <= phase + K;  // 相位累加
    end
endmodule

2、波形选择器

bash 复制代码
module waveform_selector (
    input wire [23:0] phase, // 相位地址(24位)
    input wire sel,          // 波形选择(0:正弦,1:方波)
    output reg [11:0] data_out
);
    reg [11:0] sine_data;    // 正弦波数据
    reg [11:0] square_data;  // 方波数据

    // 正弦ROM
    reg [11:0] sine_rom [0:1023];
    initial begin
        $readmemh("sine_rom.mif", sine_rom);
    end
    assign sine_data = sine_rom[phase[22:12]]; // 取中间12位地址

    // 方波ROM
    reg [11:0] square_rom [0:1023];
    initial begin
        $readmemh("square_rom.mif", square_rom);
    end
    assign square_data = square_rom[phase[22:12]];

    always @(*) begin
        if (sel) data_out = square_data;
        else     data_out = sine_data;
    end
endmodule

3、时钟分频器

bash 复制代码
module clock_divider (
    input wire clk_in,    // 50MHz输入时钟
    output reg clk_out    // 分频后的时钟(10MHz)
);
    reg [1:0] cnt;
    always @(posedge clk_in) begin
        cnt <= cnt + 1;
        if (cnt == 2'b11) // 分频系数为4(50MHz → 12.5MHz),或调整为5(10MHz)
            clk_out <= ~clk_out;
    end
endmodule

4、顶层模块

bash 复制代码
module dds_top (
    input wire clk_50m,    // 系统时钟(50MHz)
    input wire [23:0] K,   // 频率控制字(来自拨码开关)
    input wire sel,        // 波形选择(按钮控制)
    output reg [11:0] dac_data // DAC数据输出
);
    wire [23:0] phase;     // 相位累加值
    wire clk_out;          // 分频后的时钟

    // 相位累加器
    phase_accumulator phase_acc (
        .clk(clk_50m),
        .K(K),
        .phase(phase)
    );

    // 波形选择器
    waveform_selector wave_sel (
        .phase(phase),
        .sel(sel),
        .data_out(dac_data)
    );

    // 时钟分频(可选,用于DAC时钟)
    clock_divider clk_div (
        .clk_in(clk_50m),
        .clk_out(clk_out) // 连接到DAC时钟输入
    );
endmodule

5、波形图

参考博客https://blog.csdn.net/weixin_50722839/article/details/109960391

相关推荐
1750633194510 小时前
Vivado Zynq7020 生成正弦波(查表法) + 行为级仿真
fpga开发
Terasic友晶科技10 小时前
4-DE10-Nano的HDMI方块移动案例——I2C通信协议
fpga开发·i2c·hdmi·de10-nano·i2c通信协议
云雾J视界21 小时前
FPGA在AI时代的角色重塑:硬件可重构性与异构计算的完美结合
fpga开发·边缘计算·gpu·vitis·ai推理·azure云·异构编程
s09071362 天前
FPGA中CIC设计注意事项
算法·fpga开发·cic滤波器
Aaron15882 天前
RFSOC+VU13P在无线信道模拟中的技术应用分析
数据结构·人工智能·算法·fpga开发·硬件架构·硬件工程·射频工程
碎碎思2 天前
BerkeleyLab Bedrock:为 FPGA 与加速计算打造的开源基石
fpga开发·开源
zidan14122 天前
xilinx常用文档说明
fpga开发
ShiMetaPi2 天前
GM-3568JHF丨ARM+FPGA异构开发板系列教程:外设教程 04 WIFI
网络·arm开发·fpga开发·智能路由器·fpga
FPGA_小田老师2 天前
FPGA基础知识(二十):Xilinx Block Memory IP核(5)--ROM 详解
fpga开发·rom·coe文件格式·导入coe·block memory
FPGA_无线通信2 天前
压缩解压缩算法 BFP-8bit
fpga开发