Continue to Site

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.

  • 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.

help with up/down counter

Status
Not open for further replies.

josi

New Member
Hi! i need to make an up/down counter using a pic16f877, the count must b from 0 to 9 and 9 to 0 via PortD and must b displayed on a 7-segment display.
The control of the counting cycle must b done using two switches connected to PortA
switch 1 = Up (0-9)
switch 2 = down (9-0)
Reversal of the count must continue from previous count (don't reset counter when changing from switch1 to 2 or vice versa)

I know how to do this as i'v builded a counter before and displayed on a 7-segments! the tricky part is the reversal of the counting sequence! i don't know how to do it! what i mean is i can hav two sub routines, 1 for UP another for down, and i'll do a bit test to call them (or goto) but how would i make the pic remenber the previous count?
thanx
 
Hi! i need to make an up/down counter using a pic16f877, the count must b from 0 to 9 and 9 to 0 via PortD and must b displayed on a 7-segment display.
The control of the counting cycle must b done using two switches connected to PortA
switch 1 = Up (0-9)
switch 2 = down (9-0)
Reversal of the count must continue from previous count (don't reset counter when changing from switch1 to 2 or vice versa)

I know how to do this as i'v builded a counter before and displayed on a 7-segments! the tricky part is the reversal of the counting sequence! i don't know how to do it! what i mean is i can hav two sub routines, 1 for UP another for down, and i'll do a bit test to call them (or goto) but how would i make the pic remenber the previous count?
thanx
hi,:)

You would assign a Register as say 'Count'.

The UP switch would increment the Count value and the DOWN switch would decrement the Count value.

After the Count value has been changed you output that value to the display.

The UP/DOWN subr would require code that detects > 9 and < 0.

Is this what you are asking.?

EDIT: if it was necessary to remember the Count value during power down, use the EEPROM to hold the Count.
 
Last edited:
Thanx eric:) how would i display the value of count(register) on the display? so far what i do is turning on (set) the pins of the pic wich r connected to the display in order to count! can u tell me to detect >9 and <=0 ?
 
Thanx eric:) how would i display the value of count(register) on the display? so far what i do is turning on (set) the pins of the pic wich r connected to the display in order to count! can u tell me to detect >9 and <=0 ?

hi josi,

Can you post the code you have written.?
 
like i said b4, all i can do is to set the pins that r connected to the display in order to count. this is jus a short section of the code

LIST p=16F877
#include <P16F877.inc>
__config _XT_OSC & _PWRTE_ON & _WDT_OFF &_LVP_OFF
;*****Equates**********************************************
status equ 03h
PortA equ 05h
PortD equ 08h
TRISA equ 85h
TRISD equ 88h
count0 equ 25h
count1 equ 26h
count2 equ 27h
;******Main Program****************************************
org 05h
bsf status,RP0
movlw B'11111111' ; set portA as input
movwf TRISA
movlw B'00000000' ; set PortD as outpout
movwf TRISD
movlw D'6'
movwf ADCON1
bcf status,RP0


start
btfsc PortA,0
goto up
btfsc PortA,1
goto down
goto start
up
movlw b'11101111' ; display 0
movwf PortD
call delay
movlw b'10001001' ; display 1
movwf PortD
call delay
movlw b'11010111' ; display 2
movwf PortD
call delay
movlw b'11011101'
movwf PortD
call delay
movlw b'10111001'
movwf PortD
call delay

delay
movlw 05h
Movwf count2
Loop
decfsz count0,1
Goto Loop
Decfsz count1,1
Goto Loop
Decfsz count2,1
Goto Loop
Return
End

i also know that i can assign a register, increment it and send it to PortD! but how would i display the values? sice the count will b in binary! plus how to stop at 9?
 
like i said b4, all i can do is to set the pins that r connected to the display in order to count. this is jus a short section of the code

LIST p=16F877
#include <P16F877.inc>
__config _XT_OSC & _PWRTE_ON & _WDT_OFF &_LVP_OFF
;*****Equates**********************************************
status equ 03h
PortA equ 05h
PortD equ 08h
TRISA equ 85h
TRISD equ 88h
count0 equ 25h
count1 equ 26h
count2 equ 27h
;******Main Program****************************************
org 05h
bsf status,RP0
movlw B'11111111' ; set portA as input
movwf TRISA
movlw B'00000000' ; set PortD as outpout
movwf TRISD
movlw D'6'
movwf ADCON1
bcf status,RP0


start
btfsc PortA,0
goto up
btfsc PortA,1
goto down
goto start
up
movlw b'11101111' ; display 0
movwf PortD
call delay
movlw b'10001001' ; display 1
movwf PortD
call delay
movlw b'11010111' ; display 2
movwf PortD
call delay
movlw b'11011101'
movwf PortD
call delay
movlw b'10111001'
movwf PortD
call delay

delay
movlw 05h
Movwf count2
Loop
decfsz count0,1
Goto Loop
Decfsz count1,1
Goto Loop
Decfsz count2,1
Goto Loop
Return
End

i also know that i can assign a register, increment it and send it to PortD! but how would i display the values? sice the count will b in binary! plus how to stop at 9?
sorry guyz i posted the code in a redable way! i don't know y it's showing lik this
 
hi josi,
As I understand, you have one digit to display, with a value of 0 to 9

Which is 00000000 thru 00001001 so where is the binary number you refer too.?
Its BCD

If you connect the LOW PORTD pins to a 7447 BCD to 7 segment driver it will show decimal 0 thru 9 on the LED.

Do you follow.?

EDIT: Code fragment.

Code:
;test for > 9
	MOVLW 0x09	;load Wreg with 9
	SUBWF Counter,W ;sub Counter from W reg
	BTFSC STATUS,C	;if carry set goto to inc
	GOTO its9	;its 9 already
;inc the counter value
its9:
................................
;test for 0
	MOVF Counter,W	;load W with Counter
	SUBLW 0x00	;sub 0 from W
	BTFSC STATUS,Z	;if zero skip dec
	GOTO its0
;dec the counter value
its0:
 
Last edited:
hi josi,
As I understand, you have one digit to display, with a value of 0 to 9

Which is 00000000 thru 00001001 so where is the binary number you refer too.?
Its BCD

If you connect the LOW PORTD pins to a 7447 BCD to 7 segment driver it will show decimal 0 thru 9 on the LED.

Do you follow.?

EDIT: Code fragment.

Code:
;test for > 9
	MOVLW 0x09	;load Wreg with 9
	SUBWF Counter,W ;sub Counter from W reg
	BTFSC STATUS,C	;if carry set goto to inc
	GOTO its9	;its 9 already
;inc the counter value
its9:
................................
;test for 0
	MOVF Counter,W	;load W with Counter
	SUBLW 0x00	;sub 0 from W
	BTFSC STATUS,Z	;if zero skip dec
	GOTO its0
;dec the counter value
its0:

ow! i get u now! i connected the display directly to the PIC without the 7447! dat y i was talkin about binary values (BCD). now i need to get that ic!
 
ow! i get u now! i connected the display directly to the PIC without the 7447! dat y i was talkin about binary values (BCD). now i need to get that ic!

You don't need a 7447, it's pointless with a PIC - like I said before, check my tutorial, and use a simple lookup table to drive the 7 segment display.

The point of PIC's is to simplify designs, not to add old IC's that you don't need.
 
You don't need a 7447, it's pointless with a PIC - like I said before, check my tutorial, and use a simple lookup table to drive the 7 segment display.

The point of PIC's is to simplify designs, not to add old IC's that you don't need.

hi josi,
As Nigel points out, for 7 seg displays a 7447 isn't necessary.
I thought you had already connected one.:eek:

Look at the tutorials.

Hint: a look up table of the segment pattern is whats needed, using the BCD counter value as a table pointer .OK.?:)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top