Shop OBEX P1 Docs P2 Docs Learn Events
A version in C? — Parallax Forums

A version in C?

Jack3Jack3 Posts: 55
edited 2014-02-17 17:19 in Propeller 1
I need to read a square wave (from an RC receiver) and check for changes in state to 3 possible ranges and act accordingly.

Is there a function that does this already that I can include or modify for my needs that reads the pulse width? Simpliciy, this project is very simple.

I had asked this in a different way in the Propeller 1 threads,(http://forums.parallax.com/showthread.php/153533-Unsure-how-to-approach) but I am also looking for an answer in C, my education hasn't found the function yet.

Thanks

Comments

  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-02-01 11:46
    Do you have the SimpleTools library? It includes a function called pulseIn(). It uses a CTRx -- just like I showed you in Spin; though I dare say the Spin code is far easier to figure out.
    /**
     * @file pulseIn.c
     *
     * @author Andy Lindsay
     *
     * @version dev001
     *
     * @copyright Copyright (C) Parallax, Inc. 2012.  See end of file for
     * terms of use (MIT License).
     *
     * @brief pulseIn function source, see simpletools.h for documentation.
     *
     * @detail Please submit bug reports, suggestions, and improvements to
     * this code to editor@parallax.com.
     */
    
    #include "simpletools.h"                      // simpletools function prototypes
    
    long pulse_in(int pin, int state)              // pulseIn function definition
    {
      if(iodt == 0)
      {
        set_io_dt(CLKFREQ/1000000);
        set_io_timeout(CLKFREQ/4);
      }
      long tPulse;
      int ctr = ((8 + ((!state & 1) * 4)) << 26) + pin;
      input(pin);
      long tf = t_timeout;
      long t = CNT;
      while((get_state(pin) == state) && (CNT - t < tf));
      if(CTRA == 0)
      {
        CTRA = ctr;
        FRQA = 1;
        PHSA = 0;
        while((PHSA == 0) && (CNT - t < tf));
        while((get_state(pin) == state) && (CNT - t < tf));
        CTRA = 0;
        tPulse = PHSA/iodt;
      }
      else if(CTRB == 0)
      {
        CTRB = ctr;
        FRQB = 1;
        PHSB = 0;
        while((PHSB == 0) && (CNT - t < tf));
        while((get_state(pin) == state) && (CNT - t < tf));
        CTRB = 0;
        tPulse = PHSB/iodt;
      }
      else
      {
        tPulse = -1;
      }
      return tPulse;
    }
    


    The advantage, I suppose, is that the C version will a counter that is not busy doing something else. Still, I would hardly call this "simple" code....
  • Jack3Jack3 Posts: 55
    edited 2014-02-01 16:38
    I see what you mean.
  • SRLMSRLM Posts: 5,045
    edited 2014-02-01 16:54
    Hi Jack3,

    libpropeller has a pulse width reader module in C++ if you're interested: https://github.com/libpropeller/libpropeller/tree/master/libpropeller/pulsewidthreader
  • Jack3Jack3 Posts: 55
    edited 2014-02-16 14:57
    I started to try to experiment with the pulse width reader and get a failure at the class in the header file.

    I am assuming I have no idea what I am doing. So far, I got that just by adding a #include of the header file.....as below...
    #include "simpletools.h" 
    #include "pulse_width_reader.h" 
    
     int main(void)
     {
       return 0;
     }
     
    
    
    Project Directory: C:/Users/Dad/Documents/SimpleIDE/LEDProj/LEDProj/
     propeller-elf-gcc.exe -v GCC 4.6.1 (propellergcc_v1_0_0_2162)
     propeller-elf-gcc.exe -I . -L . -I ../../Learn/libpropeller -L ../../Learn/libpropeller/cmm/ -I C:/Users/Dad/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools -L C:/Users/Dad/Documents/SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/cmm/ -I C:/Users/Dad/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext -L C:/Users/Dad/Documents/SimpleIDE/Learn/Simple Libraries/Text Devices/libsimpletext/cmm/ -I C:/Users/Dad/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c -L C:/Users/Dad/Documents/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c/cmm/ -o cmm/LEDProj.elf -Os -mcmm -Wall -m32bit-doubles -fno-exceptions -std=c99 LEDProj.c -lsimpletools -lsimpletext -lsimplei2c -lsimpletools -lsimpletext -lsimpletools
     In file included from LEDProj.c:9:0:
     ../../Learn/libpropeller/pulse_width_reader.h:45:1: error: unknown type name 'class'
     ../../Learn/libpropeller/pulse_width_reader.h:45:24: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
     
    
    

    and it brings up the pulse_width_reader.h file with this first line highlighted.
    class PulseWidthReader  //this line is highlighted
        { public:
     
    
         PulseWidthReader() {
             Cog = 0;
         }
    
    

    This is using the Simple IDE to compile. Trying to back track from the compiler not being able to find the header file and was just putting in a line and doing a compile to look for the next error, got this far on the way back starting over from scratch.

    Somehow I have a feeling I made no sense at all, so please ask for clarification.
  • jazzedjazzed Posts: 11,803
    edited 2014-02-16 15:17
    SimpleIDE requires a C++ project to use C++ code. To use C++, set the Project Options -> Compiler Type to C++. You can also start a C++ project with Project -> New, then "open" a new file like "myfile.cpp" in the dialog.

    I haven't heard of SRLM using libpropeller with SimpleIDE yet. I'd be happy to see it work, but I'm not sure if the combo is supported at this time - I haven't tested it. One thing is that libpropeller should be in a folder like Learn/Simple Libraries/libpropeller ... this is shown in GCC compiler properties tab.
  • Jack3Jack3 Posts: 55
    edited 2014-02-16 16:30
    Okay, I did see some notes from you about "todo" in the header file. Will try your suggestions. I sure want to try out the pulse width once I learn how to address and use it. Thanks for your help.


    Udate....Your help did get me past the error I was getting, now time to try it with some code.
  • SRLMSRLM Posts: 5,045
    edited 2014-02-16 17:41
    Hi Jack3: I'm glad you're interested in libpropeller. Unfortunately, to the best of my understanding SimpleIDE does not support .S assembly files. The .S file is critical as it's the cog assembly driver for the pulse width reader. So you won't be able to use SimpleIDE. You could move the code to be inline assembly (or a precompiled blob), but those aren't too easy.

    Normally what I do is use a makefile, with the compile command set to:
    propeller-elf-g++ {$OPTIONS} *.cpp *.S
    

    In any case, the interesting bits are in the assembly driver. If this is purely a pedagogical exercise then you can browse that to see how the inputs are read.

    @Jazzed

    "Can libpropeller be used with SimpleIDE"? For objects that don't use an external .S file then yes. The easiest way is to just drop the .h files in the same folder as your source, and do the standard #include. You can also download the entire libpropeller folder to your computer and use the SimpleIDE "Add Include Path" to tell the compiler where to fulfill the #includes.

    BTW, is there any plan to support .S files in SimpleIDE? I couldn't find anything on that except some old threads.

    Also, I haven't forgotten about your request for a #include .cpp test. Just busy.
  • Jack3Jack3 Posts: 55
    edited 2014-02-16 18:02
    Okay, so far, so good using all sorts of functions out of it, but didn't get to the pulse reader part yet. How disappointing. guess I will have to find some other square wave reader or as a friend suggested do it myself, he seems to think I have the ability, not so sure I do, but I must admit I have been figuring out how to use multiple functions from propeller.h. So I am happy regardless.

    I read the S file, I know nothing about assembly, it was greek ...mostly ... to me

    I find C so much easier to grasp than Spin. Why I have no idea it is much pickier, I think
  • jazzedjazzed Posts: 11,803
    edited 2014-02-16 19:06
    Jack3 wrote: »
    Okay, so far, so good using all sorts of functions out of it, but didn't get to the pulse reader part yet.

    There is a pulse reader in the "simpletools.h" header and library. Look for pulse_in() here.

    SRLM wrote: »
    "Can libpropeller be used with SimpleIDE"? For objects that don't use an external .S file then yes.

    There is no support for building with them at the moment that I know of, but it's not hard to add that sort of thing.


    Actually if it's part of a pre-compiled library archive, it doesn't matter, because it's just another object to link. For example let's say you have a propeller library and header file ... library foo ....

    Today, if you have a project where the main file has this ...
    /* file footest.c */
    #include "foo.h"
    int main(void)
    {
      foo_function();
    }
    

    And the project, is called footest.side ....

    As long as the Properties -> General -> Auto Include Simple Libraries box is checked, SimpleIDE will recursively look for a library called "libfoo" in Documents/SimpleIDE/Learn/Simple Libraries/*.

    Once libfoo is found containing foo.h and cmm/libfoo.a, SimpleIDE will add everything needed to compile footest.c with the foo library assuming cmm mode. For lmm mode libfoo/lmm/libfoo.a should be found, etc....

    There is no need to Add Include Path, etc... with Auto Include Simple Libraries checked.

    SRLM wrote: »
    Also, I haven't forgotten about your request for a #include .cpp test. Just busy.

    Thanks.
  • Jack3Jack3 Posts: 55
    edited 2014-02-16 19:24
    @SLRM
    I don't understand how to make the clock freq and mode work. I think I can work this out using waitpeq and waitpne if I can figure out clock stuff.

    Could you give me some sample code to set the clock freq and mode with the propeller.h? This will need to be coded as the project is not going on a specific board, so generic profile and I don't know what clockspeed I would end up with.

    @Jazzed....will look at pulse in too. Whichever is easier for me to grasp.
  • Jack3Jack3 Posts: 55
    edited 2014-02-17 08:20
    pulse_in is the way to go.

    foovar = pulse_in(inputPin, mode);

    Where mode is 0 for low sense, 1 for high sense mand foovar is pulsewidth. Beats the heck out of what it took in Spin. Wow. Piece of cake.
  • SRLMSRLM Posts: 5,045
    edited 2014-02-17 11:42
    For clock frequency I usually use the propeller-load variable patching feature to get everything set up.

    The Propeller.h file has a clkset instruction that simply writes the two registers that control that sort of thing. Like it says there you'll have to look in the manual: I've never tried using it.
  • Jack3Jack3 Posts: 55
    edited 2014-02-17 17:19
    Okay, thanks.
Sign In or Register to comment.