当前位置:网站首页>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
边栏推荐
- 2. Preliminary exercises of C language (2)
- A brief introduction to the database of tyut Taiyuan University of technology in previous years
- 强化学习系列(一):基本原理和概念
- 13 power map
- IPv6 experiment
- 重载和重写的区别
- [the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
- arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
- 12 excel charts and arrays
- Atomic and nonatomic
猜你喜欢

3.输入和输出函数(printf、scanf、getchar和putchar)

关于双亲委派机制和类加载的过程

MySQL Database Constraints

12 excel charts and arrays

Relational algebra of tyut Taiyuan University of technology 2022 database

(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。

(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)

7. Relationship between array, pointer and array

Data manipulation language (DML)

arduino+水位传感器+led显示+蜂鸣器报警
随机推荐
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
强化学习系列(一):基本原理和概念
C语言入门指南
【九阳神功】2022复旦大学应用统计真题+解析
C语言入门指南
5.MSDN的下载和使用
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
3.C语言用代数余子式计算行列式
ArrayList的自动扩容机制实现原理
Tyut Taiyuan University of technology 2022 introduction to software engineering summary
MySQL锁总结(全面简洁 + 图文详解)
Set container
6. Function recursion
MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
hashCode()与equals()之间的关系
Cookie和Session的区别
3.输入和输出函数(printf、scanf、getchar和putchar)
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files