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 1115 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
Parents
  • genebren
    genebren over 1 year ago

    You need to declare Freq_PWM1_F outside of the function.  If it is inside a function (as it is now) it is declared on the stack frame, which is not guaranteed to be available after the function call.  I am a little confused to how this was allowed to compile without an error.   

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

    You need to declare Freq_PWM1_F outside of the function.  If it is inside a function (as it is now) it is declared on the stack frame, which is not guaranteed to be available after the function call.  I am a little confused to how this was allowed to compile without an error.   

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • 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
  • 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
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