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.

counter using pic 16f877

Status
Not open for further replies.

josi

New Member
hi there! i want to make my pic count up and down (can b any number), i'm quit familiar with the instructions but i still cant get it wright! i'm using portB as output and i'd lik to see the counting sequence using led's first and a 7 segment display later!(RB0....RB7) can sbody help?
here's what i'v done so far (i'm new to this):

status equ 03h
PortB equ 06h
TRISB equ 86h
count1 equ 22h
count2 equ 23h
count3 equ 24h
;***********************************************************
org 10h
bsf status,RP0; bank1
movlw B'00000000'
movwf TRISB; use portB as output
bcf status,RP0; bank0

start

incf count3,0 ; increment count3 and store value in w
movwf PortB ; mov w value on portB
call repeat ; repeat calls delay 5x to make it longer
goto start

repeat
call delay
call delay
call delay
call delay
call delay
return

delay
movlw D'255'
movwf count1
loop1
movlw D'255'
movwf count2
loop2
nop
decfsz count2,1
goto loop2
decfsz count1,1
goto loop1
return
end
 
You increment count, then store a copy of the result in the accumulator, so the next time you access count, it is the value that was there the last time, so you are always getting the same out.
You need to store the result in count, and move that to w, then to PortB
Instead of:
Code:
incf count3,0 ; increment count3 and store value in w
movwf PortB ; mov w value on portB

try
Code:
      incf count3,1 ; increment count3 and store value in f
      movf count3, w
      movwf PortB ; mov w value on portB
 
Also, please use 'F' and 'W' instead of '1' and '0' in the operand, please...

I agree, and I don't know why neither of us said:" Use the include file provided by Microchip, rather than do your own equates for registers." :)
 
I agree, and I don't know why neither of us said:" Use the include file provided by Microchip, rather than do your own equates for registers." :)
I only picked up on the '0' and '1' in the operands in your correctly formatted code excerpt. I'm afraid I didn't even look at the OP's listing since it wasn't formatted.

Mike
 
hi Mike,
Just had to use Pommies cody tidy.:)

Code:
status		equ	03h
PortB		equ	06h
TRISB		equ	86h
count1		equ	22h
count2		equ	23h
count3		equ	24h
;************************************************* **********  
		org	10h
		bsf	status,RP0	; bank1
		movlw	B'00000000'
		movwf	TRISB		; use portB as output
		bcf	status,RP0	; bank0

start

		incf	count3,0	; increment count3 and store value in w
		movwf	PortB		; mov w value on portB
		call	repeat		; repeat calls delay 5x to make it longer
		goto	start

repeat
		call	delay
		call	delay
		call	delay
		call	delay
		call	delay
		return	 

delay
		movlw	D'255'
		movwf	count1
loop1
		movlw	D'255'
		movwf	count2
loop2
		nop
		decfsz	count2,1
		goto	loop2
		decfsz	count1,1
		goto	loop1
		return
		end
 
Thanx guyz i got it now! the pic is counting!:), now i need to make it count up an down! thanx again!!!!!
 
Then make sure you have the _LVP_OFF in the config line.

Ahh where is your __CONFIG ? also an include file is a good idea.
 
Last edited:
Something like this
Code:
        list    p=16F877
        include <p16F877.inc>
        __CONFIG        _XT_OSC & _LVP_OFF & _WDT_OFF

count1  equ     0x20
count2  equ     0x21
count3  equ     0x22

    org    0
    bsf    STATUS,RP0    ; bank1
    movlw    B'00000000'
    movwf    TRISB        ; use portB as output
    bcf    STATUS,RP0    ; bank0

start
    incf    count3,0    ; increment count3 and store value in w
    movwf    PORTB        ; mov w value on portB
    call    repeat        ; repeat calls delay 5x to make it longer
    goto    start

repeat    
        call    delay
    call    delay
    call    delay
    call    delay
    call    delay
    return     
delay
    movlw    D'255'
    movwf    count1
loop1
    movlw    D'255'
    movwf    count2
loop2
    nop
    decfsz    count2,1
    goto    loop2
    decfsz    count1,1
    goto    loop1
    return
    end
 
Status
Not open for further replies.

Latest threads

Back
Top