• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

8-bit up/down counter

Status
Not open for further replies.

ido_onLy

New Member
Hello all..

I'm a newbie, i'm so much interesting with PICmicro since I knew this awesome stuff..
I works as a technician at a medical supplier company in Indonesia.
I'd like to spend my time on holiday to do some electronic project

I need some advance, i need some code to make a PIC16F84A become 8-bit up/down counter with 3 button; count up button, count down button, and an set button (30sec turn off delay)..

I haven't much experience for PICmicros programming, I only got a JDM programmer, my first project was succeed to program a PIC16F84A become countdown timer from Stan Ocker's project.
Next month I've got plan to take a PICmicro training at certified microcontroller learning centre on Bandung.
Ganbatte..

Best regards
 

Hakachukai

New Member
I'm not 100% sure what you want the "set button" to do, but everything else that you want to do is pretty easy.

You already know how to set the chip up, so I'll just cut to the chase and show the main code.

I don't think that you need to debounce your buttons, because they are just simple up/down counters.

Make RA0 through RA2 your button inputs.
Make all of PORTB your binary output.

You'll have to write your own delay routine (it's not hard at all)

This code is not tested, I just wrote it litterally off the top of my head. It's simple enough that you should be able to get it working
simply by seeing what it is supposed to be doing. I hope it helps
-------------------------------------------------------------------------------------------------------------------------------------------------------

movlw 0x01
movwf mode ;This tells if your counting up or down, or not at all
;2 = counting up
;1 = not counting
;0 = counting down

main_loop

btfsc PORTA,0 ;this is the count up button
goto set_mode_up

btfsc PORTA,1 ;this is the count down button
goto set_mode_down

btfsc PORTA,2 ;this is the stop button
goto set_mode_stop

set_mode_up
movlw 0x02
movwf mode
goto count

set_mode_stop
movlw 0x01
movwf mode
goto count

set_mode_down
movlw 0x00
movwf mode
goto count

count
;see if mode was 0
movfw mode
btfss STATUS,Z
goto count_down

;see if mode was 1
xorlw 0x01
btfss STATUS,Z
goto count_stop
xorlw 0x01

;see if mode was 2
xorlw 0x02
btfss STATUS,Z
goto count_stop

;if all else fails, stop the count by default
goto count_stop

count_up
incf PORTB
call delay
goto main_loop

count_down
decf PORTB
call delay
goto main_loop

count_stop
call delay

goto main_loop
 

Pommie

Well-Known Member
Most Helpful Member
Hakuchukai,

When you post code, if you type [code] before it and [/code] after it then it will keep it's formatting like this,
Code:
		movlw	0x01
		movwf	mode		;This tells if your counting up or down, or not at all
;2 = counting up 
;1 = not counting 
;0 = counting down  

main_loop

		btfsc	PORTA,0		;this is the count up button
		goto	set_mode_up

		btfsc	PORTA,1		;this is the count down button
		goto	set_mode_down

		btfsc	PORTA,2		;this is the stop button
		goto	set_mode_stop

[COLOR="Red"]; this will fall through and cause the code to count up.[/COLOR]

set_mode_up
		movlw	0x02
		movwf	mode
		goto	count

set_mode_stop
		movlw	0x01
		movwf	mode
		goto	count

set_mode_down
		movlw	0x00
		movwf	mode
		goto	count

count
;see if mode was 0 
		movfw	mode
		btfss	STATUS,Z
		goto	count_down

;see if mode was 1 
		xorlw	0x01
		btfss	STATUS,Z
		goto	count_stop
		xorlw	0x01 

;see if mode was 2 
		xorlw	0x02
		btfss	STATUS,Z
		goto	count_stop

;if all else fails, stop the count by default  
		goto	count_stop 

count_up
		incf	PORTB
		call	delay
		goto	main_loop

count_down
		decf	PORTB
		call	delay
		goto	main_loop

count_stop
		call	delay

		goto	main_loop
I think this code will count up when no button is pressed. See comment in red above.

Mike.
 

Hakachukai

New Member
thanks for the tip about the code tag.

Your right, it will fall through, but it's not an issue.

Mode is set to 1 at execution time. After that, mode can only change if you press a button. So it's not a problem

I made it quick and dirty, but it should work
 

Pommie

Well-Known Member
Most Helpful Member
Mode is set to 1 at execution time. After that, mode can only change if you press a button. So it's not a problem
No, first time through, if no button is pressed, it will fall through and set mode to 2.

Mike.
 

Hakachukai

New Member
yep, your right. That's what I get for coding quick and dirty with no testing.

In the place where it falls through just add the line "goto set_mode_stop"

That'll fix it.

Just to clarify... the point of this was to simply give Ido the basic idea of how this can be done. I figured there might be a bug or two, but if he plays with the code a little bit he'll get it working.
 

colin55

Well-Known Member
Here is a program for displaying 2 digits with up/down buttons. <snip: spam link deleted>
 

Attachments

Last edited by a moderator:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top