DIYstompboxes.com

DIY Stompboxes => Digital & DSP => Topic started by: G. Hoffman on June 09, 2010, 06:16:50 AM

Title: Help w/16F84A
Post by: G. Hoffman on June 09, 2010, 06:16:50 AM
Yes, I know it is an old outdated PIC, and I should be using something more modern.  For now, it is what the book I'm learning from uses, and so for now it is a good choice for me.  I also want to stick with it for now, because I am learning a lot with it yet.  I'm sure I'll learn more when I start using newer PICs, but not just yet.

I am still learning how to use this thing, and am slowly building up the various bits of knowledge I need for a future project.  One part of that is a (relatively) large number of switches on a (relatively) small number of pins.  By adding in a few diodes, I can get 20 switches on 5 pins, by cycling the pins between input and output.  I'm sure the idea is nothing new, but for now I want to see if it can work.  And it works great - mostly.

The problem is this;  I'm doing this on Port A, while using Port B to drive an LCD display.  I switch one pin on Port A to an output, and the other four to inputs, and scan the inputs to see which button is pushed (the diodes make sure signals don't go between the wrong pins).  It works fine when the output is A0-A3, but when I make A4 an output I get nothing.  All the pins of Port A are pulled down (100k resistors), and when I set a pin as an output, I set it high. 

Now, looking at the data sheet, I see that A4 is different from the rest.  It is an optional external clock input, plus when it is an output it is an "open drain" type.  Now, I'm assuming this is my problem, because I know the code is good - I've checked it more times than I care to count, and it was copy pasted from other parts of the code that work! - but I don't know why.  In particular, I'm afraid I'm just not sure what "open drain" means.  I know its got something to do with the CMOS logic, but that's it. 

I'm thinking my next step is to try pulling everything UP, and having the inputs pull down.  That should work, right?  Then again, I though THIS should work! >:(

The other night, I spent six hours (when I should have been sleeping!) to find about six zeros that I had typed as O's.  I was not pleased.  I'm not entirely sure I'm cut out for this programing nonsense!


Gabriel
Title: Re: Help w/16F84A
Post by: potul on June 09, 2010, 08:38:13 AM
Welcome to the wonderful world of PICs... :)

You are close to it. If I'm not wrong, the problem is the RA4 open drain. The "open drain" means that when set to LOW, RA4 is put to 0V, but when set to HIGH it is left open (not connected to anything). take a look at the datasheet, there is a block diagram of the internal circuit of RA4 and you will see the difference with the rest. If you want it to go to 5V you need to pull it up.

Going back to your circuit, you will have to remove the pull down resistor at RA4 and add a pull up resistor. You may need to modify some of the switching connections to keep the thing working when RA4 is an input. I would need to see your schematic to give a final assesment.
I think that the cleanest thing would be to change the logic so that all pins have pullup resistors instead of pulldown, as you were suggesting.

Don't be ashamed, 16F84 is the usually the first stop when starting with PICs. I also started with it, but immediately switched to 16F88, because most of the code is reusable and it has tons of extra memory and functionality. By the way, are you programming with ASM or C?
Title: Re: Help w/16F84A
Post by: G. Hoffman on June 09, 2010, 02:49:32 PM
Quote from: potul on June 09, 2010, 08:38:13 AM
I think that the cleanest thing would be to change the logic so that all pins have pullup resistors instead of pulldown, as you were suggesting.


That's my plan.  I'd have done it last night, but I figure re-writing the code and all will take an hour or two at least, so I'll probably do it tonight.


Quote from: potul on June 09, 2010, 08:38:13 AMDon't be ashamed, 16F84 is the usually the first stop when starting with PICs. I also started with it, but immediately switched to 16F88, because most of the code is reusable and it has tons of extra memory and functionality. By the way, are you programming with ASM or C?

ASM, for now.  I might choose to learn C at some point, but this just felt like a better way to understand the underlying structure of the thing.


Gabriel
Title: Re: Help w/16F84A
Post by: R.G. on June 09, 2010, 07:15:50 PM
Quote from: G. Hoffman on June 09, 2010, 06:16:50 AM
Now, looking at the data sheet, I see that A4 is different from the rest.  It is an optional external clock input, plus when it is an output it is an "open drain" type.  Now, I'm assuming this is my problem, because I know the code is good - I've checked it more times than I care to count, and it was copy pasted from other parts of the code that work! - but I don't know why.  In particular, I'm afraid I'm just not sure what "open drain" means.  I know its got something to do with the CMOS logic, but that's it. 
Open drain means that there is a FET with its source to ground, and the drain connected to the output pin. There is nothing else, no internal pullup of any kind. It is impossible for this kind of circuit to do anything by itself except pull an external load down to ground. If you want the external load to be pulled up when the drain is off, then you must supply an external pull up resistor or something to pull it high. And this makes using the pin as an input as well problematical. If you put an external pullup resistor, there is no way to tristate it when you want to use the pin as an input.

I always just avoided using A4 unless there was no other way. Better yet, use the 74HCT597 shift register and latch any number of inputs simultaneously and shift them into the PIC with only three pins. The 597 is a parallel in, serial out latch that can be daisy chained to any length. It might, um, simplify your code too.  :icon_biggrin:
QuoteThe other night, I spent six hours (when I should have been sleeping!) to find about six zeros that I had typed as O's.  I was not pleased.  I'm not entirely sure I'm cut out for this programing nonsense!
One of the things they don't tell you in programmer-school is to use a screen font that makes 0's have slashes through them so you can tell them from capital "O"s. It's kind of a rite of passage.  :icon_lol:

Of course, you will never, ever again make that mistake, having spent your six hours in hell.
Title: Re: Help w/16F84A
Post by: G. Hoffman on June 09, 2010, 10:41:55 PM
Quote from: R.G. on June 09, 2010, 07:15:50 PM
QuoteThe other night, I spent six hours (when I should have been sleeping!) to find about six zeros that I had typed as O's.  I was not pleased.  I'm not entirely sure I'm cut out for this programing nonsense!
One of the things they don't tell you in programmer-school is to use a screen font that makes 0's have slashes through them so you can tell them from capital "O"s. It's kind of a rite of passage.  :icon_lol:

Of course, you will never, ever again make that mistake, having spent your six hours in hell.


I've looked through my available fonts, and I can't seem to find one that has slashes through the zeros.  Got any to recommend?  Though you are right, I won't make that mistake again!  (Though, in the process of looking, I did find the way to increase the size from an 8 point font to a 12 point, which is a real eye saver!)

Reversing the logic works just fine.  No problems with RA4 as a input, or an output.  I had to change the direction of the diodes (well, I didn't HAVE to, but the switches would have been somewhat randomly reassigned if I didn't!), and changing the code took me much less time than I expected (a bunch of C's and S's changed places, and I actually was smart enough to only change the individual letters I needed to change, not to retype whole words!  Yeah for age, wisdom, and laziness!)  

So, I'm good and happy now, and this thing is working right.  Yeah me!



Oh, you asked for the schematic, potul.  Nothing too difficult.  I'm not sure I need the diodes on both ends of the switch strings, but it sure makes the PCB layout easier!  {edit - WHOOPS!  I forgot to make the pull-downs pull-ups - I'm sure you can do that for yourself, though!  Also, those are all, of course, momentary switches.}

(http://farm2.static.flickr.com/1281/4686516921_2311cc9634.jpg) (http://farm2.static.flickr.com/1281/4686516921_b0db019fee_o.jpg)
{click to enlarge}


Gabriel
Title: Re: Help w/16F84A
Post by: G. Hoffman on June 09, 2010, 10:43:22 PM
{nevermind - I meant to hit modify, not quote!}