【问题标题】:unable to hold some LED'S : ON , when flickering others无法保持某些 LED 灯:ON,闪烁其他 LED 时
【发布时间】:2012-09-22 18:33:17
【问题描述】:

我使用 AVR 作为微控制器,使用 ATMEGA8 作为处理器(在微控制器内部)。带有微控制器的电路板有 4 个 LED。我能够烧录程序并点亮 LED。但我无法实现特定的目标。

L1 L2  L3 L4

这些是 4 个 LED。第一轮每个 LED 间隔 3 秒后亮起。最后一个 LED (L4) 在第一轮后保持亮起。第三轮开始时,每个 LED 间隔 3 秒亮起,L3 保持亮起,同时 L4 也亮照明,它继续......直到L1。

L1 L2 L3 L4
         On
      On On
   On On On
On On On On

但我无法做到这一点。因为当我将一个 LED 设置为 ON 时,其他 LED 会熄灭。我什至尝试添加一个 10 毫秒的小时间间隔。我该怎么做呢 ?这是我到目前为止所拥有的:

    #include<avr/io.h>
    #include<util/delay.h>
    DDRB = 0xFF; // input
 //PORTB = 0xFF;

    // ob00011110 --> on all --> binary

    int i=0;

    while(i<1) {
      PORTB = 0b00010000; // first led on
      _delay_ms(3000);
      PORTB = 0b00001000; // second led on
      _delay_ms(3000);
      PORTB = 0b00000100; // third on
      _delay_ms(3000);
      PORTB = 0b00000010; // fourth on
      _delay_ms(3000);
      i += 1;
    }

    PORTB = 0b00000010; // keep the 4th on and start all over again and reach til 3rd LED

【问题讨论】:

    标签: c microcontroller avr avr-gcc atmega


    【解决方案1】:

    看来你的顺序是错误的。当您打开第二个 LED 时,您将关闭第一个 LED。顺序应该是:

      PORTB = 0b00010000; // first led only
      _delay_ms(3000);
      PORTB = 0b00011000; // first and second led on
      _delay_ms(3000);
      PORTB = 0b00011100; // first, second, and third on
      _delay_ms(3000);
      PORTB = 0b00011110; // first, second, third, and fourth on
      _delay_ms(3000);
    

    【讨论】:

      【解决方案2】:

      你可以这样使用:

      while (1){
        PORTB = 0b00010000;
        _delay_ms(3000);
        PORTB |= 0b00001000;
        _delay_ms(3000);
        PORTB |= 0b00000100;
        _delay_ms(3000);
        PORTB |= 0b0000010;
        _delay_ms(3000);
      

      它会在每次循环开始时关闭所有 LED,然后一个接一个地亮...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-15
        相关资源
        最近更新 更多