Shop OBEX P1 Docs P2 Docs Learn Events
Counters — Parallax Forums

Counters

danielstrittdanielstritt Posts: 43
edited 2013-10-24 23:30 in Propeller 1
Hello, I am having a huge time understanding counters and their purpose/use. I know they are going to be like C pointers, incredibly useful, yet makes no sense in the beginning. I read the heck out of the datasheet and manual for the prop, but I just don't "get it". I don't see why we should use them, or how we would if we wanted to. Can someone point me to any examples of the different modes and how they are used? Programming language doesn't matter, I know C/Spin/PASM. I know this is a great feature, I just need a jump start on learning about it

Thanks

Daniel

Comments

  • kwinnkwinn Posts: 8,697
    edited 2013-10-24 21:05
    Go to http://www.parallaxsemiconductor.com/an001 and download the PDF and examples.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-10-24 21:06
    Application note AN001 is the main document on how these are used. AN008 deals specifically with the use of the counters to make an Analog to Digital converter. Both include examples.
  • kitsunekitsune Posts: 22
    edited 2013-10-24 21:43
    Hello Daniel,

    you were looking for examples. Here is a quick and dirty program demonstrating the function of a HC-SR04 ultrasonic distance sensor. The program still uses waitcnt (could have used the counter for the waits as well), but also the counter POS mode to measure the length of the return pulse. If you are not familiar with this sensor: You start a measurement cycle by sending a 10 µs positive trigger pulse to one pin. The module returns a pulse, the duration of which is proportional (divide the number in µs by 58 to get the distance in cm) to the measured distance. Wait, repeat. That's all. Should you ever want to interface this module to a prop, don't forget series resistors on both data lines because the module runs on 5 V. In short: Set the counter to POS mode, send a 10 µs pulse, wait for the return pulse, wait for the return pulse to finish, read the pulse duration off PHSA.
    #include <propeller.h>
    #include <inttypes.h>
    
    int main(void) {
        // P0 input
        // P1 output
    
        OUTA&=~2;   // P1=0
        DIRA|=2;    // P1 output
        CTRA=(8<<26)+0; // POS mode, PINA=0, ignore PINB
        FRQA=1;     // max res
    
        while(1){
            OUTA|=2;    // P1=1
            _waitcnt(CNT+400); // activation pulse (10us)
            OUTA&=~2;   // end pulse
            PHSA=0;     // start value
            while (!(INA&1)); // wait for return pulse to start
            while (INA&1); // wait for pulse to end
            t_printf("dist: %d cm\r", PHSA/(58*80));
            _waitcnt(CNT+5600000);  // 70 ms
         }
        return 0;
    }
    

    You were asking for other uses of the counter modules. One among many others is the implementation of timeouts.


    Kind regards,

    Fred
  • JonnyMacJonnyMac Posts: 9,107
    edited 2013-10-24 23:30
    In this article I use both counters (different modes) to simplify measuring pulses in a Sony IR signal.

    -- http://classic.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp4.pdf

    The counter are very flexible. This is one example of many taking advantage of counters.
Sign In or Register to comment.