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.

PIC delay?

Status
Not open for further replies.

mouse9911

New Member
Hello everyone,
I am trying to make a simple timer for ages now. I have aPIC16877A chip im using. I have a program that is a binary counter that shows up on LED's that i have attached to the PORTB pins. The counter works fine when i use the sleep call, but as soon as i replace the sleep call with my or any online delay routine the counter only seems to run for part way and then somehow the program starts over without ever completing the code. I know this because at the end of the counter program upon termination the program will flash and then do a sleep call. The current delay routine i am using from online is here

delay ; 2 cycles for call
clrf DE1 ; 1 cycle to clear counter variable
Dloop
nop
decfsz DE1,F ; These two instructions provide a
goto Dloop ; (256 * 3) -1 cycle count
return ; 2 cycles for return

i got the code from this page, **broken link removed** i have setup as the following in my program

DE1 equ 25h

Any ideas as to why the delay above dosent work? it seems to work but then the program resets itself after only part way through the counting. Replacing the one line the program that calls the delay with sleep fixes the problem but i would like to use my own delay routine.
 
Last edited:
This is my overall code:
INVERTED equ 20h
COUNT equ 21h
DE1 equ 25h
DE2 equ 26h
include <p16F877A.inc>
__CONFIG 0x3F7D
org 0
bsf STATUS, RP0
movlw h'00'
movwf TRISB
movlw b'00001110'
movwf OPTION_REG
bcf STATUS, RP0
movlw h'FF'
movwf COUNT
call counter
goto eop

counter
loop call invert
movf INVERTED,0
movwf PORTB
sleep
decfsz COUNT
goto loop
RETURN

invert
movlw h'00'
movwf INVERTED
BTFSC COUNT,0 ;set the right bits in portb
BSF INVERTED, 7
BTFSC COUNT,1
BSF INVERTED, 6
BTFSC COUNT,2
BSF INVERTED, 5
BTFSC COUNT,3
BSF INVERTED, 4
BTFSC COUNT,4
BSF INVERTED, 3
BTFSC COUNT,5
BSF INVERTED, 2
BTFSC COUNT,6
BSF INVERTED, 1
BTFSC COUNT,7
BSF INVERTED, 0
RETURN

delay ; 2 cycles for call
clrf DE1 ; 1 cycle to clear counter variable
Dloop decfsz DE1,F ; These two instructions provide a
goto Dloop ; (256 * 3) -1 cycle count
return ; 2 cycles for return

eop movlw h'00'
movwf PORTB
sleep
movlw h'FF'
movwf PORTB
sleep
end
 
Last edited:
You have the watchdog timer on. Change your config to 0x3F79 to turn it off.
You are also using the sleep instruction. What are you expecting to wake the chip up?

Mike.
 
I dont have a schematic, its a very simple circuit that i have attached to the chip. Here is a picture of what i have.
 

Attachments

  • hpim2680.jpg
    hpim2680.jpg
    54.7 KB · Views: 130
:) Thank you, sorry im really new to this. I thought i saw something about the watchdog timmer but i couldnt find any info on it in the data sheet for the PIC16F877A, thanks so much. Now it all makes sense :) Including the sleep part. Do you by any chance know where there are any good tutorials? or possibly a more detailed explanation of the assembly language then is in the data sheet? I have never programmed PIC before this, i have never sodiered before :S but i think its getting better :) Thanks again!
 
Try google....
There are more PIC links and tutorials on google than you will ever be able to read in your lifetime.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top