CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

opinions about my PIC to PIC Code
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Friday
Guest







PostPosted: Tue Nov 04, 2008 2:04 pm     Reply with quote

hi,

anyone to answer the newbie ? Rolling Eyes
Friday
Guest







PostPosted: Thu Nov 06, 2008 2:16 am     Reply with quote

Hi,

could anybody tell me if the use of the switch() routine and multiple if,else if in while(bkbhit) {} loop would cause the loss of some bytes waiting in the buffer ?


despite my corrections ,I still have wrong frames .


I´m always receiving:

1_0_-24 (1 as code)


reception procedure:


Code:
void Get_Buffer_from_MB()
{

int1 GET_STX,RECEIVING_DATA;
int8 RXbyte,code ;//byte =int8

while(bkbhit)
     
    {
             
     RXbyte=bgetc();
     
     if(RXbyte==STX)
     {
     BufferIndex=0; 
     GET_STX=1;
     }
     
     else
     
     {  if (GET_STX)
         {
            RXBuffer[0]=RXbyte;
            Code=RXBuffer[0];           
            BufferIndex++;
            GET_STX=0;
            RECEIVING_DATA=1;
           
            Switch (code) 
            {
             case 0: BufferSize=1;break;
             case 1: BufferSize=1;break;
             case 2: BufferSize=2;break;
             default: BufferSize=0;break;           
           }             
         }
         
        else
          { if  ((RECEIVING_DATA==1) & (BufferSize>=1) & (BufferIndex<=BufferSize))
             
             {
                RXBuffer[BufferIndex]=RXbyte;
                BufferIndex++;
                 
             }
          }
     
     }
     
      if(RXbyte==ETX) //Check to see if end of frame has arrived.
      {
         //You now have a complete packet of data in the RX buffer.
         YOU_HAVE_DATA=1;
         RECEIVING_DATA=0;
      }   

     
    }

}




send procedure:


Code:
//=============Send Procedure=============

void Load_Buffer_MB_to_SM(int8 code,unsigned long freq_ms,int1 HF,int1 ED)
 {
 
 //----Load_Buffer Mother Board_to_Stepper Motor
 
 
 //Codes:
 
 // 0:Enable/Disable Stepper motor Controller (L297) (+ Data=1 bit)
 // 1:Half/Full  (+ Data=1 bit)
 // 2:Send Frequency (+ Data=int16)
 // 3:Get Frequency (without Data)
 // 4:Emergency Stop (without Data)
 
 
  unsigned int i,length;
  int8 SXBuffer[];
  int1 l;
 
 
 
 
  switch(code)
 
   {
    case 0:SXBuffer[1]=(int8)ED;length=1;l=1;break;
    case 1:SXBuffer[1]=(int8)HF;length=1;l=1;break;
    case 2:SXBuffer[1]=(int8)(freq_ms&0x00FF);
           SXBuffer[2]=(int8)(freq_ms>>8);
           length=2;l=1;break;
           
           
   default: break;       
   }


SXBuffer[0]=code;

//Sending
 

  bputc(STX);//send the Start of frame
 
  bputc(SXBuffer[0]);
 
  if(l)
  {
   for(i=1;i<=Length;i++)
   {
   bputc(SXBuffer[i]);   
   }
   l=0; 
  } 
   
  bputc(ETX);  // send end of frame.   
 
  // bputc("\n\r"); //CRLF
 
 }
   






Code:
#define STX  '$'

#define ETX  '€'
Friday
Guest







PostPosted: Thu Nov 06, 2008 12:08 pm     Reply with quote

hi,

I´ve made some corrections and I can get the first byte of the data. (but still false code=RXBUFFEr[0])

TRANSMITTER:


Code:
void Load_Buffer_MB_to_SM(unsigned int code,unsigned long freq_ms,int1 HF,int1 ED)
 {
 
  unsigned int i;
  int1 l=0;
  unsigned int length=0;
  unsigned int SXBuffer[];
 
 //----Load_Buffer Mother Board_to_Stepper Motor
 
 
 //Codes:
 
 // 0:Enable/Disable Stepper motor Controller (L297) (+ Data=1 bit)
 // 1:Half/Full  (+ Data=1 bit)
 // 2:Send Frequency (+ Data=int16)

 
 

 
 
 
 
  switch(code)
 
   {
    case 0:{SXBuffer[1]=(unsigned int)ED;length=1;l=1;break;}
    case 1:{SXBuffer[1]=(unsigned int)HF;length=1;l=1;break;}
    case 2:{SXBuffer[1]=(unsigned int)(freq_ms&0x00FF);
            SXBuffer[2]=(unsigned int)(freq_ms>>8);           
            length=2;l=1;break;}
           
           
   default: break;       
   }

SXBuffer[0]=code;


//Sending
 

  bputc(STX); //send the Start of frame
 
  bputc(SXBuffer[0]); //send the code
 
  if(l)
  {
   for(i=1;i<=Length;i++)
    {
    bputc(SXBuffer[i]); //send the data
    }
   l=0; 
  } 
   
  bputc(ETX);  // send end of frame.   

 
 }
   







void main() {

 int1 freqsent=0;

  set_tris_b(0x0f);
 
  enable_interrupts(global); 
  lcd_init();
  lcd_putc("Running...");



  delay_ms(1000);

 
 do{

 //  if bp1 envoi freq
 
 if(bp1 & !freqsent)
  {
   Load_Buffer_MB_to_SM(2,75,0,0);
   lcd_putc("\nFreq is sent");
   freqsent=1;

 
  }
 





RECEIVER:


Code:
void Get_Buffer_from_MB()
{

int1 GET_STX=0,RECEIVING_DATA=0;
unsigned int BufferIndex=0,RXbyte ;//byte =int8


while(bkbhit)
     
    {
             
     RXbyte=bgetc();
     
     if(RXbyte==STX)  //Check to see if start of frame has arrived.
     {
     BufferIndex=0;     
     GET_STX=1;
     }
     
     else
      {
        if (GET_STX)
          {
            RXBuffer[0]=RXbyte;         
            BufferIndex++;
            GET_STX=0;
            RECEIVING_DATA=1;
           
     
           
           if (RXBuffer[0]==0) {BufferSize=1;}
           if (RXBuffer[0]==1) {BufferSize=1;}
           if (RXBuffer[0]==2) {BufferSize=2;}
           
         }
         
   
      else {  if ((RECEIVING_DATA==1) && (BufferSize>=1) && (BufferIndex<=BufferSize))
             
               {
               RXBuffer[BufferIndex]=RXbyte;
               BufferIndex++;                 
               }
         
           }
     
      }
     
     
     
     
      if(RXbyte==ETX) //Check to see if end of frame has arrived.
      {
         //You now have a complete packet of data in the RX buffer.               
         YOU_HAVE_DATA=1;
         RECEIVING_DATA=0;         
      }   

     
    }

}


void Display_RXBuffer_from_MB()
{
int i,HF,LF;

if (BufferSize>0)
 {
 printf(lcd_putc,"SIZE:%i,FM:",BufferSize);
  for(i=0;i<=2;i++)
   {
   printf(lcd_putc,"%i,",RXBuffer[i]);
   }
   
 LF=RXBuffer[1];
 HF=RXBuffer[2];
 Freq=make16(HF,LF);
 printf(lcd_putc,"\nSENT FREQ:%Lu",Freq);
   
   
 }

BufferSize=0;
}




void main() {

   enable_interrupts(int_rda);

   enable_interrupts(global);

   lcd_init();

   lcd_putc("\f");
   
 do {
   
   
  Get_Buffer_from_MB();
 
  Display_RXBuffer_from_MB();
 
 // Read_Buffer_from_MB(RXBuffer[BufferSize]);
   
  //Excute_MB_Buffered_commands(); 
   
   
   } while (TRUE);




}


Friday
Guest







PostPosted: Thu Nov 06, 2008 12:10 pm     Reply with quote

my STX and ETX are '<' '>'

I´ll be very pleased if anyone could help me to find the origin of the problem.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 06, 2008 2:07 pm     Reply with quote

Quote:
unsigned int SXBuffer[];

case 0:{SXBuffer[1]=(unsigned int)ED;length=1;l=1;break;}
case 1:{SXBuffer[1]=(unsigned int)HF;length=1;l=1;break;}
case 2:{SXBuffer[1]=(unsigned int)(freq_ms&0x00FF);
SXBuffer[2]=(unsigned int)(freq_ms>>8);

There's no size declaration on your buffer. You've created a pointer.
There's no memory allocated to it. You're probably over-writing other
variables.

Essential C:
http://cslibrary.stanford.edu/101/EssentialC.pdf
MarcosAmbrose



Joined: 25 Sep 2006
Posts: 38
Location: Adelaide, Australia

View user's profile Send private message

Re: opinions about my PIC to PIC Code
PostPosted: Thu Nov 06, 2008 3:02 pm     Reply with quote

Friday wrote:
I was largely inspired by MarcosAmbrose code (in page 2 (vittorio topic))

Hi Friday, I'm happy to see I have "Inspired" someone. Did you try to implement the routines that I posted in the vittorio thread? For you, perhaps this may be easier than re-inventing the wheel.
Friday
Guest







PostPosted: Thu Nov 06, 2008 6:02 pm     Reply with quote

PCM programmer wrote:
Quote:
unsigned int SXBuffer[];

case 0:{SXBuffer[1]=(unsigned int)ED;length=1;l=1;break;}
case 1:{SXBuffer[1]=(unsigned int)HF;length=1;l=1;break;}
case 2:{SXBuffer[1]=(unsigned int)(freq_ms&0x00FF);
SXBuffer[2]=(unsigned int)(freq_ms>>8);

There's no size declaration on your buffer. You've created a pointer.
There's no memory allocated to it. You're probably over-writing other
variables.



Dear PCM Programmer,

you´ve found it !!!

now it works great,

a big thank you. Very Happy

thank you also for the link. (I really need it Wink )
Friday
Guest







Re: opinions about my PIC to PIC Code
PostPosted: Thu Nov 06, 2008 6:32 pm     Reply with quote

MarcosAmbrose wrote:
Friday wrote:
I was largely inspired by MarcosAmbrose code (in page 2 (vittorio topic))

Hi Friday, I'm happy to see I have "Inspired" someone. Did you try to implement the routines that I posted in the vittorio thread? For you, perhaps this may be easier than re-inventing the wheel.


Hi Marcos,

firstly, thank you for posting your code and inspiring me. Wink

as you can see in my two programs, I used your code with some changements and removals ,for instance,as I´m using the SISR.C and STISR.C routines , the RDA routine is already written, besides as I chose STX and ETX characters , so I don´t have to use the ESC and make its corresponding functions.other important thing: I can know the length of my received data since the read of the first byte (that I´m calling code).



any suggestion or question is still welcome. Smile
MarcosAmbrose



Joined: 25 Sep 2006
Posts: 38
Location: Adelaide, Australia

View user's profile Send private message

Re: opinions about my PIC to PIC Code
PostPosted: Thu Nov 06, 2008 6:44 pm     Reply with quote

Friday wrote:
Hi Marcos,

Cool, Glad you got it working. Don't forget to include some sort of checksum byte in your packet to guard against data transmission errors. A checksum byte can be as simple as just ADDing or XORing all your data together.


P.S My name's is not really Marcos. Marcos Ambrose is actually a Nascar driver from Australia.
Friday
Guest







Re: opinions about my PIC to PIC Code
PostPosted: Sun Nov 09, 2008 1:04 am     Reply with quote

MarcosAmbrose wrote:
Friday wrote:
Hi Marcos,

Cool, Glad you got it working. Don't forget to include some sort of checksum byte in your packet to guard against data transmission errors. A checksum byte can be as simple as just ADDing or XORing all your data together.
Quote:
Hi ,

thank you for the advice, I´ll add it.

P.S My name's is not really Marcos. Marcos Ambrose is actually a Nascar driver from Australia.


Wink



I've added routines in each code to have a whole communication (both of them can receive or transmit) using the protocol above and it is working fine.

So if anybody is interested, I can post them.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group