Shop OBEX P1 Docs P2 Docs Learn Events
Any one made use of pulse_in? — Parallax Forums

Any one made use of pulse_in?

NumPyNumPy Posts: 27
edited 2013-10-24 20:15 in Propeller 1
I have searched the PI/ Propeller threads with no luck.
Im still trying to get my feet wet, so be kind.:-)
I am trying to measure the freq on a tsl235r, and
in my newbie dreams, I was hoping it would be as simple
as:

/** * This is the main Measure freq program file.
*/
#include <propeller.h>
#include "simpletools.h"


int main(void)
{


int myfreq;
int pin1;


pin1=input(1);
myfreq=pulse_in(1,1);


printf("Frequency = %d",myfreq);


return 0;
}

Comments

  • NumPyNumPy Posts: 27
    edited 2013-10-23 05:17
    I believe that pulse_in tells you how long a pulse lasted and the sensor has a 50% duty cycle
    So calculating the freq would be more involved that yhus. But all I get in debug
    Is Frequency=0
    Any ideas?
  • jazzedjazzed Posts: 11,803
    edited 2013-10-23 12:25
    How is your sensor connected (power, ground, in/out) ?
    Do you know that there is a changing signal on pin 1 ?
  • NumPyNumPy Posts: 27
    edited 2013-10-23 18:35
    I have the power, gnd, and signal wires hooked up properly and power and ground decoupled with the correct cap.
    The parallax scope shows a definite change when light is applied and removed.
    The scope says max 200kHz and the sensor says 200 - 300kHz,
    but for what its worth, this is the only evidence I have:
    Pic one is very low light, pic 2 is an led flashlight shined in to the
    sensor 2 inches away.
    If you move the flashlight away slowly, there is a gradual positive
    movement.
    I was expecting to see square waves with this.
    VeryLowLight.jpg
    LEDFlashlight2inchesdistance.jpg
    984 x 604 - 190K
  • photomankcphotomankc Posts: 943
    edited 2013-10-24 06:54
    The vertical scale is 100mv? If so that's a 200mv swing on that waveform. That's not going to trigger any digital logic. Something isn't right either in the measurement or the connections. The datasheet indicates a 50% duty square wave. That isn't even close.
  • jazzedjazzed Posts: 11,803
    edited 2013-10-24 09:13
    Looks like you're reading induced 60Hz AC from your house.
  • NumPyNumPy Posts: 27
    edited 2013-10-24 12:29
    Ok, my bad, my board ground wasn't connected right. I now have nice wave forms that change with light intensity,
    however, in my debug on the serial port window all I get is "1960" from:

    pulse_duration=pulse_in(1,1); print("Pulse Duration: \%d \n"),pulse_duration;


    no matter what the light intensity is.
    I feel like I may not be using "pulse_in" correctly.
    I have verified that I am using pin 1 by driving an led from the same pin on a quickstart board
    with different led test code, so I feel confident that my connection from signal to pin 1 is valid.
    I am scoping this while driving it and have tried without scoping it as well.

    Dark Box.jpg
    IndirectFlashLight.jpg
    debug.jpg
    /**  * This is the main Measure freq program file.
      */
     #include <propeller.h>
     #include "simpletools.h"
     #include "simpletext.h"
     
    
     
    
     int main(void)
     {
     //int x;
     int pulse_duration;
     int pin1;
     pin1=input(1);
         
     for(;;)
         {
          //print("%d)",x++);
          pulse_duration=pulse_in(1,1); 
          print("Pulse Duration: \%d \n"),pulse_duration;
         }
         return 0;
     }
    

    For some reason if I remove the comments for x, pulse_duration and x always are the same, and the
    1960 value doesn't even appear.

    I have ran this code while changing the amount of light stimulating the sensor and get no change
    in the debug value pulse_duration.
    984 x 604 - 180K
    984 x 604 - 200K
    984 x 604 - 69K
  • NumPyNumPy Posts: 27
    edited 2013-10-24 12:32
    Also, if I pull the signal off of pin1 I still get 1960 as the value for pulse_duration.
  • jazzedjazzed Posts: 11,803
    edited 2013-10-24 17:46
    Since it we don't appear to be getting Andy's attention on this, I'll dive in deeper.

    This seems to be the big issue in your code.
    print("Pulse Duration: \%d \n"),pulse_duration;
    
    The statement is broken (comma is a valid and annoying operator in C).
    This is the reason you keep getting the same value all the time.
    
    It should be:
    
    print("Pulse Duration: \%d \n",pulse_duration);
    


    Below is a demo of pulse in and pulse out on the same pin.
    It is your code with some modifications.
    Comment out the cogstart/printf lines if you just want to measure a pulse.
    You only need "simpletools.h" in this program.
    /**
     * This is the main PulseMeasure program demo file.
     *
     * The program starts a COG that creates a pulse periodically.
     * The main program checks for a pulse every 100ms or so and
     * displays the measured value ad-hoc.
     */
    #include "simpletools.h"
    
    void pulseit(void *arg);
    
    int stack[60];
    
    int main(void)
    {
      int pulse_duration;
      int pin = 1;
      int cog = cogstart(pulseit, &pin, stack, sizeof(stack));
    
      pause(500);
      print("Started pulseit on pin %d in COG %d\n", pin, cog);
    
      // input for this cog
      input(pin);
       
      for(;;)
      {
        pulse_duration=pulse_in(pin, 1); 
        print("Pulse Duration: \%d \n",pulse_duration);
        pause(100);
      }
      return 0;
    }
    
    /*
     * This is a function that runs on a COG.
     */
    void pulseit(void *arg)
    {
      int pin = *(int*)arg;
      int delay = 100;
    
      // output for this cog only
      set_output(pin,1);
    
      for(;;) {
        // low before pulse makes pulse high->low
        low(pin);
        pulse_out(pin, delay);
        delay += 100;
        if(delay > 1000)
          delay = 100;
        pause(500);
      }
    }
    
  • NumPyNumPy Posts: 27
    edited 2013-10-24 20:15
    Ok, Thank you! I now have some usable numbers that change appropriately when the
    light intensity changes. I have no brain power left after getting your code changes
    and testing them. I have experimented with changing the pause time some, to
    get it more real time responsive, it seems to want to stick around 20 or so and
    with very little change zooms off into the thousands and back down.
    I must eventually get these number to conform, to be ruley.
    Not complaining, I just want to understand this much more than I do and you have
    given me the ability to do this. I will update as I learn more.
    I will labor over this first thing in the am.
    Ultimately, I will be in a black box with an led backlight watching an lcd
    screen go silver or black, so it has to be sensitive.
    I feel like I'm in the same boat as trying to take raw xyz axis data and
    turn it into something usable now.
    Thank You! Thank You!
Sign In or Register to comment.