硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3149|回复: 1
收起左侧

时序逻辑电路(3)----------计数器

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107439
QQ
发表于 2013-1-27 15:01:46 | 显示全部楼层 |阅读模式
【例9.23】可变模加法/减法计数器
module updown_count(d,clk,clear,load,up_down,qd);
input[7:0] d;
input clk,clear,load;
input up_down;
output[7:0] qd;
reg[7:0] cnt;
assign qd = cnt;
always @(posedge clk)
    begin
if (!clear)      cnt = 8'h00;    //同步清0,低电平有效
else  if (load)    cnt = d;      //同步预置
else  if (up_down)  cnt = cnt + 1;    //加法计数
else          cnt = cnt - 1;    //减法计数
    end
endmodule
【例9.24】4位Johnson计数器(异步复位)
module johnson(clk,clr,out);
input clk,clr;
output[3:0] out;
reg[3:0] out;
always @(posedge clk or posedge clr)
begin
  if (clr)   out<= 4'h0;
  else
begin  out<= out<< 1;
      out[0]<= ~out[3];
end
end
endmodule
【例3.2】4位计数器
module count4(out,reset,clk);
output[3:0] out;
input reset,clk;
reg[3:0] out;
always @(posedge clk)
begin
if (reset)  out<=0;           //同步复位
else       out<=out+1;   //计数
    end
endmodule
【例5.2】同步置数、同步清零的计数器
module count(out,data,load,reset,clk);
output[7:0] out;
input[7:0] data;
input load,clk,reset;
reg[7:0] out;
always @(posedge clk)                  //clk上升沿触发
begin
if (!reset)    out = 8'h00;    //同步清0,低电平有效
else if (load)   out = data;     //同步预置
else       out = out + 1;    //计数
end
endmodule
【例5.11】模为60的BCD码加法计数器
module count60(qout,cout,data,load,cin,reset,clk);
output[7:0] qout;
output cout;
input[7:0] data;
input load,cin,clk,reset;
reg[7:0] qout;
always @(posedge clk)                             //clk上升沿时刻计数
  begin
if (reset)      qout<=0;                                //同步复位
else  if(load)    qout<=data;                      //同步置数
else  if(cin)
begin
if(qout[3:0]==9)                                        //低位是否为9,是则
begin
qout[3:0]<=0;                                           //回0,并判断高位是否为
if (qout[7:4]==5)  qout[7:4]<=0;
else
qout[7:4]<=qout[7:4]+1;                           //高位不为5,则加1
end
else                                                          //低位不为9,则加1
qout[3:0]<=qout[3:0]+1;
end
  end
assign cout=((qout==8'h59)&cin)?1:0;    //产生进位输出信号
endmodule
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107439
QQ
 楼主| 发表于 2013-1-27 16:02:28 | 显示全部楼层
【例6.1】加法计数器中的进程
module count(data,clk,reset,load,cout,qout);
output cout;
output[3:0] qout;
reg[3:0] qout;
input[3:0] data;
input clk,reset,load;
always @(posedge clk)           //进程1,always过程块
    begin
if (!reset)    qout= 4'h00;    //同步清0,低电平有效
else if (load)   qout= data;     //同步预置
else        qout=qout + 1;    //加法计数
    end
assign cout=(qout==4'hf)?1:0;      //进程2,用持续赋值产生进位信号
【例7.3】行为描述方式实现的4位计数器
module count4(clk,clr,out);
input clk,clr;
output[3:0] out;
reg[3:0] out;
always @(posedge clk or posedge clr)
begin
if (clr)    out<=0;
else     out<=out+1;
end
endmodule
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-31 18:42 , Processed in 0.146043 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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