PS:Visio-更多形状-工程-电气工程-模拟和数字逻辑-信号波形 里面有正弦波、方波等信号波形。
(1)key_ctrl部分Verilog代码:
module key_ctrl
(
input clk ,
input reset_n ,
input [3:0] key ,
output reg [3:0] wave_selcet
);
wire key_p_sin;
wire key_p_squ;
wire key_p_tri;
wire key_p_saw;
key_filter key_filter_inst0(
.clk (clk ),
.reset_n (reset_n ),
.key_in (key[0] ),
.key_p_flag (key_p_saw ),
.key_r_flag (),
.key_state ()
);
key_filter key_filter_inst1(
.clk (clk ),
.reset_n (reset_n ),
.key_in (key[1] ),
.key_p_flag (key_p_tri ),
.key_r_flag (),
.key_state ()
);
key_filter key_filter_inst2(
.clk (clk ),
.reset_n (reset_n ),
.key_in (key[2] ),
.key_p_flag (key_p_squ ),
.key_r_flag (),
.key_state ()
);
key_filter key_filter_inst3(
.clk (clk ),
.reset_n (reset_n ),
.key_in (key[3] ),
.key_p_flag (key_p_sin ),
.key_r_flag (),
.key_state ()
);
always@(posedge clk or negedge reset_n)
if(!reset_n)
wave_selcet <= 4'b1000;
else if(key_p_sin)
wave_selcet <= 4'b1000;
else if(key_p_squ)
wave_selcet <= 4'b0100;
else if(key_p_tri)
wave_selcet <= 4'b0010;
else if(key_p_saw)
wave_selcet <= 4'b0001;
else
wave_selcet <= wave_selcet;
endmodule
(2)仿真代码:
`timescale 1ns / 1ps
module key_ctrl_tb;
reg clk ;
reg reset_n ;
reg [3:0]key ;
wire [3:0]wave_selcet ;
initial clk = 1'd1;
always#10 clk = ~clk;
initial begin
reset_n <= 1'd0;
key <= 4'd1111;
#15;
reset_n <= 1'd1;
#50000;
key <= 4'b1110;
#50000;
key <= 4'b1111;
#50000;
key <= 4'b1101;
#50000;
key <= 4'b1111;
#50000;
key <= 4'b1011;
#50000;
key <= 4'b1111;
#50000;
key <= 4'b0111;
#50000;
key <= 4'b1111;
#50000;
$stop;
end
key_ctrl key_ctrl_inst
(
.clk (clk ),
.reset_n (reset_n ),
.key (key ),
.wave_selcet (wave_selcet)
);
defparam key_ctrl_inst.key_filter_inst0.MCNT = 99;
defparam key_ctrl_inst.key_filter_inst1.MCNT = 99;
defparam key_ctrl_inst.key_filter_inst2.MCNT = 99;
defparam key_ctrl_inst.key_filter_inst3.MCNT = 99;
endmodule
(3)仿真波形:
(4)ROM配置过程:
例化模板:
附上生成coe文件的matlab源码(来自野火):
Matlab
clc; %清除命令行命令
clear all; %清除工作区变量,释放内存空间
F1=1; %信号频率
Fs=2^12; %采样频率
P1=0; %信号初始相位
N=2^12; %采样点数
t=[0:1/Fs:(N-1)/Fs]; %采样时刻
ADC=2^7 - 1; %直流分量
A=2^7; %信号幅度
s1=A*sin(2*pi*F1*t + pi*P1/180) + ADC; %正弦波信号
s2=A*square(2*pi*F1*t + pi*P1/180) + ADC; %方波信号
s3=A*sawtooth(2*pi*F1*t + pi*P1/180,0.5) + ADC; %三角波信号
s4=A*sawtooth(2*pi*F1*t + pi*P1/180) + ADC; %锯齿波信号
%创建coe文件
fild = fopen('wave_16384x8.coe','wt');
%写入coe文件头
fprintf(fild, '%s\n','MEMORY_INITIALIZATION_RADIX=10;'); %10进制数
fprintf(fild, '%s\n','MEMORY_INITIALIZATION_VECTOR=');
for j = 1:4
for i = 1:N
if j == 1 %打印正弦信号数据
s0(i) = round(s1(i)); %对小数四舍五入以取整
end
if j == 2 %打印方波信号数据
s0(i) = round(s2(i)); %对小数四舍五入以取整
end
if j == 3 %打印三角波信号数据
s0(i) = round(s3(i)); %对小数四舍五入以取整
end
if j == 4 %打印锯齿波信号数据
s0(i) = round(s4(i)); %对小数四舍五入以取整
end
if s0(i) <0 %负1强制置零
s0(i) = 0
end
if j == 4 && i == N
fprintf(fild, '%d',s0(i)); %数据写入
fprintf(fild, '%s',';'); %最后一个数使用分号结束
else
fprintf(fild, '%d',s0(i)); %数据写入
fprintf(fild, '%s\n',','); %逗号,换行
end
end
end
fclose(fild);