CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Need help in creating pulse counter

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
yoonees



Joined: 29 Jun 2009
Posts: 12

View user's profile Send private message

Need help in creating pulse counter
PostPosted: Tue Jul 28, 2009 10:04 am     Reply with quote

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!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 28, 2009 11:13 am     Reply with quote

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.
yoonees



Joined: 29 Jun 2009
Posts: 12

View user's profile Send private message

PostPosted: Tue Jul 28, 2009 7:44 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 28, 2009 7:54 pm     Reply with quote

How long does the pulse go low ?
yoonees



Joined: 29 Jun 2009
Posts: 12

View user's profile Send private message

PostPosted: Tue Jul 28, 2009 8:35 pm     Reply with quote

PCM programmer wrote:
How long does the pulse go low ?


You mean the period between 2 high pulses? It's about 400 - 500 ms.
yoonees



Joined: 29 Jun 2009
Posts: 12

View user's profile Send private message

PostPosted: Tue Jul 28, 2009 10:14 pm     Reply with quote

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).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group