[图片]请问怎么修改上方代码,从而实现红绿黄灯循环闪烁


#include <ny8.h>
#include "NY8_constant.h"
#define UPDATE_REG(x)    __asm__("MOVR _" #x ",F")

void main(void)
{
// Initialize GPIO
//    ; PORTB I/O state
//    ; PB4 set input mode and enable pull-high resistor
//    ; PB2 set output mode 
//    ; PB1 set input mode and enable pull-low resistor
//    ; PB0 set open-drain output mode
    BODCON = C_PB0_OD;                    // Set PB0 open-drain output mode 
    BPLCON = (unsigned char)~C_PB1_PLB;    // Enable PB1 Pull-Low Resistor,others disable
    BPHCON = (unsigned char)~C_PB4_PHB;    // Enable PB4 Pull-High Resistor,others disable
    IOSTB = C_PB4_Input | C_PB1_Input;    // Set PB4 & PB1 to input mode,others set to output mode
    PORTB = 0x05;                        // PB2 & PB0 output high

    while(1)
    {
        CLRWDT();
//    ; While PB4 inputs high/low, then PB2 outputs high/low
        if(PORTBbits.PB4)
            PORTBbits.PB2 = 1;
        else
            PORTBbits.PB2 = 0;
//    ; While PB1 inputs high/low, then PB0 outputs high/low
        if(PORTBbits.PB1)
            PORTBbits.PB0 = 1;
        else
            PORTBbits.PB0 = 0;
            //    ; While PB1 inputs high/low, then PB0 outputs high/low
        if(PORTBbits.PB5)
            PORTBbits.PB3 = 1;
        else
            PORTBbits.PB3 = 0;
    }

[图片]
请问怎么修改上方代码,从而实现红绿黄灯循环闪烁。


#include <ny8.h>
#include "NY8_constant.h"
#define UPDATE_REG(x)    __asm__("MOVR _" #x ",F")

void delay()
{
    unsigned char i,j;
    for(i=5;i>0;i--)
    for(j=132;j>0;j--);
}

void main(void)
{
    // Initialize GPIO
    BODCON = C_PB0_OD;                    // Set PB0 open-drain output mode 
    BPLCON = (unsigned char)~C_PB1_PLB;    // Enable PB1 Pull-Low Resistor,others disable
    BPHCON = (unsigned char)~C_PB4_PHB;    // Enable PB4 Pull-High Resistor,others disable
    IOSTB = C_PB4_Input | C_PB1_Input;    // Set PB4 & PB1 to input mode,others set to output mode
    PORTB = 0x05;                        // PB2 & PB0 output high

    while (1)
    {
        CLRWDT();

        // 红
        PORTBbits.PB2 = 1;
        PORTBbits.PB0 = 0;
        PORTBbits.PB3 = 0;
        delay();

        //绿
        PORTBbits.PB2 = 0;
        PORTBbits.PB0 = 1;
        PORTBbits.PB3 = 0;
        delay();

        // 黄
        PORTBbits.PB2 = 0;
        PORTBbits.PB0 = 0;
        PORTBbits.PB3 = 1;
        delay();
    }
}

#define LED_R    PB2 // 红灯
#define LED_G    PB3 // 绿灯  
#define LED_Y    PB4 // 黄灯

void main(void) {

  int i = 0;
  
  while(1) {
    i++;
    switch(i%3) {
      case 0:
        LED_R = 1; LED_G = 0; LED_Y = 0; // 红灯亮
        break;
      case 1:
        LED_R = 0; LED_G = 1; LED_Y = 0; // 绿灯亮  
        break;
      case 2:
        LED_R = 0; LED_G = 0; LED_Y = 1; // 黄灯亮
        break;
    }
    
    Delay_ms(300); // 延时切换
  }

}

该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:你可以通过修改while循环内的代码来实现红绿黄灯循环闪烁。具体修改如下:
1、 定义一个变量来记录当前的灯的状态,例如:

unsigned char light_state = 0;    // 0表示红灯,1表示绿灯,2表示黄灯

2、 修改while循环内的代码,让不同的状态下控制不同的灯亮灭。例如:

while(1)
{
    CLRWDT();
    // 根据当前灯的状态控制灯的亮灭
    switch(light_state) {
        case 0:    // 红灯
            PORTBbits.PB0 = 0;    // 红灯亮
            PORTBbits.PB2 = 1;    // 绿灯灭
            PORTBbits.PB3 = 1;    // 黄灯灭
            delay_ms(1000);        // 等待1秒钟
            light_state = 1;    // 切换到绿灯状态
            break;
        case 1:    // 绿灯
            PORTBbits.PB0 = 1;    // 红灯灭
            PORTBbits.PB2 = 0;    // 绿灯亮
            PORTBbits.PB3 = 1;    // 黄灯灭
            delay_ms(1000);        // 等待1秒钟
            light_state = 2;    // 切换到黄灯状态
            break;
        case 2:    // 黄灯
            PORTBbits.PB0 = 1;    // 红灯灭
            PORTBbits.PB2 = 1;    // 绿灯灭
            PORTBbits.PB3 = 0;    // 黄灯亮
            delay_ms(1000);        // 等待1秒钟
            light_state = 0;    // 切换到红灯状态
            break;
        default:    // 默认为红灯状态
            PORTBbits.PB0 = 0;    // 红灯亮
            PORTBbits.PB2 = 1;    // 绿灯灭
            PORTBbits.PB3 = 1;    // 黄灯灭
            delay_ms(1000);        // 等待1秒钟
            light_state = 1;    // 切换到绿灯状态
            break;
    }
}

上述代码中,使用了switch语句根据当前灯的状态控制不同的灯亮灭。在每个状态下,先控制相应的灯亮灭,然后等待1秒钟,最后切换到下一个状态。

需要注意的是,为了等待1秒钟,需要在程序中定义一个延时函数。可以使用循环来实现延时,也可以使用定时器中断来实现延时。


如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

在代码方面,实现红绿黄灯循环闪烁需要使用循环结构(如for、while)和条件语句(如if、else)。下面是一种实现方式:

import time

# 定义红灯、黄灯、绿灯的时间
red_time = 5
yellow_time = 2
green_time = 5

# 循环执行红绿黄灯闪烁
while True:
    # 红灯
    print("红灯(亮)")
    time.sleep(red_time)
    print("红灯(灭)")
    # 黄灯
    print("黄灯(亮)")
    time.sleep(yellow_time)
    print("黄灯(灭)")
    # 绿灯
    print("绿灯(亮)")
    time.sleep(green_time)
    print("绿灯(灭)")

上面的代码定义了红、黄、绿灯分别亮灭的时间,使用while循环不断执行红绿黄灯闪烁。具体实现如下:

  1. 执行红灯亮,等待red_time秒钟后,执行红灯灭。
  2. 执行黄灯亮,等待yellow_time秒钟后,执行黄灯灭。
  3. 执行绿灯亮,等待green_time秒钟后,执行绿灯灭。
  4. 回到步骤1。

这样,就实现了红绿黄灯循环闪烁。如果想要更加细致的控制,可以在循环中添加条件语句,如控制红绿灯的次数等。

注意事项:

  1. 在Python中使用时间控制可以借助time模块中的sleep函数。
  2. 在实际的代码实现中,可以使用实际的硬件设备来控制红绿灯的亮灭,也可以使用图形界面来模拟红绿灯的变化。

参考GPT:

#include <ny8.h>
#include "NY8_constant.h"
#define UPDATE_REG(x)    __asm__("MOVR _" #x ",F")

void main(void)
{
    unsigned char timer = 0;
    unsigned char state = 0; // 0: Red, 1: Green, 2: Yellow

    // Initialize GPIO
    // ...

    while(1)
    {
        CLRWDT();
        // Implement traffic light logic with a timer

        switch(state)
        {
            case 0: // Red
                PORTBbits.PB0 = 1; // Turn on red light
                PORTBbits.PB1 = 0;
                PORTBbits.PB2 = 0;
                break;

            case 1: // Green
                PORTBbits.PB0 = 0;
                PORTBbits.PB1 = 1; // Turn on green light
                PORTBbits.PB2 = 0;
                break;

            case 2: // Yellow
                PORTBbits.PB0 = 0;
                PORTBbits.PB1 = 0;
                PORTBbits.PB2 = 1; // Turn on yellow light
                break;
        }

        // Increment the timer and change the state after a certain duration
        timer++;
        if(timer >= 50) // Adjust this value to control the duration of each state
        {
            timer = 0;
            state = (state + 1) % 3; // Rotate through 0, 1, 2 for red, green, yellow
        }
    }
}

c语言交通灯程序闪烁
可以参考下