当前位置:网站首页>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
边栏推荐
- Set container
- [面试时]——我如何讲清楚TCP实现可靠传输的机制
- 6. Function recursion
- 最新坦克大战2022-全程开发笔记-2
- 一段用蜂鸣器编的音乐(成都)
- 7.数组、指针和数组的关系
- E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
- Tyut outline of 2022 database examination of Taiyuan University of Technology
- Change vs theme and set background picture
- 用栈实现队列
猜你喜欢

3.C语言用代数余子式计算行列式

Quickly generate illustrations

Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited

There is always one of the eight computer operations that you can't learn programming
![[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP

编写程序,模拟现实生活中的交通信号灯。

5.MSDN的下载和使用

12 excel charts and arrays

IPv6 experiment

C语言入门指南
随机推荐
更改VS主题及设置背景图片
学编程的八大电脑操作,总有一款你不会
Implement queue with stack
凡人修仙学指针-2
Network layer 7 protocol
13 power map
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
仿牛客技术博客项目常见问题及解答(三)
Set container
Mortal immortal cultivation pointer-2
[graduation season · advanced technology Er] goodbye, my student days
A brief introduction to the database of tyut Taiyuan University of technology in previous years
ABA问题遇到过吗,详细说以下,如何避免ABA问题
The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
[中国近代史] 第六章测验
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
西安电子科技大学22学年上学期《信号与系统》试题及答案
Custom RPC project - frequently asked questions and explanations (Registration Center)