Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Displaying a 16 bit integer on PIC 16F877? (http://www.chiefdelphi.com/forums/showthread.php?t=68113)

Ian Curtis 17-06-2008 22:47

Displaying a 16 bit integer on PIC 16F877?
 
So, I've been playing around with a 16F877, and I got my hands on one of the kit gyros, and am trying to write my own code for it. I've got it mostly working, but I need to display a 16 bit integer to figure out what constitutes a 90 degree turn. But, the PIC doesn't want to do it. I can't for the life of me get it to output a 16 bit integer (that I can understand) to the debugger using a printf. I've tried %ld, but it didn't give me the right number. Is there some other trick (or formatting) I can use?

Alan Anderson 18-06-2008 08:24

Re: Displaying a 16 bit integer on PIC 16F877?
 
%ld is for 32-bit numbers. For a normal int value, just use %d.

Joe Ross 18-06-2008 09:34

Re: Displaying a 16 bit integer on PIC 16F877?
 
More important then knowing the type of PIC is what compiler you are using.

In C18 %ld prints in hex and expects a long (32 bits) while an int is 16 bits
In a compiler for your computer, both an int and a long are 32 bits, and both %d and %ld would print the same thing with both.

Other compilers (especially embedded compilers) will all have small variations like that, that you can only learn about by reading the manual.

Ian Curtis 20-06-2008 12:03

Re: Displaying a 16 bit integer on PIC 16F877?
 
Quote:

Originally Posted by Joe Ross (Post 753298)
More important then knowing the type of PIC is what compiler you are using.

In C18 %ld prints in hex and expects a long (32 bits) while an int is 16 bits
In a compiler for your computer, both an int and a long are 32 bits, and both %d and %ld would print the same thing with both.

Other compilers (especially embedded compilers) will all have small variations like that, that you can only learn about by reading the manual.

I'm using CCS. I ended up solving it by making all of the variables 32 bit integers, which obviously isn't the best solution. I think the problem came when I was doing 8 bit integer math and then set the result equal to a 32 or 16 bit integer. Is there a way to go about doing that?

The piece of code in question was:

angle=angle+instant

Originally, angle was 16 bit integer, and instant was an 8 bit integer. But I couldn't get a useful number, so I changed angle to a 32 bit integer, which I knew I had the syntax right, but it was still a funny number. After I changed instant to a 32 bit number it worked fine. I think I probably have to cast it somehow, but I don't really have a grasp on what casting exactly does, and why it works.

tseres 20-06-2008 15:42

Re: Displaying a 16 bit integer on PIC 16F877?
 
on a side not, you could also have done:
angle+=instant
:p


All times are GMT -5. The time now is 00:07.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi