Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] C programming problem

Status
Not open for further replies.

pravin b

Member level 5
Member level 5
Joined
May 20, 2012
Messages
85
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Location
Mumbai, India
Activity points
2,083
Hello Friends,

I am using PIC18F4520, IDE- MPLABX v3.65, compiler- hitec picc18.
I have one variable unsigned long AdcValue which contains reading of adc corresponding to temperature. Now, I have another variable float DecTemp which contains temperature in decimal. Calculation is: DecTemp=(AdcValue/10)/1000; i.e. if AdcValue = 316589 then DecTemp will be 031.658 deg cel.

To achieve this, the simple and correct (according to me) peice of code will be;
Code:
DecTemp=(AdcValue/10.0)/1000.0;
But whenever I execute this code my PIC18f4520 gets reset on keypress and I am not able to understand why!!!:-x:-x:-x
It has something to do with memory requirement??? (I have filled code memory = 62FAh of 8000h bytes ( 77.3%) & data memory = 2EDh of 600h bytes ( 48.8%). Or is it because of 8 bit architecture on PIC18F4520???
Please help me out and let me know what I am missing. Thanks in advance.

Note: if i will comment out this line from my code, machine will not reset on keypress. Infact if i will carry out any arithmetic operation on AdcValue machine gets reset on keypress and if i comment that line it will not.
 
Last edited:

Although your code is legal C, you should also try

Code C - [expand]
1
DecTemp=0.0001*AdcValue;

 

Try

Code:
DecTemp= ((double)AdcValue * 0.0001);
 

.... or more accurately DecTemp = (float)AdcValue * 0.0001;

It shouldn't have crashed though. Maybe something that has been fixed since MPLABX 3.65, the latest version is 5.15. and the latest compiler is XC8 V2.05, the compiler and IDE you are using is quite old.

Brian.
 

But whenever I execute this code my PIC18f4520 gets reset on keypress

Unless you have declared these variables with memory allocation instead of type assertion, it should not be an issue. Are you using whatchdog ?
 

Hello friends thanks for your replies. I tried every solution given but couldn't get it working. However, to keep every variables in float I written the code like;
Code:
float tempvar;
tempvar = AdcValue /10;
DecTemp  = (tempvar*0.001);

The reason for extra tempvar is to keep everything in float. And it is working now. Though I am getting same issue with other calculations also, will find the solution soon. Thanks guys for your kind support.

Also I am thinking about upgrading my development environment as suggested by andre_teprom; but it will take some more time.

Have a good day!
 

Hi,

I can't help with "C"

But from the measurement view:
if AdcValue = 316589
In my eyes this makes no sense.
316589 is an 19 bit value. For the complete measurement range you maybe need 20 bits or more
I doubt that your temperature precision is much better than 10 bits.

Maybe you could avoid the mathematical "C" problem by using meaningful input bit width.

Klaus
 

hello guys, thinking that XC8 might be better option i have installed XC8 and trying to compile the code but i get loads of warning like following:

lcd_rtc_pgm.c:207: warning: (759) expression generates no code
:0: error: (499) undefined symbol:
_Make32(dist/default/production\BambCal_XC8_V1.X.production.obj)
(908) exit status = 1

I am not getting this, what will be the solution to this errors? Thank you.
 

Have you removed all processor specific "#include" lines from the file and added "#include <xc.h>" instead? If not, you should.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top