![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
function inside an if statement. |
Posted: Fri Feb 16, 2007 1:42 pm |
|
|
I don't understand why if I put in the little commented code the first check
start to fail. ((tho data seems ok))
Comment them out and it is fine. Can someone explain this to me?
..........
ccs V3.249
EDITED...Code now works...
Code: | #include <16F877.h>
#device *=16 //not needed for now.
#use delay(clock=4000000,restart_wdt)
#fuses hs,wdt,nolvp,protect,put,brownout
#use rs232(baud=19200,xmit=PIN_B3,INVERT,stream=DEBUG)
#case
#zero_ram
void init(void);
int8 flip(int8 in);
int8 reverse( int8 backwards);
char swap_bits(char test);
void PrintAsBinary(int8 Value);
//======================= Main ==============================//
void main(void){
int8 f,r;
int8 x=0;
init();
do{
restart_wdt();
f=flip(x);
r=reverse(x);
if(f!=r){
fprintf(DEBUG,"err x=0x%X\n\r",x);
PrintAsBinary(x);
fprintf(DEBUG,"\n\r");
PrintAsBinary(flip(x));
fprintf(DEBUG,"\n\r");
PrintAsBinary(reverse(x));
fprintf(DEBUG,"\n\r");
}
// if(flip(x)!=reverse(x)){
// fprintf(DEBUG,"err test inside the if x=0x%X\n\r",x);
// }
}while(x++!=255);
fprintf(DEBUG,"Done\n\r");
while(1){//forever loop,(send ACK/NACK, check for error, tickle watchdog)
restart_wdt();
}
}
//====== init ======//
void init(void){
fprintf(DEBUG,"Start\n\r");
setup_adc_ports(NO_ANALOGS);
}
int8 flip(int8 in)
{
int8 x, out=0;
for(x=0;x<7;++x)
{
out=out|in&1; // copy lsb from in to out
in=in>>1; // select next input bit
out=out<<1; // select next output bit
}
out=out|in; // copy the final bit
return out;
}
int8 reverse( int8 backwards )
{
int8 result=0;
int8 x;
x = 8;
do
{
if (bit_test(backwards,7))
bit_set(result,0);
rotate_left( &backwards,1);
rotate_right( &result, 1);
} while (--x);
return result;
}
//===== swap_bits =====//
char swap_bits(char test)
{
char result=0,x;
for( x=0;x<8;x++)
{
if(bit_test(test,7-x))
{
bit_set(result,x);
}
else
{
bit_clear(result,x);
}
}
return(result);
}
//********** Prints the given value as binary.*********//
void PrintAsBinary(int8 Value)
{
int8 i;
i = sizeof(Value) * 8; // Get number of bits.
do
{
if (shift_left(&Value, sizeof(Value), 0) == 1)
putc('1');
else
putc('0');
} while (--i);
}
|
Last edited by treitmey on Mon Feb 19, 2007 9:13 am; edited 2 times in total |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Feb 16, 2007 5:56 pm |
|
|
Not sure if it is the cause of the strange behaviour but there is a bug in flip(); the variable out is not initialized to zero.
Is your test by any chance initiated by my other post of today? ![Wink](images/smiles/icon_wink.gif) |
|
![](templates/subSilver/images/spacer.gif) |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Feb 19, 2007 8:59 am |
|
|
Yep, I just want to "regression test" to make sure all different ways are producing the same result. I edited the code above with "out=0"
All is well.
I guess that adding/removing the comment made the code move
from a "accidentally setting out=0" to not doing out=0 and failing. ??
I guess.??
I am ending up with a slightly modified version of my routine.
Code: | int8 reverse_6( int8 data )
{
int8 result=0;
if(bit_test(data,0)) bit_set(result,7);
if(bit_test(data,1)) bit_set(result,6);
if(bit_test(data,2)) bit_set(result,5);
if(bit_test(data,3)) bit_set(result,4);
if(bit_test(data,4)) bit_set(result,3);
if(bit_test(data,5)) bit_set(result,2);
if(bit_test(data,6)) bit_set(result,1);
if(bit_test(data,7)) bit_set(result,0);
return result;
}
|
The list file is basicly. Check bit and jump over a set if the bit is set..
.................... int8 reverse_6( int8 data )
.................... {
.................... int8 result=0;
008D: CLRF 25
.................... if(bit_test(data,0)) bit_set(result,7);
008E: BTFSC 24.0
008F: BSF 25.7
.................... if(bit_test(data,1)) bit_set(result,6);
0090: BTFSC 24.1
0091: BSF 25.6
.................... if(bit_test(data,2)) bit_set(result,5);
0092: BTFSC 24.2
0093: BSF 25.5
.................... if(bit_test(data,3)) bit_set(result,4);
0094: BTFSC 24.3
0095: BSF 25.4
.................... if(bit_test(data,4)) bit_set(result,3);
0096: BTFSC 24.4
0097: BSF 25.3
.................... if(bit_test(data,5)) bit_set(result,2);
0098: BTFSC 24.5
0099: BSF 25.2
.................... if(bit_test(data,6)) bit_set(result,1);
009A: BTFSC 24.6
009B: BSF 25.1
.................... if(bit_test(data,7)) bit_set(result,0);
009C: BTFSC 24.7
009D: BSF 25.0
.................... return result;
009E: MOVF 25,W
009F: MOVWF 78
.................... }
00A0: BCF 0A.3
00A1: BCF 0A.4
00A2: GOTO 128 (RETURN)
....................
.. |
|
![](templates/subSilver/images/spacer.gif) |
fuzzy
Joined: 26 Feb 2005 Posts: 64
|
|
Posted: Mon Feb 19, 2007 11:46 am |
|
|
Quote: | Code: | void main(void){
int8 f,r;
int8 x=0;
init();
do{
restart_wdt();
f=flip(x);
r=reverse(x);
if(f!=r){
fprintf(DEBUG,"err x=0x%X\n\r",x);
PrintAsBinary(x);
fprintf(DEBUG,"\n\r");
PrintAsBinary(flip(x));
fprintf(DEBUG,"\n\r");
PrintAsBinary(reverse(x));
fprintf(DEBUG,"\n\r");
}
// if(flip(x)!=reverse(x)){
// fprintf(DEBUG,"err test inside the if x=0x%X\n\r",x);
// }
}while(x++!=255);
fprintf(DEBUG,"Done\n\r");
while(1){//forever loop,(send ACK/NACK, check for error, tickle watchdog)
restart_wdt();
}
}
|
|
is parentesis sequence correct?? I see too many closed parentesis. ------> }
expecially this one: |
|
![](templates/subSilver/images/spacer.gif) |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Feb 19, 2007 12:26 pm |
|
|
I think its ok.. see the //end comments,.. that are matching up
Code: | #include <16F877.h>
#device *=16 //not needed for now.
#use delay(clock=4000000,restart_wdt)
#fuses hs,wdt,nolvp,protect,put,brownout
#use rs232(baud=19200,xmit=PIN_B3,INVERT,stream=DEBUG)
#case
#zero_ram
void init(void);
int8 flip(int8 in);
int8 reverse( int8 backwards);
char swap_bits(char test);
void PrintAsBinary(int8 Value);
//======================= Main ==============================//
void main(void){
int8 f,r;
int8 x=0;
init();
do{
restart_wdt();
f=flip(x);
r=reverse(x);
if(f!=r){
fprintf(DEBUG,"err x=0x%X\n\r",x);
PrintAsBinary(x);
fprintf(DEBUG,"\n\r");
PrintAsBinary(flip(x));
fprintf(DEBUG,"\n\r");
PrintAsBinary(reverse(x));
fprintf(DEBUG,"\n\r");
}//end if
// if(flip(x)!=reverse(x)){
// fprintf(DEBUG,"err test inside the if x=0x%X\n\r",x);
// }
}while(x++!=255);//end do-while
fprintf(DEBUG,"Done\n\r");
while(1){//forever loop,(send ACK/NACK, check for error, tickle watchdog)
restart_wdt();
}//end while forever
}//end main
//====== init ======//
void init(void){
fprintf(DEBUG,"Start\n\r");
setup_adc_ports(NO_ANALOGS);
}
int8 flip(int8 in)
{
int8 x, out=0;
for(x=0;x<7>>1; // select next input bit
out=out<<1; // select next output bit
}
out=out|in; // copy the final bit
return out;
}
int8 reverse( int8 backwards )
{
int8 result=0;
int8 x;
x = 8;
do
{
if (bit_test(backwards,7))
bit_set(result,0);
rotate_left( &backwards,1);
rotate_right( &result, 1);
} while (--x);
return result;
}
//===== swap_bits =====//
char swap_bits(char test)
{
char result=0,x;
for( x=0;x<8;x++)
{
if(bit_test(test,7-x))
{
bit_set(result,x);
}
else
{
bit_clear(result,x);
}
}
return(result);
}
//********** Prints the given value as binary.*********//
void PrintAsBinary(int8 Value)
{
int8 i;
i = sizeof(Value) * 8; // Get number of bits.
do
{
if (shift_left(&Value, sizeof(Value), 0) == 1)
putc('1');
else
putc('0');
} while (--i);
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Feb 19, 2007 1:09 pm |
|
|
treitmey wrote: | Code: | int8 reverse_6( int8 data )
{
int8 result=0;
if(bit_test(data,0)) bit_set(result,7);
if(bit_test(data,1)) bit_set(result,6);
if(bit_test(data,2)) bit_set(result,5);
if(bit_test(data,3)) bit_set(result,4);
if(bit_test(data,4)) bit_set(result,3);
if(bit_test(data,5)) bit_set(result,2);
if(bit_test(data,6)) bit_set(result,1);
if(bit_test(data,7)) bit_set(result,0);
return result;
}
|
| The resulting code from this function is equal to reverse_3() in http://www.ccsinfo.com/forum/viewtopic.php?p=76547. I like your function better as it is shorter to read and easier to understand. Can you add it to the other thread as well? |
|
![](templates/subSilver/images/spacer.gif) |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Feb 19, 2007 2:49 pm |
|
|
Yes. Its at the top of that link. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|