前一段时间做的一个课程设计,使用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
}
}