Counting better ?


Closed Thread
Results 1 to 17 of 17
  1. #1
    Franko's Avatar
    Franko Guest

    Default Counting better ?

    Hi,
    I'm trying to get this to count pulses from an opto detector which goes High when the beam is broken.
    Problems are,it stops the Hmtr at three pulses instead of four and if the beam isn't broken in quick succession,hmtr continues to run no matter how many times beam is broken.
    My complete thanks to Melanie for the first (most so far) part of code!



    DEFINE OSC 4
    CMCON=7

    'Declare Variables,Constants and Aliases for Ports


    In1 VAR PORTA.1 'pulse input from selector 24vac/dc via optocoupler
    In2 VAR PORTA.2 'pulse input from selector 24vac/dc via optocoupler
    In3 VAR PORTA.3 'pulse input from selector 24vac/dc via optocoupler
    Button1 VAR PORTA.4 'pulse input from selector 24vac/dc via optocoupler
    Hmtr VAR PORTB.0 ' motor on/off
    DATAPLS VAR PORTA.0 'sense output from passthru opto
    Alert VAR PORTB.1 'Beeper
    CounterA VAR BYTE 'counter
    i VAR WORD 'General purpose variable


    'Set initial state of ports

    Hmtr=0 ' motor off
    TRISA = %11111111 'Make all Porta pins Inputs
    TRISB = %00000000 'Make all Portb Outputs
    Pause 500 'Settling time


    'Start of program

    runloop:
    While In1=1 'Wait here for input pulse
    CounterA=0 'Rest time counter
    Wend



    Timerloop:

    While Button1=1 'Wait for dispense button to be pressed
    CounterA=CounterA+1 'Increment counter by 100ms
    IF CounterA=200 Then Startloop 'If 20 sec elapses,goto start and await new activation

    Pause 100 'Wait 100ms for smooth operation
    Wend



    runit:

    High Hmtr
    Pause 100
    While DATAPLS=0 'wait for High from passthru opto
    Wend
    GoSub cnt

    GoTo runloop


    cnt:
    PulsIn DATAPLS, 1, i
    IF i > 4 Then
    Low Hmtr
    Else
    GoTo runit
    EndIF
    Return


    Any suggestions ?

    Thank you

  2. #2
    Join Date
    Aug 2005
    Location
    antipolo city, philippines
    Posts
    23


    Did you find this post helpful? Yes | No

    Default use pull ups

    franko,

    what is your configuration for you PORTA inputs? did you used a pull down resistor to ground? You have an alternative to use internal weak pull-up features (OPTION_REG=7). PIC have sensitive inputs if you used pull downs. I had some problems that I also count the noise spikes if i used external pull downs, and most likely in activating motors. the motors are inductors that could spread some unwanted signals seen by the microcontroller. proper hardware design could prevent this kind of problems.

    also, use
    Code:
      ...(code here in betweens)
    for ease. thanks

    regards,
    yettie

    We should learn "KISS" - Keep It Simple Stupid...

  3. #3
    Franko's Avatar
    Franko Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Yettie,
    The config for porta pins are as inputs (if I did the code right).inputs A.1 and A.4 are pulled high via a 470 ohm. A.0 is pulled low via a 470.

    I've never tried setting the internal pull up/dn but I will.The motor is power by a seperate 24vdc supply but with a common ground.The motor is switched by a relay which is controlled by the PIC via a NPN transistor and the coil of the relay has a diode across the coil and is correct direction.

    I presume the problem is with the way I've done the code as I'm still learning (and loving it). I'll use the Quote function in the future as you said.

    Thanks for your ideas.

  4. #4
    Franko's Avatar
    Franko Guest


    Did you find this post helpful? Yes | No

    Default

    Is it that no one sees anything wrong with this ? There must be something wrong with how I've done it.

    I'm not asking for anyone to write it for me,rather just some advice on the correct way to make the motor low after four (or whatever) pulses.


    Code:
      runit:
    
    High Hmtr 
    Pause 100
    While DATAPLS=0 'wait for High from passthru opto 
    Wend
    GoSub cnt
    
    GoTo runloop
    
    
    cnt:
    PulsIn DATAPLS, 1, i
    IF i > 4 Then
    Low Hmtr
    Else
    GoTo runit
    EndIF
    Return
    Thanks

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Isn't using a 470R resistor a little low for a pull-up? Using a value this low will only increase the current the IO has to produce in order to change the pins state.

  6. #6
    Join Date
    Aug 2005
    Location
    antipolo city, philippines
    Posts
    23


    Did you find this post helpful? Yes | No

    Default typical pull-up value

    franko,

    some points:
    - cokekid seems to be right. try to increase the pull-up to 10K.
    -
    Problems are,it stops the Hmtr at three pulses instead of four and if the beam isn't broken in quick succession,hmtr continues to run no matter how many times beam is broken.
    seems to be your PULSIN DATAPLS,1,i is the culprit. if theres anyway you can monitor your WORD "i", verify it. If you have a serial port, plug it. otherwise you'll be convinced of what you will find out. then recode. it's my style of debugging.
    yettie

    We should learn "KISS" - Keep It Simple Stupid...

  7. #7
    Franko's Avatar
    Franko Guest


    Did you find this post helpful? Yes | No

    Default

    Yeah, I agree with you CocaColaKid and yettie, I usually use 1K or 10K (depending).But, I breadboarded this kinda quickly and grabbed what I had the most of.

    I'll switch them to 10K and see if that makes any difference.

    Yetti, I'm not sure what you mean by...

    " seems to be your PULSIN DATAPLS,1,i is the culprit. if theres anyway you can monitor your WORD "i", verify it."

    But I'll keep trying different methods.

    Thanks for the advice,
    Franko

  8. #8
    Join Date
    Aug 2005
    Location
    antipolo city, philippines
    Posts
    23


    Did you find this post helpful? Yes | No

    Default i mean...

    franko,

    good day!

    i mean, i think that your PULSIN method is the problem. try to monitor your variable WORD 'i' after your PULSIN method, such as,

    Code:
         ...
         PULSIN   DATAPLS,1,i
         SEROUT2 porta.0,16780,["i: ",#i]      ;connect to hyperterminal at 2400, 8N1, no parity.
         ...
    just like that.

    also I'm in doubt of what you are doing. are you actually trying to count the number of pulses at DATAPLS pin? when you use the PULSIN method, you are actually measuring the period of high signal (since your second argument is '1': PULSIN DATAPLS,1,i). if your period is less than (40us at 4MHz) it will never satisfy your IF statement, therefore the motor will not stop. hope i enlightened your mind, somehow.

    regards,
    yettie

    We should learn "KISS" - Keep It Simple Stupid...

  9. #9
    Franko's Avatar
    Franko Guest


    Did you find this post helpful? Yes | No

    Default

    Yettie,
    Your right, I'm going about it in the wrong way. I am trying to count the pulses at the DATAPLS pin, NOT to measure the time it's high.

    You have indeed enlightened me, I'll keep working at it.

    Thank you very much !

  10. #10
    Join Date
    Aug 2005
    Location
    antipolo city, philippines
    Posts
    23


    Did you find this post helpful? Yes | No

    Smile welcome...

    franko,

    good day!

    i hope you will share your conclusions after all this just let me know. ok?

    regards,
    yettie

    We should learn "KISS" - Keep It Simple Stupid...

  11. #11
    Franko's Avatar
    Franko Guest


    Did you find this post helpful? Yes | No

    Default

    I sure will. Providing I come to a conclusion.

    I'll keep at it.

  12. #12
    Franko's Avatar
    Franko Guest


    Did you find this post helpful? Yes | No

    Default

    Well,
    Here is how I got it to count 4 pulses and stop "Hmtr". This works perfect every time although I'm sure there is a way that I could be able to just change a single number from say 4 to 5 in order to change the count, I just haven't figured it out yet.

    But, if the last pulse doesn't occur at (While DATAPLS=0) the motor continues to run. Of course.

    Can I use Timer1 ETC. to set a maximum motor run time of say 10 sec. ?

    Thanks, I appreciate any help.




    Code:
    High Hmtr						
    Pause 10
    While DATAPLS=0
    Wend
    Low Hmtr
    Pause 50
    
    High Hmtr						
    Pause 10
    While DATAPLS=0
    Wend
    Low Hmtr
    Pause 50
    
    High Hmtr						
    Pause 10
    While DATAPLS=0
    Wend
    Low Hmtr
    Pause 50
    
    High Hmtr						
    Pause 10
    While DATAPLS=0 
    Wend
    Low Hmtr
    Pause 50
    
    GoTo Startloop

  13. #13
    Join Date
    Aug 2005
    Location
    antipolo city, philippines
    Posts
    23


    Did you find this post helpful? Yes | No

    Default yep...

    franko,

    yes, you can use TIMER1 module. provided that you initialized it properly and then poll a counter if it reached a 10s mark. I've been using TIMER1 register for some time. A advice you set the prescaler to 1:8 setting since you have a way to long to wait.

    I could have just gave you some of ideas since it will be more satisfying if you find it yourself.

    have a nice day!

    regards,
    yettie

    We should learn "KISS" - Keep It Simple Stupid...

  14. #14
    Franko's Avatar
    Franko Guest


    Did you find this post helpful? Yes | No

    Default

    Will the timer run independently from the main code ? Like "in the background" ?

    I ask this since the main program will be stuck at the (While) statement.

    Thanks

  15. #15
    Join Date
    Aug 2005
    Location
    antipolo city, philippines
    Posts
    23


    Did you find this post helpful? Yes | No

    Default like a thread...

    franko,

    yes. once the initialization process of TIMER1 interrupt is complete and activating the start of counter, the timer will run in background as you work on your main code. it will only then be entering the interrupt service routine once it reached the overflow anytime and anywhere while you are working on your main routine.

    regards,
    yettie

    We should learn "KISS" - Keep It Simple Stupid...

  16. #16
    Franko's Avatar
    Franko Guest


    Did you find this post helpful? Yes | No

    Default

    I read about the timer(s) running in the background in Melanie's Olympic timer.

    I've looked through the data sheets. How does one learn more about how to use the timer functions ? Proper way to write the code ? Reccomended books ?

    Thanks,
    Franko

  17. #17
    Join Date
    Aug 2005
    Location
    antipolo city, philippines
    Posts
    23


    Did you find this post helpful? Yes | No

    Default

    franko,

    good day! i had not read any reading materials except for the microchip datasheet. you can have a guide there of what bits you will manipulate. for TIMER1, such bits are T1CON, PIE1.1, PIR1.1, and for interrupts INTCON. you would want to experiment it and discover what you can do. I bet it will be more satisfying. Melanie's olympic timer example is your best guide. I'm sure you can do it.

    regards,
    yettie

    We should learn "KISS" - Keep It Simple Stupid...

Similar Threads

  1. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 04:52
  2. Remain counting while sending out a pulse
    By ultiblade in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 10th January 2007, 15:51
  3. hardware counting while software runs
    By Archangel in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 2nd October 2006, 03:26
  4. continious counting process (capture)
    By asynch in forum General
    Replies: 1
    Last Post: - 17th February 2006, 07:42
  5. Counting Button Pushes
    By Tissy in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 27th November 2005, 22:10

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts