Jump to content
Chippie

16F84 based flip flop

Recommended Posts

my problem is, I'm trying to write a program for a 16F84. I want to have 2 outputs that toggle hi and lo on the press of a button.

I have 2 leds connected to PortB and a switch connected to PortA.

When the switch is depressed, the 1st led lights for a period and then extinguishes, on the next press of the switch the 2nd led lights and extinguishes, and then loops back to the beginning...viz the next switching action will light led 1 again.....

So far I have managed to write and assemble the code to flash 2 leds alternately, I can write code to have me press a button and then the leds flash alternately, but I'm struggling to achieve my goal......

Can any one hep please? :)

Share this post


Link to post
Share on other sites

Is the problem with the PIC instruction set? Or, is it related to how this sort of activity is coded (in the generic sense)?

What specifically have you been able to code? What part do you need advice on?

Since this a good project to practice your code writing skills, spoon feeding the actual source code would not help you in the long run. Teach a man to fish, as they say.

Share this post


Link to post
Share on other sites

thanks for the reply Mr. R/C Cam.....

My problem at the moment is with how to code using the instruction set......Specifically I need help on how to use one switch to flip or toggle the 2 outputs. if that makes sense...

Share this post


Link to post
Share on other sites

This is a project that won't be hammered out in a few lines of code. As simple as it sounds, it would probably take an experienced programmer a few hours to fully code and debug in assembly (if starting from scratch).

To make it managable, you'll need to attack it one function at a time. Start by creating a switch debounce routine. Then create a simple loop timing routine that gives you around 25mS resolution (per count) or so. Then master how to write simple conditional loops (Do-whiles and For-Next sort of functions).

If you are not prepared to write these on you own, then these sort of functions can be found by googling and looking at other code examples. Once you get them up and running you can move on to utilizing them in your program.

If you just want someone to write the app for you then you will need to offer a more detailed specification. EVERYTHING needs to be known. Like, what is the window timing for a single press? For a double press? What should happen if too many presses are entered? How many mS should each output stay on? What is the desired default power up state? What happens if button presses occur while an output is on? And so on. Even if you write it, you will need to spec these out for your code development activity.

Maybe if you gave the full details to the actual application someone may be able to steer you to a similar project.

Share this post


Link to post
Share on other sites

Chippie,

Here's how I understand your desire.

When you apply power, you want led A to be lit, and led B to be off. When you press the button, you want led A to turn off, and led B to turn on. When you press the button again, you want led A to be lit, and led B to turn off... and on and on and on.

If this is your goal, then here is what I would do:

1) initialize A to be 'on' at the start of the program, and B to be 'off' at the start of the program

2) write the code to wait for a button press

3) when the is button pressed, make the assignments:

A = A xor 1

B = B xor 1

4) loop back to wait for button to be pressed

I'm not sure if xor is built into assembly language or not, but if so, it is a nifty tool that will allow you to toggle the value of a pin to the opposite value from it's current state.

For example, if A = 1, then A xor 1 = 0.

If A = 0, then A xor 1 = 1

In this manner you do not have to perform any complicated if-then-else logic to decide which state to set the pin to... performing 'xor 1' against the pin will always flip it to the opposite state.

I hope that helps,

Regards,

Bill

Share this post


Link to post
Share on other sites

For sure, XOR 1 is the king of bit toggling. You'll need to debounce the switch or the states can change several times with each press. Not a big deal though.

But, I understand he wants one output to turn on for a short period when the button is pressed once. But if the button is pressed twice, the other output turns on for a short period. If true, then it will require a bit more work than a simple output toggle. But perhaps I don't understand the app.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×