集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 3368|回复: 8

如何用VERILOG实现串-并转换?请各位达人指教!

[复制链接]
tim 发表于 2010-6-28 00:02:24 | 显示全部楼层 |阅读模式
本帖最后由 fpgaw 于 2010-7-3 06:37 编辑

如何用VERILOG实现串-并转换?请各位达人指教!
用FPGA实现л/4DQPSK的调制解调,这是我第一次做项目,不会的很多,请大家帮忙,感激不尽!
CHA 发表于 2010-6-28 01:50:24 | 显示全部楼层
串并转换用一个任务实现,然后在通过在always里调用任务;如把一串数据转换位一个8位并行数据输出:注意代码如下<br>
...<br>
reg&nbsp; &nbsp;serial; //存储串行数据的寄存器<br>
reg&nbsp; &nbsp;[7:0]&nbsp; &nbsp;parallel;&nbsp;&nbsp;//并行数据的输出寄存器<br>
reg&nbsp; &nbsp;[8:0]&nbsp;&nbsp;shift_state;&nbsp;&nbsp;//状态机变量<br>
parameter&nbsp; &nbsp;shift_bit7 = 9'b000000001,<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_bit6 = 9'b000000010,<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_bit5 = 9'b000000100,<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_bit4 = 9'b000001000, <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_bit3 = 9'b000010000, <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_bit2 = 9'b000100000,<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_bit1 = 9'b001000000,<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_bit0 = 9'b010000000,<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_end = 9'b100000000;<br>
<br>
always&nbsp;&nbsp;@ (posedge clk)<br>
begin<br>
&nbsp; &nbsp;shift_state&nbsp;&nbsp;&lt;=&nbsp;&nbsp;shift_bit7; //初始化移位状态变量,使其有一个初状态<br>
&nbsp; &nbsp;if(...) <br>
&nbsp; &nbsp;shift;&nbsp; &nbsp;//在条件成立的时候调用任务&nbsp;&nbsp;<br>
end<br>
<br>
//定义串并移位的任务<br>
task&nbsp; &nbsp;shift;<br>
begin<br>
&nbsp; &nbsp;&nbsp; &nbsp; case(shift_state)<br>
&nbsp; &nbsp;&nbsp; &nbsp; shift_bit7: begin&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;parallel[7]&nbsp;&nbsp;&lt;= serial;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_state &lt;= shift_bit6; <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp; shift_bit6: begin&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;parallel[6]&nbsp;&nbsp;&lt;= serial;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_state &lt;= shift_bit5; <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp; shift_bit5: begin&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;parallel[5]&nbsp;&nbsp;&lt;= serial;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_state &lt;= shift_bit4; <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp; shift_bit4: begin&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;parallel[4]&nbsp;&nbsp;&lt;= serial;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_state &lt;= shift_bit3; <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp; shift_bit3: begin&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;parallel[3]&nbsp;&nbsp;&lt;= serial;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_state &lt;= shift_bit2; <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp; shift_bit2: begin&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;parallel[2]&nbsp;&nbsp;&lt;= serial;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_state &lt;= shift_bit1; <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp; shift_bit1: begin&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;parallel[1]&nbsp;&nbsp;&lt;= serial;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_state &lt;= shift_bit0; <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp; shift_bit0: begin&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;parallel[0]&nbsp;&nbsp;&lt;= serial;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;shift_state &lt;= shift_end; <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp; shift_end: shift_state &lt;=&nbsp;&nbsp;shift_end;<br>
&nbsp; &nbsp;&nbsp; &nbsp; default:shift_state &lt;= shift_end;<br>
&nbsp; &nbsp;&nbsp; &nbsp; endcase<br>
end<br>
endtask
CHANG 发表于 2010-6-28 03:25:01 | 显示全部楼层
如果只是2位的输出的话,有没有比较简单的方法?
VVIC 发表于 2010-6-28 03:57:10 | 显示全部楼层
两位的话直接在alwas里用一个状态变量为两位的状态机实现就可,不用单独写任务也可以吧<br>
任务就是实现某种功能,然后调用,这样程序更有条理,个人理解
AAT 发表于 2010-6-28 05:08:27 | 显示全部楼层
真的很谢谢你的热心帮助!<br>
如果这样的话&nbsp;&nbsp;能否实现:<br>
input&nbsp; &nbsp;in;&nbsp; &nbsp;&nbsp;&nbsp;//定义输入是1位的数据<br>
output [1:0] out;&nbsp; &nbsp;//定义输出是2位的数据<br>
reg [1:0] out;<br>
....<br>
out={out,in};&nbsp; &nbsp;//使用拼接符
CHA 发表于 2010-6-28 05:53:09 | 显示全部楼层
串行数据是每个时钟下只有一个数据,要用位拼接,就要把前一个时钟的数据保存一个时钟然后再和后一个时钟的数据一起用位拼接输出吧<br>
input&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; rst_n;<br>
input&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; in;<br>
output&nbsp;&nbsp;[1:0] out;<br>
reg&nbsp; &nbsp;&nbsp; &nbsp; [1:0] out;<br>
reg&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; in_reg;&nbsp;&nbsp;//锁存in的寄存器<br>
reg&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; cnt;&nbsp; &nbsp;&nbsp; &nbsp; //一位计数器<br>
<br>
always&nbsp;&nbsp;@ (posedge clk or negedge&nbsp;&nbsp;rst_n)<br>
begin&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if(!rst_n)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cnt&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&lt;= 1'b0;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;in_reg&nbsp;&nbsp;&lt;= 1'b0; <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;out&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&lt;= 2'b00;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;else&nbsp;&nbsp;if(cnt == 1'b1)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cnt&nbsp;&nbsp;&lt;= 1'b0;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;out&nbsp;&nbsp;&lt;= {in_reg,in}; //当两个时钟后用位拼接方式并行输出一次<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;end<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; else<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;cnt &lt;= cnt + 1'b1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;in_reg &lt;= in;&nbsp; &nbsp;//对in锁存一个时钟<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;end<br>
end
ICE 发表于 2010-6-28 06:32:04 | 显示全部楼层
太好了,找了很久才找到的东西,谢谢
inter 发表于 2010-6-28 07:11:57 | 显示全部楼层
学习学习<br>
不错
VVIC 发表于 2010-6-28 08:05:23 | 显示全部楼层
3x 谢谢
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|手机版|Archiver|fpga论坛|fpga设计论坛 ( 京ICP备20003123号-1 )

GMT+8, 2024-12-24 10:41 , Processed in 0.078238 second(s), 24 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表