PBP projects for R/C models


Closed Thread
Results 1 to 40 of 772

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Pulse width measurement

    For pulse width measurement, I ran across an interesting page.

    http://www.mcmanis.com/chuck/robotic...e_measure.html

    I borrowed a little bit of the code, and then added it to some DT_Interrupts. I am just measuring a servo pulse width, and sending raw tmr1 result to the serial port here, but you can change it to make it do other things. This was done with a PIC18f2520 in a LAB-X2. But the above gentleman used a PIC16F628, so it should be pretty flexible if you chose the corresponding DT_INTS include file. I would like to see more examples of pulse width measurement as well as pulse generation using interrupts, so if you have some, please share!

    Code:
    DEFINE OSC 20
    DEFINE LOADER_USED 1         
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_SPBRG 42  ' 115200 Baud @ 20MHz, 0.94%
    SPBRGH = 0
    BAUDCON.3 = 1         ' Enable 16 bit baudrate generator
    
    LED0 VAR portb.0
    LED1 VAR portb.1
    LED2 VAR portb.2
    
    adcon1=15        ;sets all to digital
    TRISA=%00000000                         ' Set PORTA  
    TRISB=%01110000                         ' Set PortB
    TRISC=%10000100                         ' Set PortC bit.2 for input (ccp) and bit.7 for ser input
    
    INCLUDE "DT_INTS-18.bas"  ; Base Interrupt System
    INCLUDE "sub16.inc"       ; subtract 16 bit macro
                       
    LED0=0
    LED2=0
    
    risetime VAR WORD
    falltime VAR WORD
    falltime_l VAR falltime.Byte0
    falltime_h VAR falltime.Byte1
    risetime_l VAR risetime.Byte0
    risetime_h VAR risetime.Byte1
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR0_INT,   ToggleLED0,   ASM,  yes
            INT_Handler    CCP1_INT,   PulseWidth,   ASM,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    T0CON = %10000100
    T1CON = %00000001
    T2CON = %00111101
    CCP1CON = %00000101
    @   INT_ENABLE  TMR0_INT      ; Enable Timer 0 Interrupts 
    @   INT_ENABLE  CCP1_INT      ; Enable Capture Compare for pulse width measurement
    
    Main:
        PAUSE 1000
        HSEROUT [DEC falltime,10]
    GOTO Main
    
    ASM
    ToggleLED0
        btg  _LED0        ;blinky light
        INT_RETURN
    ENDASM
    
    
    ASM
    PulseWidth
        BTFSS   CCP1CON, CCP1M0 ; Check for falling edge watch
        GOTO    FALL_EDGE       ; Go pick up the falling edge
        MOVF    CCPR1L,W        ; else store leading edge value
        MOVWF   _risetime_l         ; into 16 bit word risetime
        MOVF    CCPR1H,W
        MOVWF   _risetime_h
        BCF     CCP1CON, CCP1M0 ; Now capture the trailing edge
        GOTO    ISR_2           ; Exit the interrupt service routine
            
    FALL_EDGE:
        BSF     CCP1CON, CCP1M0 ; Re-set for trailing edge capture
        MOVF    CCPR1L,W        ; Store the captured value into
        MOVWF   _falltime_l         ; falltime_l and ...
        MOVF    CCPR1H,W
        MOVWF   _falltime_h       ;             ... falltime_h
        ;
        ; 16 bit subtract 
        ;     falltime = falltime - risetime
        ;
        SUB16   _falltime, _risetime          ;this subroutine performs 16 bit subtraction
    ISR_2
        INT_RETURN
    ENDASM
    Attached Files Attached Files
    Last edited by ScaleRobotics; - 10th January 2010 at 09:21.

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


    Did you find this post helpful? Yes | No

    Default I'm in trouble..

    You all posted code specifically for the 12F675. My PIC is a 16F887.

    I am at a loss to translate from one PIC to the other. My PBP compiler (set up for 16F887) has no idea what to do with GPIO.x references. When I set it for 12F675 it is happy, but the resulting code is useless to my PICkit2.

    Is it my job to read about the 12F675 and try to figure out which register in my 16F887 is equivalent? That would be an excellent homework assignment.

    Ken

  3. #3
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kenjones1935 View Post
    Is it my job to read about the 12F675 and try to figure out which register in my 16F887 is equivalent? That would be an excellent homework assignment.
    Well, yes... but it will take a whole lot longer to open the file than it will to hit ctrl-f and search for gpio.

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


    Did you find this post helpful? Yes | No

    Default DT_INTS-xx.BAS ?

    I do not have all the necessary includes. Judging from the code snip offered by scalerobotics I need the base interrupt system for the 16 bit machines.

    "INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
    INCLUDE "sub16.inc" ; subtract 16 bit macro"

    I have a PICkit 2 with a 16F887.
    Does a DT_INTS-16.BAS exist? Where?

    ken

  5. #5
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kenjones1935 View Post
    I do not have all the necessary includes. Judging from the code snip offered by scalerobotics I need the base interrupt system for the 16 bit machines.

    "INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
    INCLUDE "sub16.inc" ; subtract 16 bit macro"

    I have a PICkit 2 with a 16F887.
    Does a DT_INTS-16.BAS exist? Where?

    ken
    Hello Ken,

    The DT_INTS-14.bas covers the PIC16 devices. The newest version 1.00 can be found here (at botom left). http://darreltaylor.com/DT_INTS-14/intro2.html The code should be able to be modified for your chip, but there are PIC chips with more timers, and more ccp pins out there.

    It might be best to try out some of the ready made samples that Darrel gives, just to get the hang of using his interrupts, and making sure your includes, and configs are working.


    I am working on a DT_INTS based RC servo passthrough (servo pulse measurement and pulse generation, but it is a little jittery right now, so I have try to trouble shoot that.
    Last edited by ScaleRobotics; - 17th January 2010 at 02:22.

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


    Did you find this post helpful? Yes | No

    Default Maybe I should change my design

    Mr or Ms scalerobotics,

    My original idea for an RC car that could toggle between RC control and autonomous control was to have DPDT relays switching between the RC receiver PWM outputs and the PIC PWM outputs. Please see:

    http://www.ziplink.net/users/kjones/...color_copy.jpg

    I discounted the idea of routing the RC commands through the PIC as that route might be slow and/or noisy. It sounds like that is exactly what you are designing. Please keep me in touch with your progress.
    I think that design would force me to choose another PIC as it would require three inputs from the radio receiver and two outputs to the electronic speed control and the steering servo. -- all interrupt driven I would think.

    I am sort of stuck with the PICkit 2 since I have purchased three of them. They are also not toooo big. If I could get more confidence prototyping I might consider building my own PIC carrier, but just getting the prototyping bits, parts and pieces entails an hour's drive to a store with which I am not familiar.

    Ken

  7. #7
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Hey Ken, OK, now I understand your project, and the use of the PicKit's!

    Well, I was able to correct my servo jitter, an error on my part of course.

    Here is some code for a single servo channel pass through, using DT_INTS. Next I will try adding the other CCP pin.



    Code:
    define OSC 20
    DEFINE LOADER_USED 1         
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_SPBRG 42  ' 115200 Baud @ 20MHz, 0.94%
    SPBRGH = 0
    BAUDCON.3 = 1         ' Enable 16 bit baudrate generator
    
    LED0 var portb.0
    LED1 var portb.1
    LED2 var portb.2
    halftime_bit var bit
    adcon1=15               ;sets all to digital
    TRISA=%00000000         ' Set PORTA  
    TRISB=%01110000         ' Set PortB switch inputs (not used)
    TRISC=%10000100         ' Set PortC.2 to input for ccp1
                            ' set portC.7 to input for serial port
    
    INCLUDE "DT_INTS-18.bas"  ; Base Interrupt System
    include "sub16.inc"       ; subtract 16 bit macro
    
    LED1=1                    ; Set to Output Low
    LED0=0
    led2=1
    portb.3 = 0
    servo_out var portb.3
    
    risetime var word      ;used for pulse width measure start of pw
    falltime var word      ;time at end of pulse width
    timer0 var word        ;used to load tmr0l and h bytes
    falltime_l var falltime.byte0
    falltime_h var falltime.byte1
    risetime_l var risetime.byte0
    risetime_h var risetime.byte1
    
    
    pause 1000
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            ;INT_Handler   TMR1_INT,   ToggleLED1,   ASM,  yes
            INT_Handler   TMR0_INT,     _PWMout,      ASM,  yes
            INT_Handler   CCP1_INT,      PWMeasure,   ASM,  yes
            INT_Handler   TMR2_INT,      _twentyMs,   ASM,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    T0CON = %00000001
    T1CON = %00000001
    T2CON = %01011111
    CCP1CON = %00000101
    ;@   INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts 
    @   INT_ENABLE  TMR0_INT 
    @   INT_ENABLE  CCP1_INT
    @   INT_ENABLE  TMR2_INT
    
    Main:
        NOP
        
    GOTO Main
    
    twentyMs:  ;really 10ms, but only acted on half the time
        toggle halftime_bit 
        if halftime_bit  = 1 then ;only perform this every other time this is called  
            timer0 = falltime/4   ;measured pulse length, send to 
            ;hserout [dec timer0,",",dec falltime,10,13]
            tmr0h = 255 - timer0.byte1
            tmr0l = 255 - timer0.byte0
            T0CON.7 = 1    ;start t0
            servo_out = 1     ;set servopin high        
        endif   
        @ INT_RETURN
        
    PWMout:
        T0CON.7 = 0 
        servo_out = 0         ;at completion of timer set servo pin low
        @ NOP
        @ INT_RETURN
    
    asm
    PWMeasure
    
        BTFSS   CCP1CON, CCP1M0 ; Check for falling edge watch
        GOTO    FALL_EDGE       ; Go pick up the falling edge
        MOVF    CCPR1L,W        ; else store leading edge value
        MOVWF   _risetime_l         ; into 16 bit word risetime
        MOVF    CCPR1H,W
        MOVWF   _risetime_h
        BCF     CCP1CON, CCP1M0 ; Now capture the trailing edge
        GOTO    ISR_2           ; Exit the interrupt service routine
            
    FALL_EDGE:
        BSF     CCP1CON, CCP1M0 ; Re-set for trailing edge capture
        MOVF    CCPR1L,W        ; Store the captured value into
        MOVWF   _falltime_l         ; falltime_l and ...
        MOVF    CCPR1H,W
        MOVWF   _falltime_h       ;             ... falltime_h
        ;
        ; 16 bit subtract 
        ;     falltime = falltime - risetime
        ;
        SUB16   _falltime, _risetime
    ISR_2
        INT_RETURN
    
    endasm
    Walter

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


    Did you find this post helpful? Yes | No

    Default I am stuck..

    Scalerobotics,

    The code you sent me has some incompatibilities with my system. I can fix some. Others....

    You include DT_INTS-18.bas. I have DT_INTS-14.bas a la Derral Taylor. I changed the code.

    You use "BAUDCON.3 = 1 ' Enable 16 bit baudrate generator". My compiler kicks that out. I do not know how to find out whether I have the equivalent. I do not even know how to start.

    You use T0CON.7. My system has no such thing. Again I have no idea how to find out what is my equivalent.

    Nuts! 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