5 Replies Latest reply on Jun 27, 2019 6:09 PM by JoSi_3317786

    Delay routine

    JoSi_3317786

      Hey, guys, I need some help with my program. I using Timer component to generate a 5 seconds delay while execute somethings. Using a clock@10kHz the period should be 50000 to give me 5 seconds, but when I set Timer_WritePeriod(50000) the count never hits zero to generate a pulse at TC.

      When I set Timer_WritePeriod(20) the TC occur, but I dont know how accurate the delay is.

       

      Is there something wrong?

       

       

      void Tempo(void)//5 seconds

          { 

             Control_Reg_Timer_Write(0);//Enable Timer

              Timer_Stop();//Stop timer

              //Timer_ClearFIFO();

              Timer_WriteCounter(0);//

              Timer_WritePeriod(20);//

              Timer_Enable()  ;//Enable Timer

              while(Status_Reg_Read() == 0)//wait for the TC rising edge

                  {

                  UART_PC();

                  LED_Write(1);

                  }

              Control_Reg_Timer_Write(1);//Reset Timer

              LED_Write(0);

              return;

          }

       

      Sem título.png

        • 1. Re: Delay routine
          LinglingG_46

          Hi,

           

          As my opinion, you can refer to the UDB Timer datasheet: Figure 2.Fefault UDB Timer Implementation Example Waveform.

          From the figure we can see that tc only one period. And uint8 StatusReg_Read (void)  Reads the value of a status register.

          Return Value:  Returns the current value of a status register.

          So maybe when you read it, the TC output has changed from high to low.

          • 2. Re: Delay routine
            NoriakiT_91

            It is available to capture the 1 cycle TC signal if the status register bit is set to "stick" mode.

             

            Please ensure that you initialized the Timer instance anywhere prior calling this function.

            Timer_Start() is the typical initialization function.

             

            Regards,

            Noriaki

            • 3. Re: Delay routine
              MoTa_728816

              Hi,

               

              Since you already have ISR_Timer attached TC will be caught as an interrupt.

              I think that using interrupt and isr (interrupt service routine) is easier.

               

              I tried with CY8CKIT-059, and I modified your schematic as below

              001-Schematic.JPG

              main.c

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

              #include "project.h"

              #include "stdio.h"

               

              volatile int timer_flag = 0 ;

               

              inline void LED_ON(void) { LED_Write(1) ; }

              inline void LED_OFF(void) { LED_Write(0) ; }

              inline void Reset_Timer(int mode) { Control_Reg_Write(mode) ; }

               

              char str[64] ; /* print buffer */

              void print(char *str)

              {

                  UART_PutString(str) ;

              }

               

              CY_ISR(timer_isr)

              {

                  Timer_Stop() ;

                  ISR_Timer_ClearPending() ;

                  timer_flag = 1 ;

                  Reset_Timer(1) ;

              }

               

              void init_hardware(void)

              {

                  UART_Start() ;

                 

                  Reset_Timer(1) ;

                  Control_Reg_Write(0) ;

                  Timer_Init() ;

                  ISR_Timer_ClearPending() ;

                  ISR_Timer_StartEx(timer_isr) ;

                  CyGlobalIntEnable; /* Enable global interrupts. */

                 

                  LED_OFF() ;

              }

               

              void splash(void)

              {

                  sprintf(str, "Timer INT (5s) Test (%s %s)\n", __DATE__, __TIME__) ;

                  print(str) ;

              }

               

              void trigger_timer(void)

              {

                  timer_flag = 0 ;

               

                  ISR_Timer_ClearPending() ;

                  Reset_Timer(0) ;

                  Timer_Enable() ;

              }

               

              int main(void)

              {

                  init_hardware() ;

                 

                  splash() ;

               

                  print("Start Timer for 5sec (LED ON)\n") ;

                  LED_ON() ; ;

                  trigger_timer() ;

                     

                  for(;;)

                  {

                      if (timer_flag) {

                          LED_OFF() ; ;

                          print("Waiting 3sec interval (LED OFF)\n") ;

                          CyDelay(3000) ;

                          print("Starting 5sec timer (LED ON)\n") ;

                          LED_ON() ; ;

                          trigger_timer() ;

                      }

                  }

              }

               

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

               

              The UART log (Tera Term) was like, and I observed that the LED was on about 5secs and off about 3 secs.

              000-TeraTerm_log.JPG

               

              moto

              • 4. Re: Delay routine
                MoTa_728816

                Hi,

                 

                Reading the question and answers again, I noticed that you could connect "Interrupt" component to the "tc" pin, too.

                With this you can catch the rising edge of "tc" without much hassle.

                 

                IMHO, using "interrupt" seems more "formal", though.

                 

                schematic

                001-schematic.JPG

                Note: main.c is the same with my previous post

                 

                Tera Term log

                000-TeraTerm-log.JPG

                 

                moto

                1 of 1 people found this helpful
                • 5. Re: Delay routine
                  JoSi_3317786

                  THANK YOU, THANK YOU, THANK YOU

                   

                  Works like a charm