could sombody please help getting me started?

Thread Starter


Joined Nov 20, 2007
hi all, i was wondering if anybody could help me with some psudocode or just some logic.
I've just bought a pic16f876 and programming board. I haven't had any experiance with PIC programming, only c++ but before i start learning the pbasic i was hoping somone could tell me the best way to go about my problem....

i'll will be using 5 switches as inputs into the pic and 12 outputs acting as a virtual matrix.

With the 5 switches there's 32 different possible combinations of inputs, but i would only like the state of the switches to be read, or the value of the switches taken when one of the switches is realeased (the auto repeat will be turned off)

For instance, say i wanted to send rbo1 and rb2 high when ra1, ra2 and ra3 was high and 0 & 4 low. does anybody know how i might go about it?

any help would be appriciated.

Thread Starter


Joined Nov 20, 2007
this is one method i have found, there may be a way to scan in the value on each clock cycle, compaire it to the previous scan and if it is lower, somehow take the value of which switches had previously been pressed

any sugestions? i'd prefer to know how im going to go about doing this before reading my pic book.

"TIP #5 Scanning Many Keys With One Input (image below)

The time required to charge a capacitor depends
on resistance between VDD and capacitor. When a
button is pressed, VDD is supplied to a different
point in the resistor ladder. The resistance
between VDD and the capacitor is reduced, which
reduces the charge time of the capacitor. A timer
is used with a comparator or changing digital input
to measure the capacitor charge time. The charge
time is used to determine which button is pressed.
Software sequence:

1. Configure GP2 to output a low voltage to
discharge capacitor through I/O resistor.
2. Configure GP2 as one comparator input and
CVREF as the other.
3. Use a timer to measure when the comparator
trips. If the time measured is greater than the
maximum allowed time, then repeat; otherwise
determine which button is pressed."

but an coding solution would be preferable



Joined Aug 15, 2007
First. it will be easier if you have a maximum time in which the switches must be pressed - 1/2 second, 1 second, whatever. And don't forget settling (debounce) times for the switches.

After the time has passed, save the pattern of the switches, and then wait for the pattern to change:

// This assumes that a keypress gives a HIGH signal
while (PortA == 0)
// A key was pressed

while (TIMER--) // or use the hardware timer
Pattern = PortA ; // get all the keys that were pressed.

do {
while (PortA == Pattern) // wait for a change
Test = PortA; // save it

Test ^= Pattern; // Get the keys that have changed
Test &= Pattern; // But ONLY the ones that were released
}while (Test == 0);

// Do whatever you want with the key pattern.


Thread Starter


Joined Nov 20, 2007
thanks for that, but before i read your post i'd read a pbasic book and it turned out to be a piece of piss. only 35 instructions!!! wicked.

now on to the next problem......