I've just realised that my little program which counts the number of time a switch has been closed (a magnetic switch, which is closed as 10 successive magnets glued to the perimeter of a DC fan motor pass it by!)...isn't counting correctly - & I don't know why (though I suspect it';s either down to the debounce paramters or perhaps the length of time taken to 'do stuff' in between successive magnets).
here's my simple code (stripped down for clarity)...
RB7pin10 VAR PortB.7 ;explicity name the switch pin for button command.
RA3pin4 VAR PortA.4 ;explicity name the switch pin for button command.
Temp VAR BYTE ; this is used for debounce/delay of the fan switch)
total_counter VAR WORD ; this will count the total pulses rx'ed from magnets.
pitch_counter VAR BYTE ; this will utlimately derive the pitch thread (a divider)
direction_counter VAR word ; this will changed traversal direction word for counts bigger than 255
pulse VAR PortC.0 ; stepper feed pin 16 (to pin 11 on stepper IC)
direction var PortC.1 ;direction level pin 15 (to pin 14 on Stepper IC)
stepper_enable VAR PortC.3 ; Pin 7 this logic level 0 enables the stepper IC (stepper IC Pin 15)
Start:
total_counter = 0 ; make sure we start from zero
direction_counter = 0 ; make sure we start from zero
direction = 1 ;we want the feed stepper to move left/right first.
low stepper_enable ;enable the stepper chip.
Main:
Button RB7pin10, 0, 255, 0, Temp, 1, magnetic_switch_closed ; monitor the magnetic switch
goto Main ;if switch not closed, loop until it does close.
magnetic_switch_closed: ;ok, so switch is now closed - let's count!
total_counter = total_counter +1 'increment counter to track number of magnets passing switch.
IF total_counter = 500 THEN
goto finish ' if using 10 magnets, after 50 turns of the motor exit the program
endif
pulse_the_stepper:
pulsout Pulse, 20 ; pulse the stepper on Port C.0 with 20us pulse.
Goto main ;return to start & monitor for the switch being closed again
finish:
HIGH stepper_enable ;disable the stepper chip
End
So to my question....
Is there a more scientific way to approach this?
The motor starts off slow, & then increases under manual control up to a speed somewhere between 150RPM & 350RPM. So the rate of magnets passing the switch can be anywhere between 0 per second & 58 passes per second (350/60 = 5.8 turns per second...there are 10 magnets)




Bookmarks