繁体   English   中英

如何使用按钮增加或减少 7 段显示 00-99

[英]How to increment or decrement a 7-segment display 00-99 using button

我正在使用 code composer studio,数据信号 (D0-7) 是从 tms320f28335 获得的,我使用 switch 语句在 7 段上增加和减少了特定的 LED。 但如果手动递增(我已经提供如下所示的值),那很好

   void display(void)
   {
    B1 +=1;
    if(B1 > B1_lim) { B1 = 0; }

    switch(B1)
    {
     case  0:  Display[0] = 0xf7; break;  //9
     case 1:  Display[0] = 0xfF; break;   //8
     case 2:  Display[0] = 0xf0; break;   //7
     case 3:  Display[0] = 0x9F; break;
    }

我也尝试过运行计数器,但由于 while(1) 语句,它永远不会退出循环,然后我不能使用其他按钮,代码如下所示,它适用于 00-99,永无止境。 此外,我尝试在下面的代码中使用按钮,但它像按钮一样循环可以启动递增/递减过程

    void display(void)
    {
    unsigned char a[]={0xfb,0xb0,0xeD,0xf5,0xb6,0xd7,0x9F,0xf0,0xfF,0xf7}; //[0,1,2,3,4,5,6,7,8,9]
    unsigned char b[]={0xfb,0xb0,0xeD,0xf5,0xb6,0xd7,0x9F,0xf0,0xfF,0xf7}; //[0,1,2,3,4,5,6,7,8,9]
    int k,j;
     while(1)
       {
        Display[0] = 0xfb;
        Display[1] = 0xfb;
        DELAY_US(200000L);
        for(j=0;j<=9;j++)
        {
            Display[1]=b[j];
            led_display(0x00);    DELAY_US(200000L);
              for(k=0;k<=9;k++)
            {
                Display[0] = a[k];
                led_display(0x00);    DELAY_US(200000L);
                DELAY_US(2L);
            }
        }
    }
    }

在我的 7 段驱动板上,我有 4 个按钮,我想使用其中两个来进行递增和递减。 我想知道是否有任何合乎逻辑的方法来使用 switch 语句,以便每次按下按钮我都可以递增或递减。 上面的代码显示了显示增量的最简单方法,但我想使用第二个代码。

我认为你应该为按钮使用中断,不要在你的 function 中使用 while(1)。创建一个 function 来更新 7 段的值。 当您按下按钮时,中断 function 将呼叫。 在此,您更新要显示的值。

首先,您需要一个遵循紧密内聚松散耦合原则的连贯设计。 您的显示 function 试图做太多 - 它没有凝聚力- 它包含与您的特定应用程序相关但与显示无关的逻辑。 它应该只做它的名字所暗示的并显示所需的值。 例如:

void display( uint8_t value )
{
    //                              0    1    2    3    4    5    6    7    8    9
    static const uint8_t digits[]={0xfb,0xb0,0xeD,0xf5,0xb6,0xd7,0x9F,0xf0,0xfF,0xf7}; 
    if( value < 100 )
    {
        uint8_t display[2] = {0,0} ;
        display[0] = value % 10u ; // least significant digit
        display[1] = value / 10u ; // most significant digit
        led_display( 0x00 ) ;
    }
}

然后让我们假设(因为你没有告诉我们)你有一些用于按钮和时钟源的 GPIO 输入,假设我将调用这些getSwitchState()getClockMillisec() ,那么你可能有一个按钮 state function 类似:

bool getButtonState()
{
    static const uint32_t DEBOUNCE_MILLIS = 20 ;
    static uint32_t timestamp = getClockMillisec() ;
    static bool debounced_button_state = getSwitchState() ;
    
    uint32_t now = getClockMillisec() ;
    uint32_t button_state = getSwitchState() ;
    
    if( now - timestamp > DEBOUNCE_MILLIS &&
        button_state != debounced_button_state )
    {
        debounced_button_state = button_state ;
        timestamp = now ;
    }
    
    return debounced_button_state ;
}

那么您将只想对 state 的按钮更改进行操作,因此您可能有:

bool isButtonDownEvent()
{
    static bool button_state = getButtonState() ;
    bool button_down_event = false ;
    
    // On change of state
    if( getButtonState() != button_state )
    {
        button_state = !button_state ;
        
        // True if button change was released to pressed (down event)
        button_down_event = button_state ; 
    }
    
    return button_down_event ;
}

然后您的应用程序循环可以从这些原语构建:

uint8_t num = 0 ;

for(;;)
{
    display( num ) ;
    if( isButtonDownEvent() )
    {
        num++ ;
        num %= 99 ;
    }
}

显然,如果您有多个用于单独递增/递减的按钮,则需要进行一些修改以处理单独的按钮,但由于您没有提供有关按钮硬件或软件接口的信息,我将把它留给您考虑。

当然,这也不是按钮输入和去抖动的唯一可能解决方案——您可能已经有了合适的代码。 也许有用的是,此方法不需要专用的硬件定时器或中断。 然而,它确实需要定期轮询以避免丢失事件。

重要的是要注意该解决方案没有延迟也没有阻塞,您可以在保持显示的同时在循环中做其他有用的工作(只要它也是非阻塞的并且不会占用过多的 CPU 时间以致可能错过按钮事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM