TITL 'Traffic Lights Example Control Program' *----------------------------------------------------------- * Erland Muller * 30/07/02 * Simple Traffic Lights Example Control Program * ============================================= * * This TMS9995 Macro Assembler program is part of the * documentation of the macro library for PLC-IL style * controller programs (controlmacro_05.asm) and is intended * to demonstrate the application of the macros provided at: * http://desyntwww.desy.de/~erlandm/tms/controlmacro.html * * Copyright (C) 2002, Erland Muller * This is free software with ABSOLUTELY NO WARRANTY. The * GNU General Public License as published by Free Software * Foundation version 2 or later applies. *----------------------------------------------------------- * * The sample program implements simple pedestrian crossing * traffic lights. Upon request, expressed by pressing the * push button, the traffic lights turn red after a while in * order to stop the cars and to permit pedestrians crossing * the road. * * The sequence of events is defined in the chart below. * * Sequence Function Chart (SFC) Traffic Lights * -------------------------------------------- * * +=========+ * || Start || * +=========+ * | * + not started * | * +---->---+ * | | * | +---------+ +--------------------+ * | | green |----------| green light on | * | +---------+ | set started on | * | | +--------------------+ * | + request valid * | | * | +---------+ +--------------------+ * | | yellow |----------| yellow light on | * | +---------+ | yellow time running| * | | +--------------------+ * | + yellow time up * | | +--------------------+ * | +---------+ | red light on | * | | red |----------| red time running | * | +---------+ | reset dom. request | * | | +--------------------+ * | + red time up * | | +--------------------+ * | +---------+ | red light on | * | | redyel |----------| yellow light on | * | +---------+ | yellow time running| * | | +--------------------+ * | + yellow time up * | | * +--------+ * * * Steps of the control sequence are implemented using * elementary function blocks in the following manner. * * Control Sequence Standard Block * ------------------------------- * current * step * +----+ +-----+ * previous step--| & | | SR | * transition--| |--------|S1 | * +----+ | Q| * next step--|R | * +-----+ * * There is only one interesting transition here. The * evaluation of the "request valid" transition is best * shown in a Function Block Diagram (FBD). * * Transition: Request Valid * * BPrssd * +-----+ ReqVal * push | RS | +----+ +-----+ * button--|S Q|---------| & | | TON | * | | | |--------| Q|--request * red--|R1 | green--| | cPPDT--| | valid * +-----+ +----+ +-----+ * * cPPDT = const pedestrian penalty delay time * *----------------------------------------------------------- * * Now, solving this problem using pure assembler is straight * forward and you might ask, why all this formalism. While a * small problem like this demonstration is easily manageable * without any formalism, a simple, single elevator control * gets rather messy without formalism and macros. * *----------------------------------------------------------- * * Include Macro Files * COPY CTRLMACR.ASM Controller Macros COPY STRCMACR.ASM Structured Progr. * * Declarations and Definitions * ---------------------------- * IDT 'TRAFFICL' * * Register Usage * RVKE EQU 0 Result of Logical Operation RTMP EQU 2 Temporary Store RI EQU 7 Index register RSTACK EQU 10 Stack RCRU EQU 12 Dedicated CRU Address Reg. * * Constant Time Intervals * CPPDT EQU 20000 ms pedestrians wait 20 seconds CYELT EQU 2000 ms yellow time 2 seconds CREDT EQU 10000 ms red time 10 seconds * * Constant I/O-Lines * CRUIN EQU >300 CRU address digital input CRUOUT EQU >380 CRU address digital output * * Variable Declaration * DBEGIN EQU $ Begin of data area EBUTTN BSS 1 Bool push button AGREEN BSS 1 Bool output green light AYELLO BSS 1 Bool output yellow light ARED BSS 1 Bool output red light MSTART BSS 1 Bool Sequence is Started SGREEN BSS 1 SR Step green SYELLO BSS 1 SR Step yellow SRED BSS 1 SR Step red SREDYE BSS 1 SR Step red/yellow BPRSSD BSS 1 RS Button pressed EVEN TRQVAL BSS 6 Timer Request Valid Delay Time TRED BSS 6 Timer Red Time TYELLO BSS 6 Timer Yellow Time DATEND EQU $ End of data area DSTACK BES 20 Stack Area * * Traffic Lights Main Program * --------------------------- * DEF TLIGHT Export Start Address TLIGHT LI RSTACK,DSTACK initialise stack BL @INIMEM initialise memory LOOP BL @READIN read input BL @TRANSI transitions BL @CTRLSQ control sequence BL @ACTION actions BL @WRITOU write output JMP LOOP continue for ever * * INIMEM: Initialise Data Memory Area * INIMEM PUSH RLINK pure formalism here LI RI,DBEGIN DBEGIN to DATEND INILP CLR *RI+ initalised to zero CI RI,DATEND JL INILP POP RLINK RT * * CTRLSQ: Control Sequence * CTRLSQ PUSH RLINK * step yellow LD @SGREEN S1:= predecessor AND @TRQVAL AND transition SR SYELLO,@SRED R := successor * step red LD @SYELLO predecessor yellow AND @TYELLO transition time up SR SRED,@SREDYE successor red/yellow * step red/yellow LD @SRED AND @TRED SR SREDYE,@SGREEN * step green, which has two predecessors NOT @MSTART transition from start ST RTMP to temporary store LD @SREDYE AND @TYELLO transition from yel. OR RTMP or previously stored SR SGREEN,@SYELLO * end of CTRLSQ POP RLINK RT * * TRANSI: Compute Transitions - Request Valid * TRANSI PUSH RLINK LD @EBUTTN S := PushButton RS BPRSSD,@SRED R1 := StepRed AND @SGREEN TON TRQVAL,CPPDT delay request POP RLINK RT * * ACTION: Determine Actions * ACTION PUSH RLINK * green light and sequence started LD @SGREEN load step green ST @AGREEN store green light SET @MSTART set started * yellow light and yellow time LD @SYELLO step yellow and OR @SREDYE step red/yellow ST @AYELLO TON TYELLO,CYELT * red light and red time running LD @SRED OR @SREDYE ST @ARED LD @SRED TON TRED,CREDT * end of ACTION POP RLINK RT * * READIN: Read Input * READIN PUSH RLINK LI RCRU,CRUIN Load Input Immediate LINP @EBUTTN,0 with 0-bit offset POP RLINK RT * * WRITOU: Write Output * WRITOU PUSH RLINK LI RCRU,CRUOUT OUTP @AGREEN,2 Output to CRU-Lines OUTP @AYELLO,1 with offsets 2, 1, OUTP @ARED,0 and 0 bits POP RLINK RT END TLIGHT