Blink LED on Segger Embedded Studio

huangapple go评论66阅读模式
英文:

Blink LED on segger embedded studio

问题

我如何在nrf52840上使用Seger嵌入式IDE同时让2个LED闪烁,一个在500毫秒处,另一个在1秒处,准确的时间。我尝试使用内置的延迟函数来延迟它,但它只是停止了处理器,找不到确切的答案。我尝试了Seger嵌入式的示例,但找不到解决方案,我是初学者,对此不太了解。如果能提到更多相关的学习资料将非常有帮助。谢谢!

英文:

How can I blink 2 Leds at the same time i.e. one at 500ms and the other at 1sec exact time on nrf52840 using Seger embedded as ide.
I tried to delay it using the inbuilt delay function but it just halts the processor and can't find the exact answer.
I tried the examples on the Seger embedded but I can't find the solution I am beginner so, don't so I don't have that much idea about it.
It would be very helpful mentioned to learn mor stuffs on it.
Thank you!

答案1

得分: 2

以下是代码部分的中文翻译:

有许多方法可以实现这个目标,其中一些比其他方法更复杂,涉及硬件定时器、中断等。

然而,总的来说,根据您提供的有限信息以及伪代码,一个简单的解决方案是使用较短的延迟,并计算“滴答声”的数量,当该执行切换每个LED时:

static const uint32_t LED1_PERIOD = 500;
static const uint32_t LED2_PERIOD = 1000;
bool led1 = false;
bool led2 = false;
uint32_t led1_time = 0;
uint32_t led2_time = 0;

for(uint32_t tick_count = 0; ; tick_count++)
{
    if(led1_time >= LED1_PERIOD)
    {
        led1 = !led1;
        led1_time = 0;
    }

    if(led2_time >= LED2_PERIOD)
    {
        led2 = !led2;
        led2_time = 0;
    }

    output(GPIO_LED1, led1);  
    output(GPIO_LED2, led2);

    delay(1);
    led1_time++;
    led2_time++;
}

这是相当通用的。对于您非常具体的要求,您可以简化如下:

bool led1 = false;
bool led2 = false;

for(unsigned count = 0;; count++)
{
    output(GPIO_LED1, led1);  
    output(GPIO_LED2, led2);

    delay(500);

    led1 = !led1;

    if((count & 0x01) != 0u)
    {
        led2 = !led2;
    }
}

如果您希望在闪烁LED时执行其他处理,并且没有使用实时操作系统(RTOS),那么您可能根本不希望有延迟。相反,您可以简单地轮询可用的时钟源:

static const uint32_t LED1_PERIOD = CLOCKS_PER_SEC / 2u;
static const uint32_t LED2_PERIOD = CLOCKS_PER_SEC;
bool led1 = false;
bool led2 = false;
uint32_t led1_time = clock();
uint32_t led2_time = led1_time;

for(;;)
{
    uint32_t now = clock();
    if(now - led1_time >= LED1_PERIOD)
    {
        led1 = !led1;
        led1_time += LED1_PERIOD;
    }

    if(now - led2_time >= LED2_PERIOD)
    {
        led2 = !led2;
        led2_time += LED2_PERIOD;
    }

    output(GPIO_LED1, led1);  
    output(GPIO_LED2, led2);

    // 在这里执行其他工作...
}
英文:

There are many ways you could do that some more sophisticated than others involving hardware timers, interrupts etc.

However in general, with the little information you have provided and in "pseudocode", a simple solution is to use a much shorter delay and count the number of "ticks" and toggle each LED when it is time to do so:

static const uint32_t LED1_PERIOD = 500 ;
static const uint32_t LED2_PERIOD = 1000 ;
bool led1 = false ;
bool led2 = false ;
uint32_t led1_time = 0 ;
uint32_t led2_time = 0 ;


for(uint32_t tick_count = 0; ; tick_count++ )
{
    if( led1_time >= LED1_PERIOD )
    {
        led1 = !led1 ;
        led1_time = 0 ;
    }

    if( led2_time >= LED2_PERIOD )
    {
        led2 = !led2 ;
        led2_time = 0 ;
    }

    output( GPIO_LED1, led1 ) ;  
    output( GPIO_LED2, led2 ) ;

    delay( 1 ) ;
    led1_time++ ;
    led2_time++ ;

}

That is quite generalised. For your very specific requirements you could simplify that:

bool led1 = false ;
bool led2 = false ;

for(unsigned count = 0;; count++)
{
    output( GPIO_LED1, led1 ) ;  
    output( GPIO_LED2, led2 ) ;

    delay( 500 ) ;

    led1 = !led1 ;

    if( (count & 0x01) != 0u )
    {
        led2 = !led2 ;
    }
}

If you want to perform other processing whilst flashing the LED and you are not using an RTOS, then you probably don't want a delay at all. Instead you would simply poll an available clock source:

static const uint32_t LED1_PERIOD = CLOCKS_PER_SEC / 2u;
static const uint32_t LED2_PERIOD = CLOCKS_PER_SEC ;
bool led1 = false ;
bool led2 = false ;
uint32_t led1_time = clock() ;
uint32_t led2_time = led1_time ;


for( ;; )
{
    uint32_t now = clock() ;
    if( now - led1_time >= LED1_PERIOD )
    {
        led1 = !led1 ;
        led1_time += LED1_PERIOD ;
    }

    if( now() - led2_time >= LED2_PERIOD )
    {
        led2 = !led2 ;
        led2_time += LED2_PERIOD ;
    }

    output( GPIO_LED1, led1 ) ;  
    output( GPIO_LED2, led2 ) ;

    // Do other work here...
}

huangapple
  • 本文由 发表于 2023年7月18日 16:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76711058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定