View previous topic :: View next topic |
Author |
Message |
yoonees
Joined: 29 Jun 2009 Posts: 12
|
Need help in creating pulse counter |
Posted: Tue Jul 28, 2009 10:04 am |
|
|
Hi all,
I'm trying to create a pulse counter to count pulses for 15 seconds and the value will be displayed on an LCD. This value will be refreshed every 15 seconds. Earlier, I modified PCM programmer's debouncing code (http://www.ccsinfo.com/forum/viewtopic.php?t=19874) and it worked just fine. However, it is a non-multitasking code and therefore I am unable to run other functions other than the pulse counter code. Right now, I'm trying to write a similar pulse counter that is able to count pulses and at the same time run other functions as well. In the idle state, the input is low. When there is a pulse, the input will be high.
Here are my codes. However, they are not complete. Since the 15 seconds timer and the LCD are working fine I left out the codes. The codes can only seem to count a few pulses out of about 20 pulses. I'm using PIC18LF4550 with 20 MHz crystal oscillator. Compiler version: PCWHD 4.057.
Code: | #define HIGH_PERIOD_IN_MS 10
#define LOW_PERIOD_IN_MS 30
static unsigned char high_flag = 0;
void main ()
{
lcd_init();
lcd_putc("\f");
while(true)
{
getPulse();
otherFunction1();
otherFunction2();
}
}
void getPulse (void)
////////////////////////////////////////////////////////////////////////////
// Count pulses
// The function is to detect a high pulse followed by a low pulse
// before the counter is to increase by 1.
////////////////////////////////////////////////////////////////////////////
{
if(input(PIN_A1) == 0 && high_flag == 0) // No pulse detected
return;
if(input(PIN_A1) == 1 && high_flag == 1) // Current state same as previous state
return;
if(input(PIN_A1) == 1 && high_flag == 0) // High input detected
{
delay_ms(HIGH_PERIOD_IN_MS);
while(input(PIN_A1) != 1)
{
delay_ms(HIGH_PERIOD_IN_MS);
}
high_flag = 1; // A flag is created to show that input was high
return;
}
if(input(PIN_A1) == 0 && high_flag == 1) // Low input detected
{
delay_ms(LOW_PERIOD_IN_MS);
while (input(PIN_A1) != 0)
{
delay_ms(LOW_PERIOD_IN_MS);
}
pulse++;
high_flag = 0;
return;
}
} |
Thanks! |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 28, 2009 11:13 am |
|
|
Are you counting button pushes ? Here's a program that will
debounce pushbuttons, and record the keypresses in a buffer.
It's interrupt-driven, so it's suitable for multi-tasking:
http://www.ccsinfo.com/forum/viewtopic.php?t=39585&start=1
If you're not counting keypresses, then describe the input pulse
signal in detail. |
|
![](templates/subSilver/images/spacer.gif) |
yoonees
Joined: 29 Jun 2009 Posts: 12
|
|
Posted: Tue Jul 28, 2009 7:44 pm |
|
|
PCM programmer wrote: | Are you counting button pushes ? Here's a program that will
debounce pushbuttons, and record the keypresses in a buffer.
It's interrupt-driven, so it's suitable for multi-tasking:
http://www.ccsinfo.com/forum/viewtopic.php?t=39585&start=1
If you're not counting keypresses, then describe the input pulse
signal in detail. |
Hi PCM programmer,
I'm actually trying to count pulses in the form of square waves so it's very similar to counting button pushes. That is why when I used your non-multitasking codes, it worked.
When there is not pulse, the input signal line remains low. When there is a pulse, the input signal like goes up high for about 200 - 400 ms.
If it is possible, I would prefer not to involve interrupt in the counting process.
Thank you ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 28, 2009 7:54 pm |
|
|
How long does the pulse go low ? |
|
![](templates/subSilver/images/spacer.gif) |
yoonees
Joined: 29 Jun 2009 Posts: 12
|
|
Posted: Tue Jul 28, 2009 8:35 pm |
|
|
PCM programmer wrote: | How long does the pulse go low ? |
You mean the period between 2 high pulses? It's about 400 - 500 ms. |
|
![](templates/subSilver/images/spacer.gif) |
yoonees
Joined: 29 Jun 2009 Posts: 12
|
|
Posted: Tue Jul 28, 2009 10:14 pm |
|
|
Just to add, the reason why I used the delays in the codes is because sometimes there are unwanted short pulses (something like bouncing in button pushes). |
|
![](templates/subSilver/images/spacer.gif) |
|