📄 traffic.c
字号:
/**************************************************************************
*
* ARM Strategic Support Group
*
***************************************************************************/
/***************************************************************************
*
* Module : traffic.c
* Description : Traffic light program is designed to simulate the US and
* European Traffic Light System. It is designed to work
* with the ARM Evaluator7T Board.
* Tool Chain : ARM Developer Suite v1.0
* Platform : Evaluator7T
* History :
*
* 980301 ASloss
* - started working on the design and structure.
*
* 980408 ASloss
* - added in-line assembler example.
*
* 980428 ASloss
* - added interrupt handler
*
* 980911 ASloss
* - added switch_write to output reset banner
*
* 990202 ASloss
* - added Multi-ICE support
*
* 2000-03-27 Andrew N. Sloss
* - ported to Evaluator7T
*
**************************************************************************/
/**************************************************************************
* IMPORTS
**************************************************************************/
#include <stdio.h>
#include <assert.h>
#include "timer.h"
#include "led.h"
#include "irq_ser.h"
#include "segment.h"
/**************************************************************************
* MACROS
**************************************************************************/
/* -- uncomment for EUROPEAN style traffic lights ..... */
#define EUROPEAN 1
/* -- Non embedded library ............................ */
// #define FULL_LIB 1
#define DEBUG 1
#define HARDWARE 1
#define LIGHT int
#define RED 0
#define AMBER 1
#define GREEN 2
#define COMPLETE 3
#define STATE int
#define ON 1
#define OFF 0
#define pRED active.light_red
#define pAMBER active.light_amber
#define pGREEN active.light_green
#define angel_SWI 0x123456
#define MAXCYCLES 100000 // maximum number of cycles ......
/**************************************************************************
* MISC
**************************************************************************/
__swi (angel_SWI) void _Exit(unsigned op, unsigned except);
#define Exit() _Exit(0x18,0x20026)
__swi (angel_SWI) void _WriteC(unsigned op, const char *c);
#define WriteC(c) _WriteC (0x3,c)
/**************************************************************************
* DATATYPES
**************************************************************************/
typedef struct {
LIGHT light_red;
LIGHT light_amber;
LIGHT light_green;
} traffic_lightstr;
/**************************************************************************
* STATICS
**************************************************************************/
static traffic_lightstr active;
static int toggle = 0;
static int cycles = 0; // start of the cycles...
static int phase = 0; // start of the traffic phase
/**************************************************************************
* ROUTINUES
**************************************************************************/
/* -- traffic_change -----------------------------------------------------
*
* Description : This routine changes the state of a particular LED.
*
* Parameters : LIGHT w - RED | GREEN | AMBER
* : STATE s - ON | OFF
* Return : none...
* Notes : Updates 'active' an internal global variable.
*
*/
void traffic_change (LIGHT w, STATE s)
{
// -- process ................................................
switch (w) {
case RED :
pRED = s;
if(s) LED_2_ON; else LED_2_OFF;
break;
case AMBER :
pAMBER = s;
if(s) LED_3_ON; else LED_3_OFF;
break;
case GREEN :
pGREEN = s;
if(s) LED_4_ON; else LED_4_OFF;
break;
case COMPLETE :
if(s) LED_1_ON; else LED_1_OFF;
}
}
/* -- traffic_init --------------------------------------------------------
*
* Description : Initializes the LED's on the ARM Evaluation board to be
* OFF. Setup the internal GLOBALS to indicate that the
* LED's are OFF.
*
* Parameters : none...
* Return : none...
* Notes : none...
*
*/
void traffic_init (void)
{
// -- init internal ..........................................
pRED = 0;
pAMBER = 0;
pGREEN = 0;
// -- init hardware ..........................................
traffic_change (RED, OFF);
traffic_change (AMBER, OFF);
traffic_change (GREEN, OFF);
traffic_change (COMPLETE, OFF);
irq_init ();
timer_init();
segment_init();
}
/* -- traffic_addone -----------------------------------------------------
*
* Description : return an number incremented by 1
*
* Parameters : int num - number passed into the function.
* Return : int
* Notes : an example of using in-line assembler.
*
*/
int traffic_addone ( int num )
{
__asm
{
mov r0, num // r0 = num
add r0, r0, #1 // r0 = r0 + 1
mov num, r0 // num = r0
}
return num;
}
/* -- traffic_sequence ------------------------------------------------
*
* Description : Runs the traffic light sequence...
*
* Parameters : int cycles - number of cycles
* Return : none...
* Notes : none...
*
*/
/****************************************************
* USA Traffic Lights
****************************************************/
#ifndef EUROPEAN
void traffic_sequence (void)
{
// -- init ...............................................................
if ( cycles >= MAXCYCLES ) Exit();
// -- USA Traffic Lights - Terminate
segment_set (phase);
// -- process ............................................................
switch (phase) {
case 0 : // -- switch the GREEN(off) then AMBA(on) ...
traffic_change (GREEN,OFF);
traffic_change (AMBER,ON);
phase = 1;
break;
case 1 : // -- switch the AMBA(off) then RED(on) ....
traffic_change (AMBER, OFF);
traffic_change (RED, ON);
phase = 2;
break;
case 2 : // -- switch the RED(off) then GREEN(on) ...
traffic_change (RED,OFF);
traffic_change (AMBER,OFF);
traffic_change (GREEN,ON);
phase = 0;
cycles = traffic_addone (cycles);
break;
}
}
#else
/****************************************************
* EUROPEAN Traffic Lights
****************************************************/
void traffic_sequence ( void )
{
// -- init ...............................................................
if ( cycles >= MAXCYCLES ) Exit();
// -- European Traffic Lights - Terminate
segment_set (phase);
// -- process ............................................................
switch (phase) {
case 0 : // -- switch the GREEN(off) then AMBA(on) ...
traffic_change (GREEN,OFF);
traffic_change (AMBER,ON);
phase = 1;
break;
case 1 : // -- switch the AMBA(off) then RED(on) ....
traffic_change (AMBER,OFF);
traffic_change (RED,ON);
phase = 2;
break;
case 2 : // -- switch the RED(on) then AMBER(on) ...
traffic_change (AMBER,ON);
phase = 3;
break;
case 3 : // -- switch the RED(off) then GREEN(on) ...
traffic_change (RED,OFF);
traffic_change (AMBER,OFF);
traffic_change (GREEN,ON);
phase = 0;
cycles = traffic_addone (cycles);
break;
}
}
#endif
/* -- traffic_interrupt ----------------------------------------------------
*
* Description : The interrupt button has been pressed. Toggles D1 LED...
*
* Parameters : none...
* Return : none...
* Notes : none...
*
*/
void traffic_interrupt (void)
{
if( toggle == 0 ) {
traffic_change (COMPLETE, ON);
toggle = 1;
} else {
toggle = 0;
traffic_change (COMPLETE, OFF);
}
}
/* -- traffic_writes --------------------------------------------------------
*
* Description : write a string via the Angel SWI call WriteC
*
* Parameters : const char *string - string to be written
* Return : none...
* Notes : none...
*
*/
void traffic_writes (const char *string)
{
int pos = 0;
while (string[pos] != 0) WriteC(&string[pos++]);
}
/* -- traffic_start --- */
void traffic_start (void)
{
timer_start();
}
void traffic_idle (void)
{
do { } while (1); // -- endless loop for Multi-ICE
}
/* -- C_Entry --------------------------------------------------------------
*
* Description : entry point for traffic lights
*
* Parameters : none...
* Return : return 1 if successful
* Notes : none...
*
*/
int C_Entry ( void )
{
// -- initialize all internal structures .......................
traffic_init ();
traffic_start();
// -- reset banner .............................................
traffic_writes ("\n -- Traffic Light Example Now Initialized \n");
#ifdef EUROPEAN
traffic_writes (" -- European Traffic Lights \n\n");
#else
traffic_writes (" -- USA Traffic Lights \n\n");
#endif
traffic_writes ("\n ********************************************* \n");
traffic_writes (" *** Remember to RESET the board after USE *** \n");
traffic_writes (" ********************************************* \n");
// -- process ..................................................
traffic_sequence ();
// -- end banner ...............................................
traffic_idle();
// Should never get here...
return 0;
}
/**************************************************************************
* END OF traffic.c
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -