当前位置:网站首页>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
边栏推荐
- 【九阳神功】2019复旦大学应用统计真题+解析
- 6. Function recursion
- [中国近代史] 第六章测验
- Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
- [the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
- C语言入门指南
- [the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
- MySQL锁总结(全面简洁 + 图文详解)
- Mortal immortal cultivation pointer-2
- [the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
猜你喜欢

Application architecture of large live broadcast platform

MPLS experiment

8.C语言——位操作符与位移操作符

Conceptual model design of the 2022 database of tyut Taiyuan University of Technology

Tyut Taiyuan University of technology 2022 introduction to software engineering summary

透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰

8. C language - bit operator and displacement operator

一段用蜂鸣器编的音乐(成都)

View UI plus released version 1.3.0, adding space and $imagepreview components

1. C language matrix addition and subtraction method
随机推荐
Tyut Taiyuan University of technology 2022 introduction to software engineering summary
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
西安电子科技大学22学年上学期《信号与系统》试题及答案
关于双亲委派机制和类加载的过程
【手撕代码】单例模式及生产者/消费者模式
3.C语言用代数余子式计算行列式
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
12 excel charts and arrays
【九阳神功】2016复旦大学应用统计真题+解析
[中国近代史] 第九章测验
13 power map
MPLS experiment
3.输入和输出函数(printf、scanf、getchar和putchar)
[Topic terminator]
Atomic and nonatomic
MySQL lock summary (comprehensive and concise + graphic explanation)
Share a website to improve your Aesthetics
Relational algebra of tyut Taiyuan University of technology 2022 database