c - Is my method of passing a struct as both value and reference correct? -
i have looked @ examples of passing struct both value , reference. code compiles not working should. using c program micro-controller hard check if working properly, not getting desired output.
so, per instructions, first define structure:
struct package //define structure type called package. { unsigned char wavtype,startfreq1,startfreq2,startfreq3,startfreq4, stopfreq1,stopfreq2, stopfreq3,stopfreq4,step,dura,amp,sett; //define bytes use };
in main method create instance of it:
struct package p; //create new instance of package
now pass reference (pointer - because i'm using c) function:
getpackage(&p);
within function getpackage() update values of respective elements of p:
getpackage(struct package *p) //get data package { p->wavtype = receive(); p->startfreq1 = receive(); p->startfreq2 = receive(); p->startfreq3 = receive(); p->startfreq4 = receive(); p->stopfreq1 = receive(); p->stopfreq2 = receive(); p->stopfreq3 = receive(); p->stopfreq4 = receive(); p->step = receive(); p->dura = receive(); p->amp = receive(); p->sett = receive(); }
this receive function:
unsigned char receive(void) { unsigned char datar = 0x00; for(signed char = 0; <=7 ;i++) { datar |= portbbits.rb1 << i; //move value on data pin bit in datar } return datar; }
question: correctly update bytes in package p? also, package p need returned if want use elsewhere? ask because....
i pass package p, value, function using:
sendsine(p);
this function makes use of value of bytes in package p:
void sendsine(struct package p) { datal = p.startfreq1; datah = p.startfreq2; send(datal,datah); datal = p.startfreq3; datah = p.startfreq4; send(datal,datah); }
i know function send(datal,datah) working because have tested setting datal , datah hand , required result, there must error along way struct - cant figure out where... can me might be?
the receive function seems procedure in question. try writing stub-replacement receive, such as:
unsigned char receive(void) { unsigned char x = 'a'; // or whatever value want simulate being received return x; }
and try running complete application, tf works go , re-think original receive per of comments have been made.
Comments
Post a Comment