|
|
View previous topic :: View next topic |
Author |
Message |
chava
Joined: 07 Sep 2003 Posts: 56
|
CCS produce a wrong code! |
Posted: Sun Apr 04, 2004 4:04 pm |
|
|
Hello
I wrote a simple function which receives a number, 0-256 (char) and makes a division to separate its ten digit and it's unit digit (example: 65=6 ten + 5 unit)
simple, isnt it?
here it is:
Code: |
div_t x;
x=div(65,10);
righ=5;
midd=x.rem;
left=x.quot;
|
dum, simple, every starting programmer should know that
but...
the values "righ","midd","left" are transfered to 3 different functions, each use a lookup table , to convert the value (0-9) to 7 segment format, then to portb.
simple.. yet...
when I run the code the variable "midd" gets the value of the variable "left", no metter what value I use.
Originaly the "div" function divided a variable that way:
char t;
x=div(t,10);
when it didnt work, I did "x=div(65,10)" to see if the problem lies in the div function.
I even built a division function of my own - but the result was the same.
Help me out here
Am I vaiolating some kind of CCS PIC "C" Compiler rull?
I even did
Code: |
righ=displayed_val._fraction;
midd=6;
left=5;
|
and again, "midd" gets the value 5 |
|
|
Ttelmah Guest
|
Re: CCS produce a wrong code! |
Posted: Mon Apr 05, 2004 3:04 am |
|
|
chava wrote: | Hello
I wrote a simple function which receives a number, 0-256 (char) and makes a division to separate its ten digit and it's unit digit (example: 65=6 ten + 5 unit)
simple, isnt it?
here it is:
Code: |
div_t x;
x=div(65,10);
righ=5;
midd=x.rem;
left=x.quot;
|
dum, simple, every starting programmer should know that
but...
the values "righ","midd","left" are transfered to 3 different functions, each use a lookup table , to convert the value (0-9) to 7 segment format, then to portb.
simple.. yet...
when I run the code the variable "midd" gets the value of the variable "left", no metter what value I use.
Originaly the "div" function divided a variable that way:
char t;
x=div(t,10);
when it didnt work, I did "x=div(65,10)" to see if the problem lies in the div function.
I even built a division function of my own - but the result was the same.
Help me out here
Am I vaiolating some kind of CCS PIC "C" Compiler rull?
I even did
Code: |
righ=displayed_val._fraction;
midd=6;
left=5;
|
and again, "midd" gets the value 5 |
There are a lot of things wrong with your code.
First (which won't cause a problem with the value '65', but will latter), the 'div' function is only specified for _signed_ integers. Secondly (and hopefully just a typing error), an integer, can only hold 0-255, not 0-256.
These faults will 'bite you' latter, but should not cause your problem.
As written, the code should give left=6, midd=5, and righ=5. If this is not what you see, I have to then ask how you are actually 'checking' this. I have set up the same code to 'printf' the digits + 0x30, to an RS232, and this is exactly what is generated...
The correct sequence for a three digit conversion is:
x=div(65,10);
righ=x.rem;
x=div(x.quot,10);
midd=x.rem;
left=x.quot;
Which gives 065 for the three digits.
Best Wishes |
|
|
chava
Joined: 07 Sep 2003 Posts: 56
|
I did too |
Posted: Wed Apr 07, 2004 12:45 am |
|
|
Hello
I also run this code but instead of printing the value on a 7 segment display I transmitted the data to a LCD, the result was as axpected:65.
but note: the program which I did with the LCD is for pic16f877a, and the one for the 7 segment is for 16f76
does that metter?
do you think that the pic16f76 is defected in someway?
Chava |
|
|
|
|
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
|