Building a debounce isn't too hard, if it's even needed. It just depends on the magnetic switch you are using, if it's solid state you won't need to do much more than check if it's closed, being the way I am, I'd double check it again before actually stepping the moter etc. If it's a mechanical relay, that will need a "real" debounce and maybe some additional external hardware, like a cap.
To debounce you need a loop that checks if the switch is closed. If it is then increase a debounce var. and check it again, if it's not closed set your debounce var = 0 and keep checking. Determine the amount of time / number if times you want to increase your debounce var. you can put a pauseus 100 and recheck if it's still closed, and when your debounce var = 10 then you know the switch has been closed for at least 1 ms ( 100 us * 10 = 1ms)
Joe S. had the idea of using count, I've used it and it's simple.
The above code counts the pulses on porta.1 over 150 ms and stores the value in the Var "freqval".Code:count porta.1, 150 , freqval
In your case you would count for 10 ms? something like this:
Joe S. may have a better idea, and better code for you, above is where I would start, I'm no pro at this myself. I'd use the above code as an example, I just typed it out, so it still needs more work.Code:Counting_Mags: count portx.x, 10, mag_count ' count port x pin x for 100 ms Num_Mags = Num_Mags + mag_count if Num_Mags > 9 then One_Rev ' counted at least ten magnets so one revolution has occured, so step motor. goto Counting_Mags One_Rev: Num_Mags =0 ' reset count 'do the rest of you stuff here Goto Counting_Mags ' go back and count another revolution




Bookmarks