View previous topic :: View next topic |
Author |
Message |
mcad
Joined: 25 Nov 2007 Posts: 48
|
long type negative value occurance |
Posted: Thu Dec 06, 2007 1:25 pm |
|
|
Hi all,
I am displaying the speed and the duty cycle of the dc motor.
But, in spite of making the speed variable long data type, it sometimes displays negative numbers. So, would you please tell me how I can fix it ?
Code: |
Taken back for a while
|
Last edited by mcad on Thu Dec 06, 2007 3:39 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 06, 2007 1:26 pm |
|
|
Use "%lu" to display unsigned 16-bit values. Don't use "%ld". |
|
|
mcad
Joined: 25 Nov 2007 Posts: 48
|
|
Posted: Thu Dec 06, 2007 1:38 pm |
|
|
thanks sir.
But, I also saw "%04lu" . which is better to use sir ? |
|
|
Ttelmah Guest
|
|
Posted: Thu Dec 06, 2007 3:43 pm |
|
|
The '04', just says how many digits to always display, and to use leading zeros. 'u' means to treat the value given as an unsigned value, while 'd' says to treat it as a signed value. The 'key' is to understand that the output specification, is independant of the real 'type' of the internal number. Hence if you have a value of (hex) FFFF, and display this using 'ld', it gets printed as -1, while the same internal value, if printed with 'lu', gets displayed as 65535.
I you have a value of '0', and print it with %lu, you get '0' displayed, but use %04lu, and '0000' gets displayed.
Best Wishes |
|
|
mcad
Joined: 25 Nov 2007 Posts: 48
|
|
Posted: Thu Dec 06, 2007 5:25 pm |
|
|
Thanks very much sir for your clarification.
Regards |
|
|
|