CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Can a 16bit (Nx8bit) variable be returned?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








PostPosted: Mon Jul 24, 2006 3:58 pm     Reply with quote

Ttelmah wrote:
The obvious comment, is 'maths overflow'. You are doing a 0 to 65 _inclusive_ loop. 66 samples. Each sample can be up to 1023 in value. 1023 * 66 = 67518. Too large to fit in an int16. You can only sum a maximum of 64*10 bit values in a int16.
Second comment. You seem to be trying to 'simulate', by writing to the ADRES registers. This won't work. The registers behave as a _pair_, and should really be treated as read only. In fact you can write to them, but writing to the low register wll clear the high register. I suspect this is why you are seeing an 8bit value...

Best Wishes


I realize that 66 values are above 16bit, but the conversion is never greater than 800, resistor values etc. The problem is that it doesn't pass the correct value even if i write it manually in MPLABSIM just before the return voltage_ line.

I don't write anything to the ADRESH/L registers, my code is in my posts. I think Mark's code got scrambled somehow.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 24, 2006 8:36 pm     Reply with quote

I ran it in the simulator so I fudged by setting some bogus values for the ADRES just to prove that the function will return a 16 bit number.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 24, 2006 9:09 pm     Reply with quote

1. Post a full test program. Display the result with a printf statement.
Make sure you use "%LX", with the "L" which is necessary with a 16-bit
or 32-bit variable. Show all #include, #use, and #fuses statements.

2. Post your compiler version.

3. When you post the code, make sure you select the box below
the posting window, to disable HTML. It looks like this:
Code:
x  Disable HTML in this post
eager



Joined: 24 Jul 2006
Posts: 6

View user's profile Send private message

PostPosted: Tue Jul 25, 2006 12:06 pm     Reply with quote

PCM programmer wrote:
1. Post a full test program. Display the result with a printf statement.
Make sure you use "%LX", with the "L" which is necessary with a 16-bit
or 32-bit variable. Show all #include, #use, and #fuses statements.

2. Post your compiler version.

3. When you post the code, make sure you select the box below
the posting window, to disable HTML. It looks like this:
Code:
x  Disable HTML in this post


1.I can not post a full test program, it's too long and too undestilled to read. I also have no idea what do you mean with "displaying the result with a printf statement". Keep in mind you are dealing with a noob here. However I can show you this:
Code:

#include <16f877A.h>
#include "16f877Areg.h"

#fuses HS ,NOWDT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, PUT
#use delay(clock=20000000)

2. compiler version:3.187; MPLAB v7.21

3. I will use it.
_________________
Best regards.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 25, 2006 12:13 pm     Reply with quote

Full test program does not mean all of your code. It means a complete minimum program that demonstrats your problem.
eager



Joined: 24 Jul 2006
Posts: 6

View user's profile Send private message

PostPosted: Tue Jul 25, 2006 12:30 pm     Reply with quote

Mark wrote:
Full test program does not mean all of your code. It means a complete minimum program that demonstrats your problem.


If I take the minimum code in a new *.c file, add the necessary definitions and keep the fuses etc. intact, it works. Everything is the same except that it is copy-pasted in the new *.c file. It seems that somewhere in the original project code I did something stupid. The only thing that comes to my mind right now is defining the same variable twice. But the compiler warns you if you do that. And besides I checked, it's not defined twice. Confused


EDIT: the stack is ok too.

EDIT1: note, I added "minimum code" in the begining of this post, to be more clear.
_________________
Best regards.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jul 26, 2006 2:26 am     Reply with quote

Quote:
If I take the minimum code in a new *.c file, add the necessary definitions and keep the fuses etc. intact, it works. Everything is the same except that it is copy-pasted in the new *.c file. It seems that somewhere in the original project code I did something stupid.
This is one of the main reasons why we always ask people to post a small but complete working program, just to rule out any side effects.

Quote:
The only thing that comes to my mind right now is defining the same variable twice. But the compiler warns you if you do that. And besides I checked, it's not defined twice.
A fix like that is only a quick workaround, not a solution and in the long term it will come back to haunt you. It sounds like you have a problem with data in RAM being overwritten. The most common cause for this is writing to non-existing array elements, for example when you copy a string and forgot to count in the terminating zero causing the copy to go 1 position further than accounted for.

A good way for zooming into the problem is by having a look at the list file (*.lst). The list file shows all variables sorted by memory location. Look up the return variable of your function and study the variables that are located directly before this address, is here an array or other variable that you access using pointers? If yes, then most likely that is your culprit.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 26, 2006 6:19 am     Reply with quote

Do you have a debugger? That's the simplest way to find out what is causing the problem. Try and make the code run in the simulator to find the problem.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
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