Leading/Trailing Edge


Closed Thread
Results 1 to 6 of 6
  1. #1
    DelBoy's Avatar
    DelBoy Guest

    Default Leading/Trailing Edge

    This may appear to be a strange question, but how can i detect switching on the leading or trailing edge of a switch only. I want to be able to count the number of times a switch or contact is operated.

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


    Did you find this post helpful? Yes | No

    Default

    depending what else your PIC will do, you can use COUNT or internal timers to count everything in background.
    Steve

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

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


    Did you find this post helpful? Yes | No

    Default

    Here's one very simple solution. Use Timer 0 as a background counter. This will count transitions of high-to-low, or low-to-high on RA4/T0CKI
    Code:
    DEFINE OSC 20
    DEFINE LOADER_USED 1
    
    P_Count var byte       ' Holds count value
    TRISA.4 = 1            ' RA4/T0CKI = input
    INTCON.7 = 0           ' Disable interrupts
    
    ' Set 1:1 prescaler & assign to WDT
    ' Set TMR0 clock to external input on RA4/T0CKI
    ' Increment on high-to-low transition on RA4/T0CKI pin
    OPTION_REG = %00111000 
    
    Main:
        P_Count = 0  ' Clear pulse counter
        TMR0 = 0     ' Clear TMR0 counter
        
    Loops:
        P_Count = TMR0 ' Get TMR0 count
        HSEROUT ["Counts = ",dec P_Count,13,10]
        IF P_Count = 255 THEN Main  ' Clear when TMR0 reaches 255
        PAUSE 500
        GOTO Loops
    If OPTION_REG.4 = 1 Increment on high-to-low transition on T0CKI pin.

    If OPTION_REG.4 = 0 Increment on low-to-high transition on T0CKI pin.

    Super easy, and all counting is done in the background while your program code plods along. Just check whenever it's convenient to find the count.
    Regards,

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

  4. #4
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Question

    Quote Originally Posted by Bruce
    Here's one very simple solution. Use Timer 0 as a background counter. This will count transitions of high-to-low, or low-to-high on RA4/T0CKI
    Code:
    DEFINE OSC 20
    DEFINE LOADER_USED 1
    
    P_Count var byte       ' Holds count value
    TRISA.4 = 1            ' RA4/T0CKI = input
    INTCON.7 = 0           ' Disable interrupts
    
    ' Set 1:1 prescaler & assign to WDT
    ' Set TMR0 clock to external input on RA4/T0CKI
    ' Increment on high-to-low transition on RA4/T0CKI pin
    OPTION_REG = %00111000 
    
    Main:
        P_Count = 0  ' Clear pulse counter
        TMR0 = 0     ' Clear TMR0 counter
        
    Loops:
        P_Count = TMR0 ' Get TMR0 count
        HSEROUT ["Counts = ",dec P_Count,13,10]
        IF P_Count = 255 THEN Main  ' Clear when TMR0 reaches 255
        PAUSE 500
        GOTO Loops
    If OPTION_REG.4 = 1 Increment on high-to-low transition on T0CKI pin.

    If OPTION_REG.4 = 0 Increment on low-to-high transition on T0CKI pin.

    Super easy, and all counting is done in the background while your program code plods along. Just check whenever it's convenient to find the count.
    Would Timer still counts if the Pic is aspleep.
    i want to do something similar with aa 18f452 but the problem is that my pic needs to count even when the pic is asleep is this posible?

    Isaac

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    I am using an 18f452 and Timer1 (Capture ) is already being used for my measurement circuit so i decided to use Timer0 configured as a 16 bit counter.
    i am counting the 1Hz pulse coming out of my DS1307 RTC.
    But i need to be able to count those pulses even when the pic is asleep because it is those pulses that
    i am using as a service counter so that after a years time this can display a message that the unit needs calibration.
    I have just learn't how to use Timer 0 as a counter only to discover that it only counter when the Pic is awake but not when in sleep mode.
    Is there a way i can do this ?
    i do need some help on this one and any suggestions would be every much appreciated
    Why not Read the RTC at the start of your year, convert to Linear DAYS and add 365 (do a search on Julian to see the whole thread) and save this as a Reference. Then when awake just read the RTC, convert to Linear DAYS and if it exceeds your Reference then a year has elapsed. Using this method saves having to keep track of individual elapsed Years, Months, and Days of Month.

  6. #6
    Join Date
    May 2004
    Location
    brighton
    Posts
    149


    Did you find this post helpful? Yes | No

    Thumbs up

    I Never ever thought of that i am just going through the trend right now
    Thank you every much

    Isaac

Similar Threads

  1. mS Timer
    By whmeade10 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 8th September 2020, 13:12
  2. RC Servo decoding/encoding using 12F683
    By ScaleRobotics in forum Code Examples
    Replies: 13
    Last Post: - 14th September 2010, 01:49
  3. PIC driven transistor - smoothed signal edge - why?
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th June 2008, 09:58
  4. newbie Q - edge detection?
    By RMCRAVEN in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 9th October 2006, 09:20
  5. positive going edge trigger?
    By rossfree in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 11th January 2005, 18:41

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