1 Reply Latest reply on Feb 25, 2020 2:48 PM by MoTa_728816

    Is there a simple blinking led code using timer interrupt I can look at for reference?

    RiTh_4546911

      I am trying to build adc that samples for 5 seconds then sleeps for a minute.

        • 1. Re: Is there a simple blinking led code using timer interrupt I can look at for reference?
          MoTa_728816

          Hi,

           

          There could be many approach to this.

           

          I tried a couple of samples with CY8CKIT-059.

          (1) Using a timer component (timer_intr_200226A)

          (2) Using a SysTick (timer) (timer_intr_200226B)

           

          I imagine that what you intended was (1), but as a timer is a precious resource

          it may be smarter to use ARM core built-in "SysTick" which can be used without consuming a timer component.

           

          (1) Using a timer component

          schematic

          000-schematic.JPG

          pins

          001-pins.JPG

           

          main.c

          ================

          #include "project.h"

           

          volatile int pit_flag = 0 ;

           

          CY_ISR(timer_isr)

          {

              Timer_ReadStatusRegister() ;

              pit_flag = 1 ;

          }

           

          int main(void)

          {

              CyGlobalIntEnable; /* Enable global interrupts. */

           

              isr_timer_ClearPending() ;

              isr_timer_StartEx(timer_isr) ;

             

              Timer_Start() ;

           

              for(;;)

              {

                  if (pit_flag) {

                      pit_flag = 0 ;

                      LED_Write( !LED_Read() ) ;

                  }

              }

          }

          ================

          Note: You could do "LED_Write( !LED_Read()) inside the isr,

          but in general, I would recommend to make an isr as short as possible.

           

          (2) Using a SysTick (timer)

          schematic

          010-schematic.JPG

          pins

          011-pins.JPG

          main.c

          ==============

          #include "project.h"

           

          #define PIT_PERIOD 500 // 500ms = 0.5 sec

           

          volatile int pit_flag = 0 ;

          volatile int tick_count = 0 ;

           

          CY_ISR(timer_isr)

          {

              tick_count++ ;

              if (tick_count >= PIT_PERIOD) {

                  tick_count = 0 ;

                  pit_flag = 1 ;

              }

          }

           

          int main(void)

          {

              CyGlobalIntEnable; /* Enable global interrupts. */

             

              CySysTickStart() ;

              CySysTickSetCallback(0, timer_isr) ;

           

              for(;;)

              {

                  if (pit_flag) {

                      pit_flag = 0 ;

                      LED_Write( !LED_Read() ) ;

                  }

              }

          }

          ==============

          Note: In the default configuration, the interrupt of SysTick comes every 1ms,

          so I needed to count it up to the required time. This time 500 = 0.5 sec.

           

          moto