Search code examples
arduinoembeddedledtransmission

Data Tx/Rx status LED function


A lot of hardware comes with a Tx/Rx status LED, but what if your hardware doesn't and you want to see if data is transmitting? If you just set the LED to the value of the line it may go on and off before you even see it (<1/100th of a sec.).

My question is, how do I write an interrupt driven function to drive the LED state? I've searched the internet and found nothing and I could use a counter with a modulus, but that seems clunky. Any other ideas?

PS - I'd like to use this in either Arduino or Mbed, but I doubt it makes a difference to the question as to which one...


Solution

  • void receive_or_transmit_interrupt()
    {
         g_traffic = true;
         /* other stuff. */
    }
    
    void timer_that_fires_every_100_milliseconds()
    {
         if ( led == ON)
         {
              led = OFF;
              g_traffic = false;
         }
         else if ( g_traffic )
         {
              led = ON;
         }
    }
    

    If you don't want the timer to always be firing even when there's not traffic, you could change the receive_or_transmit_interrupt to enable the timer, and the timer could disable itself when it turns off the LED.