PDA

View Full Version : Leading/Trailing Edge



DelBoy
- 21st December 2004, 10:40
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.

mister_e
- 21st December 2004, 12:09
depending what else your PIC will do, you can use COUNT or internal timers to count everything in background.

Bruce
- 21st December 2004, 17:42
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

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.

isaac
- 22nd March 2005, 16:14
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

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

Melanie
- 23rd March 2005, 07:09
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.

isaac
- 23rd March 2005, 12:29
I Never ever thought of that i am just going through the trend right now
Thank you every much

Isaac