View previous topic :: View next topic |
Author |
Message |
alyeomans
Joined: 26 Feb 2014 Posts: 24
|
Int8 to Binary Function Trouble |
Posted: Sat Jul 02, 2016 1:30 am |
|
|
Hi All
I have an issue with the the function which takes an int8 and outputs the binary equivalent:
Code: | #include <18F4620.h>
//FUSES
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC //Internal RC Osc, with CLKOUT
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT
#FUSES NOPUT //No Power Up Timer
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage programming
#FUSES NOWRT //Program memory not write protected
#FUSES CCP2B3 //CCP2 output on RB3 and not RC1
#FUSES MCLR //Master Clear pin enabled
#define TX PIN_C6
#define RX PIN_C7
#use delay(clock=8000000)
#use RS232 (BAUD = 9600, XMIT = TX, RCV = RX, STREAM = COM_A, ERRORS)
//prints binary from int8 var
void printb(int8 b){
for (int8 i = 8; i > 0; i--) {
if(bit_test(b, i))
printf("1");
else
printf("0");
}
}
void main(void) {
while(1){
for(int8 s = 0; s < 8; s++){
printb(s);
printf("\n\r");
}
}
} //end main |
The expected output should be binary counting to 8 however I get:
Code: | 00000000
00000000
00000001
00000001
00000010
00000010
00000011
00000011 |
This seems silly - the answer must be staring me in the face!
Thanks
Al _________________ I type therefore I press buttons |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sat Jul 02, 2016 4:08 am |
|
|
Bits are 7 to 0.....
You are trying to start on bit 8, and counting to bit 1.
C has a function to do this already. Look at itoa. |
|
|
alyeomans
Joined: 26 Feb 2014 Posts: 24
|
|
Posted: Sat Jul 02, 2016 5:34 am |
|
|
Thanks again, Ttelmah. I'll have a look at the itoa function but what you pointed to with the bit index range was part of the problem. It seems that the issue was in the for loop condition: testing for i>=0 in this function caused the issue.
Does not work:
Code: | void printb(int8 b){
for (int8 i = 8; i>=0; i--) {
if(bit_test(b, i))
printf("1");
else
printf("0");
}
} |
Does work:
Code: | void printb(int8 b){
for (int8 i = 8; i>0; i--) {
if(bit_test(b, i-1))
printf("1");
else
printf("0");
}
} |
Cheers
Al _________________ I type therefore I press buttons |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Sat Jul 02, 2016 7:32 am |
|
|
With an unsigned integer, the index is _always_ going to be >=0. An unsigned can only ever be +ve, so how can it ever be <0?......
Simply use a signed int. |
|
|
alyeomans
Joined: 26 Feb 2014 Posts: 24
|
|
Posted: Mon Jul 04, 2016 6:54 am |
|
|
Thanks again...but to slide the last word in I was testing for equal or greater than 0 (i>=0) which adheres to the unsigned int rule when it failed.
A _________________ I type therefore I press buttons |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Mon Jul 04, 2016 7:47 am |
|
|
The problem is that when you decrement 0, this wraps. It can never go -ve in an unsigned, so can never be <0. Think about it. |
|
|
alyeomans
Joined: 26 Feb 2014 Posts: 24
|
|
Posted: Tue Jul 05, 2016 12:38 am |
|
|
Oww yes. I see. Red face. Thank you for setting me straight _________________ I type therefore I press buttons |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19520
|
|
Posted: Tue Jul 05, 2016 3:32 am |
|
|
Don't worry.
It's one of those ones that _everyone_ here will have been caught by at some point!.... |
|
|
|