RS232串口通信实验Verilog

独孤求真 posted @ 2010年9月21日 20:24 in CPLD,FPGA学习 with tags RS232;串口;Verilog; , 17487 阅读

RS232串口通信实验几乎是入门必做实验,这是本人学习过程中做的练习。首先是分频模块,分频模块是学习了OpenCore上的uart2bus项目的。http://opencores.org/project,uart2bus
该模块接收任意频率的输入频率(clk_i),输出频率(记为clk_o)由baud_freq_i和baud_limit_i根据以下公式计算,使用时首先需要根据输入频率和输出频率计算出baud_freq_i和baud_limit_i这两个参数。

[tex] baud\_freq = \frac{clk\_o}{GCD(clk\_i, clk\_o)}[/tex]

[tex] baud\_limit = \frac{clk\_i}{GCD(clk\_i, clk\_o)} - baud\_freq [/tex]

公式里的GCD(Greatest Common Divisor)表示取2个数的最大公约数。

这里系统时钟频率为50MHz,串口频率为9600,通常我们是在中间进行采样。因此分频模块分出来的频率为16*9600。模块如下:

/* 
 * this module has been changed to receive the baud rate 
 * dividing counter from registers.
 * the two registers should be calculated as follows:
 * first register:
 * 	baud_freq = 16*baud_rate / gcd(global_clock_freq, 16*baud_rate)
 * second register:
 * 	baud_limit = (global_clock_freq / 
 *                   gcd(global_clock_freq, 16*baud_rate)) - baud_freq
 */
module baud_gen(clk_i, rst_i, ce16_o, baud_freq_i, baud_limit_i);
input		clk_i;
input		rst_i;
output		ce16_o;
input [11:0]	baud_freq_i;
input [15:0]	baud_limit_i;

reg ce16_o;
reg [15:0] count;

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		count <= 16'h0;
	else if (count >= baud_limit_i)
		count <= count - baud_limit_i;
	else
		count <= count + baud_freq_i;
end

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		ce16_o <= 1'b0;
	else if (count >= baud_limit_i)
		ce16_o <= 1'b1;
	else
		ce16_o <= 1'b0;
end

endmodule

分频模块的仿真波形如下,上面是50M的输入时钟,下面是分频后的,需要注意的是,分频后的时钟的占空比不是1:1的,每一个9600Hz的周期中产生16个高电平,高电平维持一个20ns(50MHz的一个周期)

计算baud_freq和baud_limitC程序如下,程序中的输入频率clk_i为50MHZ,输出频率为9600*16。

#include <stdio.h>

int GCD(int a, int b)
{
        if (b == 0)
                return a;
        else
                return GCD(b, a%b);
}

int main()
{
        int clk_i = 50*1000*1000;
        int clk_o = 9600*16;

        int gcd = GCD(clk_i, clk_o);
        int baud_freq = clk_o/gcd;
        int baud_limit = (clk_i/gcd) - baud_freq;
        printf("baud_freq: 0x%x, baud_limit: 0x%x\n", baud_freq, baud_limit);
        return 0;
}

 

然后是发送模块:

module uart_tx(clk_i, 
		rst_i, 
		ce16_i, 
		ser_o, 
		tx_data_i, 
		new_tx_i, 
		tx_int_o, 
		tx_busy_o);
input		clk_i;
input		rst_i;
input		ce16_i;
output		ser_o;
input [7:0]	tx_data_i;
input		new_tx_i;
output		tx_int_o;
output		tx_busy_o;

reg		ser_o;
reg		tx_int_o;
reg		tx_busy_o;
reg [3:0]	count;
reg [3:0]	bit_count;
reg [8:0]	tx_data;

wire		ce1_end;
assign ce1_end = (count == 4'b1111) & ce16_i;

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		count <= 4'h0;
	else if (tx_busy_o & ce16_i)
		count <= count + 1'b1;
	else if (!tx_busy_o & ce16_i)
		count <= 1'b0;
end

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		tx_busy_o <= 1'b0;
	else if (!tx_busy_o & new_tx_i)
		tx_busy_o <= 1'b1;
	else if (tx_busy_o & ce1_end & (bit_count == 4'd9))
		tx_busy_o <= 1'b0;
end

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		bit_count <= 4'h0;
	else if (tx_busy_o & ce1_end)
		bit_count <= bit_count + 1'b1;
	else if (!tx_busy_o)
		bit_count <= 4'h0;
end

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		tx_data <= 9'h0;
	else if (!tx_busy_o)
		tx_data <= {tx_data_i, 1'b0};
	else if (tx_busy_o & ce1_end)
		tx_data <= {1'b1, tx_data[8:1]};
end

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		ser_o <= 1'b1;
	else if (tx_busy_o)
		ser_o <= tx_data[0];
	else
		ser_o <= 1'b1;
end


always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		tx_int_o <= 1'b0;		
	else if (tx_busy_o & ce1_end & (bit_count == 4'd9))
		tx_int_o <= 1'b1;
	else
		tx_int_o <= 1'b0;
end

endmodule

接收模块:

module uart_rx(clk_i,
		rst_i,
		ce16_i,
		ser_i,
		rx_data_o,
		rx_int_o);
input		clk_i;
input		rst_i;
input		ce16_i;
input		ser_i;
output [7:0] 	rx_data_o;
output		rx_int_o;

reg [7:0] 	rx_data_o;
reg		rx_int_o;

reg [7:0] 	rx_data;
reg [1:0]	ser_in;
reg [3:0]	count;
reg [3:0]	bit_count;
reg		rx_busy;

wire		ce1_mid;
wire		ce1_end;

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		ser_in <= 2'b11;
	else
		ser_in <= {ser_in[0], ser_i};
end

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		count <= 4'h0;
	else if ((rx_busy | (ser_in[1] == 1'b0)) & ce16_i)
		count <= count + 1'b1;
	else if (!rx_busy & ce16_i)
		count <= 4'h0;
end

assign ce1_mid = ((count == 4'b0111) & ce16_i);
assign ce1_end = ((count == 4'b1111) & ce16_i);

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		rx_busy <= 1'b0;
	else if (!rx_busy & ce1_mid)
		rx_busy <= 1'b1;
	else if (rx_busy & ce1_end & (bit_count == 4'd9))
		rx_busy <= 1'b0;
end

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		bit_count <= 4'h0;
	else if (rx_busy & ce1_mid)
		bit_count <= bit_count + 1'b1;
	else if (!rx_busy)
		bit_count <= 4'h0;
end

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		rx_data <= 8'h0;
	else if (ce1_mid)
		rx_data <= {ser_in[1], rx_data[7:1]};
end

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i) begin
		rx_data_o <= 8'h0;
		rx_int_o <= 1'b0;
	end else if (ce1_end & (bit_count == 4'd8)) begin
		rx_data_o <= rx_data;
		rx_int_o <= 1'b1;
	end else
		rx_int_o <= 1'b0;
end

endmodule

顶层模块:

module uart_loop_top(clk_i, rst_i, ser_i, ser_o);
input		clk_i;
input		rst_i;
input		ser_i;
output		ser_o;

// baud rate generator parameters for 9600 baud on 50MHz clock
`define D_BAUD_FREQ		12'h30
`define D_BAUD_LIMIT		16'h3CD9

wire		ce16;
wire [11:0]	baud_freq;
wire [15:0]	baud_limit; 

wire [7:0]	rx_data;
reg [7:0] 	rx_reg;
reg		start_tx;
wire		tx_int;
wire		tx_busy;

assign baud_freq = `D_BAUD_FREQ;
assign baud_limit = `D_BAUD_LIMIT;


baud_gen baud_gen1(.clk_i(clk_i), 
		.rst_i(rst_i), 
		.ce16_o(ce16),
		.baud_freq_i(baud_freq),
		.baud_limit_i(baud_limit));

uart_rx uart_rx1(.clk_i(clk_i), 
		.rst_i(rst_i),
		.ce16_i(ce16),
		.ser_i(ser_i),
		.rx_data_o(rx_data),
		.rx_int_o(rx_int));

uart_tx uart_tx1(.clk_i(clk_i),
		.rst_i(rst_i),
		.ce16_i(ce16),
		.ser_o(ser_o), 
		.tx_data_i(rx_reg),
		.new_tx_i(start_tx),
		.tx_int_o(tx_int),
		.tx_busy_o(tx_busy));

always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		rx_reg <= 8'h0;
	else if (rx_int & ~tx_busy)
		rx_reg <= rx_data;
end


always @(posedge clk_i or negedge rst_i)
begin
	if (!rst_i)
		start_tx <= 1'b0;
	else if (rx_int & ~tx_busy)
		start_tx <= 1'b1;
	else
		start_tx <= 1'b0;
end

endmodule
Avatar_small
jim 说:
2014年4月28日 21:39

这里的串口时钟是一直开启的?

Avatar_small
메이저사이트 说:
2024年4月27日 14:04

How can i make bigger the variety of a Wireless N router? I have an Xtreme N hand held router (Dlink). I should amplify the reach in the wireless signal. I understand a way to do it for the G signal. I need to know how to do the idea for an N sign. Is it feasible employ ordinary N routers as repeaters. If so, a way to configure them. Thanks for that data.

Avatar_small
먹튀검증사이트 说:
2024年4月27日 14:04

Awesome and truely exciting positioned up right here. I very lots experience web sites that need to do with dropping weight, in order that is right to me to find out what you've got right here. Keep up the exquisite artwork! The manner to shed pounds fast

Avatar_small
안전토토사이트 说:
2024年4月27日 14:04

I don’t even realise how I ended up proper right here, however I concept this publish modified into excellent. Fototapety I do now not apprehend who you're but absolutely you’re going to a famous blogger if you aren’t already Cheers!

Avatar_small
카지노탐구생활 说:
2024年4月27日 15:14

There are very a notable deal of information this way to reflect onconsideration on. It surely is a tremendous factor to retrieve. I offer the mind above as wellknown suggestion but actually there are questions together with the one you retrieve the spot that the most important factor are going to be getting work carried out in sincere properly religion. I don?T decide if high-quality practices have emerged round matters consisting of that, however Almost truly that the process is clearly described as a honest sport. Both boys and women have the effect of most effective a second’s satisfaction, through-out their lives.

Avatar_small
먹튀폴리스 说:
2024年4月27日 15:15

*When I to begin with commented I clicked the -Notify me at the same time as new remarks are added- checkbox and now each time a commentary is brought I get 4 emails with the equal commentary. Is there any manner you may eliminate me from that service? Thanks!

Avatar_small
카지노사이트 说:
2024年4月27日 15:20

Expert in STD testing and treatment privately with many same day test result so you don’t have to wait few days. They have their own pharmacy so you don’t have to go to pharmacy for embarrassing STD medicine. They are expert in area of Southern California such as Los Angeles , Beverly Hills , Hollywood. Santa Monica, They give test result for syphilis in 20 minutes. They test for NGU which is difficult std to test and treat worth resilt I. 20 minutes. They won’t accept any health insurance but they accept credit card debit or cash to provide convinence fast reliable result and treatment on same day.

Avatar_small
엠카지노 说:
2024年4月27日 15:20

Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.ThanksThis is my first time visit here. From the tons of comments on your articles,I guess I am not only one having all the enjoyment right here!

Avatar_small
카지노사이트 说:
2024年4月27日 15:21

I hope this helps almost anyone searching for this type of topic. I think this website is the best for such topics. Good work and quality of article.

Avatar_small
토크리 说:
2024年4月27日 15:43

Awesome and truely exciting positioned up right here. I very lots experience web sites that need to do with dropping weight, in order that is right to me to find out what you've got right here. Keep up the exquisite artwork! The manner to shed pounds fast

Avatar_small
사설토토사이트 说:
2024年4月27日 15:45

I’m simply commenting to allow you to recognize what a exceptional discovery my cousin’s toddler obtained reading the weblog. She observed hundreds of factors, which covered how it's miles need to personal an great training person to get brilliant mother and father effects check decided on not possible subjects. You actually exceeded readers’ expected effects. Thank you for rendering such beneficial, stable, instructional or maybe fun steerage for your issue count to Lizeth.

Avatar_small
토랜드 说:
2024年4月27日 15:46

I don’t even realise how I ended up proper right here, however I concept this publish modified into excellent. Fototapety I do now not apprehend who you're but absolutely you’re going to a famous blogger if you aren’t already Cheers!

Avatar_small
토토사이트 说:
2024年4月27日 16:23

I take delight in reading what you needed to precise, You have an extraordinary expertise on the trouble informationa nd I sit up for examing extra of what you've got got to say. I will pay attention and bookmark your put up and are available lower back for your internet internet site on line whilst an update is published.

Avatar_small
먹튀검증사이트 说:
2024年4月27日 16:25

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.

Avatar_small
메이저놀이터 说:
2024年4月27日 16:25

Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.ThanksThis is my first time visit here. From the tons of comments on your articles,I guess I am not only one having all the enjoyment right here!

Avatar_small
먹튀검증백과 说:
2024年4月27日 16:39

Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.ThanksYou make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers.You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers.

Avatar_small
ok토토먹튀검증 说:
2024年4月27日 16:40

It’s captivating to consider this malfunction. As possible locate, movers Adsense with regard to Content fabric, which in turn describes that specific classified ads turning into around the web sites in that you’ve composed articles, is honestly shown preliminary. In this text you will have a look at the volume of article mind indexed via the first column. When a page opinion is generally precisely like the net site take a look at, that is honestly not necessarily usually the case. To get a extra special distinction of an internet page affect, observe Yahoo Adsense help, test out The Adsense Consideration and then mouse click on on A person’s Adsense Article.

Avatar_small
사설토토 说:
2024年4月27日 16:41

I hope this helps almost anyone searching for this type of topic. I think this website is the best for such topics. Good work and quality of article.

Avatar_small
먹튀검증사이트 说:
2024年4月27日 17:41

My neighbor and I were sincerely debating this particular topic, he’s usually attempting to find to show me wrong. Your view on this is fine and exactly how I without a doubt feel. I really now mailed him this internet site to suggest him your personal view. After searching over your website I ebook marked and may be coming lower back to examine your new posts!

Avatar_small
사설토토먹튀 说:
2024年4月27日 17:42

Thanks for taking the time to discuss this, I feel strongly approximately it as well as love know-how extra approximately this topic. If possible, while you acquire expertise, could you thoughts upgrading your weblog with extra statistics? It is extraordinarily perfect for me.

Avatar_small
먹튀제보 说:
2024年4月27日 17:42

Took me time to research all the remarks, however I simply cherished the write-up. It proved to be Very beneficial to me and I’m superb to all of the commenters here! It truly is always superior while you can not solely be knowledgeable, but moreover entertained! I am tremendous you had exciting penning this publish.

Avatar_small
먹튀신고 说:
2024年4月27日 17:43

I take delight in reading what you needed to precise, You have an extraordinary expertise on the trouble informationa nd I sit up for examing extra of what you've got got to say. I will pay attention and bookmark your put up and are available lower back for your internet internet site on line whilst an update is published.

Avatar_small
온라인카지노 说:
2024年4月27日 17:47

Magnificent website. Lots of useful statistics proper right right here. I’m sending it to three friends ans additionally sharing in delicious. And certainly, thanks for your sweat!

Avatar_small
안전카지노사이트 说:
2024年4月27日 17:48

My neighbor and I were sincerely debating this particular topic, he’s usually attempting to find to show me wrong. Your view on this is fine and exactly how I without a doubt feel. I really now mailed him this internet site to suggest him your personal view. After searching over your website I ebook marked and may be coming lower back to examine your new posts!

Avatar_small
토토하이 说:
2024年4月27日 18:06

This article can be very attractive to questioning humans like me. It’s no longer quality perception-horrifying, it draws you in from the beginning. This is nicely-written content fabric. The perspectives here are also attractive to me. Thank you.

Avatar_small
เว็บไซท์ แทงบอลออนไล 说:
2024年4月27日 18:17

I’m impressed, I need to say. Very rarely do I stumble upon a blog that’s both informative and exciting, and let me tell you, you’ve hit the nail on the pinnacle. Your blog is essential, the problem is some thing that now not enough people are speakme intelligently approximately

Avatar_small
먹튀히어로 说:
2024年4月27日 18:35

This must be clearly one of my preferred posts! And on top of thats its furthermore very beneficial trouble rely for freshmen. High-first-rate one plenty for the data!

Avatar_small
검증사이트 说:
2024年4月27日 18:53

Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.I love seeing blog that understand the value of providing a quality resource for free.I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article...I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article...

Avatar_small
메이저사이트 说:
2024年4月27日 18:54

I take delight in reading what you needed to precise, You have an extraordinary expertise on the trouble informationa nd I sit up for examing extra of what you've got got to say. I will pay attention and bookmark your put up and are available lower back for your internet internet site on line whilst an update is published.

Avatar_small
메이저놀이터모음 说:
2024年4月27日 18:54

Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.I love seeing blog that understand the value of providing a quality resource for free.I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article...I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article...

Avatar_small
토토사이트 说:
2024年4月27日 18:55

I don’t even realise how I ended up proper right here, however I concept this publish modified into excellent. Fototapety I do now not apprehend who you're but absolutely you’re going to a famous blogger if you aren’t already Cheers!

Avatar_small
먹튀검증사이트 说:
2024年4月27日 19:07

We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work.Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
안전놀이터 说:
2024年4月27日 19:12

We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great work.Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
메이저사이트 说:
2024年4月27日 19:13

My neighbor and I were sincerely debating this particular topic, he’s usually attempting to find to show me wrong. Your view on this is fine and exactly how I without a doubt feel. I really now mailed him this internet site to suggest him your personal view. After searching over your website I ebook marked and may be coming lower back to examine your new posts!

Avatar_small
토토사이트검증 说:
2024年4月27日 19:15

QuickBooks is the biggest accounting software in the world. One of the most common error series in QuickBooks is the “H” series.  we’ll tell you How to fix Error Code H505.  If you’ll read this blog, then it means you’ll definitely solve this error.Visit our website to know How to fix quickbooks Error Code H505

Avatar_small
토토사이트코드 说:
2024年4月27日 19:15

When choosing an online casino game site, it's important to research and verify the site's reputation and safety. Look for sites that are licensed and regulated by recognized regulatory bodies, offer secure payment options, and have a responsive customer support team. Additionally, always gamble responsibly and within your means.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter