• AVR Freaks

Hot!Trouble passing unsigned long out of "C Function Call" block - dsPic 33EP32MC202

Author
heath75
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2019/05/12 10:10:54
  • Location: 0
  • Status: offline
2019/05/13 15:11:10 (permalink)
0

Trouble passing unsigned long out of "C Function Call" block - dsPic 33EP32MC202

I'm having some trouble sending unsigned long values out of the C Function Call Block.  I've attached a model I put together to help troubleshoot the issue.  It makes output A0 pulse to make an LED blink and has 3 functions (testa, testb, and testc) that return values of 16384, 32768, and 65536 respectively, or so it should.  It is also setup to connect to Matlab via the serial port through external mode to display those values.
 
The actual values are that are displayed in Matlab are correct for testa=16384, but not so for testb=4294934528 and testc=0.  It seems the variables are overflowing.
 
Has anyone run into this issue or see any issues with the model/code?
 
Thanks in advance.
#1

6 Replies Related Threads

    Lubin
    Moderator
    • Total Posts : 359
    • Reward points : 5
    • Joined: 2007/03/31 07:38:15
    • Location: Bayonne, France
    • Status: offline
    Re: Trouble passing unsigned long out of "C Function Call" block - dsPic 33EP32MC202 2019/05/14 02:14:31 (permalink)
    0
    Hi,
     
    I duplicated the issue based on your model and a microstick II dev board.
     
    The function you are adding are custom functiosn (i.e. not printf or delay from the compiler library).
    When the function is called, it should be declared first.
     
    In this example, the model compiled and I did not noticed warning at compilation time. However the compiler does not have informations about the function input/output datatype.
     
    You might add a .h file and include the file in the header.
    Easier than this, just check the "Add this function declaration in header file" for each C function call block with a new function. This will ensure the function is properly declared prior to using it at compilation time.

     
    Thsu External Mode shows correct results.
     
     

    Attached Image(s)

    #2
    heath75
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2019/05/12 10:10:54
    • Location: 0
    • Status: offline
    Re: Trouble passing unsigned long out of "C Function Call" block - dsPic 33EP32MC202 2019/05/14 04:44:06 (permalink)
    0
    Hi Lubin,
     
    Thank you very much for the help!  At one point I did try to include a header file declaring the function, but it did not seem to help, but the check box certainly does.
     
    The next thing I would like to do is declare the function as a pointer in order to pass an array out of the function.  When I do this, the output of the C Function Call block disappears.  I tried to put "a" in the "Block Output" box, but that did not help.  
     
    Am I doing something wrong to allow this functionality?
     
    Thanks,
    Heath
     
    #3
    Lubin
    Moderator
    • Total Posts : 359
    • Reward points : 5
    • Joined: 2007/03/31 07:38:15
    • Location: Bayonne, France
    • Status: offline
    Re: Trouble passing unsigned long out of "C Function Call" block - dsPic 33EP32MC202 2019/05/14 06:51:06 (permalink)
    0
    i Heath,
     
    One example shipped with the blockset shows how using pointer with the C funciton call block: see file Microstick_II_33EP128MC202_CustomCFunCall.mdl.
     
    To copy this file in the working folder, type picInfo at the matlab prompt, and follow menu to click on: picInfo('examples')
     
    Lubin
     
     

    Attached Image(s)

    #4
    heath75
    New Member
    • Total Posts : 3
    • Reward points : 0
    • Joined: 2019/05/12 10:10:54
    • Location: 0
    • Status: offline
    Re: Trouble passing unsigned long out of "C Function Call" block - dsPic 33EP32MC202 2019/05/15 10:47:32 (permalink)
    0
    Hi Lubin,
     
    That example was exactly what I needed.  Not sure how I missed it. Thanks again.
     
    Heath
    #5
    FilippoMonti
    Starting Member
    • Total Posts : 31
    • Reward points : 0
    • Joined: 2017/11/03 03:10:15
    • Location: 0
    • Status: offline
    Re: Trouble passing unsigned long out of "C Function Call" block - dsPic 33EP32MC202 2019/05/15 23:30:28 (permalink)
    0
    Hi Lubin,
     
    I take the opportunity to ask about CFunctionCall block.
     
    Is it possible to blink a led using CFunctionCall block with the function __delay_us()?
    I just can't compile the model if I use such function:
     
    bool __delay_us(unsigned int t)
     
    I take the output of the block (boolean) and connect it to digital output.
     
    Thank you.
     
    Filippo
    #6
    Lubin
    Moderator
    • Total Posts : 359
    • Reward points : 5
    • Joined: 2007/03/31 07:38:15
    • Location: Bayonne, France
    • Status: offline
    Re: Trouble passing unsigned long out of "C Function Call" block - dsPic 33EP32MC202 2019/05/20 02:29:10 (permalink)
    0
    Hi Heath,
     
    For completeness, this example also shows how to pass or return structure from the C function call block.
    C structure input/output is a Simulink BUS equivalent.
     
    Hi Filippo,
     
    delay_us function is declared in the dsPIC base functions library.
    It might not be available for PIC32. Which chip are you targeting ?
     
    Such delay function should be avoided as much as possible as they are blocking functions. The CPU is just waiting for the delay to pass and can't handle anything else in the meantime except interrupts.

    It's better to use rate. You can use an offset for any task: a sample time of [0.01 0.002] is 100Hz rate shifted by 2ms from initial time 0. Both vector value [step-size offset] must be multiple of the base time step of the model.
     
    Lubin
    #7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5