Daddy's amateur MCU learning: Using Digital tubes and LEDs to achieve traffic lights (first edition)

Source: Internet
Author: User

/*

* Sidewalk traffic light program: led2 (red light), led3 (yellow light), and led4 (green light)
* The red light is 30 seconds, the green light is 3 seconds, the green light is 15 seconds, and the countdown is
The lighting time is displayed on the digital tube.
* The crystal oscillator is 11.0592 MHz. LEDs and digital tubes are controlled by 38 decoders and t0 is interrupted.

* Program idea: As the decoder can only output one low level at a time, the digital tube and led must be lit in dynamic scanning mode, with the interruption interval of 1 ms.

* Countdown of the Light-on time, 60 seconds for the red light, 20 seconds for the green light, and 3 seconds for the yellow light. The sequence is: red-green-Yellow-red cycle. Therefore, every 1000 interruptions occur, and the number is decreased.

*/

# Include <reg52.h>
Unsigned char K; // 1 s timing flag
Unsigned char smg_buffer [2] = {0xff, 0xff}; // Digital Display Buffer

Unsigned char red, yellow, green;
Sbit led2_red = P0 ^ 0; // red light Interface
Sbit led3_yellow = P0 ^ 1; // yellow light Interface
Sbit led4_green = P0 ^ 2; // green light Interface

// The third-eight decoder controls the digital tube, with one digit: 01000, ten digits: 01001
Sbit addr0 = p1 ^ 0;
Sbit addr1 = p1 ^ 1;
Sbit addr2 = p1 ^ 2;
Sbit addr3 = p1 ^ 3;
Sbit enled1 = p1 ^ 4;

// Digital tube 0 ~ 9
Unsigned char code SMG [10] =
{0xc0, 0xf9, 0xa4, 0xb0, 0x99,0x92,0x82, 0xf8, 0x80, 0x90 };

// Digital Display
Void smg_display ()
{
Unsigned char Shijian; // lighting time
Static unsigned char color = 0; // color: 0: Red, 1: yellow, 2: Green, starts from red by default
If (Shijian <= 1) // starts to change when the last to one second,
{
Switch (color)
{
Case 0: color = 2; Shijian = 10; Red = 1; break; // The red light turns green after 60 seconds. The red light marks the position 1.
Case 1: color = 0; Shijian = 3; yellow = 1; break; // yellow light for 3 seconds, yellow to red after the end, yellow to 1
Case 2: color = 1; Shijian = 20; Green = 1; break; //; the green light turns yellow after 20 seconds. The green light marks the position 1.
Default: break;
}
}
Else Shijian --; // decrease the time
If (Shijian = 1) // time changes until 1
{
Red = 0; yellow = 0; Green = 0; // the position of each flag is 0.
}
Smg_buffer [0] = SMG [Shijian % 10]; // digital display, single-digit
Smg_buffer [1] = SMG [Shijian/10% 10]; // ten
}

// Timer Configuration
Void config_interruptime0 ()
{
Tmod = tmod & 0xf0;
Tmod = tmod | 0x01;
Th0 = 0xfc;
Tl0 = 0x66;
Ea = 1;
Et0 = 1;
Tr0 = 1;
}

Void Init ()
{
Addr3 = 1;
Enled1 = 0;
}
Int main ()
{
Init ();
Config_interruptime0 ();
While (1)
{
If (k = 1) // check whether the 1-second scheduled time has been reached
{
K = 0; // K Reset
Smg_display (); // refresh the display
}
}
Return 0;
}

Void interruptime0 () interrupt 1
{
Static unsigned int CNT = 0;
Static unsigned int I = 0;
Th0 = 0xfc; // initial value of 1 ms
Tl0 = 0x66;
CNT ++;

Switch (I)
{
Case 0: p0 = 0xff; addr0 = 0; addr1 = 0; addr2 = 0; I ++; p0 = smg_buffer [0]; break; // single-digit Digital Display
Case 1: p0 = 0xff; addr0 = 1; addr1 = 0; addr2 = 0; I ++; p0 = smg_buffer [1]; break; // display 10 digits
Case 2: p0 = 0xff; addr0 = 0; addr1 = 1; addr2 = 1; I = 0; // LED Display
If (Red = 1)
{
Led2_red = 0;
}
If (yellow = 1)
{
Led3_yellow = 0;
}
If (Green = 1)
{
Led4_green = 0;
}
Break;
}
If (CNT = 1000)
{
K = 1; // when the 1 second is reached, k = 1
CNT = 0;
}
}

Daddy's amateur MCU learning: Using Digital tubes and LEDs to achieve traffic lights (first edition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.