Best way to actively monitor the state (high/low) of an input pin?
JeremyJ
Posts: 30
What's the most efficient way to actively monitor a pin for a high/low state? I've tried using a "repeat while" loop comparing two local variables in my method (1 of which calls another method that measures the pin's state, monitoring for changes). This seems way too complicated to do something so simple. Any suggestions?
Comments
Of course, we have no idea at what rate the lines are changing. Using two cogs provides you with 20 MHz sampling time; 50 nsec resolution;
Check the state of PIN 0 for a high.
Is your Opinion that something like the code below
is too complicated for a simple thing like this?
Would you like to solve this through using the hardwarecounters without a codeloop involved to change between
duty 10% and 90%?
write more about what your doing in your project. In minimum 50% of all cases if the forum has the overview about a project completely different and much better solutions to problems will be found.
keep the questions coming
best regards
Stefan
I'm thinking that what I should do in this case - for processor/program efficiency - is to launch a cog to continually monitor the state of my input pin. The idea being that this cog can keep looping and return a state variable that can be fed into a method to generate a pwm signal that also involves a loop.
Does this make sense?
I am trying to build a command signal to control a motor. I'll ultimately program A/D conversion (Chpt 7 of the Prop Education Kit explains methods to do this without a external A/D converter) to give me more discrete steps, versus just two, but trying to start simple.
I would code it his way:
as the PWM-signal is something that should be created continously all the time with the same duty exept for changing the duty
the creating of the PWM-signal should run it its own cog that does nothing else but creating the PWM-signal.
The check of the potentiometer can then be done in the "main"-cog. If the state of the pot-pin changes the main-cog changes the variable
controlling the duty. (or if you use one of the PWM-objects from the OBEX the main cog does call a SetDuty-method (method=subroutine)
keep the questions coming
best regards
Stefan
I understand your idea, and I think I'm thinking the same thing, but I'm still a little hazy on execution. Could you put up some code to give me an example or maybe point out a specific file in the OBEX library to review?
Jeremy
In the follwing code this methode can generate 2 PWMs, but the second one is commented out.
It also shows in a commented out line how you can query the pin and calculate the PWM duty within a single line.
You need to add your pin numbers in the CON section.
Andy
Very helpful for learning how to use the counter modules....
I'm going to try to modify this with a DAC...
Then you can use the counter in DUTY mode, which needs no second cog: With an R-C filter at the pwmPin you get a clean analog voltage, the values of R and C depends on the needed speed, you can try 10k and 1uF if you have only slow changes.
Andy
So the pwm_cog runs independant parallel to the main loop and generates the PWM signal. The interface is the global variable pwm1 which is set in the main methode and read in the pwm_cog methode.
Andy