Hot!monitoring code

Author
kurt
Junior Member
  • Total Posts : 110
  • Reward points : 0
  • Joined: 2017/05/07 13:18:17
  • Location: 0
  • Status: offline
2018/07/29 09:47:01 (permalink)
0

monitoring code

hello, 
 
in my microchip c code i have a calculation and the outcome of it is not exactly as i expect,  there are some issues in it. 
i am more a siemens plc programmer and in siemens there is a function that you can go online and monitor the code while it is executed.  so that you can see were the problem is.
 
is there also a possibility that i can monitor my microchip c code.   and if possible for instance from microsecond to microsecond
 
gr kurt  
#1

19 Replies Related Threads

    katela
    Super Member
    • Total Posts : 1012
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: monitoring code 2018/07/29 10:25:30 (permalink)
    +1 (1)
    Try to use the simulator.

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/user/StudentCompanionSA
    #2
    Jim Nickerson
    User 452
    • Total Posts : 5578
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: monitoring code 2018/07/29 10:39:08 (permalink)
    0
    I wonder which particular PIC you are intending to use ?
    #3
    NKurzman
    A Guy on the Net
    • Total Posts : 16668
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/07/29 11:10:30 (permalink)
    0
    Is there a question in your post?
    Your code does not work the way you would think it would. You added no code, no pic, no programming language , and posted about the ide.
    Can you see data from micro second to micro second? You have a lot of options. Starting with using a simulator or a debugger to single step.
    #4
    kurt
    Junior Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2017/05/07 13:18:17
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/07/30 10:34:41 (permalink)
    0
    hey hello,
     
    i use a pic18f2220. this simulator i googled it and found something on youtube.
    i'am going to try something with it 
     
     
    #5
    Jim Nickerson
    User 452
    • Total Posts : 5578
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: monitoring code 2018/07/30 10:42:18 (permalink)
    0
    kurt,
    Would you care to share the code for the calculation ?
    #6
    kurt
    Junior Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2017/05/07 13:18:17
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/07/30 10:50:15 (permalink)
    +1 (1)
    yes of course, 
     
    the entire code or just the piece i'am struggling with
    #7
    Jim Nickerson
    User 452
    • Total Posts : 5578
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: monitoring code 2018/07/30 10:51:22 (permalink)
    +1 (1)
    We could start with the piece you are struggling with.
     
    #8
    kurt
    Junior Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2017/05/07 13:18:17
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/07/30 11:14:42 (permalink)
    0
    first i need to explain something
    for a long time i am trying to make a tachometer (with help from this forum) on a fan  on the back site i put a ir led and on the front site a ir reciever,  , every time a blade of the fan passes. the ir light to the ir receiver is interrupted which results in a puls.  this part on the hardware site works good i have a  device to check this
     
    the code is constructed in a way that it counts the time between two pulses and in this way i use it to calculate the rpm.  everything in my code works except the capture part.  with time passing on i got many variants to capture the time between two pulses but the one underneath works the best.  but still at low rpm when the tmr1if is used 
    how do i say it:  'if i turn of the fan the displayed value doesn't drop fluently'
     
    look at it 
     
    ///////////////////////////////////
    if (PIR1bits.CCP1IF && step ==0 && PIR1bits.TMR1IF==0)
    {
    capture1 = CCPR1 ; /////
    PIR1bits.CCP1IF = 0;
    step=2;
    }

    else if (PIR1bits.TMR1IF==1 )
    {
    step = 1;
    timeoverflow = timeoverflow + 1;
    PIR1bits.TMR1IF = 0;
    }

    else if ( PIR1bits.CCP1IF && step ==1)
    {
    capture2 = timeoverflow * 65536 ;
    step = 3;
    timeoverflow = 0;

    }
    else if (step ==2) {cap_val = capture1; step = 0; WriteTimer1( 0x0000 ); }
    else if (step ==3) {cap_val = capture1 + capture2; step = 0; WriteTimer1( 0x0000 );}
    }
     
     
    rev= 60000000 / cap_val;
    #9
    Jim Nickerson
    User 452
    • Total Posts : 5578
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: monitoring code 2018/07/30 11:36:50 (permalink)
    +2 (2)
    Firstly, this forum displays code better if it is within "code tags", thusly

    ///////////////////////////////////
    if (PIR1bits.CCP1IF && step ==0 && PIR1bits.TMR1IF==0)
    {
    capture1 = CCPR1 ; /////
    PIR1bits.CCP1IF = 0;
    step=2;
    }

    else if (PIR1bits.TMR1IF==1 )
    {
    step = 1;
    timeoverflow = timeoverflow + 1;
    PIR1bits.TMR1IF = 0;
    }

    else if ( PIR1bits.CCP1IF && step ==1)
    {
    capture2 = timeoverflow * 65536 ;
    step = 3;
    timeoverflow = 0;

    }
    else if (step ==2) {cap_val = capture1; step = 0; WriteTimer1( 0x0000 ); }
    else if (step ==3) {cap_val = capture1 + capture2; step = 0; WriteTimer1( 0x0000 );}
    }

    Code tags are explained in this post https://www.microchip.com/forums/FindPost/777101
    #10
    Jim Nickerson
    User 452
    • Total Posts : 5578
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: monitoring code 2018/07/30 12:00:34 (permalink)
    +1 (1)
    IMHO you should run the project using the simulator and single step to watch what your logic is doing.
    edit: to ensure what you want is actually happening.
    #11
    Jim Nickerson
    User 452
    • Total Posts : 5578
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: monitoring code 2018/07/30 12:03:03 (permalink)
    +1 (1)
    It might help your debugging if your if statements did not test so many things at once.
    While debugging it is good to see eachcondition one at a time.
    #12
    NKurzman
    A Guy on the Net
    • Total Posts : 16668
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/07/30 15:00:28 (permalink)
    0
    capture2 = timeoverflow * 65536 ;
     
    You have a PIC18 ints are 16 bits. so if capture2 AND timeoverflow are not long ints in this case then you will not get the numbers you hope for.
    #13
    kurt
    Junior Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2017/05/07 13:18:17
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/07/31 13:16:17 (permalink)
    0
    Capture2 and timeoverflow are long ints,
     
    first i used TMR1 in my code,  but then someone told me that it's better to use CCPR1
     
    to monitor timer 1,  i added TMR1 to my 'watches list'  and i can monitor/see it increasing as i go from on line to the next
    but the ccpr1 doesnt increase as i go from one line to the next 
     
    how is this possible/  what is the difference between tmr1 and ccpr
    #14
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: monitoring code 2018/07/31 13:27:42 (permalink)
    0
    If the CCP is in capture mode, CCPR1 will only change when there is a capture event. You are not going to see it change "as you go from one line to the next".
    If a timer is in 16 bit mode, you need to be very careful having it in the WATCH list, as that means the debugger is actively reading the timer in parallel with your code, which can have unexpected effects.
     
    I think at this stage it would be better to post your entire code, we're not getting a good picture of how you are using the timer.

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #15
    kurt
    Junior Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2017/05/07 13:18:17
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/07/31 13:51:37 (permalink)
    0
    ]
    /*
    * Input Capture 1
    */
    #include <xc.h>
    #include "Capture1.h"
    #include "Timer1.h"
    void InitCapture1(void)
    {
    InitTimer1(); 
    #if defined _18F2220 //_18F45K20/
    TRISCbits.TRISC2 = 1; 
    T3CONbits.T3CCP1 = 0;
    CCP1CONbits.CCP1M = 4; 
    IPR1bits.CCP1IP = 1; 
    PIR1bits.CCP1IF = 0;
    PIE1bits.CCP1IE = 1; 
    #endif
    }

    volatile unsigned long int cap_val;
    volatile unsigned long int minus;
    volatile unsigned long int capture_time;  
    volatile unsigned long int step = 0 ;
    volatile unsigned long int timeoverflow = 0;
    volatile unsigned long int capture1 ;
    volatile unsigned long int capture2 ;
    volatile unsigned long int capture3 ;
    volatile unsigned long int capture4 ;
    volatile bit marker = 0;
    void interrupt InterruptHandlerHigh(void) // InterruptHandler
    {
    if (PIR1bits.CCP1IF) //variant 1 // this one won't go higher than +/- 740 hz orso
    {
    PIR1bits.CCP1IF = 0;
    cap_val = CCPR1 + capture_time;
    capture_time = 0 - CCPR1;
    }

    else if (PIR1bits.TMR1IF)
    {
    capture_time += 65536;
    PIR1bits.TMR1IF = 0;
    }
    }

    /*{ //variant 2 //this one won't work below 600 hz,
    if (PIR1bits.CCP1IF && step ==0 && PIR1bits.TMR1IF==0)
    { PIR1bits.CCP1IF = 0;
    step=2;
    capture1 = CCPR1 ; }

    else if (PIR1bits.TMR1IF==1 && capture1 <32000 )// this is what you say if : If CCPR1.bit15 is zero
    { step = 1; // i dont know how to write ccpr1.bit15 ==1;
    timeoverflow = timeoverflow + 1;
    PIR1bits.TMR1IF = 0; }

    else if (PIR1bits.TMR1IF==1 && capture1 >32000 ) // this is what you say if : If CCPR1.bit15 is zero
    { step = 2;
    PIR1bits.TMR1IF = 0; // proberen timeroverflow = 0; naar laatste regel toe
    timeoverflow = 0; } // nog een step er tussen maken voor als

    else if ( PIR1bits.CCP1IF && step ==1)
    { capture2 = timeoverflow * 65536 ; // proberen om deze in 2e if else statement
    step = 3; // toch proberen capture1 + capture2 buiten isr
    timeoverflow = 0; } // deze proberen naar onderste regel


    else if (step ==2) {cap_val = capture1; step = 0; WriteTimer1( 0x0000 );marker = 0; }
    else if (step ==3) {cap_val = capture1 + capture2; step = 0; WriteTimer1( 0x0000 );marker = 0;}
    }*/
    /*{ //variant 3 // this one works till low hz but jumps a lot as frequency is falling

    if (PIR1bits.CCP1IF && step ==0 && PIR1bits.TMR1IF==0)
    {
    capture1 = CCPR1 ; /////
    PIR1bits.CCP1IF = 0;
    step=2;
    }

    else if (PIR1bits.TMR1IF==1 )
    {
    step = 1;
    timeoverflow = timeoverflow + 1;
    PIR1bits.TMR1IF = 0;
    }

    else if ( PIR1bits.CCP1IF && step ==1) // ccp1if . flag moet verwijderd worden
    {
    capture2 = timeoverflow * 65536 ;
    step = 3;
    timeoverflow = 0;

    } // misschien moeten onderste twee zinnen er wel in
    else if (step ==2) {cap_val = capture1; step = 0; WriteTimer1( 0x0000 ); }
    else if (step ==3) {cap_val = capture1 + capture2; step = 0; WriteTimer1( 0x0000 );}
    }*/
    //////////
    [
    post edited by kurt - 2018/07/31 13:54:30
    #16
    kurt
    Junior Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2017/05/07 13:18:17
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/07/31 14:00:46 (permalink)
    +1 (1)
    i tried to use code tags from link 
     
    in my code i have 3 capture variants to capture time in us between to pulses from my pick up of my fan 
    all three are working with issues those i try to monitor in simulator,  here and there i made some notices for myself 
    to see what will happen if i change them and test them in real
    #17
    kurt
    Junior Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2017/05/07 13:18:17
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/08/09 10:51:06 (permalink)
    0
    I was looking through the tips that everyone gave and noticed this one
     
    "Better still, you should stop clearing the timer, and just subtract the current CCPR value from the previous CCPR value. That would be much more accurate than what you are doing now."
     
    regarding this piece of code:
    if(PIR1bits.CCP1IF){                  //Check If TMR1 Interrupt Flag Is Set
    cap_val = ReadTimer1();           //Get Timer1 Value For the Detected Rising Edge;
    WriteTimer1( 0x0000 );             //Clear Timer
    PIR1bits.CCP1IF = 0;                //Clear CCP1 Overflow Flag Bit
    }
    else if(PIR1bits.TMR1IF){           //Check If CCP1 Interrupt Flag Is Set
     
    Why is it that it's better not to clear the timer??
     
    #18
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: monitoring code 2018/08/09 15:36:45 (permalink)
    0
    The comment on the first line is wrong, it's the CCP interrupt not the timer interrupt.
    Calling ReadTimer1() instead of just reading CCPR means you are executing a number of instructions after the CCPR was triggered before you actually read the timer, and you have the complication of reading a value that is changing.
    Why go through all that faff, when the value you want at precisely the moment the trigger occured has already been saved into the CCPR register. that is precisely what it is designed for.
     
    Clearing the timer also means you are losing a number of clock counts before the next period starts.
    All that mucking around adds latency to your values. It's silly when the hardware is there to get a precise value.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #19
    kurt
    Junior Member
    • Total Posts : 110
    • Reward points : 0
    • Joined: 2017/05/07 13:18:17
    • Location: 0
    • Status: offline
    Re: monitoring code 2018/08/10 07:08:49 (permalink)
    0
    hey  qub,
     
    thanks,  you gave that tip, long time ago,   i made two variants   one who's clearing the timer   and starts counting
    from zero  each time,  and one,   in which the ccpr counts on and on and with calculating extracting the capture value.
     
    With my fan with infra red pick sensor first code is doing   3508  pulses/min (not rpm because fan has 3 blades)
    and with ccpr calculating code  i have 3494   pulses /min.    i think   given your explanation that is oke
     
     
    #20
    Jump to:
    © 2018 APG vNext Commercial Version 4.5