PDA

View Full Version : MPLAB SIM timing vs Real life



Bronurstomp
- 15th July 2008, 21:25
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?

Acetronics2
- 16th July 2008, 09:30
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 ...




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

Bruce
- 16th July 2008, 13:59
Also - you have goto R_FWD with a RETURN at the end of R_FWD.

Bronurstomp
- 17th July 2008, 16:32
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!