PBP projects for R/C models


Closed Thread
Results 1 to 40 of 772

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Hi Ken,

    I always get confused with this part of things too, so I will just point you to a couple of threads that I refer to when the need arises.

    I think maybe the parts about
    TMR1L
    TMR1H
    are the things you need to look at???

    http://www.picbasic.co.uk/forum/showthread.php?t=961
    http://list.picbasic.com/forum/messa...tml?1049090059
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default

    You could simplify this a great deal if your PIC has Timer1 available, and Compare,
    Capture, PWM .. by just using the Compare peripheral.

    Here's an example:
    Code:
        DEFINE OSC 4
    
        Match VAR WORD
        
        Match   = 20000         ' 20,000 * 1uS = 20mS until compare match
        CCPR1H  = Match.HighByte' Load compare register high
        CCPR1L  = Match.LowByte ' Load compare register low
        CCP1CON = %00001011     ' Compare mode, auto reset Timer1
        
        '/ TRIS & Port Setup
        PORTB.0=1
        TRISB = $FE  ' PORTB.0 output, rest inputs
        
        '/ disable interrupts
        INTCON.7 = 0 ' Disable global ints
        INTCON.6 = 0 ' Disable peripheral ints
        PIE1.0   = 0 ' Disable Timer1 int
        PIE1.2   = 0 ' Disable CCP1 int
    
        T1CON = %00000000 ' Timer1 off, internal clock, 1:1 prescale
        TMR1H = 0         ' Clear Timer1 high
        TMR1L = 0         ' Clear Timer1 low
        
        T1CON.0 = 1  ' Turn on Timer1
    
    LOOPS: ' NOTE: PIR1.2 is the CCP1IF compare interrupt flag bit.
           ' this bit is set when Timer1 reaches the value in
           ' CCPR1L & CCPR1H
        IF PIR1.2 = 1 THEN   ' If CCP1 to TMR1 compare match occured
           PORTB = PORTB ^ 1 ' Toggle PORTB.0
           PIR1.2 = 0        ' Clear compare interrupt flag bit
        ENDIF
        GOTO LOOPS
    
        END
    Just 1 single interrupt could take care of your 20mS timing VS a bunch to accumulate the
    total 20mS time period.

    A single interrupt every 20mS & no reloading anything. Timer1 is automatically cleared on
    match, and the CCPR1L/CCPR1H registers contain the value loaded into them until you
    change it.
    Regards,

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

  3. #3
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    For 20mS timing, you can do it with a single interrupt with any of the 3 timers on the PIC16F887.

    Timer0 - Set prescaler to 1:256
    Timer1 - As shown above by Bruce
    Timer2 - Set prescaler to 1:16, set postscaler to 1:16

  4. #4
    Join Date
    Nov 2009
    Location
    Fitchburg, Mass
    Posts
    483


    Did you find this post helpful? Yes | No

    Default I think I am beginning to understand.

    It seems to me that the most important feature that must be continuously timed in my RC car in autonomous control is the 50 pulses per second Pulse Width Modulated simulation of the RC Receiver to Electronic Speed Control signal. Doing this job in background with one Interrupt sounds correct.

    The oscillator seen by TMR0 with pre-scaler runs at 1000000Hz

    The pre-scaler 1:256 cuts that down to 3906.25. ie 4kHz.

    A post-scaler of 1:64 cuts this down to 61.035Hz. Close enough is my guess. I don't have a grip on how to code this yet. Do I at least understand the concept?

    -----------------------another way---------------

    Bruce suggests counting up to 20,000 in bits of 1 microsecond. That should be easy enough with a 16 bit register built from TMR1H and TMR1L.

    ------------now how do I get only one PWM pulse?----------------

    Page 135 of the PBP manual seems to indicate that the command:

    PWM Pin,Duty,Cycle

    will produce "Cycle" number of pulses. It says, "This PWM cycle is repeated "Cycle" times." It also says that, "If a 4mHz oscillator is used, each "Cycle" is about 5ms long."

    This seems to mean that the following command:

    PWM 1, 75, 1

    would produce one pulse about 1.5msec long. This is the neutral pulse for a ESC.

    If this is true then writing a routines to control the drive wheels and the steering servo will not be difficult at all.

    What do you think?

    Ken

  5. #5
    Join Date
    Nov 2009
    Location
    Fitchburg, Mass
    Posts
    483


    Did you find this post helpful? Yes | No

    Default Bruce's counter works

    I can count up to 60,000 micro seconds using Bruce's mechanism. This is long enough to make the LED on PORTD.0 blink visibly!

    Next step is to add the PWM command and connect the output to my RC car's ESC box.

    Thanks again, All

    Ken

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


    Did you find this post helpful? Yes | No

    Default

    This is pretty crude, but see if it does what it should when changing to Forward, Back, etc
    with your ESC.
    Code:
        DEFINE OSC 4
    
        Match VAR WORD
        
        Forward CON 1000     ' ~1mS pulse
        Neutral CON 1500     ' ~1.5mS pulse
        Back    CON 2000     ' ~2mS pulse
        
        Match   = 20000         ' 20,000 * 1uS = 20mS until compare match
        CCPR1H  = Match.HighByte' Load compare register high
        CCPR1L  = Match.LowByte ' Load compare register low
        CCP1CON = %00001011     ' Compare mode, auto reset Timer1
        
        '/ TRIS & Port Setup
        PORTB.0=1
        TRISB = $FE  ' PORTB.0 output, rest inputs
        
        '/ disable interrupts
        INTCON.7 = 0 ' Disable global ints
        INTCON.6 = 0 ' Disable peripheral ints
        PIE1.0   = 0 ' Disable Timer1 int
        PIE1.2   = 0 ' Disable CCP1 int
    
        T1CON = %00000000 ' Timer1 off, internal clock, 1:1 prescale
        TMR1H = 0         ' Clear Timer1 high
        TMR1L = 0         ' Clear Timer1 low
        
        T1CON.0 = 1  ' Turn on Timer1
    
    LOOPS: ' NOTE: PIR1.2 is the CCP1IF compare interrupt flag bit.
           ' this bit is set when Timer1 reaches the value in
           ' CCPR1L & CCPR1H
        IF PIR1.2 = 1 THEN   ' If CCP1 to TMR1 compare match occured
           HIGH 0
           PAUSEUS Forward   ' change this to Neutral, Back, Forward to see effect
           LOW 0
           PIR1.2 = 0        ' Clear compare interrupt flag bit
        ENDIF
        GOTO LOOPS
    
        END
    Regards,

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

  7. #7
    Join Date
    Nov 2009
    Location
    Fitchburg, Mass
    Posts
    483


    Did you find this post helpful? Yes | No

    Smile Bruce, SUCCESS

    Not only does your code work (with a couple of adjustments specific to my PICkit 2 setup) but it also makes my car go not-so-fast, faster, and fastest.
    It also successfully drives the steering servo left, center and right. This validates the rumors that I was getting from the RC people about the shape and timing of their PWM signals.

    GREAT!! Now on to bigger and better stuff like light sensing and proximity sensing. First I need to write some car control routines

    FORWARD speed,duration
    BACKWARD speed, duration
    STEER_RIGHT ?
    STEER_LEFT ?



    Ken

Similar Threads

  1. PBP Book
    By Bruce in forum Off Topic
    Replies: 83
    Last Post: - 4th October 2021, 12:55
  2. PBP Extensions, What are they?
    By PJALM in forum PBP Extensions
    Replies: 9
    Last Post: - 28th September 2021, 11:26
  3. Compiler differences between PBP 2.33 & 2.46
    By nikopolis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd May 2006, 19:01
  4. Newby- PBP wont compile for 18F (MPLAB)
    By jd76duke in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th December 2005, 23:30
  5. Making PBP code more modular
    By forgie in forum General
    Replies: 30
    Last Post: - 25th October 2005, 16:24

Members who have read this thread : 0

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