Shop OBEX P1 Docs P2 Docs Learn Events
Pulse counting using MonitorPWM object. — Parallax Forums

Pulse counting using MonitorPWM object.

msiriwardenamsiriwardena Posts: 301
edited 2011-06-22 15:06 in Propeller 1
I am trying to count pulses using the MonitorPWM object.
The count is to be displayed on the TV monitor.
When I run the attached code what I see on the TV is " Pulse Count:0 "and nothing else happens.
The code is attached.

Comments

  • msiriwardenamsiriwardena Posts: 301
    edited 2011-06-21 12:10
    Iam attaching the MonitorPWM object so you do not have to go looking fir it.

    Thanks for your help.
    Siri
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-06-21 12:47
    Hi Siri,

    that's the way I like it! You did your "homework" very well. You provided all files that were nescessary to compile your code.

    There is an even easier way to do provide code
    the propellertool has a archive-project function
    it can be found in the mainmenu under file - archive - project ...
    This will create a zip-file by one click that includes all files needed to compile

    Now to your code. That was a quite easy one

    you have to call your methods PulseCount and HallEffect repeately
    so inserted code
    '' Counting pulses using MonitorPWM object
    ''   Using  TV TO DISPLAY - OUT-PUT
    ''
    ''    LED is connected to LedPin - 5 and Hall sensor probe connected to pin 1
    ''
    ''    LED to monitor the Hall effect sensor activity
    ''           
    
    OBJ
       'tv    : "tv_text"
       tv    : "PC_Text_advanced"
       Probe : "MonitorPWM"
    
       
    VAR   
      long  tHprobe,Tlprobe,pulseCnt
    
    
    CON
      _clkmode        = xtal1 + pll16x
      _xinfreq        = 5_000_000
                              
      LEDpin    = 5                                                         
      ProbePin  = 1 'Hall effect sensor connected to PIN 1
    
      
    Pub Main
         Probe.start(1, @tHprobe, @tLprobe, @pulseCnt)               ' probe.start(8, @tHprobe, @tLprobe, @pulseCnt)  
         tv.start(12)
    
       repeat  ' <<== you have to repeat the call of the methods PulseCount and HallEffect 
         PulseCount  
         HallEffect
         
    
    Pub PulseCount
       tv.str(string("Pulse Count :"))
       Tv.dec(pulseCnt)
       Tv.out($0D)                                                 
    
    
    pub HallEffect
    
     dira[LEDpin]~~
          
     'Repeat  '<<== and DON'T repeat inside the method
       If ina[ProbePin] == 0          'The Hall effect pin is actvated  "Low=0"
         outa[LEDpin]~~               ' LED1 is on  
          
                              
       if  ina[ProbePin] == 1          ' The hall Effect pin deactivated - making pin "High =1 "                    
         outa[LEDpin]~                     ' LED1 is off     
    
    As I don't have a TV handy I used PC_Text_advanced which emulates the tv_text-object through the PropTerminal-Tool on the PC
    Simply change back to the normal TV_Text-object to use it again with a TV

    keep the questions coming
    best regards

    Stefan
  • msiriwardenamsiriwardena Posts: 301
    edited 2011-06-21 13:32
    Stefan,

    Thanks you for the help.I am now going to try it.

    Siri
  • msiriwardenamsiriwardena Posts: 301
    edited 2011-06-22 05:47
    Stefan,
    It did work as intended.Thanks.
    Please can you explain to me where in my thought process ,I was incorrect.
    My logical thinking was that each method would run on its own cog for ever and keep updating the global variables and use the updated variables in another method in its own cog.
    I had more cogs available and not being used.

    Thanks again

    Siri
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-06-22 07:00
    Hi Siri,

    again congrats to your stile in asking questions!! Well done. You added your own thoughts to the question how you think it works.
    This is very important information for me. Now it is much easier for me what details should be included in my explanation:


    If you write a program all methods run in one single cog as long as the command "cognew" is not used.
    Only the cognew-command starts another cog.

    If you are using objects like MonitorPWM.spin
    these object have a start-method. Inside the method start there is the command cognew.
    To code in this way that an object has a start-method and INSIDE this start-method is the cognew-command is recommended as standard.

    This means as long as methods - even if they are distributed over several objects (= other *.spin-files) they all use the same cog.
    only the command cognew starts another cog and then in this second cog only those methods that are called from the initial cognew-command are executed in this new cog

    Each method runs as long as there are codelines left to be executed. Except that you code a loop with the command repeat.
    This means if a method has no repeat-loop the code is executed one time and if the method ends the code returns "upwards" to the calling method.
    if there is no calling method "above" then the cog is stopped.

    Have you ever taken a look into the PE Kit-labs?
    There are lot's of exercises to examine how spin works with objects and cogs

    http://www.parallax.com/Portals/0/Downloads/docs/prod/prop/PELabsFunBook-v1.1.pdf
    In Chapter 5 Methods and cogs lab you find code examples and more explanation on that.

    keep the questions coming
    best regards

    Stefan
  • msiriwardenamsiriwardena Posts: 301
    edited 2011-06-22 15:06
    Stefan,
    Thanks for explaining that to me.I do have the PE Education Kit and that is where I got the MonitorPwm object.
    My problem is I read/do the lab for a couple of days and then my job takes me away at times a long
    periods - so everything looks new again - but I still like to tinker with electronics.

    Thanks again
    Siri

    P.S: I am sure I will have more questions later.
Sign In or Register to comment.