3 Replies Latest reply on Feb 20, 2020 4:10 PM by MoTa_728816

    global variables updated in ISR does not updated in main loop

    CK_4512541

      I am using CY8C4025AZI-S413 with PSoC4 and PSoC 4000S series microcontroller.

      I have chosen SCB mode as UART , enabled receive interrupt in ISR routine

      Please refer following program :

      /* ========================================

      *

      * Copyright YOUR COMPANY, THE YEAR

      * All Rights Reserved

      * UNPUBLISHED, LICENSED SOFTWARE.

      *

      * CONFIDENTIAL AND PROPRIETARY INFORMATION

      * WHICH IS THE PROPERTY OF your company.

      *

      * ========================================

      */

       

       

      #include "project.h"

       

       

      volatile uint8 gb_char = 0;

      volatile char flag = 0 ;

       

       

      CY_ISR(Uart_Rx_Interrupt)

      {

          gb_char = UART_UartGetChar();

          if(gb_char == 0x23)

          {

             flag = 1;

              UART_SpiUartWriteTxData(gb_char);

          }

         

         

      }

       

       

      void Uart_Initialize (void)

      {

          UART_Start();

          Uart_isr_StartEx(Uart_Rx_Interrupt);

      }

       

       

      int main(void)

      {

          CyGlobalIntEnable; /* Enable global interrupts. */

       

       

          /* Place your initialization/startup code here (e.g. MyInst_Start()) */

      Uart_Initialize();

         

         

          for(;;)

          {

              /* Place your application code here. */

              if(flag == 1)

              {

                  led17_Write(1);

              }

              if(flag == 0)

              {

                  led17_Write(0);

              }

             

          }

      }

       

       

      /* [] END OF FILE */

      Output:

      Flag is not checked and led on off is not functioning , what should I do?

        • 1. Re: global variables updated in ISR does not updated in main loop
          MoTa_728816

          Hi,

           

          Since I don't have a board with your device, I tried a board with CY8C4146LQI-S433.

          And it was working without any modification to your main.c

           

          schematic

          000-schematic.JPG

          UART config advanced

          001-Uart_config.JPG

          main.c

          Note: same with yours

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

          #include "project.h"

           

          volatile uint8 gb_char = 0;

          volatile char flag = 0 ;

           

          CY_ISR(Uart_Rx_Interrupt)

          {

              gb_char = UART_UartGetChar();

              if(gb_char == 0x23)

              {

                 flag = 1;

                 UART_SpiUartWriteTxData(gb_char);

              }

          }

           

          void Uart_Initialize (void)

          {

              UART_Start();

           

              Uart_isr_StartEx(Uart_Rx_Interrupt);

          }

           

          int main(void)

          {

              CyGlobalIntEnable; /* Enable global interrupts. */

           

              /* Place your initialization/startup code here (e.g. MyInst_Start()) */

           

              Uart_Initialize();

             

              for(;;)

              {

                  /* Place your application code here. */

                  if(flag == 1)

                  {

                      led17_Write(1);

                  }

                  if(flag == 0)

                  {

                      led17_Write(0);

                  }

              }

          }

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

           

          Tera Term log

          Note: When I activated the Tera Term Window and typed a "#" letter, it was echoed back.

          002-TeraTerm-log.JPG

          moto

          • 2. Re: global variables updated in ISR does not updated in main loop
            MoTa_728816

            Hi,

             

            It was my bad, I noticed that I had not checked the LED after posting the previous response.

            And Yes, LED did not light.

             

            I debugged some time

            And I found that after first interrupt, the interrupt was being called repeatedly,

            there for the program could never reach the point where to turn LED on.

             

            Probably we need to clear interrupt but "UART_rx_ClearInterrupt() ;" did not seem to be enough.

            For the time, being I disabled the interrupt in the ISR and

            re-enabled it in the (flag == 1) block.

             

            main.c (modified)

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

            #include "project.h"

             

            volatile uint8 gb_char = 0;

            volatile char flag = 0 ;

             

            CY_ISR(Uart_Rx_Interrupt)

            {

                UART_SpiUartDisableIntRx() ;

                UART_rx_ClearInterrupt() ;

              

                if (UART_SpiUartGetRxBufferSize()) {

                    gb_char = UART_UartGetChar();

                    if(gb_char == 0x23)

                    {

                        flag = 1;

                        UART_SpiUartWriteTxData(gb_char);

                    }

                }

            }

             

            void Uart_Initialize (void)

            {

                UART_Start();

             

                UART_SpiUartClearRxBuffer() ;

                UART_SpiUartEnableIntRx(UART_INTR_RX_NOT_EMPTY) ;

                Uart_isr_StartEx(Uart_Rx_Interrupt);

            }

             

            int main(void)

            {

                CyGlobalIntEnable; /* Enable global interrupts. */

             

                /* Place your initialization/startup code here (e.g. MyInst_Start()) */

             

                Uart_Initialize();

              

                for(;;)

                {

                    /* Place your application code here. */

                    if(flag == 1)

                    {

            //          flag = 0 ;

                        led17_Write(1);

                        UART_SpiUartEnableIntRx(UART_INTR_RX_NOT_EMPTY) ;

                    }

                    if(flag == 0)

                    {

                        led17_Write(0);

                    }

                }

            }

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

             

            moto

            • 3. Re: global variables updated in ISR does not updated in main loop
              MoTa_728816

              Hi,

               

              This morning finally I seriously read the datasheet ...

              For the RX interrupt, we need SCB_ClearRxInterruptSource().

               

              So I modified main.c as below

               

              main.c

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

              #include "project.h"

               

              volatile uint8 gb_char = 0;

              volatile char flag = 0 ;

               

              CY_ISR(Uart_Rx_Interrupt)

              {

                  UART_ClearRxInterruptSource(UART_INTR_RX_NOT_EMPTY) ;

               

                  if (UART_SpiUartGetRxBufferSize()) {

                      gb_char = UART_UartGetChar();

                      if(gb_char == 0x23)

                      {

                          flag = 1;

                          UART_SpiUartWriteTxData(gb_char);

                      }

                  }

              }

               

              void Uart_Initialize (void)

              {

                  UART_Start();

                  UART_SpiUartClearRxBuffer() ;

                  UART_SpiUartEnableIntRx(UART_INTR_RX_NOT_EMPTY) ;

                  Uart_isr_StartEx(Uart_Rx_Interrupt);

              }

               

              int main(void)

              {

                  CyGlobalIntEnable; /* Enable global interrupts. */

                  Uart_Initialize();

               

                  for(;;)

                  {

                      if(flag == 1)

                      {

              //          flag = 0 ;

                          led17_Write(1);

                      }

                      if(flag == 0)

                      {

                          led17_Write(0);

                      }

                  }

              }

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

               

              moto

              1 of 1 people found this helpful