I am a beginner wanting to know how to interface the spartan 3e board with the pickit2- PIC16F887. I have so far connected j2- pin a6(spartan) to port a Pin1 on the PIC, the ground Pins and j2 pin e7 to output port RD 1 on the PIC. I'm trying to configure a code that will run an elevator and I am also having trouble putting the pull-up resistors for the pushbuttons on the spartan board- they seem to be super-sensitive. Any advice would be great or if you know of some tutorials that involve interfacing the two boards. Thanks ahead
1 Answer
There are a number of things that you're battling. I think the best thing to do is take a step back and define what exactly it is you want to do before you start hooking things up. This is much simpler than it may sound, and having something ON PAPER which you can reference will save you endless headaches. Just draw out two tables: one for inputs and one for outputs. Now for each output write down what the output is for, and what has to happen for the output to become '1' or '0'. For the inputs, write down what each one is for and what a change to a '1' or a '0' means to the system. From that simple document you can start figuring out which physical pins on the FPGA and the microcontroller are best to use for each purpose, and start building a schematic.
Now... super-sensitive pushbuttons? I think what you're describing is known as "contact bounce" -- you cannot hook a mechanical switch up to a high speed digital circuit and expect to get nice clean off-to-on and on-to-off transitions. Mechanical switches bounce, and that is just how they are. You need to debounce them.
You can do it in hardware, in HDL or in software. Where you decide to do it is up to you, and in fact for a beginner it is probably a good exercise to try various methods for each to get a good feel for how they work. Debouncing boils down to either ignoring or "absorbing" extraneous switch transitions until the switch has stopped bouncing. A really simple way to do it in software is to look for the switch transition you're interested in (1->0, 0->1 or maybe both) and if a transition is seen, initialize a counter. Ignore any more switch transitions until the counter is done, then look again at the switch and see what the state is. If the value is the same, you have your new value. If it's not, it was a glitch and should be ignored. The value you initialize the counter to depends on the responsiveness you need and just how "noisy" your switches are, but a good starting value would be 50-100ms.
Very simple software debouncing for button presses:
/* main loop code, executed regularly */
if(debounce1 == 0) { /* count is 0, we are looking for button presses */
if(button1 == 0) {
debounce1 = DEBOUNCE_COUNT;
}
} else { /* count isn't 0, we are debouncing. */
--debounce1;
if(debounce1 == 0 && button1 == 0) {
do_something_because_of_button1();
}
}
if(debounce2 == 0) {
if(button2 == 0) {
debounce2 = DEBOUNCE_COUNT;
}
} else {
--debounce2;
if(debounce2 == 0 && button2 == 0) {
do_something_because_of_button2();
}
}
Of course here DEBOUNCE_COUNT is some value which represents 50ms. If your main loop executes every 1ms, this value would be 50. Adjust as needed.
There are more elegant ways to do this, but this is probably good enough to get you started, and it's straightforward enough to understand.
If the concept of button debouncing is new to you I would put the FPGA away for now and focus just on basic interfacing with the PIC for a while. Once you're comfortable with how the PIC operates and have some experience under your belt, put the PIC away and do the same for the FPGA. THEN hook the two up. Trying to do too much too soon will just leave you frustrated and hating electronics.
-
\$\begingroup\$ Here's a good debouncing tutorial ikalogic.com/debouncing.php \$\endgroup\$ Commented Jul 26, 2010 at 21:15