PIC 16f877a Microcontroller help

Thread Starter

yasir_66

Joined Jun 25, 2009
71
hi friends
i need help in developing a program which will count each time by push button. I am using proton ide for programming.
i added one 7segment on B0-B7
i am able to light up a digit but i don't know how to put command for push button as an increament
here i started,

Device 16F877A
Xtal 20

TRISB=%00000000

PORTB = %00111111 'zero' PORTB = %00000110 'one' PORTB = %01011011 'two & so on' PORTB = %01001111 PORTB = %01100110 PORTB = %01101101 PORTB = %01111100 PORTB = %00000111 PORTB = %01111111 PORTB = %01100111
main:

GoTo main
 

Dodgydave

Joined Jun 22, 2012
11,302
use increment file routine>>

setup porta,1 as an input
setup d1 as a file





START
clrf d1

Wait
BTFSS PORTA,1 ; is button pressed? test for +ve input
goto Wait ;wait till button pressed
BTFSC PORTA,1 ; is button released, test for 0v input
goto $-1 ; no wait here
incf d1,f
(put a delay here of 30msec for debounce)
goto Wait
end
-------------------------------
to decrease the file use decf d1,f
the file d1 will increment on each press and release of the button, you can then move file d1 to the seven segment, like

movfw d1
movwf PORTB; this will be displayed in binary, to convert to seven segment, you need to use a lookup table
So you will use the Call instruction "Call TABLE" and movwf PORTB, this will put the segment value on port b.



TABLE

movfw d1
addwf pcl,f
retlw b'00111111'; zero
retlw b'00000110'; one
retlw b'01011011'; two
...etc to nine
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
The Proton IDE is for a Basic compiler? Got a link for the language syntax? Basic was the 2nd language I learned (going on to literally 10+ dialects), so seeing what they use will help me help you.

Setting TRIS is good, you made then outputs. You have all of Port A to pick a place for the button. One thing you will learn is computers need some sort of debouncing or they will count each button press multiple times.

The debounced button will increment a counter, and the count will change the 7-segment data you place on the Port B lines.
 

GopherT

Joined Nov 23, 2012
8,009
The Proton IDE is for a Basic compiler? Got a link for the language syntax? Basic was the 2nd language I learned (going on to literally 10+ dialects), so seeing what they use will help me help you.

Setting TRIS is good, you made then outputs. You have all of Port A to pick a place for the button. One thing you will learn is computers need some sort of debouncing or they will count each button press multiple times.

The debounced button will increment a counter, and the count will change the 7-segment data you place on the Port B lines.
He can also do a debounce in software - about 50 milliseconds for a decent switch - make the increment on release.

You need to connect your button with a pull-up resistor or a pull-down resistor (depending if you want the default input to be high (5 v) or low (0 v). You can google those phrases. Basically, you want your input pin to see 5 v or 0 v , you don't want some floating value when the pin is connected to an open switch only.
 

THE_RB

Joined Feb 11, 2008
5,438
He can also do a debounce in software - about 50 milliseconds for a decent switch - make the increment on release.
...
A minor nitpick; activation on release (or increment on release) feels faulty to the user. I've been caught by that before and had to re-code it. People mash buttons and start swearing. ;)

Better to increment on first detecting the button press, then require release for >XmS before allowing next increment. :)
 

ErnieM

Joined Apr 24, 2011
8,377
A minor nitpick
100% agreement (except it may not be so minor). Just look at the computer keyboard you are using: keys get accepted on the "key down" event.

(If you want to have some fun you can add in a rrrrrr repeat function when they keep the key down, but leave that for the next project. :D )
 

THE_RB

Joined Feb 11, 2008
5,438
But funny enough, the entire Windows GUI standard works on activation on release. You have to click AND release a button on a window to activate it. The complete opposite of what every user knows and accepts in hardware buttons and switches.

Good ol' Bill Gates and his twisted universe. ;)
 

ErnieM

Joined Apr 24, 2011
8,377
Gack...at the risk of hijacking poor yasir_66's thread...

I actually am quite aware of the Windows mouse using the click_up event, and appreciate it very much. It gives me one last change NOT to click on something since you can slide off and release and toss the click into the bit bucket of no effect.
 
Top