当前位置:网站首页>Write a program to simulate the traffic lights in real life.
Write a program to simulate the traffic lights in real life.
2022-07-06 13:38:00 【axu_ nine hundred and ninety thousand seven hundred and seven】
Programming , Simulate traffic lights in real life .
The source program and simulation have been packaged You can download what you need ( Pure originality Please cherish the fruits of your labor Welcome to forward )
link : Click to download
Traffic light test requirements :
1、 The normal operation of traffic lights can be divided into 4 Status :
state 1: The green light in the east-west direction is on , thing Blue Nixie tube countdown display ; The red light is on in the north-south direction , North South Red nixie tube countdown display ( Time is up to you );
state 2: The remaining 3 second , The yellow light flashes in the East, West, North and south directions , Final steering state 3;
state 3: The red light in the east-west direction is on , Things red digital tube countdown display ; The green light is on in the north-south direction , north and south Blue Nixie tube countdown display ( Time is up to you );
state 4: The remaining 3 second , The yellow light flashes in the East, West, North and south directions , Final steering state 1.
2. Abnormal state ( Interrupt implementation ):
When something is abnormal , The yellow light flashes in the east-west direction , Flash 60 second ;
When anomalies occur in the north and South , The yellow light flashes in the north-south direction , Flash 60 second .
( notes : Simulate the occurrence of abnormal conditions through interruption .)
Code implementation
1. The core idea of the program
①: This experiment adds header files (.h file ). All functions used in the experiment ( The time delay function void delay(int j), Judge button function int key(), Two digit nixie tube display function void showNumByCA(int num)). By setting , It can minimize the coupling degree of the main program , The structure is clear .
②: In the header file showNumByCA There are more than one... In the function , The difference lies in different showNumByCA Control different nixie tubes .
③: Principle of traffic light program : All two digital tubes use the same port (P0). Display the same number , By using different showNumByCA function (showNumByCA1,showNumByCA2…) Control different nixie tubes to achieve the function of switching between red and blue nixie tubes .
④: About interruptions 0: interrupt 0 Its function is to make things dim , The traffic lights flash yellow , North South normal count . So a global variable is set in the program repair=0, When the button is pressed repair Into a 10. In the main program , Each state switch will be checked repair Value , On duty 0 when , Means the button is not pressed , Perform normal display . When the button is pressed ,repair=10. Represents abnormal state , At this time, different showNumByCA function (showNumByCA1,showNumByCA2…) Turn off the corresponding nixie tube , Adjust the color rendering of the corresponding traffic lights . After each number displayed ,repair Will reduce itself 1, therefore , When it comes to repair=0 when , The program returns to normal . The abnormal state is repair=10 Time .
⑤: About interruptions 1: Analogy to ④, The only difference is when the button is pressed ,repair=-10, After each number displayed ,repair Will be on the 1, therefore , When added to repair=0 when , The program returns to normal . The abnormal state is repair=-10 Time .
⑥: The traffic lights in this experiment are accurate 1S timing ( Timing interrupt implementation )
2. Program source code
// Header file section Create additional .h file See the main function introduction for the file name
void delay(unsigned char k){
unsigned char i,j,h;
for(h=0;h<k;h++){
for(i=0;i<25;i++){
for(j=0;j<20;j++);
}
}
}
void twoDigitDisplay(unsigned char num,unsigned char time,unsigned char portNumber){
unsigned char box[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
unsigned char k;
if(portNumber==0){
if(num<0||num>99||time<0){
for(k=0;k<40;k++){
P2=0x00;
}
}else{
for(k=0;k<time;k++){
unsigned char numR = num%10;
unsigned char numL = num/10;
P2=0x11;
P0=box[numL];
delay(12);
P2=0x22;
P0=box[numR];
delay(12);
}
}
}
}
void twoDigitDisplay1(unsigned char num,unsigned char time,unsigned char portNumber){
unsigned char box[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
unsigned char k;
if(portNumber==0){
if(num<0||num>99||time<0){
for(k=0;k<40;k++){
P2=0x00;
}
}else{
for(k=0;k<time;k++){
unsigned char numR = num%10;
unsigned char numL = num/10;
P2=0x44;
P0=box[numL];
delay(12);
P2=0x88;
P0=box[numR];
delay(12);
}
}
}
}
void twoDigitDisplay2(unsigned char num,unsigned char time,unsigned char portNumber){
unsigned char box[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
unsigned char k;
if(portNumber==0){
if(num<0||num>99||time<0){
for(k=0;k<40;k++){
P2=0x00;
}
}else{
for(k=0;k<time;k++){
unsigned char numR = num%10;
unsigned char numL = num/10;
P2=0x10;
P0=box[numL];
delay(12);
P2=0x20;
P0=box[numR];
delay(12);
}
}
}
}
void twoDigitDisplay3(unsigned char num,unsigned char time,unsigned char portNumber){
unsigned char box[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
unsigned char k;
if(portNumber==0){
if(num<0||num>99||time<0){
for(k=0;k<40;k++){
P2=0x00;
}
}else{
for(k=0;k<time;k++){
unsigned char numR = num%10;
unsigned char numL = num/10;
P2=0x40;
P0=box[numL];
delay(12);
P2=0x80;
P0=box[numR];
delay(12);
}
}
}
}
void twoDigitDisplay4(unsigned char num,unsigned char time,unsigned char portNumber){
unsigned char box[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
unsigned char k;
if(portNumber==0){
if(num<0||num>99||time<0){
for(k=0;k<40;k++){
P2=0x00;
}
}else{
for(k=0;k<time;k++){
unsigned char numR = num%10;
unsigned char numL = num/10;
P2=0x01;
P0=box[numL];
delay(12);
P2=0x02;
P0=box[numR];
delay(12);
}
}
}
}
void twoDigitDisplay5(unsigned char num,unsigned char time,unsigned char portNumber){
unsigned char box[] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0xf82,0xf8,0x80,0x90};
unsigned char k;
if(portNumber==0){
if(num<0||num>99||time<0){
for(k=0;k<40;k++){
P2=0x00;
}
}else{
for(k=0;k<time;k++){
unsigned char numR = num%10;
unsigned char numL = num/10;
P2=0x04;
P0=box[numL];
delay(12);
P2=0x08;
P0=box[numR];
delay(12);
}
}
}
}
// The main function part
#include<reg51.h>
#include"ShowNum.h"
char repair=0;
void main(){
char num;
IT0=1;EA=1;EX0=1;
IT1=1;EX1=1;
P1=0x00;
while(1){
for(num=10;num>=0;num--){
if(num<=3&&num>=0){
if(repair==0){
P1=0x22;twoDigitDisplay(num,8,0);
}
if(repair<=10&&repair>=1){
P1=0x22;twoDigitDisplay2(num,8,0);
repair--;
}
if(repair<=-1&&repair>=-10){
P1=0x22;twoDigitDisplay4(num,8,0);
repair++;
}
P1=0x00;
if(repair==0){
twoDigitDisplay(num,8,0);
}
if(repair<=10&&repair>=1){
twoDigitDisplay2(num,8,0);
repair--;
}
if(repair<=-1&&repair>=-10){
twoDigitDisplay4(num,8,0);
repair++;
}
continue;
}
if(repair==0){
P1=0x41;twoDigitDisplay(num,15,0);
}
if(repair<=10&&repair>=1){
P1=0x42;
twoDigitDisplay2(num,8,0);
P1=0x40;
twoDigitDisplay2(num,8,0);
repair--;
}
if(repair<=-1&&repair>=-10){
P1=0x21;
twoDigitDisplay4(num,8,0);
P1=0x01;
twoDigitDisplay4(num,8,0);
repair++;
}
}
for(num=10;num>=0;num--){
if(num<=3&&num>=0){
if(repair==0){
P1=0x22;twoDigitDisplay1(num,8,0);
}
if(repair<=10&&repair>=1){
P1=0x22;twoDigitDisplay3(num,8,0);
repair--;
}
if(repair<=-1&&repair>=-10){
P1=0x22;twoDigitDisplay5(num,8,0);
repair++;
}
P1=0x00;
if(repair==0){
twoDigitDisplay1(num,8,0);
}
if(repair<=10&&repair>=1){
twoDigitDisplay3(num,8,0);
repair--;
}
if(repair<=-1&&repair>=-10){
twoDigitDisplay5(num,8,0);
repair++;
}
continue;
}
if(repair==0){
P1=0x14;twoDigitDisplay1(num,15,0);
}
if(repair<=10&&repair>=1){
P1=0x42;
twoDigitDisplay2(num,8,0);
P1=0x40;
twoDigitDisplay2(num,8,0);
repair--;
}
if(repair<=-1&&repair>=-10){
P1=0x21;
twoDigitDisplay4(num,8,0);
P1=0x01;
twoDigitDisplay4(num,8,0);
repair++;
}
}
}
}
void int0() interrupt 0 {
// Things are abnormal The yellow light in the east-west direction flashes 10 Normal after times North South normal timing
repair=10;
}
void int1() interrupt 2 {
// North South anomaly The yellow light flashes in the north-south direction 10 Normal after times North South normal timing
repair=-10;
}
Two 、 Simulation demonstration
Simulation demonstration timing 10S Round Parameters can be adjusted in the program !
The source program and simulation have been packaged You can download what you need
link : Click to download
Friends who like SCM can communicate together
边栏推荐
- Floating point comparison, CMP, tabulation ideas
- MySQL Database Constraints
- The latest tank battle 2022 - Notes on the whole development -2
- Implement queue with stack
- Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
- 【九阳神功】2018复旦大学应用统计真题+解析
- 更改VS主题及设置背景图片
- 优先队列PriorityQueue (大根堆/小根堆/TopK问题)
- There is always one of the eight computer operations that you can't learn programming
- [中国近代史] 第九章测验
猜你喜欢
Rich Shenzhen people and renting Shenzhen people
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Quickly generate illustrations
View UI plus released version 1.2.0 and added image, skeleton and typography components
受检异常和非受检异常的区别和理解
5. Download and use of MSDN
4.分支语句和循环语句
Relational algebra of tyut Taiyuan University of technology 2022 database
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
随机推荐
IPv6 experiment
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
4.分支语句和循环语句
Change vs theme and set background picture
强化学习系列(一):基本原理和概念
5. Download and use of MSDN
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
学编程的八大电脑操作,总有一款你不会
2. C language matrix multiplication
Comparison between FileInputStream and bufferedinputstream
IPv6 experiment
[while your roommate plays games, let's see a problem]
View UI plus released version 1.2.0 and added image, skeleton and typography components
2.C语言初阶练习题(2)
5月14日杂谈
Cloud native trend in 2022
Atomic and nonatomic
Leetcode.3 无重复字符的最长子串——超过100%的解法
C language Getting Started Guide