Doubling the pusle count


Closed Thread
Results 1 to 40 of 44

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Well, for whomever will be using it in the end ...

    Here's version 3. Hopefully the last one.
    I think I covered all the bases.

    Added another input. (see schematic above) GPIO.1 that determines if the output is inverted or not. It has a weak Pull-up, so you can just leave it disconnected for use with the NPN transistor.

    Code:
    '****************************************************************
    '*  Name    : FREQx2.bas                                        *
    '*  Author  : Darrel Taylor                                     *
    '*  Date    : 3/2/2007                                          *
    '*  Version : 3.0                                               *
    '*  Notes   : Doubles the frequency of input pulses,            *
    '*          : and maintains the same DutyCycle as the Input.    *
    '*          : Range = .1hz to 500 hz  (0-40% max DutyCyle)      *
    '*          : Target = PIC12F629/675                            *
    '****************************************************************
    @  __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
    DEFINE  NO_CLRWDT 1
    
    Clear
    
    SpeedPin   VAR GPIO.2                       ; Input from speed sensor
    SpeedX2Pin VAR GPIO.4                       ; Output to Speedometer
    IdleState  VAR GPIO.0                       ; State of input when sensor
                                                ;    is not on a "Slot"
    Invert     VAR GPIO.1                       ; Inverts output if = 1
    
    OPTION_REG.7 = 0                            ; enable weak Pull-ups
    WPU = 3                                     ; weak Pull-up on GP0 and GP1      
    ;_____________________________________________________________________________
    
    LastState  VAR BIT                          ; Last state the Input changed to
    LoopCount  VAR byte                         ; Counts the loops of each pulse
    LoopCount1 VAR byte
    LoopCount2 VAR byte
    
    PulseWidth  VAR byte  BANK0                 ; Counts the loops of output pulse
    PulseWidth1 VAR byte  BANK0
    PulseWidth2 VAR byte  BANK0
    
    LastPulseWidth  VAR byte                    ; saved width of last pulse
    LastPulseWidth1 VAR byte                    ;    so it can duplicate it
    LastPulseWidth2 VAR byte
    
    CycleWidth  VAR byte  BANK0                 ; Counts the loops of output cycle
    CycleWidth1 VAR byte  BANK0
    CycleWidth2 VAR byte  BANK0
    
    OutPulseCount VAR BIT
    
    
    CMCON = 7
    'ANSEL = 0    ; for 12F675
    
    SpeedX2Pin = IdleState
    OUTPUT SpeedX2Pin
    
    Main:
        LoopCount = LoopCount + 1               ; 24-bit counter
        If LoopCount = 0 Then
            LoopCount1 = LoopCount1 + 1         
            if LoopCount1 = 0 then
                LoopCount2 = LoopCount2 + 1
            endif
        endif
        IF SpeedPin <> LastState then           ; If Input State Changed?
            LastState = SpeedPin
            
            if LastState = (IdleState ^ 1) then         ; Start of Input Pulse
                CycleWidth  = LoopCount
                CycleWidth1 = LoopCount1
                CycleWidth2 = LoopCount2
                ASM
                    bcf   STATUS,C              ; CycleWidth = LoopCount / 2
                    rrf   _CycleWidth2, F       ; Output Cycle is half of Input
                    rrf   _CycleWidth1, F
                    rrf   _CycleWidth, F
                ENDASM
                LoopCount  = 0                  ; reset LoopCount
                LoopCount1 = 0
                LoopCount2 = 0
            else                                ; End of Input Pulse
                PulseWidth  = LoopCount
                PulseWidth1 = LoopCount1
                PulseWidth2 = LoopCount2
                ASM
                    bcf   STATUS,C              ; PulseWidth = PulseWidth / 2
                    rrf   _PulseWidth2, F       ; Output Pulse is half of Input
                    rrf   _PulseWidth1, F
                    rrf   _PulseWidth, F
                ENDASM
                LastPulseWidth = PulseWidth
                LastPulseWidth1 = PulseWidth1
                LastPulseWidth2 = PulseWidth2
                SpeedX2Pin = ((IdleState ^ 1) ^ Invert) ; start First Output Pulse
                OutPulseCount = 0
            endif
        else
            if SpeedX2Pin = ((IdleState ^ 1) ^ Invert) then
                if PulseWidth = 0 then          ; countdown output time (24bit)
                    if PulseWidth1 = 0 then
                        if PulseWidth2 = 0 then
                            SpeedX2Pin = (IdleState ^ Invert) ; end of pulse, 
                        else                       ; wait for next transition
                            PulseWidth2 = PulseWidth2 - 1
                            PulseWidth1 = 255
                            PulseWidth  = 255
                        endif
                    else
                        PulseWidth1 = PulseWidth1 - 1
                        PulseWidth  = 255
                    endif
                else
                    PulseWidth = PulseWidth - 1
                endif
            else
              @ NOP                             ; Keep the loop symmetrical
              @ NOP
              @ NOP
              @ NOP
              @ NOP
              @ NOP
            endif
            
            if OutPulseCount = 0 then
                if CycleWidth = 0 then          ; countdown to Midpoint of cycle
                    if CycleWidth1 = 0 then
                        if CycleWidth2 = 0 then
                            SpeedX2Pin = ((IdleState ^ 1) ^ Invert) ; Start second pulse
                            OutPulseCount = 1
                            PulseWidth  = LastPulseWidth
                            PulseWidth1 = LastPulseWidth1
                            PulseWidth2 = LastPulseWidth2
                        else
                            CycleWidth2 = CycleWidth2 - 1
                            CycleWidth1 = 255
                            CycleWidth  = 255
                        endif
                    else
                        CycleWidth1 = CycleWidth1 - 1
                        CycleWidth  = 255
                    endif
                else
                    CycleWidth = CycleWidth - 1
                endif
            else
              @ NOP                             ; Keep the loop symmetrical
              @ NOP
              @ NOP
              @ NOP
              @ NOP
            endif
        endif
    goto Main
    end
    You come up with some fun projects Malcolm. Trains, Automobiles, All we need are planes, and we're set.
    <br>
    DT

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


    Did you find this post helpful? Yes | No

    Wink Joking ???

    Hi, Darrel

    You really want something about planes ....???

    I must have a project somewhere for you ... one minute please !

    Alain
    Last edited by Ioannis; - 20th January 2011 at 12:05. Reason: slash in [i]
    ************************************************** ***********************
    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
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    I've just received an e-mail from my mate, thought you guys might like to know that he passes on his thanks

    Malc...really looks like I've given you guys something to think about with this one. Thank them for their efforts will you and just for the record the car is a 1995 Vauxhall Corsa 1.2 injection...LS
    I've programmed the PIC with Darrels code and will build the circuit over the weekend as he doesn't have them to hand

    Thanks guys for your help, hopefully it will be positive news when he receives it and fits it to his wifes car

  4. #4
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    Darrel,

    I've made a small PCB based on your schematic above, and sent it to my mate with the car to try. The PIC was programmed with your version 3 code.

    I've received a mail from him asking the following
    Do you think I should leave the two "control" wires open for starters and only mess with them if it doesn't work right stright off???
    The two control wires he mentions are two wires for the idle and invert options. I suggested he leaves these un-connected first off and see if there is a change in the speedo reading, but wondered if you could clarify (in laymans terms for both me and Brian) how these options affect the processing of the signal from the sensor (or how it should affact the output)

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


    Did you find this post helpful? Yes | No

    Default

    As long as the internal pull-up are good enough to reject the possible incoming noise, it won't cause any problem... but i'm not a internal-pull-up oriented guy for a final product... but once again it's me.
    Steve

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

  6. #6
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    I thought about adding a couple of external pull up resistors, but didn't know ( I know, should of asked ) if taking them high would of caused problems.. Its not a problem as they can be added at the switch

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I think if there are antenna's (a.k.a. long wires) hooked up to the option pins, the internal pull-ups may not be good enough. Small jumpers or dip switches should be ok though. No problem adding external pull-ups if desired. Or you can tie them directly to VDD or VSS.

    With the IdleState pin left open, the program will assume the input idles HIGH, with negative going pulses, which is the opposite of the 0-12-0 polaraity described before. So I'd start with that pin grounded.

    The Invert input should be left open, without any wires, since that circuit uses the NPN transistor that inverts the output. It was just an option available for anyone else that might pick up the code from the forum. The output follows the same polarity as the input Idlestate, so there shouldn't be a need to change it with that circuit.

    When you programmed the chip, did you uncomment the ANSEL line?
    I was using a 12F629, but I think you're using a 675.
    <br>
    DT

Similar Threads

  1. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 05:52
  2. Can't get COUNT to count
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 24th March 2009, 00:14
  3. Count pulses between VARIABLE TIME
    By RodSTAR in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2007, 13:44
  4. Multiplex Display with count comand
    By mdaweb in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th October 2005, 05:09
  5. Count command
    By hawk72501 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 6th September 2005, 20:04

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