serial data to counter

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
anko_3968661
Level 1
Level 1
First like given

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!='D')

{

     if(arr!='D')

         {

                i--;

          }

}

if(arr=='D')

      

  {

       uint32 n=1;

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

              {  

                        i--;   

                      div = (div+(arr*n));

                       n=n*10;

               }

  }

if(div==14397)

    {

            CyPins_SetPin(Pin_2_0);

     }

i--;

if(arr=='B')

{

        i--;

       dir=arr;

        i--;

}

if(arr=='B')

{

        uint32 n=1;

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

        {  

            i--;

          step = (step+(arr*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=ch;

          i++;

       }

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

CyGlobalIntDisable;

UART_1_Stop();

i=16;                                //point to last array location

while(arr!='D')                 

{

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

        {

             i--;

        }

}

if(arr=='D')    

  {

     uint32 n=1;

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

        { 

             i--;   

             div = (div+(arr*n));

            n=n*10;

         }

  }

i--;

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

  {

       i--;

      dir=arr;

       i--;

  }

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

  {

    uint32 n=1;

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

    { 

       i--;

      step = (step+(arr*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

0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

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=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 = 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) ;

//     UART_1_PutString(str) ;

// }

//  UART_1_PutString("\n") ;

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

moto

View solution in original post

0 Likes
3 Replies