Fast Loop in PICBASIC Pro


Results 1 to 12 of 12

Threaded View

  1. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Why not using the internal PWM module instead? it will run in background and you don't need to care about latency etc etc.

    use something like this...
    Code:
    DEFINE OSC 20
    TRISC=0
    duty var word
    duty = 250                                  ' duty value for 50% duty cycle
    PR2 = 124                                   ' period for 40KHZ
    T2CON = %00000100                           ' timer2 on, prescale 1:1
    CCPR1L = duty>>2                            ' eight MSB of duty cycle value
    CCP1CON = (duty.lowbyte&3)<<5 | %00001100   ' store  2 LSB of duty 
                                                '  & set PWM mode
    
    START:
          PAUSE 1
          GOTO START
    and look what happen on PORTC.2 pin.

    Need to start it and stop it ? here's something using PORTB.0 as trigger source
    Code:
    DEFINE OSC 20
    TRISC=0
    duty        var word
    PWMMode     var byte
    StartStop   var bit
    
    duty = 250                                  ' duty value for 50% duty cycle
    PR2 = 124                                   ' period for 40KHZ
    T2CON = %00000100                           ' timer2 on, prescale 1:1
    CCPR1L = duty>>2                            ' eight MSB of duty cycle value
    PWMMode = (duty.lowbyte&3)<<5 | %00001100   ' store  2 LSB of duty 
                                                '    & set PWM mode
    
    START:
        IF PORTB.0=0 then            ' 
            startstop=startstop ^ 1  ' toggle between start/stop
            if startstop=1 then      '
                CCP1CON=PWMMODE      ' Start PWM
                ELSE                 '
                    CCP1CON=0        ' Stop PWM
                    PORTC.2=0        ' make sure the pin is low   
                ENDIF                '
            WHILE PORTB.0=0 : WEND   ' wait until trigger release
            PAUSE 100                ' debounce delay 
            ENDIF
        GOTO START
    How to calculate those value easy? PicMultiCalc software available bellow for FREE
    http://mister-e.org/pages/utilitiespag.html

    ScreenShot
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1280&stc=1&d=116639098 4">
    HTH
    Attached Images Attached Images  
    Last edited by mister_e; - 17th December 2006 at 22:33.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Controlsystem for compact sporting (clay shooting)
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th July 2009, 17:48
  2. Serial Relays
    By tazntex in forum General
    Replies: 3
    Last Post: - 17th May 2007, 18:42
  3. How to configure SPI in PICBASIC PRO?
    By moogle in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 15th April 2007, 19:31
  4. Question for all that use MELABS PICBASIC PRO
    By oskuro in forum Off Topic
    Replies: 2
    Last Post: - 24th March 2005, 18:15
  5. PicBasic Pro & PicBasic syntax different
    By Billyc in forum General
    Replies: 5
    Last Post: - 16th April 2004, 22:19

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