【问题标题】:Detect Button Signal on Microcontroller ATmega328P Using C Language使用 C 语言检测微控制器 ATmega328P 上的按钮信号
【发布时间】:2014-10-19 10:04:51
【问题描述】:

微控制器:ATmega328P

我遇到以下代码的问题。用于通过连接PB0的按钮控制LED灯。

有两种状态:
1. state_0 - 所有 LED 均关闭。
2. state_1 - 所有 LED 都亮起。

#include <avr/io.h>

int main(void)
{
    DDRB = 0x00;    //set PINB as a input port for receiving PB0 signal
    DDRD = 0xFF;    //set PORTD as a output port for driving the LEDs
    unsigned char state_0 = 0x00;   //all LED bits are off
    unsigned char state_1 = 0xFF;   //all LED bits are on

    PORTD = state_0 //initialize the state  <----Still work here, Not work after this instruction.

    while(1)
    {   
        if(PINB0 == 0 && PORTD == state_0)      //when the button is not pressed and the LEDs are off
        {
            PORTD = state_0;                    //the LED states remain all off 
        }
        else if(PINB0 == 0 && PORTD == state_1) //when the button is not pressed and the LEDs are on
        {
            PORTD = state_1;                    //the LED states remain all on
        }
        else if(PINB0 == 1 && PORTD == state_0) //when the button is pressed and the LEDs are off
        {
            PORTD = state_1;                    //the LED states are turned to all on
        }
        else                                    //when the button is pressed and the LEDs are on
        {
            PORTD = state_0;                    //the LED states are turned to all off
        }
    }
}

经过一些测试,我发现微控制器无法检测到来自 PB0 的任何信号。当我直接将 Vcc 连接到 PB0 或直接将 GND 连接到 PB0 时,我得到相同的结果。当我按下按钮时,什么都没有改变。

while(1)
{
    if (PINB0 == 0)
        PORTD = 0x00;
    else
        PORTD = 0xFF;
}

上述代码的连接:
不行


为了测试按钮,我尝试了以下操作。它可以正常工作,所以按钮可以正常工作。


你知道哪里错了吗?
感谢您的帮助。

【问题讨论】:

  • 如果与 +Vdd 和接地的直接电流连接也不起作用,那么这似乎是硬件问题。然而,还有另一个错误 IIRC:从输出端口读取没有任何好处(即它不会导致端口的实际状态)。您最好自己跟踪端口的状态(在一个单独的变量中)。

标签: c microcontroller led


【解决方案1】:

问题是您的程序实际上并没有检测到状态变化 - 如果while 循环运行得足够快(并且可能是),即使是短暂按下按钮也会触发if(PINB0 == 0 &amp;&amp; PORTD == state_0)else 部分。

考虑添加某种检查状态 - 像这样:

int old_state = 0;

while(1) {   
    if (old_state != PINB0) {
        if(PINB0 == 0 && PORTD == state_0)      //when the button is not pressed and the LEDs are off
         {
            PORTD = state_0;                    //the LED states remain all off 
        }
        else if(PINB0 == 0 && PORTD == state_1) //when the button is not pressed and the LEDs are on
        {
            PORTD = state_1;                    //the LED states remain all on
        }
        else if(PINB0 == 1 && PORTD == state_0) //when the button is pressed and the LEDs are off
        {
            PORTD = state_1;                    //the LED states are turned to all on
        }
        else                                    //when the button is pressed and the LEDs are on
        {
            PORTD = state_0;                    //the LED states are turned to all off
        }
      }
      old_state = PINB0;
    }

(抱歉缩进不好,在这里很难找到)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多