交通灯控制系统程式

原创 2005年02月26日 21:11:00

前一段时间做的一个课程设计,使用SST 89c54 MSC做中心控制器,本来要求使用asm来写程序,写了一半我觉得开发效率太低,还是c来的快,于是重新写了一下,现在无聊帖出来玩玩。IDE用的Keil。

#include <SST/SST89x5x4.h>

sbit NS_RED=P0^0;
sbit NS_YELLOW=P0^1;
sbit NS_GREEN=P0^2;
sbit EW_RED=P0^3;
sbit EW_YELLOW=P0^4;
sbit EW_GREEN=P0^5;

#define RED 0
#define YELLOW 1
#define GREEN 2

//KEY is controlled by the pedestrians
sbit  KEY=P2^0;
// NS_COLOR:
// 0 - RED / 1 - YELLOW / 2 - GREEN

// LED_POS: 0,1,2,3 - NS_H,NS_L,EW_H,EW_L
unsigned char NS_COLOR, EW_COLOR, NS_TIME, EW_TIME, LED_POS,debounce,counter;

//Var for numeric display LED(LED_POS)
#define NS_H 2
#define NS_L 4
#define EW_H 8
#define EW_L 1

bit pressed;

/*******************************
 * Key Pressed
 *******************************/

void keyPressed() {
 if ((NS_COLOR == RED) && (NS_TIME > 5)) { //the pedestrian wanna cross the NS way
  pressed = 1;
  NS_TIME = 5;
  EW_COLOR = YELLOW;
  EW_TIME = 5;
 }
}

/*****************************
 * Update per second
 *****************************/

void secondUp() {
 // make sure time is never negative
 NS_TIME--;
 EW_TIME--;

 if (NS_TIME == 0) {
  switch (NS_COLOR) {
   case RED://when NS is RED,if key is pressed,NS will be 20s of GREEN,or NS should be 18s of GREEN
    NS_COLOR = GREEN;
    if (pressed)
     {NS_TIME = 20;pressed = 0;}
    else
     NS_TIME = 18;
    break;
   case YELLOW:
    NS_COLOR = RED;
    NS_TIME = 20;
    break;
   case GREEN:
        NS_COLOR = YELLOW;
    NS_TIME = 2;
       break;
  }
 }

 if (EW_TIME == 0) {
  switch (EW_COLOR) {
   case RED:
    EW_COLOR = GREEN;
    EW_TIME = 18;
    break;
   case YELLOW://when EW is YELLOW,if key is pressed,EW will be 22s of RED,or will be 20s of RED
    EW_COLOR = RED;
   if (pressed)
     {EW_TIME = 22;pressed = 0;}
    else
     EW_TIME = 20;
    break;
   case GREEN:
        EW_COLOR = YELLOW;
    EW_TIME = 2;
       break;
  }
 }
}

void display() {
 unsigned char DISP,LED;
 NS_RED = (NS_COLOR != RED);
 NS_YELLOW = (NS_COLOR != YELLOW);
 NS_GREEN = (NS_COLOR != GREEN);
 EW_RED = (EW_COLOR != RED);
 EW_YELLOW = (EW_COLOR != YELLOW);
 EW_GREEN = (EW_COLOR != GREEN);
 switch (LED_POS) {
  case NS_H:
   LED_POS = NS_L;
   DISP = (int) (NS_TIME / 10);
   break;
  case NS_L:
   LED_POS = EW_H;
   DISP = (int) (NS_TIME % 10);
   break;
  case EW_H:
   LED_POS = EW_L;
   DISP = (int) (EW_TIME / 10);
   break;
  case EW_L:
   LED_POS = NS_H;
   DISP = (int) (EW_TIME % 10);
   break;
 }
 // LED defined as char (8 bits)(4 bits for the number,and 4 bits for the position)
 LED = (DISP << 4) | LED_POS;
 P1 = LED;//output the one bit number
}

void main(void){
//initialization
NS_COLOR=RED;
EW_COLOR=GREEN;
NS_TIME=20;
EW_TIME=18;
LED_POS=NS_H;
pressed=0;
debounce=0;
counter=0;
//Timer 2e16-5*10e-3/(11.0592e-6/12)=60536=0xec78
TMOD=0x01;
TR0=1;
for(;;){
 TH0 = 0xEC;
 TL0 = 0x78;
 do{}while(!TF0);
//DO IT!
// in main pressed = 0; initialize time to a positive value

if (!KEY){//the key control by the pedestrians
 debounce++;
}

if (debounce >= 20) {//pressed and holded the key over 5 x 20 = 100 ms
 debounce = 0;
 keyPressed(); 
}

counter++;

if (counter >= 200) {//Now one second just passed.
 counter = 0;
 secondUp();
}

display();//Display the right light with the current second time.


//END OF DOIT
TF0=0;//reset the timer overflow status bit
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

交通灯定时控制系统的设计

目        录一、  内容摘要……………………………………………1二、  设计内容及要求……………………………………1三、  总设计原理………………………...

黑马程序员_十字路口交通灯控制系统笔记

面向对象的思想举例: 1. 人在黑板上画yuan

交通灯控制系统课程设计(基于STAR-ES598PCI)

LedDisplay PROC NEAR MOV AL,Counter MOV AH,AL AND AL,0FH MOV BUFFER,...

黑马程序员-模拟交通灯控制系统

------- android培训、java培训、期待与您交流! ---------- /*  * 思路分析:  * 十字路口,按上下左右的方向来分析,一共有12条路线,其中4条右转弯是不受...

java自学笔记之面试题交通灯控制系统

交通灯管理系统 项目需求: 1、异步随机生成按照各个路线行驶的车辆。 例如: 由南向而来去往北向的车辆 ---- 直行车辆 由西向而来去往南向的车辆 ---- 右转车辆 由东向而来去往南向的车辆 -...

只能交通灯控制系统

  • 2013-11-04 21:30
  • 504KB
  • 下载

单片机交通灯控制系统

  • 2013-05-17 10:38
  • 4.40MB
  • 下载

交通控制系统

【设计要求】  用VHDL语言设计一个由一条主干道和一条支干道的汇合点形成的十字交叉路口的交通灯控制器,要求如下: (1)主、支干道各设一个红、绿、黄指示灯,LED显示 (2)主干道处于常允许...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)