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 support@ccsinfo.com

Help to this code :(

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
albe01



Joined: 02 Jul 2010
Posts: 30
Location: italy

View user's profile Send private message

Help to this code :(
PostPosted: Tue Jul 13, 2010 6:19 am     Reply with quote

Hello to all, i'd like solve expecting declaration problem in this code:
someone can help me? i use a pic 18f4525

#define rcvPin PIN_A5
#define LED PIN_B7
// CICLO PRINCIPALE RICEZIONE

int bte[768]; //Array dove viene memorizzato il pacchetto ricevuto
byte escirx; //Flag per uscire dal ciclo di ricezione

int test, newbit, numbyte, ones, inbyte;

// ************************ RX SUBROUTINES *****************



// --------------------------------------------------------------


short fcscheck(){ //computes the fcs
byte lblo,lbhi;
int k ,bt;


fcslo=fcshi=0xFF; //intialize FCS values
for (i=0;i<(numbyte-2);i++){ //calculate the FCS for all except the last two bytes
inbyte = bte[i];
for(k=0;k<8;k++){ //perform this procedure for each bit in the byte
bt = inbyte & 0x01;

lbhi=fcshi&0x01; // Memorizza il bit meno significativo di fcshi
lblo=fcslo&0x01; // Memorizza il bit meno significativo di fcslo
fcshi=fcshi>>1; // Sposta a destra di 1 bit tutti i bit di fcshi
fcslo=fcslo>>1; // Sposta a destra di 1 bit tutti i bit di fcslo
fcslo=(lbhi*128)+fcslo; // Oppure fcslo=(lbhi<<7)+fcslo; è equivalente e più veloce
if ((lblo^tbyte) == 0x01){
fcshi = fcshi^0x84;
fcslo = fcslo^0x08;
} // end of if

rotate_right(&inbyte,1); // get next bit
} //end of for
} // end of for
fcslo = fcslo^0xff;
fcshi = fcshi^0xff;
if ((bte[numbyte-1] == fcshi) && (bte[numbyte-2] == fcslo)) {
return 1;
}
else return 0; //if the computed values equal the last two data bytes
} //end of fscheck()


// --------------------------------------------------------------



int bitin(){ //function to read a bit

static int oldstate; //oldstate retained between runs of this function
int k;

for (k=0;k<121;k++){ //this loop allows 838 us to go by. If no state change, bit is 1
if (input(rcvPin) != oldstate){ //if state has changed
oldstate = input(rcvPin); //update oldstate
delay_us(430); // move to halfway thru the next bit
return 0; //return 0 if state changed
}//end of if
}//end of for

return 1; //return 1 if state did not change

}//end of bitin()



while (escirx == 0){ // il ciclo deve essere continuo
//Look for the 1st flag:

int cbyte = 0; //initialize
while (cbyte != 0x7e){ //find the first flag
shift_right(&cbyte,1,bitin()); //add a new bit to the left of cbyte, discard
right bit
} //end of while
output_high(LED); //turn on the DCD light


while (cbyte == 0x7e){ //find the other flags
for(i=0;i<8;i++){ //repeat this 8 times
shift_right(&cbyte,1,bitin()); //add a new bit to the left of cbyte, discard right bit
} //end of for
} //end of while -- now at end of all the flags

bte[0] = cbyte; //you've now got the first address byte

//Collect the rest of the data:


test =0;
numbyte = 1; //we already collected 1 byte
while (test != 1){ //do this until the flag at the end of the packet
for(i=0;i<8;i++){ //collect 8 bits
newbit = bitin(); //get a bit
if (newbit==1) (ones++); //increment the ones counter
else (ones = 0); //if bit is a zero, reset the ones counter
if(ones==5) { //removes bit stuffing
test = bitin(); //get the next bit but don't add it to cbyte
ones = 0; //reset the ones counter
}
shift_right(&cbyte,1,newbit); //append the new bit to cbyte
} //end of for
if(test==0){
bte[numbyte] = cbyte; //add cbyte to the array
numbyte++; //increment the number of received bytes
}
} //end of while

//Checking the FCS and Formatting the Output

if (fcscheck()) (printout()); //if the fcs checks output the packet.

} //fine ciclo principale ricezione


void printout(){ //function to display the received packet
int i,L,m,temp;

for (m=7;m<13;m++){ //print the source callsign
if (bte[m] != 0x40) (putc(bte[m]>>1)); //note spaces (40) are not printed
}

putc('-');
putc(((bte[13] & 0x7F)>>1)); //print source SSID
putc('>');
for (m=0;m<6;m++){ //print the destination callsign
if (bte[m] != 0x40) (putc(bte[m]>>1));
} //end of for
putc('-');
putc(((bte[6] & 0x7F)>>1)); //print the dest SSID
L = 7;

if ((bte[13] & 0x01) != 1){ //print any path that may exist
do{
putc(',');
L=L+7;
for (m=L;m<(L+6);m++){
if (bte[m] != 0x40) (putc(bte[m]>>1));
} //end of for
putc('-');
putc(((bte[(L+6)] & 0x7F)>>1));
}while ((bte[L+6] & 0x01) != 1);
} //end of if
putc(':');
putc(' ');
L=L+9; //add 9 to move past the last callsign, cntl and PID
while (L< numbyte-2){ //print the text (not including fcs bytes)
putc(bte[L]);
L++;
} //end of while

printf("\n"); //add carriage return/line feed at the end
} //end of printout()
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 13, 2010 7:23 am     Reply with quote

Ok, a few things:

-the reason why no one has yet looked at your code is because its very difficult to read. Use the BBCode option to put code into code brackets.

-indicate where exactly your problem occurs.

-if possible, give a complete compilable example program replicating the same problem. The example program should have only simple variable names like 'a' and 'b' (and not super_cool_variable!)


Rohit
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 13, 2010 7:29 am     Reply with quote

I had a look at your code. The following things are missing:
Code:
#include "18F4525.h"
#fuses //put appropriate fuses here
#use delay (clock=xxxx) ///put appropriate clock speed


Additionally, almost all the variables are not initialized. Fix this by using 'int' '#define' etc.

Rohit
albe01



Joined: 02 Jul 2010
Posts: 30
Location: italy

View user's profile Send private message

PostPosted: Tue Jul 13, 2010 8:19 am     Reply with quote

this is the main:
Code:

#define (__PCH__)

#if defined(__PCH__)
#include <18F4525.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOMCLR
#use delay(clock=14318000)
#endif

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <tastiera.c>
#include <HDM64GS12.c>
#include <modemTX.c>
#include <graphics.c>
#define FAST_GLCD
#use rs232(baud=1200, xmit=PIN_A0,rcv=PIN_A1)

.
Code:


#define rcvPin PIN_A5
#define LED PIN_B7
// CICLO PRINCIPALE RICEZIONE

int bte[768]; //Array dove viene memorizzato il pacchetto ricevuto
byte escirx; //Flag per uscire dal ciclo di ricezione

int test, newbit, numbyte, ones, inbyte;

// ************************ RX SUBROUTINES *****************



// --------------------------------------------------------------


short fcscheck(){ //computes the fcs
byte lblo,lbhi;
int k ,bt;


fcslo=fcshi=0xFF; //intialize FCS values
for (i=0;i<(numbyte-2);i++){ //calculate the FCS for all except the last two bytes
inbyte = bte[i];
for(k=0;k<8;k++){ //perform this procedure for each bit in the byte
bt = inbyte & 0x01;

lbhi=fcshi&0x01; // Memorizza il bit meno significativo di fcshi
lblo=fcslo&0x01; // Memorizza il bit meno significativo di fcslo
fcshi=fcshi>>1; // Sposta a destra di 1 bit tutti i bit di fcshi
fcslo=fcslo>>1; // Sposta a destra di 1 bit tutti i bit di fcslo
fcslo=(lbhi*128)+fcslo; // Oppure fcslo=(lbhi<<7)+fcslo; è equivalente e più veloce
if ((lblo^tbyte) == 0x01){
fcshi = fcshi^0x84;
fcslo = fcslo^0x08;
} // end of if

rotate_right(&inbyte,1); // get next bit
} //end of for
} // end of for
fcslo = fcslo^0xff;
fcshi = fcshi^0xff;
if ((bte[numbyte-1] == fcshi) && (bte[numbyte-2] == fcslo)) {
return 1;
}
else return 0; //if the computed values equal the last two data bytes
} //end of fscheck()


// --------------------------------------------------------------



int bitin(){ //function to read a bit

static int oldstate; //oldstate retained between runs of this function
int k;

for (k=0;k<121;k++){ //this loop allows 838 us to go by. If no state change, bit is 1
if (input(rcvPin) != oldstate){ //if state has changed
oldstate = input(rcvPin); //update oldstate
delay_us(430); // move to halfway thru the next bit
return 0; //return 0 if state changed
}//end of if
}//end of for

return 1; //return 1 if state did not change

}//end of bitin()



while (escirx == 0){ // il ciclo deve essere continuo
//Look for the 1st flag:

int cbyte = 0; //initialize
while (cbyte != 0x7e){ //find the first flag
shift_right(&cbyte,1,bitin()); //add a new bit to the left of cbyte, discard
right bit
} //end of while
output_high(LED); //turn on the DCD light


while (cbyte == 0x7e){ //find the other flags
for(i=0;i<8;i++){ //repeat this 8 times
shift_right(&cbyte,1,bitin()); //add a new bit to the left of cbyte, discard right bit
} //end of for
} //end of while -- now at end of all the flags

bte[0] = cbyte; //you've now got the first address byte

//Collect the rest of the data:


test =0;
numbyte = 1; //we already collected 1 byte
while (test != 1){ //do this until the flag at the end of the packet
for(i=0;i<8;i++){ //collect 8 bits
newbit = bitin(); //get a bit
if (newbit==1) (ones++); //increment the ones counter
else (ones = 0); //if bit is a zero, reset the ones counter
if(ones==5) { //removes bit stuffing
test = bitin(); //get the next bit but don't add it to cbyte
ones = 0; //reset the ones counter
}
shift_right(&cbyte,1,newbit); //append the new bit to cbyte
} //end of for
if(test==0){
bte[numbyte] = cbyte; //add cbyte to the array
numbyte++; //increment the number of received bytes
}
} //end of while

//Checking the FCS and Formatting the Output

if (fcscheck()) (printout()); //if the fcs checks output the packet.

} //fine ciclo principale ricezione


void printout(){ //function to display the received packet
int i,L,m,temp;

for (m=7;m<13;m++){ //print the source callsign
if (bte[m] != 0x40) (putc(bte[m]>>1)); //note spaces (40) are not printed
}

putc('-');
putc(((bte[13] & 0x7F)>>1)); //print source SSID
putc('>');
for (m=0;m<6;m++){ //print the destination callsign
if (bte[m] != 0x40) (putc(bte[m]>>1));
} //end of for
putc('-');
putc(((bte[6] & 0x7F)>>1)); //print the dest SSID
L = 7;

if ((bte[13] & 0x01) != 1){ //print any path that may exist
do{
putc(',');
L=L+7;
for (m=L;m<(L+6);m++){
if (bte[m] != 0x40) (putc(bte[m]>>1));
} //end of for
putc('-');
putc(((bte[(L+6)] & 0x7F)>>1));
}while ((bte[L+6] & 0x01) != 1);
} //end of if
putc(':');
putc(' ');
L=L+9; //add 9 to move past the last callsign, cntl and PID
while (L< numbyte-2){ //print the text (not including fcs bytes)
putc(bte[L]);
L++;
} //end of while

printf("\n"); //add carriage return/line feed at the end
} //end of printout()
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jul 13, 2010 3:20 pm     Reply with quote

Ok, you have found the Code button when posting code.
But is this really how you write your code? Without indentation?

A more common notation is like:
Code:
while(something)
{
    for(i=0; bla; bla)
    {
        if (condition)
        {
        }
    }
}
Now the level of indentation makes it clear to you where the blocks start/stop and which groups belong to each other.

I cannot compile your example code because there are at least four C-files missing. Same for the definition of variables like i, fcslo,fcshi and tbyte.
When posting code make it a short and complete program.

Rohit de Sa wrote:
-indicate where exactly your problem occurs.
You didn't do this. So how are we to know where your problem is?

One major error is that you have code that is not inside a function. This will never compile.
Code:
return 1; //return 1 if state did not change
}//end of bitin()

            <<<<<<<   Function start is missing here

while (escirx == 0){ // il ciclo deve essere continuo
//Look for the 1st flag:
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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