11 Replies Latest reply on Jul 14, 2015 8:53 PM by MichaelF_56

    How to use 100us interval timer?

    moicci

      I want to attach 2x2 LED matrix to a BCM20737, and implement dynamic lighting.

       

      The following simple code works correctly using with bleapptimer_startFineTimer, but does not work using with hw_timer because the callback function is not called.


      I want to use hw_timer because the function shiftRow must be called in 100us interval.

      Let me know how to use 100us interval timer.


      #define USE_HW_TIMER
      
      // 2x2 LED matrix: 1 is on, 0 is off
      static UINT8 matrix[2][2] = {
          { 1, 0 },
          { 0, 1 }
      };
      
      // GPIO pins connected to LED matrix
      static int col_pins[2] = {2, 13};
      static int row_pins[2] = {27, 25};
      
      // row (anode) to light
      static int prev_row;
      static int next_row;
      
      static void shiftRow() {
          // LOW: previous row (anode)
          int pin = row_pins[prev_row];
          gpio_setPinOutput(pin / 16, pin % 16, GPIO_PIN_OUTPUT_LOW);
      
          // LOW: cathode of current row
          int col;
          for (col = 0; col < 2; col++) {
              UINT8 val = matrix[next_row][col];
              pin = col_pins[col];
              gpio_setPinOutput(pin / 16, pin % 16, val ? GPIO_PIN_OUTPUT_LOW: GPIO_PIN_OUTPUT_HIGH);
          }
      
          // HIGH: current row
          pin = row_pins[next_row];
          gpio_setPinOutput(pin / 16, pin % 16, GPIO_PIN_OUTPUT_HIGH);
      
          prev_row = next_row;
          if (++next_row >= 2)
              next_row = 0;
          
      #ifdef USE_HW_TIMER
          hw_timer_start(100);
      #endif
      }
      
      static void test2x2() {
      #if USE_HW_TIMER
          hw_timer_register_timer_expired_callback(shiftRow);
          hw_timer_start(100);
      #else
          bleapptimer_startFineTimer(shiftRow, 1);
      #endif
      }