实验:同步时序逻辑电路的仿真与设计
设计方案论述、详细设计、结果及分析
如下是模拟交通灯信号控制,你可以根据如下参考修改一下。
交通灯信号控制是交通工具现代化的产物,在平面交叉口,为了把可能发生冲突的车流从时空上分离,必须通过交通信号对交通流进行有效的引导和调度。
设计要求:
(1) 设计一个十字路口的交通灯控制电路,要求南北方向和东西方向两个交叉路口的车辆交替运行,每个方向绿灯亮30秒,两个方向能根据车流量的大小自动调节通行时间,车流量大,通行时间增加30秒,车流量小,通行时间仍然是30秒。
(2) 在路灯转为红灯时,要求黄灯先亮3秒钟,才能变换运行车道。
源码实现
#include<iostream>
#include<windows.h>
using namespace std;
void way1(int a,int y); //交通灯的函数
void way2(int b);//交通灯的函数
int green1, green2; //定义交通灯的红,黄,绿灯的变量和赋初值
int yellow1, yellow2;
int red1, red2;
int green[2] = {31,61};
int yellow = 4;
int red[3] = {34,64};
int main()
{
int car1, car2, car3, car4; //车辆数变量
int i = 0, j;
char d;
cout<<endl<<"***开始模拟智能交通灯系统***"<<endl<<endl;
while(1)
{
cout<<"请设置东车道车辆数:"; //设置车辆数
cin>>car1;
cout<<"请设置西车道车辆数:";
cin>>car2;
cout<<"请设置北车道车辆数:";
cin>>car3;
cout<<"请设置南车道车辆数:";
cin>>car4;
cout<<endl<<endl;
cout<<"******开始模拟******"<<endl;
if((car1+car2)-(car3+car4)>10) //根据车辆数来选择交通灯计时函数
{
j = i+1;
way1(j, i);
}
else if(((car1+car2)-(car3+car4)>=0)&&((car1+car2)-(car3+car4)<=10))
{
way2(i);
}
else
{
j = i+1;
way1(i, j);
}
cout<<"若想重新设置车辆数请按Y,若退出请按N。";
cin>>d;
if(d == 'Y'||d == 'y')
{
cout<<endl;
continue;
}
else if(d == 'N'||d == 'n')
break;
}
return 0;
}
void way1(int a,int y)
{
green1=green[a]; //先给交通灯赋初值
green2=0;
yellow1=yellow2=0;
red1=0;
red2=red[a];
int c;
char d;
while(1) //东西车道的交通灯
{
for(c=99;c>0;c--)
{
cout<<endl<<"------东西车道------"<<endl;
if(green1>0)
{
if(green1==green[a])
{
green1--;
}
else
{
cout<<" >>> 绿灯 <<< "<<green1<<" 秒\n";
green1--;
}
if(green1==0)
yellow1=yellow;
}
if(yellow1>0)
{
if(yellow1==4)
yellow1--;
else
{
cout<<" >>> 黄灯 <<< "<<yellow1<<" 秒\n";
yellow1--;
}
if(yellow1==0)
{
red1=red[y];
}
}
if(red1>0)
{
if(red1==red[y])
red1--;
else
{
cout<<" >>> 红灯 <<< "<<red1<<" 秒\n";
red1--;
}
if(red1==0)
{
green1=green[a];
}
}
/***********************************************************************************/
cout<<endl<<"------南北车道------"<<endl; //南北车道的交通灯
if(red2>0)
{
if(red2==red[a])
{
red2--;
}
else
{
cout<<" >>> 红灯 <<< "<<red2<<" 秒\n";
red2--;
}
if(red2==0)
green2=green[y];
}
if(green2>0)
{
if(green2==green[y])
green2--;
else
{
cout<<" >>> 绿灯 <<< "<<green2<<" 秒\n";
green2--;
}
if(green2==0)
{
yellow2=yellow;
}
}
if(yellow2>0)
{
if(yellow2==4)
yellow2--;
else
{
cout<<" >>> 黄灯 <<< "<<yellow2<<" 秒\n";
yellow2--;
}
if(yellow2==0)
{
red2=red[a];
}
}
Sleep(1000);
system("cls");
}
cout<<"若想继续请按C,若想返回上一级请按R。";
cin>>d;
if(d == 'C'||d == 'c')
{
cout<<endl;
continue;
}
else if(d == 'R'||d == 'r')
{
cout<<endl<<endl;
break;
}
}
}
void way2(int b)
{
green1=green[b];
green2=0;
yellow1=yellow2=0;
red1=0;
red2=red[b];
int c;
char d;
while(1)
{
for(c=69;c>0;c--)
{
cout<<endl<<"------东西车道------"<<endl;
if(green1>0)
{
if(green1==green[b])
{
green1--;
}
else
{
cout<<" >>> 绿灯 <<< "<<green1<<" 秒"<<endl;
green1--;
}
if(green1==0)
yellow1=yellow;
}
if(yellow1>0)
{
if(yellow1==4)
yellow1--;
else
{
cout<<" >>> 黄灯 <<< "<<yellow1<<" 秒"<<endl;
yellow1--;
}
if(yellow1==0)
{
red1=red[b];
}
}
if(red1>0)
{
if(red1==red[b])
red1--;
else
{
cout<<" >>> 红灯 <<< "<<red1<<" 秒"<<endl;
red1--;
}
if(red1==0)
{
green1=green[b];
}
}
/***********************************************************************************/
cout<<endl<<"------南北车道------"<<endl;
if(red2>0)
{
if(red2==red[b])
{
red2--;
}
else
{
cout<<" >>> 红灯 <<< "<<red2<<" 秒"<<endl;
red2--;
}
if(red2==0)
green2=green[b];
}
if(green2>0)
{
if(green2==green[b])
green2--;
else
{
cout<<" >>> 绿灯 <<< "<<green2<<" 秒"<<endl;
green2--;
}
if(green2==0)
{
yellow2=yellow;
}
}
if(yellow2>0)
{
if(yellow2==4)
yellow2--;
else
{
cout<<" >>> 黄灯 <<< "<<yellow2<<" 秒"<<endl;
yellow2--;
}
if(yellow2==0)
{
red2=red[b];
}
}
Sleep(1000);
system("cls");
}
cout<<"若想继续请按C,若想返回上一级请按R。";
cin>>d;
if(d == 'C'||d == 'c')
{
cout<<endl;
continue;
}
else if(d == 'R'||d == 'r')
{
cout<<endl<<endl;
break;
}
}
}
这个需要涉及以下几个内容:
1、根据设计要求选择控制器及匹配的硬件电路,本例应该用的是FPGA吧;
2、控制电路设计,绘制电路原理图,本例输入输出都相对简单,控制电路基本可以使用手册或教材的原图,稍作修改即可。用quartus II绘出原理图;
3、根据控制要求,选择算法,做出控制流程图,用VHDL语言编写程序,交通灯的算法在很多PLC的应用中都算的最简单,所有很容易也能找到。
4、仿真测试,这个按quartus II仿真步骤来就可以了。
5、仿真调试中碰到问题,修改程序,再测试,重复3-4步骤即可。直到程序满足要求。
时序逻辑电路设计与仿真
https://blog.csdn.net/qq_44528283/article/details/120698707
参考一下时序逻辑电路实验报告,原文http://www.fanwen118.com/c/216737.html