MPLAB SIM timing vs Real life


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2006
    Posts
    21

    Default MPLAB SIM timing vs Real life

    Hello again.

    Still using PIC16F876A...

    This bit of code works quiet well: ie The PWM appears as expected on both channels.

    DEFINE OSC 20 ' use 20mhz xtal.
    DEFINE HPWM1_TMR 2 'Use timer 2 for both channels
    DEFINE HPWM2_TMR 2
    DEFINE CCP1_REG PORTC ' Hpwm 1 pin port
    DEFINE CCP1_BIT 2 ' Hpwm 1 pin bit
    DEFINE CCP2_REG PORTC ' Hpwm 2 pin port
    DEFINE CCP2_BIT 1 ' Hpwm 2 pin bit
    CVRCON = 0 ' DIS-ABLE REFERENCE MODULE
    CMCON = 7 ' Disable analog comparator
    ADCON1 = 7 ' Set Port to digital
    TRISA = 255 ' SET PORTA TO INPUTS
    TRISB = 0 ' SET PORTB TO OUTPUTS
    TRISC = 0 ' SET PORTC TO OUTPUTS
    MAIN:
    goto R_FWD ' Got here by doing the right thing.

    R_FWD:
    IF STEERY > (THROTL*2) THEN ' PREVENTS DECREASING PWM FROM GOING BELOW 0
    STEERY = (THROTL*2)
    ENDIF
    HPWM 1, THROTL, 2000
    HPWM 2, THROTL-(STEERY/2), 2000
    PORTC.7 = 0 'LEFT_BACKWARD OFF
    PORTC.6 = 1 'LEFT_FORWARD ON
    PORTB.5 = 1 'RIGHT_FORWARD ON
    PORTB.4 = 0 'RIGHT_BACKWARD OFF
    PORTB.6 = PORTB.5 & PORTC.1 'RIGHT_PWM_FORWARD
    PORTB.7 = 0 'RIGHT_PWM_BACKWARD OFF
    PORTC.4 = PORTC.6 & PORTC.2 'LEFT_PWM_FORWARD
    PORTC.5 = 0 'LEFT_PWM_BACKWARD OFF
    RETURN

    However; when I run the simulator, I see the PWM on PORTC.1 and PORTC.2 as expected but the output on PORTC.4 and PORTB.6 is slower by an order of magnitude.
    Is this the simulator or some kind of aliasing, or is this as fast as the chip can run?
    I've changed the frequency from as low as it can go 1221 to as high as 5000 without much change in the simulated outputs. If I set it much faster than that then the PORTB.6 and PORTC.4 outputs stay on all the time. Again I repeat... the PWM outputs look like they should.
    I was hoping the simulator would let me avoid having to build a hardware prototype.
    Anyone have much experience with MPLAB SIM?
    Attached Images Attached Images  
    The less you expect, the more you get.

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Talking ... nothing new under the sun ...

    Hi,

    What I can tell you is the twin PWM, as you intend to use it, Works pretty well ...

    I have played with it for 10 years aboard my twin model boats. ( 18F876 used with PWM @ 4000 Hz ... and steering comp. included )


    What can't work properly is this section ...

    Code:
    PORTC.7 = 0 'LEFT_BACKWARD OFF
    PORTC.6 = 1 'LEFT_FORWARD ON
    PORTB.5 = 1 'RIGHT_FORWARD ON
    PORTB.4 = 0 'RIGHT_BACKWARD OFF
    PORTB.6 = PORTB.5 & PORTC.1 'RIGHT_PWM_FORWARD 
    PORTB.7 = 0 'RIGHT_PWM_BACKWARD OFF
    PORTC.4 = PORTC.6 & PORTC.2 'LEFT_PWM_FORWARD 
    PORTC.5 = 0 'LEFT_PWM_BACKWARD OFF
    so, use a different bridge wiring or a PIC dedicated to bridge driving ... ( 18F ???)


    Now, stop thinking simulators are the truth ... MPSIM is one of the "less worse" but also has its own limitations ...

    just good to waste your time and money ... no more !!!

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Also - you have goto R_FWD with a RETURN at the end of R_FWD.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Feb 2006
    Posts
    21


    Did you find this post helpful? Yes | No

    Default to reiterate, and code correction.

    Quote Originally Posted by Bronurstomp View Post
    Hello again.

    Still using PIC16F876A...

    This bit of code works quiet well: ie The PWM appears as expected on both channels.

    DEFINE OSC 20 ' use 20mhz xtal.
    DEFINE HPWM1_TMR 2 'Use timer 2 for both channels
    DEFINE HPWM2_TMR 2
    DEFINE CCP1_REG PORTC ' Hpwm 1 pin port
    DEFINE CCP1_BIT 2 ' Hpwm 1 pin bit
    DEFINE CCP2_REG PORTC ' Hpwm 2 pin port
    DEFINE CCP2_BIT 1 ' Hpwm 2 pin bit
    CVRCON = 0 ' DIS-ABLE REFERENCE MODULE
    CMCON = 7 ' Disable analog comparator
    ADCON1 = 7 ' Set Port to digital
    TRISA = 255 ' SET PORTA TO INPUTS
    TRISB = 0 ' SET PORTB TO OUTPUTS
    TRISC = 0 ' SET PORTC TO OUTPUTS
    MAIN:
    goto R_FWD ' Got here by doing the right thing.

    R_FWD:
    IF STEERY > (THROTL*2) THEN ' PREVENTS DECREASING PWM FROM GOING BELOW 0
    STEERY = (THROTL*2)
    ENDIF
    HPWM 1, THROTL, 2000
    HPWM 2, THROTL-(STEERY/2), 2000
    PORTC.7 = 0 'LEFT_BACKWARD OFF
    PORTC.6 = 1 'LEFT_FORWARD ON
    PORTB.5 = 1 'RIGHT_FORWARD ON
    PORTB.4 = 0 'RIGHT_BACKWARD OFF
    PORTB.6 = PORTB.5 & PORTC.1 'RIGHT_PWM_FORWARD
    PORTB.7 = 0 'RIGHT_PWM_BACKWARD OFF
    PORTC.4 = PORTC.6 & PORTC.2 'LEFT_PWM_FORWARD
    PORTC.5 = 0 'LEFT_PWM_BACKWARD OFF
    RETURN

    However; when I run the simulator, I see the PWM on PORTC.1 and PORTC.2 as expected but the output on PORTC.4 and PORTB.6 is slower by an order of magnitude.
    Is this the simulator or some kind of aliasing, or is this as fast as the chip can run?
    I've changed the frequency from as low as it can go 1221 to as high as 5000 without much change in the simulated outputs. If I set it much faster than that then the PORTB.6 and PORTC.4 outputs stay on all the time. Again I repeat... the PWM outputs look like they should.
    I was hoping the simulator would let me avoid having to build a hardware prototype.
    Anyone have much experience with MPLAB SIM?
    As Bruce pointed out the correct syntax is "gosub R_FWD". This is correct in my code.
    But neither reply answers my actuall question. Also, as a fall back position I intend to hard wire the PWM outputs to PORTA.2 and PORTA.3 and use them instead of PORTC.1 and PORTC.2 in my equations. This is due to the fact I need the PWM on 4 pins. I have the PIC16F876A chips in house, so changing chips is NOT an option.
    The difficulty there is telling the simulator that there's a wire external to the chip! I tried generating the identical PWM signals on those pins but Arggggg... I give up lol. I even tried
    PORTA.2 = PORTC.2
    PORTA.3 = PORTC.1
    That doesn't work either. Seems like at 1221 HZ PWM the chip could "keep up" with a 20MHZ Xtal ?!?

    By the way, is there any plan to allow display of US or MS as opposed to only CYC in the Logic Analyser output? Converting CYC to real time is a pain!

    Cheers!
    The less you expect, the more you get.

Similar Threads

  1. MPLab SIM question
    By presario1425 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 3rd December 2007, 21:22
  2. Linker/COFF debugging in MPLAB SIM
    By RichardBowser in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 14th January 2007, 18:28
  3. Using MPLAB SIM
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 7th July 2006, 04:20
  4. Tracing code in MPLAB Sim
    By ErnieM in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th August 2005, 16:00
  5. pause loop lockup with mplab sim
    By power67 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th February 2005, 13:10

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts