📄 traffic2.c
字号:
}
}
}
/****************************************************************************/
/* signalon: check if clock time is between start and end */
/****************************************************************************/
bit signalon () {
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 cur. time is outside start & end time */
/****************************************************************************/
void blinking (void) _task_ BLINKING { /* blink yellow light */
red___1 = 0; /* all lights off */
yellow1 = 0;
green_1 = 0;
stop__1 = 0;
walk__1 = 0;
red___2 = 0;
yellow2 = 0;
green_2 = 0;
stop__2 = 0;
walk__2 = 0;
while (1) { /* endless loop */
phaseno = 10;
yellow1 = 1; /* yellow light on */
yellow2 = 1;
os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/
yellow1 = 0; /* yellow light off */
yellow2 = 0;
os_wait (K_TMO, 30, 0); /* wait for timeout: 30 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 cur. time is between start & end time */
/****************************************************************************/
void lights (void) _task_ LIGHTS { /* traffic light operation */
/* *** P H A S E 9 *** */
/* dir 1: all red */
/* dir 2: all red */
phaseno = 9;
red___1 = 1; /* red & stop lights on */
yellow1 = 0;
green_1 = 0;
stop__1 = 1;
walk__1 = 0;
red___2 = 1;
yellow2 = 0;
green_2 = 0;
stop__2 = 1;
walk__2 = 0;
while (1) { /* endless loop */
if (!signalon ()) { /* if signal time over */
os_create_task (BLINKING); /* start blinking */
os_delete_task (LIGHTS); /* stop lights */
}
/* *** P H A S E 0 *** */
/* dir 1: prepare for green */
/* dir 2: stick to red */
phaseno = 0;
red___1 = 1;
yellow1 = 1;
yellow2 = 0;
red___2 = 1;
os_clear_signal (LIGHTS);
keypressed1 = 0;
keypressed2 = 0;
cardetected1 = 0;
cardetected2 = 0;
os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/
/* *** P H A S E 1 *** */
/* dir 1: switch to green */
/* dir 2: stick to red, allow walk */
phaseno = 1;
red___1 = 0;
yellow1 = 0;
green_1 = 1;
stop__2 = 0;
walk__2 = 1;
os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/
/* *** P H A S E 2 *** */
/* dir 1: accept pedestrian button */
/* dir 2: accept car detect */
phaseno = 2;
os_wait (K_TMO + K_SIG, 250, 0); /* wait for timeout & signal */
/* *** P H A S E 3 *** */
/* dir 1: switch to yellow */
/* dir 2: stick to red, forbid walk */
phaseno = 3;
green_1 = 0;
yellow1 = 1;
stop__2 = 1;
walk__2 = 0;
os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/
/* *** P H A S E 4 *** */
/* dir 1: switch to red */
/* dir 2: prepare for green */
phaseno = 4;
red___1 = 1;
yellow1 = 0;
yellow2 = 1;
os_clear_signal (LIGHTS);
keypressed1 = 0;
keypressed2 = 0;
cardetected1 = 0;
cardetected2 = 0;
os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/
/* *** P H A S E 5 *** */
/* dir 1: stick to red, allow walk */
/* dir 2: switch to green */
phaseno = 5;
stop__1 = 0;
walk__1 = 1;
red___2 = 0;
yellow2 = 0;
green_2 = 1;
os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/
/* *** P H A S E 6 *** */
/* dir 1: accept car detect */
/* dir 2: accept pedestrian button */
phaseno = 6;
os_wait (K_TMO + K_SIG, 250, 0); /* wait for timeout & signal */
/* *** P H A S E 7 *** */
/* dir 1: stick to red, forbid walk */
/* dir 2: switch to yellow */
phaseno = 7;
stop__1 = 1;
walk__1 = 0;
green_2 = 0;
yellow2 = 1;
os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/
}
}
/****************************************************************************/
/* Task 5 'keyread': process key strokes from pedest. push buttons */
/****************************************************************************/
void keyread (void) _task_ KEYREAD {
while (1) { /* endless loop */
if (phaseno < 4) { /* phaseno = 0..3 */
if (!key2) { /* if key pressed */
keypressed2 = 1;
os_send_signal (LIGHTS); /* send signal to 'lights' */
os_wait (K_TMO, 5, 0); /* wait for timeout: 5 ticks */
}
}
else { /* phaseno = 4..7 */
if (!key1) { /* if key pressed */
keypressed1 = 1;
os_send_signal (LIGHTS); /* send signal to 'lights' */
os_wait (K_TMO, 5, 0); /* wait for timeout: 5 ticks */
}
}
os_wait (K_TMO, 2, 0); /* wait for timeout: 2 ticks */
}
}
/****************************************************************************/
/* Task 6 'car_det1': process interrupt from car detector 1 */
/****************************************************************************/
void car_det1 (void) _task_ CAR_DET1 {
os_attach_interrupt (0); /* Attach INT0 */
TCON |= 0x01; /* Use edge-triggered */
while (1) { /* endless loop */
os_wait (K_INT, 0xff, 0); /* Wait for interrupt */
if (phaseno > 3) { /* phaseno = 4..7 */
if (!cardetected2 && !keypressed1 && !cardetected1) {
os_send_signal (LIGHTS); /* send signal to 'lights' */
}
}
cardetected1 = 1;
}
}
/****************************************************************************/
/* Task 7 'car_det2': process interrupt from car detector 2 */
/****************************************************************************/
void car_det2 (void) _task_ CAR_DET2 {
os_attach_interrupt (2); /* Attach INT1 */
TCON |= 0x04; /* Use edge-triggered */
while (1) { /* endless loop */
os_wait (K_INT, 0xff, 0); /* Wait for interrupt */
if (phaseno < 4) { /* phaseno = 0..3 */
if (!cardetected1 && !keypressed2 && !cardetected2) {
os_send_signal (LIGHTS); /* send signal to 'lights' */
}
}
cardetected2 = 1;
}
}
/****************************************************************************/
/* MAIN : Start the system */
/****************************************************************************/
void main(void)
{
os_start_system (INIT); /* start the first task */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -