当前位置: 首页 > java >正文

使用C51和RTX-51微型交通灯控制器

“+*****使用C51和RTX-51微型交通灯控制器******+\n” 
“|此程序是一个简单的交通灯控制器 
“|系统控制红绿灯的开始时间和结束时间|\n” 
“|使用行人自助服务。超出此时间范围|\n” 
“|黄色警告灯闪烁。|\n” 
“+命令-+语法-----+函数------------------+\n” 
“|显示|D|显示时间|\n” 
“|时间|T hh:mm:ss|设置时钟时间|\n” 
“|开始|S hh:mm:ss|设置开始时间|\n” 
“|结束|E hh:mm:ss|设置结束时间|\

/******************************************************************************/
/*                                                                            */
/*   TRAFFIC.C:  Traffic Light Controller using the C-51 COMPILER             */
/*                                                                            */
/******************************************************************************/

char code menu[] = 
   "\n"
   "+***** TRAFFIC LIGHT CONTROLLER using C51 and RTX-51 tiny *****+\n"
   "| This program is a simple Traffic Light Controller.  Between  |\n"
   "| start time and end time the system controls a traffic light  |\n"
   "| with pedestrian self-service.  Outside of this time range    |\n"
   "| the yellow caution lamp is blinking.                         |\n"
   "+ command -+ syntax -----+ function ---------------------------+\n"
   "| Display  | D           | display times                       |\n"
   "| Time     | T hh:mm:ss  | set clock time                      |\n"
   "| Start    | S hh:mm:ss  | set start time                      |\n"
   "| End      | E hh:mm:ss  | set end time                        |\n"
   "+----------+-------------+-------------------------------------+\n";

#include <REG52.H>                   /* special function registers 8052      */
#include <rtx51tny.h>                 /* RTX-51 tiny functions & defines      */
#include <stdio.h>                    /* standard I/O .h-file                 */
#include <ctype.h>                    /* character functions                  */
#include <string.h>                   /* string and memory functions          */

#include "traffic.h"                  /* project specific header file         */

struct time ctime = { 12,  0,  0 };   /* storage for clock time values        */
struct time start = {  7, 30,  0 };   /* storage for start time values        */
struct time end   = { 18, 30,  0 };   /* storage for end   time values        */

sbit  red    = P2^6;                  /* I/O Pin:  red    lamp output         */
sbit  yellow = P2^7;                  /* I/O Pin:  yellow lamp output         */
sbit  green  = P1^0;                  /* I/O Pin:  green  lamp output         */
sbit  stop   = P1^1;                  /* I/O Pin:  stop   lamp output         */
sbit  walk   = P1^2;                  /* I/O Pin:  walk   lamp output         */
sbit  key    = P3^2;                  /* I/O Pin:  self-service key input     */

char idata inline[16];                /* storage for command input line       */


/******************************************************************************/
/*        Task 0 'init': Initialize       任务0“初始化”:初始化               */
/******************************************************************************/
void init (void) _task_ INIT  {       /* program execution starts here程序开始*/
  serial_init ();                     /* initialize the serial interface串口初始化*/
                                      /* configure I/O port                   */
  P2M1 = 0x1F;                        /* P2.0-P.2.4 output, P2.5-P2.7 input   */

  os_create_task (CLOCK);             /* start clock task    启动时钟任务     */
  os_create_task (COMMAND);           /* start command task  启动命令任务     */
  os_create_task (LIGHTS);            /* start lights task   启动灯光任务     */
  os_create_task (KEYREAD);           /* start keyread task  启动密钥读取任务 */
  os_delete_task (INIT);              /* stop init task (no longer needed)    */
}                                      /* 停止初始化任务(不再需要)           */


bit display_time = 0;                 /* flag:  signal cmd state display_time */
                                      /* 标志:信号cmd状态显示时间            */
/******************************************************************************/
/*        Task 2 'clock'              任务2  时钟                             */
/******************************************************************************/
void clock (void)  _task_ CLOCK  {      /*时钟是一个无止境的循环*/
  while (1)  {                        /* clock is an endless loop             */
    if (++ctime.sec == 60)  {         /* calculate the second                 */
      ctime.sec = 0;                  /*秒计时*/
      if (++ctime.min == 60)  {       /* calculate the minute                 */
        ctime.min = 0;                  /*分计时*/
        if (++ctime.hour == 24)  {    /* calculate the hour                   */
          ctime.hour = 0;              /*小时计时*/
        }
      }
    }                                  /*如果命令状态=显示时间 */
    if (display_time)  {              /* if command_status == display_time    */
      os_send_signal (COMMAND);       /* signal to task command: time changed */
    }                                  /* 向任务命令发送信号:时间已更改*/
    os_wait (K_IVL, 100, 0);          /* wait interval:  1 second             */
  }                                      /* 等待间隔:1秒*/
}                                      /* */


struct time rtime;                    /* temporary storage for entry time     */
                                      /*进入时的临时存储*/
/******************************************************************************/
/*        readtime: convert line input to time values & store in rtime        */
/*        将行输入转换为时间值并存储在rtime中                                 */
/******************************************************************************/
bit readtime (char idata *buffer)  {           /* 参数数量                    */
  unsigned char args;                          /* number of arguments         */
                                               /* 预设秒                      */
  rtime.sec = 0;                               /* preset second        预设秒 */
  args = sscanf (buffer, "%bd:%bd:%bd",        /* scan input line for         */
                 &rtime.hour,                  /* hour, minute and second     */
                 &rtime.min,                   /* 扫描输入行中的时分秒        */
                 &rtime.sec);
  
  if (rtime.hour > 23  ||  rtime.min > 59  ||  /* check for valid inputs      */
      rtime.sec > 59   ||  args < 2        ||  args == EOF)  {
    printf ("\n*** ERROR: INVALID TIME FORMAT\n");    /*  检查有效输入 */
    return (0);
  }
  return (1);
}

#define ESC  0x1B                     /* ESCAPE character code                */

static bit   escape;                  /* flag: mark ESCAPE character entered  */

/******************************************************************************/
/*        Task 6 'get_escape': check if ESC (escape character) was entered    */
/*任务6“获取转义”:检查是否输入了ESC(转义字符)*/
/******************************************************************************/
void get_escape (void) _task_ GET_ESC  {
  while (1)  {                                 /* endless loop   无尽的循环   */
    if (_getkey () == ESC)  escape = 1;        /* set flag if ESC entered     */
    if (escape)  {                             /* if escape flag send signal  */
      os_send_signal (COMMAND);                /* to task 'command' 任务“命令”*/
    }                                           /*如果输入ESC,则设置标志*/
  }                                               /*如果退出标志发送信号*/
}                                               /**/

                                               /**/
/******************************************************************************/
/*        Task 1 'command': command processor */
/******************************************************************************/
void command (void) _task_ COMMAND  {
  unsigned char i;

  printf (menu);                               /* display command menu  显示命令菜单*/
  while (1)  {                                 /* endless loop   无尽的循环   */
    printf ("\nCommand: ");                    /* display prompt  显示提示    */
    getline (&inline, sizeof (inline));        /* get command line input      */
                                               /*获取命令行输入*/
    for (i = 0; inline[i] != 0; i++)  {        /* convert to uppercase        */
      inline[i] = toupper(inline[i]);           /*转换成大写*/
    }

    for (i = 0; inline[i] == ' '; i++);        /* skip blanks   跳过空格      */

    switch (inline[i])  {                      /* proceed to command function */
      case 'D':                                /* Display Time Command        */
        printf ("Start Time: %02bd:%02bd:%02bd    "     /*进入命令功能*/
                "End Time: %02bd:%02bd:%02bd\n",     /*显示时间命令*/
                 start.hour, start.min, start.sec,
                 end.hour,   end.min,   end.sec);
        printf ("                        type ESC to abort\r");

        os_create_task (GET_ESC);              /* ESC check in display loop   */
        escape = 0;                            /* clear escape flag           */
        display_time = 1;                      /* set display time flag       */
        os_clear_signal (COMMAND);             /* clear pending signals       */

        while (!escape)  {                     /* while no ESC entered        */
          printf ("Clock Time: %02bd:%02bd:%02bd\r",      /* display time     */
                   ctime.hour, ctime.min, ctime.sec);
          os_wait (K_SIG, 0, 0);               /* wait for time change or ESC */
        }

        os_delete_task (GET_ESC);              /* ESC check not longer needed */
        display_time = 0;                      /* clear display time flag     */
        printf ("\n\n");
        break;

      case 'T':                                /* Set Time Command            */
        if (readtime (&inline[i+1]))  {        /* read time input and         */
          ctime.hour = rtime.hour;             /* store in 'ctime'            */
          ctime.min  = rtime.min;
          ctime.sec  = rtime.sec;
        }
        break;

      case 'E':                                /* Set End Time Command        */
        if (readtime (&inline[i+1]))  {        /* read time input and         */
          end.hour = rtime.hour;               /* store in 'end'              */
          end.min  = rtime.min;
          end.sec  = rtime.sec;
        }
        break;

      case 'S':                                /* Set Start Time Command */
        if (readtime (&inline[i+1]))  {        /* read time input and         */
          start.hour = rtime.hour;             /* store in 'start'            */
          start.min  = rtime.min;
          start.sec  = rtime.sec;
        }
        break;

      default:                                 /* Error Handling              */
        printf (menu);                         /* display command menu        */
        break;
    }   
  }
}


/******************************************************************************/
/*        signalon: check if clock time is between start and end              */
/******************************************************************************/
static bit signalon (void)   {
  if (memcmp (&start, &end, sizeof (struct time)) < 0)  {
    if (memcmp (&start, &ctime, sizeof (struct time)) < 0  &&
        memcmp (&ctime, &end,   sizeof (struct time)) < 0)  return (1);
  }
                                                
  else  { 
    if (memcmp (&end,   &ctime, sizeof (start)) > 0  &&
        memcmp (&ctime, &start, sizeof (start)) > 0)  return (1);
  }
  return (0);                                  /* signal off, blinking on     */
}


/******************************************************************************/
/*        Task 3 'blinking': runs if current time is outside start & end time */
/******************************************************************************/
void blinking (void) _task_ BLINKING  {        /* blink yellow light          */
  red    = 0;                                  /* all lights off              */
  yellow = 0;                                   /*闪烁黄灯*/
  green  = 0;                                   /*关灯*/
  stop   = 0;                                   /**/
  walk   = 0;                                   /**/

  while (1)  {                                 /* endless loop                */
    yellow = 1;                                /* yellow light on             */
    os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks  */
    yellow = 0;                                /* yellow light off            */
    os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks  */
    if (signalon ())  {                        /* if blinking time over       */
      os_create_task (LIGHTS);                 /* start lights                */
      os_delete_task (BLINKING);               /* and stop blinking           */
    }
  }
}


/******************************************************************************/
/*      Task 4 'lights': executes if current time is between start & end time */
/******************************************************************************/
void lights (void) _task_ LIGHTS  {            /* traffic light operation     */
  red    = 1;                                  /* red & stop lights on        */
  yellow = 0;
  green  = 0;
  stop   = 1;
  walk   = 0;
  while (1)  {                                 /* endless loop                */
    os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks */
    if (!signalon ())  {                       /* if traffic signal time over */
      os_create_task (BLINKING);               /* start blinking              */
      os_delete_task (LIGHTS);                 /* stop lights                 */
    }
    yellow = 1;
    os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks */
    red    = 0;                                /* green light for cars        */
    yellow = 0; 
    green  = 1;
    os_clear_signal (LIGHTS);
    os_wait (K_TMO, 200, 0);                   /* wait for timeout: 200 ticks */
    os_wait (K_TMO + K_SIG, 250, 0);           /* wait for timeout & signal   */
    yellow = 1;
    green  = 0;
    os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks */
    red    = 1;                                /* red light for cars          */
    yellow = 0;
    os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks */
    stop   = 0;                                /* green light for walkers     */    
    walk   = 1;
    os_wait (K_TMO, 250, 0);                   /* wait for timeout: 250 ticks */
    os_wait (K_TMO, 250, 0);                   /* wait for timeout: 250 ticks */
    stop   = 1;                                /* red light for walkers       */        
    walk   = 0;
  }
}


/******************************************************************************/
/*        Task 5 'keyread': process key stroke from pedestrian push button    */
/******************************************************************************/
void keyread (void) _task_ KEYREAD  {
  while (1)  {                                 /* endless loop                */
    if (key)  {                                /* if key pressed              */
      os_send_signal (LIGHTS);                 /* send signal to task lights  */
    }
    os_wait (K_TMO, 2, 0);                     /* wait for timeout: 2 ticks   */
  }
}

http://www.xdnf.cn/news/11932.html

相关文章:

  • 一种基于Service自动生成Controller的实现
  • 1.springmvc基础入门(一)
  • 栈-20.有效的括号-力扣(LeetCode)
  • 《复制粘贴的奇迹:原型模式》
  • C++课设:学生成绩管理系统
  • 【Axure视频教程】下载和安装Axure汉化包
  • 什么是单光谱
  • Python学习(6) ----- Python2和Python3的区别
  • 嵌入式学习笔记 - freeRTOS任务设计要点
  • 树莓派系列教程第九弹:Cpolar内网穿透搭建NAS
  • H5项目实现图片压缩上传——2025-06-04
  • 无法通过windows功能控制面板自动安装或卸载windows server角色或功能
  • 低成本奶泡棒解决方案WD8001功能说明
  • Hadoop企业级高可用与自愈机制源码深度剖析
  • docker的基本命令
  • AI界面遭劫持:Open WebUI被滥用于挖矿程序与隐蔽AI恶意软件
  • 如何快速找出某表的重复记录 - 数据库专家面试指南
  • 【力扣】3403. 从盒子中找出字典序最大的字符串 I
  • 判断:有那种使用了局部变量的递归过程在转换成非递归过程时才必须使用栈
  • AI批改作文的软件推荐:提升写作效率的智能工具
  • 厂商与经销商供应链数据协同:策略、实践与深度价值挖掘
  • 在WPS中如何启用宏VBA wps.vba.exe下载和安装
  • 【JVM】Java类加载机制
  • Python 多进程编程全面学习指南
  • Unity 大型手游碰撞性能优化指南
  • Axure高保真LayUI框架 V2.6.8元件库
  • [蓝桥杯]卡片换位
  • Modbus转EtherNET IP网关开启节能改造新范式
  • 细说C语言将格式化输出到字符串的函数sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l
  • IEC 61347-1:2015 灯控制装置安全标准详解