element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Blog division operation with float variable mplabx v6.00 xc16 (v2.00)
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: hazard
  • Date Created: 18 May 2024 10:55 AM Date Created
  • Views 1109 views
  • Likes 3 likes
  • Comments 10 comments
Related
Recommended

division operation with float variable mplabx v6.00 xc16 (v2.00)

hazard
hazard
18 May 2024

hi every one.

I have some problem trying make some division with float variable.

some one can help me please.

main.c

while(0)
{
    menu_state = PARAM_MENU;
    param_menu = PARAM_MENU_FREQ;
    pwmq_freq_ui16 = encoder_knob_read();
    set_PWM1_FREQ();
    //set_PWM1_DT();
    sprintf(buffer1,"2-FREQ:%5uHz",pwmq_freq_ui16);
    oled_setcursor(0,2);
    oled_print_string(buffer1);
    
    return 0;
    }
    
    
    // prototype function
    
    void set_PWM1_FREQ(void)
{
    PWM1_set_Freq(pwmq_freq_ui16);  
}

pwm.c file

void PWM1_set_Freq(uint16_t Freq_PWM1)
{
     float Freq_PWM1_F = (float)1/Freq_PWM1;

    
    //uint16_t new_valor = (uint16_t)((65535*Freq_PWM1_F))/PWM1_CONST_2;
//    
//    if(OC1RS != new_valor)
//    {
//        OC1RS = (uint16_t)new_valor;
//    }
}

if I do this        float Freq_PWM1_F = (float)1/;

I get a:

image

in this moment  ( break point on main.c in line 8     "set_PWM1_FREQ();" )

image

when I step the line 44 the watches editor clear all the value.

but if I try do this:

pwm.c

void PWM1_set_Freq(uint16_t Freq_PWM1)
{

    float Freq_PWM1_F = (float)1/1000;

    
    //uint16_t new_valor = (uint16_t)((65535*Freq_PWM1_F))/PWM1_CONST_2;
//    
//    if(OC1RS != new_valor)
//    {
//        OC1RS = (uint16_t)new_valor;
//    }
}

works fine.

image

I know is confused my discription.

but if some one can try read and help I will aprecciate the kindness.

  • Sign in to reply
  • shabaz
    shabaz over 1 year ago in reply to jc2048

    I think it might be zero as you say. Impossible to be 100% sure with the limited code provided, but there's a good chance there's a divide by an uninitialized (random) or zero value, since the code has a while(0), which would mean that encoder isn't read, at least not in the snippet of code there.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • javagoza
    javagoza over 1 year ago in reply to hazard

    If you define a variable but it is not used later as is the case don't expect it to be evaluated. I think you'll have a compiler warning about that.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jc2048
    jc2048 over 1 year ago in reply to genebren

    I am a little confused to how this was allowed to compile without an error.

    There must be headers, so we aren't seeing it all. No idea where variables are declared and whether they are initialised or not.

    The OP seems to be hacking an existing PWM library.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jc2048
    jc2048 over 1 year ago

    Can the value returned by the read encoder routine be zero? If so, you're going to hit the problem of dealing with a divide by zero error at some point when doing the floating point reciprocal.

    It's possible that that might be what's going on here. The watch variables going to zero hints at a reset, which might be coming from the floating routines throwing an exception (it's a long time since I worked with PICs, and I never used the float stuff much for anything, anyway - they waste too much time in an embedded system where there's a lot going on - so I can't tell you directly).

    Even if it isn't the problem here, it's still a good idea to anticipate it and deal with it in your code.

    You could a) ensure the read encoder never returns zero; b) test for zero just before doing the division and substitute whatever answer you want under those circumstances; or c) understand properly what happens after the float division by zero and deal with it there, either dealing with the trap, if there is one, or handling a NaN or 'infinity' value handed back, if that's what the floating routines do.

    The last may not be very portable - I don't think floating point libraries are very consistent in how they deal with the outcome of a divide by zero.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • hazard
    hazard over 1 year ago in reply to genebren

    I hane tryid declare a global variable but the same hapen.

    I think I solve this with:

    double PER_PWM1_F = (double)1/(double)Freq_PWM1;

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube