3 Replies Latest reply on May 2, 2019 3:27 AM by MoTa_728816

    serial data  to counter

    anko_3968661

      Hello all,

       

      I am writing a code where i am receiving data serially, this data is nothing but count and frequency. I am giving this data to counter.

      I am giving count to counter with instruction Counter_1_WriteCounter(step)

      and required frequency using instruction Clock_1_SetDividerValue(div)

       

      This program works fine if i am not taking serial input from UAR, when i use constant number then counter is working fine with given step count and frequency, but when i include serial input code. then Counter is not working.

       

      i am giving this code below which works fine without serial input

       

      #include <device.h>

      //#define LCD_NUM_COLUMNS (16u)

      int main()

      {

       

      char8 arr[17]={'A',2,0,'B',1,'B',9,0,0,9,'D','F','F','F','F','F','F'};

      //char8 ch=0;

      uint8 i=0;

       

      uint32 div=0,step=0,dir=0;

      ControlReg_1_Write(0);

      Counter_1_Init();

       

      //LCD_Char_1_Start();

      CyGlobalIntEnable; /* Enable all interrupts by the processor. */

       

      i=16;

      while(arr[i]!='D')

      {

           if(arr[i]!='D')

               {

                      i--;

                }

      }

       

      if(arr[i]=='D')

            

        {

             uint32 n=1;

             while(arr[i-1]!='B')

                    {  

                              i--;   

                            div = (div+(arr[i]*n));

                             n=n*10;

                     }

        }

      if(div==14397)

          {

                  CyPins_SetPin(Pin_2_0);

           }

      i--;

       

      if(arr[i]=='B')

      {

              i--;

             dir=arr[i];

              i--;

      }

      if(arr[i]=='B')

      {

              uint32 n=1;

              while(arr[i-1]!='A')

              {  

                  i--;

                step = (step+(arr[i]*n));

                 n=n*10;

               }

      }

      if(step==1504000)

        {

            CyPins_ClearPin(Pin_2_0);

         }

      /*-------------------------------------------------*/

       

      Counter_1_Stop();

      Counter_1_WriteCounter(step);

      ControlReg_1_Write(1);

      ControlReg_1_Write(0);

      Clock_1_Stop();

       

      switch(div)

      {

      case 9009:

                  Clock_1_SetSource(CYCLK_SRC_SEL_IMO);

                  break;

      case 36036:

                     Clock_1_SetSource(CYCLK_SRC_SEL_SYNC_DIG);

                 break;

      case 3000:

                     Clock_1_SetSource(CYCLK_SRC_SEL_IMO);

                 break;

      case 18005:

                     Clock_1_SetSource(CYCLK_SRC_SEL_SYNC_DIG);

                 break;

      case 14397:

                     Clock_1_SetSource(CYCLK_SRC_SEL_SYNC_DIG);    

                 break;

      }

      Clock_1_Start();

      Clock_1_SetDividerValue(div);

      Counter_1_Enable();

      while(1)

      {}

      }

       

      -------------------------------------------------------------------------------------------------------------

      Now the code that doesn't work when i include serial input.

       

      #include <device.h>

       

      int main()

      {

       

      char8 arr[17];

      char8 ch=0;

      uint8 i=0,buff_size=0,dir=0;

      uint32 div=0,step=0;

      ControlReg_1_Write(0);

      Counter_1_Start();

       

       

      CyGlobalIntEnable; /* Enable all interrupts by the processor. */

      UART_1_Start(); //Enable UART

       

       

      /*-----------Receive data from UART and store it in array------------*/

      UART_1_ClearRxBuffer();                       //Clears the memory array of all received data

      buff_size=UART_1_GetRxBufferSize();           //read the buffer size

       

      while(buff_size!=17)                          //wait until buffer size is not equal to 17

               {

                 buff_size=UART_1_GetRxBufferSize();

               }

       

          while(i!=17)                                    //stores the data in array upto termination character

             {

               ch=UART_1_GetChar();                   //Returns the next byte of received data

               if(ch<0x41)

                 {

                     ch=(ch-0x30);           

                 }

                arr[i]=ch;

                i++;

             }

      /*-----------------Data received and stored in array------------------*/

      CyGlobalIntDisable;

      UART_1_Stop();

       

      i=16;                                //point to last array location

      while(arr[i]!='D')                 

      {

             if(arr[i]!='D')                  //Decrement array until it points to 'D'

              {

                   i--;

              }

      }

       

      if(arr[i]=='D')    

        {

           uint32 n=1;

           while(arr[i-1]!='B')                //Extact the clock divider value form array

              { 

                   i--;   

                   div = (div+(arr[i]*n));

                  n=n*10;

               }

        }

       

      i--;

      if(arr[i]=='B')                           //Extract the direction from the array

        {

             i--;

            dir=arr[i];

             i--;

        }

       

       

      if(arr[i]=='B')                            //Extract the step value from array

        {

          uint32 n=1;

          while(arr[i-1]!='A')

          { 

             i--;

            step = (step+(arr[i]*n));

            n=n*10;

          }

      }

          

      /*----------Program to start counter and output the result to pin according to source----*/

      Counter_1_Stop();

      Counter_1_WriteCounter(step);

      ControlReg_1_Write(1);

      ControlReg_1_Write(0);

      Clock_1_Stop();

       

      switch(div)

      {

      case 9009:

                  Clock_1_SetSource(CYCLK_SRC_SEL_IMO);

                  break;

      case 36036:

                     Clock_1_SetSource(CYCLK_SRC_SEL_SYNC_DIG);

                 break;

      case 3000:

                     Clock_1_SetSource(CYCLK_SRC_SEL_IMO);

                 break;

      case 18005:

                     Clock_1_SetSource(CYCLK_SRC_SEL_SYNC_DIG);

                 break;

      case 14397:

                     Clock_1_SetSource(CYCLK_SRC_SEL_SYNC_DIG);    

                 break;

      }

      Clock_1_Start();

      Clock_1_SetDividerValue(div);

      Counter_1_Enable();

      while(1)

      {}

      }

       

       

      Best Regards,

      Anuja

        • 1. Re: serial data  to counter
          MoTa_728816

          Hi,

           

          With brief look looking at your code,

          I think following code may be a problem

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

              UART_1_Start(); //Enable UART

           

          /*-----------Receive data from UART and store it in array------------*/

              UART_1_ClearRxBuffer(); //Clears the memory array of all received data

           

              buff_size=UART_1_GetRxBufferSize();         //read the buffer size

              while(buff_size!=17) {                     //wait until buffer size is not equal to 17

                  buff_size=UART_1_GetRxBufferSize();

              }

           

              while(i!=17) {                             //stores the data in array upto termination character

                  ch=UART_1_GetChar();                   //Returns the next byte of received data

                  if(ch<0x41) {

                      ch=(ch-0x30);             

                  }

                  arr[i]=ch;

                  i++;

              }

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

           

          I'd rather write

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

              UART_1_ClearRxBuffer() ; // Clear Receive buffer (I'd like to clear it before starting UART_1)

              UART_1_Start() ; // Enable UART

           

              buff_size = UART_1_GetRxBufferSize() ; // read the buffer size

              while(buff_size < 17) { // buff_size could be bigger, please do not forget set buffer size of UART_1 to 17 or larger

                   buff_size = UART_1_GetRexBufferSize() ;

             }

           

             while(i < 17) {

                ch = UART_1_GetChar() ;

                if (ch < 0x41) {

                    ch = (ch -0x30) ;

                }

                arr[i] = ch ;

                i++ ;

             }

           

          // NOTE: Here, may be you need to print and check if arr[] contains the data you are expecting.

          // something like

          // #include <stdio.h>

          // char str[32] ;

          // for (i = 0 ; i < 17 ; i++ ) {

          //     sprintf(str, "%02X ", arr[i]) ;

          //     UART_1_PutString(str) ;

          // }

          //  UART_1_PutString("\n") ;

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

           

          moto

          • 2. Re: serial data  to counter
            anko_3968661

            hello Motoo,

             

            After writing my code i.e.

            UART_1_Start(); //Enable UART

             

            /*-----------Receive data from UART and store it in array------------*/

                UART_1_ClearRxBuffer(); //Clears the memory array of all received data

             

                buff_size=UART_1_GetRxBufferSize();         //read the buffer size

                while(buff_size!=17) {                     //wait until buffer size is not equal to 17

                    buff_size=UART_1_GetRxBufferSize();

                }

             

                while(i!=17) {                             //stores the data in array upto termination character

                    ch=UART_1_GetChar();                   //Returns the next byte of received data

                    if(ch<0x41) {

                        ch=(ch-0x30);            

                    }

                    arr[i]=ch;

                    i++;

                }

             

            I am getting all correct array elements . i received the data into arr[i]. and then printed same element to LCD. it is showing correct data.

            also i printed whatever 'step' and 'div' i am calculating after reception of array elements. it is showing correct result. but when i put these calculated numbers in counter then it is not giving result

            • 3. Re: serial data  to counter
              MoTa_728816

              Dear Anujakokil-san,

               

              I think that you have passed most error prone part of the program.

              And rest should be the same with the case without serial input.

               

              So can you test the 'step' and 'div' values you get from the serial with your "without UART" version?

              (1) In case "without UART" version works, and "with UART" version does not,

                  there must be difference between two after receiving the data to arr[].

              (2) If both does not work, problem is the algorithm or the value.

               

              Meantime, can you feed the serial the 'step' and 'div' value which worked with your "without UART" version?

              (3) If "with UART" version works with those values, the value you gave to the "with UART" version was not appropriate for your algorithm.

              (4) If "with UART" version does not work with those values, again the remaining part must be different from the "without UART" version. 

               

              If you can provide us a sample project and let us know what "result" you are expecting,

              then we may be able to try to find the problem.

              But with the information you provided us so far, this is about as far as I can get.

               

              Best Regards,

              2-May-2019

              Motoo Tanaka