Traffic light control system program

xiaoxiao2021-03-06  42

A course design for the previous period, using SST 89C54 MSC to do a central controller, I originally requested to use ASM to write the program, I think that the development efficiency is too low, or C came, so I'm bored now. Post it out to play. KEIL used by IDE. #include

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 pedestriansbit key = p2 ^ 0; // ns_color: // 0 - Red / 1 - Yellow / 2 - Green

// LED_POS: 0, 1, 2, 3 - ns_h, ns_l, ew_h, ew_lunsigned 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) {copy red: // When NS is red, if key is presssed, ns will be 20s of green, or ns shouth be 18s of green ns_color = green; if (PRESSED) {Ns_time = 20; PRESSE = 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; PRESSE 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) {// initizationns_color = red; ew_color = green; ns_time = 20; ew_time = 18; LED_POS = ns_h; presssed = 0; debounce = 0; counter = 0; // Timer 2e16-5 * 10E-3 /(11.0592e-6/12 )=60536=0Xec78tmod=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; second; secondup ();

Display (); // Display The Right Light with The Current Second Time.

// end of doittf0 = 0; // reset the timer overflow status bit}}

转载请注明原文地址:https://www.9cbs.com/read-53208.html

New Post(0)