Simple design of traffic lights with C51 MCU 2

Together with the simple traffic light design of the C51 microcontroller above .

In Design 2, I chose to use a timer instead of a delay function. I found a simple and easy-to-understand article about the introduction of the timer - C51 timer_From entry to snake catcher's blog

The difference between the delay function and the timer: the delay function will occupy the right to use the CPU. If the delay is in progress, other actions need to wait for the end of the delay to proceed, and the timer can be configured to enable the interrupt function, which can be performed independently without occupying the CPU. .

Overview 1: The digital tube is used to display numbers.

Two: LED traffic lights are essentially diodes, containing red, green and yellow, as traffic lights.

Three: Configure the timer for timing. (use of timer)

For the second item, I wanted to use the lcd1602 display to do it, but... the lcd1602 should be connected to the pins when the bottom layer is configured. When the lcd1602 is displayed, it will interfere with the diode's on and off, so I still use it. A nixie tube from the previous article.

#include <REGX51.H>
unsigned int count=0,led_count=0;
unsigned int Number;
sbit Green_Light = P1^5;
sbit Yellow_Light = P1^6;
sbit Red_Light = P1^7;
unsigned int i=0;
unsigned int x[23]={10,9,8,7,6,5,4,3,2,1,3,2,1,10,9,8,7,6,5,4,3,2,1};
void Timer0_init()
{
    TMOD = 0x01;
    TL0 = 0x00;
    TH0 = 0xDC;//定时器的初始化配置成10ms进入一次中断函数。
    TF0 = 0;
    TR0 = 1;//用以计时,当爆表后TF0会跳转为1(通过上述配置即10ms后TF0=1)
    ET0=1;
    EA=1;
    PT0=0;
}
void NiXie(unsigned char Number)
{
        switch(Number)
    {
        case 0:P0_0=P0_1=P0_2=P0_3=P0_4=P0_5=0;break;
        case 1:P0_1=P0_2=0;break;
        case 2:P0_0=P0_1=P0_3=P0_4=P0_6=0;break;
        case 3:P0_0=P0_1=P0_2=P0_3=P0_6=0;break;
        case 4:P0_1=P0_2=P0_5=P0_6=0;break;
        case 5:P0_0=P0_2=P0_3=P0_5=P0_6=0;break;
        case 6:P0_0=P0_2=P0_3=P0_4=P0_5=P0_6=0;break;
        case 7:P0_0=P0_1=P0_2=0;break;
        case 8:P0_0=P0_1=P0_2=P0_3=P0_4=P0_5=P0_6=0;break;
        case 9:P0_0=P0_1=P0_2=P0_3=P0_5=P0_6=0;break;
        case 10:P0_6=0;break;
    }
}
void main()
{
    Green_Light = 1,Red_Light = 1,Yellow_Light=1;
    Number = 10;
    Timer0_init();//初始化工作
    while(1)
    {}
}
Timer0() interrupt 1//TF0为一时执行终端函数,执行完后TF0自动归0
{
    TL0 = 0x00;
    TH0 = 0xDC;//再把初值设为9126,保证下次还能再进入中断函数,并且也用时10ms
    count++,led_count++;
    NiXie(x[i]);//数码管的闪烁,其实每个数字闪烁了100下,每次10ms。
    if(led_count >= 100)
    {
        P0_0=P0_1=P0_2=P0_3=P0_4=P0_5=P0_6=P0_7=1;//return
        i++;
        led_count = 0;
        if(i==23)
        {i=0;}
    }
    
    if(count>0&&count<1000)//0~第10秒
    {
        Green_Light = 0;
    }
    if(count>=1000&&count<1300)//第10秒~第13秒
    {
        Green_Light = 1;//关绿灯开黄灯
        Yellow_Light = 0;
    }
    if(count>=1300&&count<2300)//第13秒~第23秒
    {
        Yellow_Light = 1;//关黄灯开红灯
        Red_Light = 0;
    }
    if(count>=2300)
    {
        Red_Light = 1;
        count = 0;//一遍后归零
    }
}

Machine cycle = 12 x clock period = 12 x (1/clock frequency) seconds = 12 / clock frequency seconds = 12 / 11059200 seconds = 12 000 000 / 11059200 microseconds = 1.085 microseconds

Configure the timing of ten milliseconds: 10ms needs to count 9216 machine cycles, you let it change from 65536-9126=56320 (hexadecimal expressed as

0xDC00) start counting

In this way TL0=0x00; TH0=0xDC.

Interrupt function : Like the external interrupt, the timer interrupt also has an interrupt function. Similarly, when the program executes the interrupt function, the interrupt flag bit of TF0 will be automatically cleared to 0, so as long as we use the timer interrupt function, then TF0 can be used without Appears in programming again.

In the code, I used count and led_count to count separately. After the main function jumps to the interrupt function each time, it takes 10ms. Each time the value of count and led_count is increased by one, it takes 1 second to enter 100 times.

If there is something wrong, I look forward to your correction.

Experimental phenomena:

Guess you like

Origin blog.csdn.net/qq_62262788/article/details/128600845