how to add delimiter to code MIKROC pic

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
hi
i have this code ( mikroc with pic18f2550 ) usb
how can i add delimiter to it
mean if data coming end with 255 /255 /255 then mikro will do the code

for example my code to control duty cycle (( M1pwm= )) any value after = it will change duty cycle
i need to add 255/255/255
it will be like that
M1pwm= 44 255 255 255
if i receive data without delimiter 255/ 255/ 255
mcu will not response
how to make it


Code:
char read_buffer[64]  absolute 0x500;
char write_buffer[64] absolute 0x540;
char procesa[30];
char dato=0;
char cuenta=0;
  char kk;
  void interrupt()
   {
     usb_interrupt_proc();
   }


void main() {

     trisb.f0=1;

     HID_Enable(&read_buffer,&write_buffer);
     PWM1_Init(3000);
     PWM1_Set_Duty(0);
     PWM1_Start();
     read_buffer[cuenta]=dato;
  while(1)
   {
 
    if (portb.f0==1){delay_ms(300);
    sprintf(write_buffer, "start");
    while(!HID_Write(&write_buffer,64)) ;
                     }
    if (portb.f1==1){delay_ms(300);
    sprintf(write_buffer, "stop");
 
    while(!HID_Write(&write_buffer,64)) ;
                         }
      kk = HID_Read();
      if (kk != 0) {

      cuenta++;
       if (cuenta>=30)
      {
      cuenta=0;
      }

      if ( strstr(read_buffer,"START" )) { PWM1_Set_Duty(44); }
      else if ( strstr(read_buffer,"STOP" )) { PWM1_Set_Duty(0);}
      else if ( strstr(read_buffer,"M1pwm=" ))
                                        {
     strcpy(procesa, strtok(read_buffer, "="));
     strcpy(procesa, strtok(0, "M"));

        dato=atoi(procesa);
        PWM1_Set_Duty(dato);

                                }
       memset(read_buffer,0,30);
       cuenta=0;

       }
       }
      }
 

djsfantasi

Joined Apr 11, 2010
9,237
So it looks like you already have a delimiter between values. It is the “/“ character. If I understand your question, you need a delimiter to indicate the end of receiving the three values.

One way is to parse the input for three values separated by a “/“. If you are always expecting three values, then add another “/“ after the triplet. Like this:
“255/255/255/“.

Or another way is to add another character as an end of line indicator. Common characters are the null (0x00) or a CR (0x13).

Start by initializing M1pwm to “44 “.

Then your parse routine would start look for digits until you see a “/“. Append it to your “m1pwm” string plus another space character.

When you find a “/“, append another space to M1pwm and continue parsing as in the previous character.

When you find a CR or null character, you know the M1pwm string is complete. Do whatever you need with it.

Then continue by initializing M1pwm to “44 “ again and continue.
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
i have ready code for UART serial and its working

i need same code but in usb

i try to converted from UART to usb and add some code not working

here code for UART ( have delimiter )


Code:
signed char trama[30];
char x=0;
char cuenta=0;
char flag_rx=0;

char dato=0;

void Interrupcion() iv 0x0004 ics ICS_AUTO
{
    if (RCIF_bit==1)
  {
      dato=RCREG;
       trama[cuenta]=dato;
           if (( cuenta>3) && ((unsigned char)trama[cuenta]==0xff) && ((unsigned char)trama[cuenta-1]==0xff) && ((unsigned char)trama[cuenta-2]==0xff) )
           {
             flag_rx=1;
            CREN_bit=0;
             GIE_bit=0;
           }

       cuenta++;

        if (cuenta>=30)
      {
      flag_rx=0;
     cuenta=0;
      }

      RCIF_bit=0;
  }

}

  void procesa_rx()
  {
        if (flag_rx==1)

      {

                 if ( strstr(trama,"STOP" )) { PWM1_Set_Duty(0);  }
                else if ( strstr(trama,"M1pwm" ))
                {
                dato=trama[7];
                PWM1_Set_Duty(dato);
                }

       memset(trama,0,30);
      cuenta=0;
      flag_rx=0;
      RCIF_bit=0;
      CREN_bit=1;
      GIE_bit=1;
       }
   }

void serial_send(const char *info)
{
while(*info) UART1_Write(*info++);
}
void main()
{
trisb.f1=1;
trisb.f2=1;
trisb.f4=1;
trisb.f5=1;

adcon1=0;
INTCON=0b01000000;

UART1_Init(9600);

memset(trama,0,30);




RCIF_bit=0;
RCIE_bit=1;
GIE_bit=1;

PWM1_Init(1000);
PWM1_Set_Duty(0);
PWM1_Start();

  while (1)
  {



  // if(x==0){
  if (portb.f1==0) { delay_ms(10); //x=1;   // start
    serial_send("b.val=1");
    serial_send("\xFF\xFF\xFF");
  
                } //}

     if (portb.f2==0)   { delay_ms(10); x=0;  // stop
     serial_send("va1.val=43");
     serial_send("\xFF\xFF\xFF");

                }

     procesa_rx();
     Delay_ms(50);
  }

}


only i need to add this line to first code for usb
if (( cuenta>3) && ((unsigned char)trama[cuenta]==0xff) && ((unsigned char)trama[cuenta-1]==0xff) && ((unsigned char)trama[cuenta-2]==0xff) )
{
flag_rx=1;



any data comming end off 255 /255 /255
then mcu will do process

(((( the problem how to add interrupt line to usb
if (( cuenta>3) && ((unsigned char)trama[cuenta]==0xff) && ((unsigned char)trama[cuenta-1]==0xff) && ((unsigned char)trama[cuenta-2]==0xff) )
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
example

i for usb code i need to add this line but not working

Code:
  void interrupt()
   {
     usb_interrupt_proc();
     if (( cuenta>3) && ((unsigned char)trama[cuenta]==0xff) && ((unsigned char)trama[cuenta-1]==0xff) && ((unsigned char)trama[cuenta-2]==0xff) )
           {
             flag_rx=1;
            CREN_bit=0;
             GIE_bit=0;
           }
   }

how can i but if statment to test data comming for USB

??
 
Top