Shop OBEX P1 Docs P2 Docs Learn Events
Event Counter in C Propeller P8X32A - New user needs help — Parallax Forums

Event Counter in C Propeller P8X32A - New user needs help

SparkoSparko Posts: 8
edited 2015-04-13 20:54 in Propeller 1
I would like to add an event counter to an existing C program I have written. Perhaps the simplest use of the Propeller counter but I am stuck on getting started.

I realize the counter modes need to be set first. Mode NEGEDGE %01110

The counter reading the pulse as it goes negative would be preferred.

The counter needs to know what input to look at.

Reading the tally, decrementing the tally and clearing the tally is necessary.


I found simpletools.h by Andy Lindsay. A great resource for the general idea but does not seem to cover this specific mode.

Any help with any of the above would be very much appreciated.

Thanks in advance

Sparko

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2015-04-09 09:37
    Read the application note on the counters (AN001 - https://www.parallax.com/downloads?title_1=an0). The examples are in Spin, but should be applicable to C as well. The input pin used is specified in the counter mode register. The negative edge mode is indeed the one you want. The phase register (PHSA/PHSB) is the one you want to use ... clear it to zero first. It'll get incremented by 1 for each negative edge. After whatever time delay you want, read it for the pulse count.
  • AribaAriba Posts: 2,690
    edited 2015-04-09 10:06
    I always set up a counter with: CtrX = Mode<<26 + pin
    Mode %01110 = hex 0E, so in your case a possible C snippet could look like:
       CTRA = (0x0E << 26) + PINNUMBER;
       FRQA = 1;
    
       PHSA = 0;
       //wait some time
       counts = PHSA;
    

    Andy

    Edit: you need to include <propeller.h> for that
  • edited 2015-04-09 10:59
    In case it helps to examine it in a running C program, there's also a positive edge counter example in the Learn forum's Mixed mode programming tutorial thread. The counter module is used to measure how execution speed (indicated by a toggled output pin) responds to code modifications. The output pin's positive transitions are counted by the counter module.
  • SparkoSparko Posts: 8
    edited 2015-04-11 23:10
    I now have a counter that works. I monitor PIN0 , that I have held high using a 100K resistor between Vin and Pin0. When I ground Pin0 it realizes a negative going edge and counts. The only trouble is that multiple counts always occur.

    Thanks, Andy, Ariba, Mike.

    Please give me advice on the best way to debounce input PIN0 in this kind of application.

    Thanks again

    Sparko
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2015-04-13 08:28
    A counter running in a second cog is pretty simple. The forum and OBEX have a number of examples. One simple method I've used to count and flag is to maintain a counter in a second cog, and then simply raise an unused pin to indicate a change, which the main program polls for. Read the count, clear, and move on. I'm sure others here have better approaches.

    A capacitor debouncer might also work for this. The circuit is simple, and you can either experiment with the component values, or arrive at it using standard RC tables.
  • Cluso99Cluso99 Posts: 18,069
    edited 2015-04-13 12:53
    Sparko,
    First, welcome to the forum.
    Next, it is better to post propeller questions in the propeller 1 forum. Many of us don't read the general forum as often because here is about other things no necessarily prop related.

    Now to your question. You need to give us more info about what you are trying to count and speed.
    It seems you are using a switch which are notoriously noisy, hence the need to denounce. If you need to count some form of mechanical switching, you wil either have to use a hardware denounce circuit, or use a software debounce and not use counters.
    So we need more info to help.
  • SparkoSparko Posts: 8
    edited 2015-04-13 20:54
    Cluso,

    I appreciate the information you provided. I brought the debounce help request over to propeller 1 as you suggested.

    I am not sure what info might be needed but here is what I see and know about the requirements for speed ect.

    The pulse goes from high to low (zero volts) and noise can be seen for about the first 1ms or less. After the 100ms the level returns to 3.5V and stays there for at least 50ms. No subsequent pulse can occur for at least 50ms.

    I tried a capacitor first but did not get good results. Could have been that the value or type of capacitor was not correct.

    I added a software loop that checks for a change in input level over a 50ms period and debounce was no longer a problem. However this ties up the uP for 50ms. There are other operations that must be ready to proceed and cannot be held up.

    Your suggestion to not use a counter may be the way to go. One cog accepting the input and performing the debounce outside and independent of the main program may be best.

    Thanks

    Sparko
Sign In or Register to comment.