768516985 发表于 2011-3-24 12:12:57

菜鸟求老鸟改一下交通灯的程序

我们老板要我烧个程序,我左烧右烧就是烧不出。题目要求为道路较窄而车辆通行较多,支线、干线的车辆通行时间不等,并设有左右弯道通行时间,允许人工监控或修改各线通行时间,同时设有道路应急控制。具体的情况是:在正常的情况下,干道通行时间为40秒,其中左右弯道占时5秒,直道通行时间为25秒,其中左右弯道占时5秒,直道占时20秒。并且能够在人工监控状态下,干道、支道通行时间通过键盘修改或通过开关人为控制。
最后一个要求“并且能够在人工监控状态下,干道、支道通行时间通过键盘修改或通过开关人为控制。”这个我用那个状态机,不懂做,希望老鸟帮我改一下程序。我把已经能实现功能的基础部分拷贝如下:

注:A方向即为主干道方向B方向为支干道方向

/*信号定义与说明:
CLK :同步时钟;
EN:使能信号,为1的话,则控制器工作;
LAMPA:控制A方向四盏灯的亮灭;其中LAMPA0~LAMPA3,分别控制A方向的左拐灯、绿灯、黄灯和红灯。
LAMPB:控制B方向四盏灯的亮灭;其中LAMPB0~LAMPB3,分别控制B方向的左拐灯、绿灯、黄灯和红灯。
acount:用于A方向灯的时间显示,8位,可驱动两个数码管;
bcount:用于B方向灯的时间显示,8位,可驱动两个数码管。*/

module traffic(CLK,EN,LAMPA,LAMPB,acount,bcount);
input CLK,EN;
output LAMPA,LAMPB;
output acount,bcount;
reg tempa,tempb;
reg counta,countb;
reg LAMPA,LAMPB;
reg ared,ayellow,agreen,aleft,bred,byellow,bgreen,bleft;
reg numa,numb;
   
always @(EN)
if(!EN)
         begin
         ared <=8'd35;
         ayellow<=8'd5;
         agreen <=8'd35;
         aleft <=8'd5;
         bred <=8'd50;
         byellow <=8'd5;
         bgreen <=8'd20;
         bleft <=8'd5;
         end
         assign acount=numa;
         assign bcount=numb;
         always @(posedge CLK)
         begin
             if(EN)
               begin
               if(!tempa)
                   begin
               tempa<=1;
               case(counta)
               0:begin numa<=agreen;LAMPA<=2;counta<=1;end
               1:begin numa<=ayellow;LAMPA<=4;counta<=2;end
               2:begin numa<=aleft;LAMPA<=1;counta<=3;end
               3:begin numa<=ayellow;LAMPA<=4;counta<=4;end
               4:begin numa<=ared;LAMPA<=8;counta<=0;end
               default: LAMPA<=8;
               endcase
                end
               else begin
                  if(numa>1)
                  if(numa==0)
                  begin numa<=4'b1001;numa<=numa-1;end
                  else numa<=numa-1;
                if(numa==2) tempa<=0;
                end
         end
         else begin LAMPA<=4'b1000;counta<=0;tempa<=0;end
          end
         
         
      always @(posedge CLK)
          begin
             if(EN)
               begin
               if(!tempb)
                   begin
               tempb<=1;
               case(countb)
               0:begin numb<=bred;LAMPB<=8;countb<=1;end
               1:begin numb<=bgreen;LAMPB<=2;countb<=2;end
               2:begin numb<=byellow;LAMPB<=4;countb<=3;end
               3:begin numb<=bleft;LAMPB<=1;countb<=4;end
               4:begin numb<=byellow;LAMPB<=4;countb<=0;end
               default: LAMPB<=8;
               endcase
                end
               else begin
                  if(numb>1)
                  if(numb==0)
                  begin numb<=9;numb<=numb-1;end
                  else numb<=numb-1;
                if(numb==2) tempb<=0;
                end
         end
         else begin LAMPB<=4'b1000;countb<=0;tempb<=0;end
          end
         
         
endmodule

768516985 发表于 2011-3-24 12:15:19

回复【楼主位】768516985
-----------------------------------------------------------------------

希望能帮我把后面的那个用按键改变预制数的功能实现出来
老鸟要是没时间的话
发个类似的程序参考下
菜鸟也会在这感激不尽的

aaron96031 发表于 2011-3-24 12:44:08

多分析逻辑 才是硬道理

768516985 发表于 2011-3-24 16:04:03

回复【2楼】aaron96031 龙飞(LF)
-----------------------------------------------------------------------

谢谢哈 我源程序代码看懂了
要是大家有解决的办法 或是程序
大家写一下哦 这样才能互相学习
谢谢

768516985 发表于 2011-3-26 12:02:44

好多菜鸟与青鸟
页: [1]
查看完整版本: 菜鸟求老鸟改一下交通灯的程序