Intro: I seem to be stuck on a relatively simple issue involving the timer/counter module. I know clearly what my objective is, but cant seem to wrap my head around the timer/counter module. Im not even sure how to get started so I cant even offer any example code. I have read through the datasheet for my pic, but I am completely confused about how to even go about solving my problem. Any insight would be most appreciated. By the way I am using the PIC12F683 if thats important.
My Objective: I have a source of intermittent pulses with an extremely low frequency, lets say less than 100Hz (even all the way down to 10-20Hz). All I want to do is count the number of pulses and store the count as a variable in the pic. I dont care about actual values of things like frequency, duty cycle, pulse length, etc. The only thing I care about is counting how many times the wire goes high. The actual number of pulses can be anywhere from 1-75ish in a row. After the set of pulses is finished I need the pic to wait indefinately for more and add those new pulses to the original count.
There is no need at this point to display the count in any way, I only wish to store the count as a variable in the pic for later use in the program. At a later time I will tackle the next step which is running a program while constantly monitoring for new incoming pulses and adding them to the count still, even while the program is running.
I imagine I will be using things like
count = count + 1
. Because my pulserate is so low I can think of snips of code to monitor an input pin with if-then statements and waits to determine if the pin changed state:
IF GPIO1 = 1 THEN COUNT += 1
then wait until GPIO1 = 0 before running the IF statement again.
Somehow I am led to believe that the counter/timer module will take care of all of that but Im not even sure how to set that up and use it correctly. If someone could point me in the right direction, maybe offer some example code for me to try, and explain more clearly how timer/counter would work in my application it would really help. Thanks again.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
My first post sounds a bit wordy and confusing. To put it simply, I need to do the following things:
1: Monitor a pin for pulses and count how many pulses there are.
2: Store the number of pulses in a variable called COUNT.
3: Run a program that can make use of the variable COUNT while doing simple things like driving LEDs and Servos, and doing simple math operations, and running IF-THEN statements.
4: The program needs to run with the continuously updated COUNT variable.
I am asking for help with the pulse count sub-routine I suppose. How do I continuously monitor a pin to count low frequency pulses while running my regular program? I hope this clarified post helps
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Use the ON Interrupt command to keep track of the pulses. You can try ExtINT0, or GPIOChange, to call a sub which increments the count. Make sure the interrupt pin is made an input. Verify by showing count on some leds like:
Sub CntPulses
count += 1
lownib = count
If lownib.0 On Then
set GPIO.0 On
Else
set GPIO.0 Off
end if
If lownib.1 On Then
set GPIO.1 On
Else
set GPIO.1 Off
end if
If lownib.2 On Then
set GPIO.4 On
Else
set GPIO.4 Off
end if
end sub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Intro: I seem to be stuck on a relatively simple issue involving the timer/counter module. I know clearly what my objective is, but cant seem to wrap my head around the timer/counter module. Im not even sure how to get started so I cant even offer any example code. I have read through the datasheet for my pic, but I am completely confused about how to even go about solving my problem. Any insight would be most appreciated. By the way I am using the PIC12F683 if thats important.
My Objective: I have a source of intermittent pulses with an extremely low frequency, lets say less than 100Hz (even all the way down to 10-20Hz). All I want to do is count the number of pulses and store the count as a variable in the pic. I dont care about actual values of things like frequency, duty cycle, pulse length, etc. The only thing I care about is counting how many times the wire goes high. The actual number of pulses can be anywhere from 1-75ish in a row. After the set of pulses is finished I need the pic to wait indefinately for more and add those new pulses to the original count.
There is no need at this point to display the count in any way, I only wish to store the count as a variable in the pic for later use in the program. At a later time I will tackle the next step which is running a program while constantly monitoring for new incoming pulses and adding them to the count still, even while the program is running.
I imagine I will be using things like
. Because my pulserate is so low I can think of snips of code to monitor an input pin with if-then statements and waits to determine if the pin changed state:
then wait until GPIO1 = 0 before running the IF statement again.
Somehow I am led to believe that the counter/timer module will take care of all of that but Im not even sure how to set that up and use it correctly. If someone could point me in the right direction, maybe offer some example code for me to try, and explain more clearly how timer/counter would work in my application it would really help. Thanks again.
My first post sounds a bit wordy and confusing. To put it simply, I need to do the following things:
1: Monitor a pin for pulses and count how many pulses there are.
2: Store the number of pulses in a variable called COUNT.
3: Run a program that can make use of the variable COUNT while doing simple things like driving LEDs and Servos, and doing simple math operations, and running IF-THEN statements.
4: The program needs to run with the continuously updated COUNT variable.
I am asking for help with the pulse count sub-routine I suppose. How do I continuously monitor a pin to count low frequency pulses while running my regular program? I hope this clarified post helps
Use the ON Interrupt command to keep track of the pulses. You can try ExtINT0, or GPIOChange, to call a sub which increments the count. Make sure the interrupt pin is made an input. Verify by showing count on some leds like: