+ Post New Thread
Results 1 to 9 of 9
  1. #1
    Member level 4
    Points: 1,629, Level: 9

    Join Date
    May 2012
    Location
    Mumbai, India
    Posts
    73
    Helped
    1 / 1
    Points
    1,629
    Level
    9

    C programming problem

    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!!!
    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 by pravin b; 18th April 2019 at 17:18.

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 257,269, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    44,887
    Helped
    13656 / 13656
    Points
    257,269
    Level
    100

    Re: C programming problem

    Although your code is legal C, you should also try
    Code C - [expand]
    1
    
    DecTemp=0.0001*AdcValue;



  3. #3
    Advanced Member level 2
    Points: 2,848, Level: 12
    baileychic's Avatar
    Join Date
    Aug 2017
    Posts
    562
    Helped
    49 / 49
    Points
    2,848
    Level
    12

    Re: C programming problem

    Try

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



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 79,807, Level: 68
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,063
    Helped
    4372 / 4372
    Points
    79,807
    Level
    68

    Re: C programming problem

    .... 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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   AltAdvertisement

        
       

  5. #5
    Super Moderator
    Points: 29,375, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,763
    Helped
    1108 / 1108
    Points
    29,375
    Level
    41
    Blog Entries
    9

    Re: C programming problem

    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 ?
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  6. #6
    Member level 4
    Points: 1,629, Level: 9

    Join Date
    May 2012
    Location
    Mumbai, India
    Posts
    73
    Helped
    1 / 1
    Points
    1,629
    Level
    9

    Re: C programming problem

    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!



  7. #7
    Super Moderator
    Points: 75,476, Level: 67
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,299
    Helped
    3485 / 3485
    Points
    75,476
    Level
    67

    Re: C programming problem

    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
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  8. #8
    Member level 4
    Points: 1,629, Level: 9

    Join Date
    May 2012
    Location
    Mumbai, India
    Posts
    73
    Helped
    1 / 1
    Points
    1,629
    Level
    9

    Re: C programming problem

    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.



    •   AltAdvertisement

        
       

  9. #9
    Super Moderator
    Points: 79,807, Level: 68
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,063
    Helped
    4372 / 4372
    Points
    79,807
    Level
    68

    Re: C programming problem

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

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



--[[ ]]--