help with programming please

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View


(this request is a duplication of the newsgroup alt.microcontrollers !!!)

Hi,

I'm having trouble getting my head around programming a pic (from scratch)

Basically i want it to do :-

Loop
look at 3 inputs (not analogue)
compare with the inputs that were stored in the last loop around
if they are the same wait say .5 sec
then loop again
if the inputs are different then output (for a relay) for .5 sec

this project, i thought would be fairly easy or so i thought.

Anyone willing to help me write a program if it is that easy.

Any help would be most appreciated or am i getting too old for this
technology !!

Andy Sutton




Re: help with programming please


Andy,

On Wed, 18 Nov 2009 12:13:58 -0000, "Andy Sutton"

Quoted text here. Click to load it

Maybe specify in more detail what you need help with:

- programming itself

- the programming language you intend to use (you didn't specify)

- (the pic) microcontrollers (setting up I/O etc)

- technical aspects of getting your program compiled and onto the pic

- all of the above :-)

Unfortunately, I'm not familiar with the pic as a microcontroller nor
the BASIC language that is often used to program it, but I decided
that won't keep me from trying to help :-)

Your main loop, in a C-style language, could look something like this:

int main(void)
{
    InitializeHardware();
    
    int previousBits = 0;
    int currentBits = 0;
    int firstTime = 1;

    for(;;) {
       currentBits = GetInputs();

       if(currentBits != previousBits) {
            if(!firstTime) {
                ActivateRelay(500);
            }          
       }
       else {
          MsDelay(500);
       }
       firstTime = 0;
       previousBits = currentBits;
   }

   return 0;
}

Disclaimer: this is from the top of my head and untested in any way,
form or fashion.

I'm assuming that you do NOT want to trigger on the first time around
in the loop, since there is no known/specified "previous value". If
there is, remove the firstTime variable and its if statement (but not
the call to ActivateRelay), and initialize previousBits to that
specified inital value.

I've left out the pic-specific bits above - InitializeHardware should
set up your three inputs as such, and the relay bit as output.

GetInputs should read from the port where your three inputs are
connected. (I assume they are connected to the same port)

Make sure that you filter out any other input lines on that same port
by using the AND operator and a bit mask. In C, assuming your inputs
are on e.g. bit 0, bit 4, and bit 7 of an 8 bit port, you'd do
something like this:

     int filterMask = (1 << 0) | (1 << 4) | (1 << 7);
     int filteredValue = valueFromPort & filterMask;

(then you'd return that value. The above is rather clumsily done, for
didactical purposes)

MsDelay simply waits for the specified number of milliseconds. (This
is just a name I came up with - the routine may be called differently
on your system, but I guess it is there in some form or fashion)

ActivateRelay - left as an exercise for the reader - sets the output
bit that triggers the relay, waits for the specified number of
milliseconds, and resets the bit again.

Hope this helps somewhat - let us know if it makes sense to you at
all, and where the remaining problems are.

   Regards,
   Gilles.


Site Timeline