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 16F84A 1 second delay help

Status
Not open for further replies.

Blueprint

New Member
Hi, I'm wondering if you can help me. I'm a total beginner with PIC programming.

I've read many tutorials, but just can't seem to get this working right. I want to make a binary clock, simply counting seconds for now, but in the end going on to cound just hours and minutes. I'm using a Velleman K8048 PIC programming kit, which utilises 6 LED's connected to RB0-RB5, and four Switches connected to RA0-RA3 (using a PIC16F84A).

Basically, I can get the LED's (1-4) counting in binary from 0 to 10, but it's going waaay too fast. Here's my code (WARNING I'm totally crap so please don't laugh at me):

Code:
		processor	pic16f84a
		include		"p16f84a.inc"
		__config	_XT_OSC & _WDT_OFF & _PWRTE_ON

;----- Register Files------------------------------------------------------

INDF                         EQU     H'0000'
TMR0                         EQU     H'0001'
PCL                          EQU     H'0002'
STATUS                       EQU     H'0003'
FSR                          EQU     H'0004'
PORTA                        EQU     H'0005'
PORTB                        EQU     H'0006'

INTCON                       EQU     H'000B'
OPTION_REG                   EQU     H'0081'
TRISA                        EQU     H'0085'
TRISB                        EQU     H'0086'
CMCON                        EQU     H'001F'

;----- STATUS Bits --------------------------------------------------------
IRP                          EQU     H'0007'
RP1                          EQU     H'0006'
RP0                          EQU     H'0005'
NOT_TO                       EQU     H'0004'
NOT_PD                       EQU     H'0003'
Z                            EQU     H'0002'
DC                           EQU     H'0001'
C                            EQU     H'0000'


;==========================================================================
;       Variable Definition
;==========================================================================
TIMER1		EQU	H'20'		;Used in delay routine
TIMER2		EQU	H'21'		; "	"	"	
PATERN		EQU	H'22'		;Pattern data for effect's
NUM0		EQU B'00000000'
NUM1		EQU B'00000001'
NUM2		EQU B'00000010'
NUM3		EQU B'00000011'
NUM4		EQU B'00000100'
NUM5		EQU B'00000101'
NUM6		EQU B'00000110'
NUM7		EQU B'00000111'
NUM8		EQU B'00001000'
NUM9		EQU B'00001001'

		ORG	0		;Reset vector address
		GOTO	RESET		;goto RESET routine when boot.


;		*********************************************
;		*  Example of a delay routine               *
;		*********************************************

DELAY_ROUTINE   MOVLW   D'255'         ;54 Generate approx 10mS delay at 4Mhz CLK
                MOVWF   TIMER2
DEL_LOOP1       MOVLW   D'255'	       ;60	
                MOVWF   TIMER1
DEL_LOOP2       DECFSZ  TIMER1,F
                GOTO    DEL_LOOP2
                DECFSZ  TIMER2,F
                GOTO    DEL_LOOP1
		RETLW   0


;	       **********************************
;              **  RESET :  main boot routine  **
;              **********************************

RESET		MOVLW	B'00000111'	;Disable Comparator module's
		MOVWF	CMCON
		;
		BSF	STATUS,RP0	;Switch to register bank 1
					;Disable pull-ups
					;INT on rising edge
					;TMR0 to CLKOUT
					;TMR0 Incr low2high trans.
					;Prescaler assign to Timer0
					;Prescaler rate is 1:256
		MOVLW	B'11010111'	;Set PIC options (See datasheet).
		MOVWF	OPTION_REG	;Write the OPTION register.
		;
		CLRF	INTCON		;Disable interrupts
		MOVLW	B'11000000'
		MOVWF	TRISB		;RB7 & RB6 are inputs.
					;RB5...RB0 are outputs.
		MOVLW	B'11111111'	;all RA ports are inputs
		MOVWF	TRISA
		BCF	STATUS,RP0	;Switch Back to reg. Bank 0
		CLRF	PORTB		
		;

EFFECT_1
		MOVLW	NUM0	;BINARY 0
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		MOVLW	NUM1	;BINARY 1
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		MOVLW	NUM2	;BINARY 2
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		MOVLW	NUM3	;BINARY 3
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		MOVLW	NUM4	;BINARY 4
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		MOVLW	NUM5	;BINARY 5
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		MOVLW	NUM6	;BINARY 6
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		MOVLW	NUM7	;BINARY 7
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		MOVLW	NUM8	;BINARY 8
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		MOVLW	NUM9	;BINARY 9
		MOVWF	PORTB
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		nop
		CALL	DELAY_ROUTINE
		GOTO	EFFECT_1
		END

I know there's too many nop's but I just got frustrated in the end :(. I have found 2 pieces of code which I'd love to use instead, but they don't even work at all with my set-up. They are located at the following URL's:

http://techref.massmind.org/techref/piclist/cheapic/binclock.htm
http://techref.massmind.org/techref/piclist/cheapic/bincnt.htm

Any help would be much appreciated. In fact, I'd prefer it if someone could tell me why the binary clock located at the 1st URL i gave you ^ won't work with my PIC16F84A.

Blueprint.
 
Your delay is approximately 0.2 seconds. So you could change your main loop to something like.

Code:
EFFECT_1 
      CALL   DELAY_ROUTINE 
      CALL   DELAY_ROUTINE 
      CALL   DELAY_ROUTINE 
      CALL   DELAY_ROUTINE 
      CALL   DELAY_ROUTINE 
      INCF   PATERN,F
      MOVF   PATERN,W
      SUBLW  0Ah
      BTFSC  STATUS,Z
      CLRF   PATERN
      MOVF   PATERN,W
      MOVWF  PORTB 
      GOTO   EFFECT_1

I noted that you already had a variable called PATERN and figured that you intended to do something similar to the above.

I had a quick look at the link you posted and the first thing I noticed was the capacitors from the crystal go to +4.5V instead of 0V.

Mike.
 
Your software is configured for XT_OSC. You are proabably using a 4Mhz crystal, which makes it run fast. The circuit in the url uses a 32Khz crystal which requires a low power crystal configuration (_LP_OSC).
 
i dont see why you have defined zero through nine , which are natural binary numbers??
and really dont need to be defined..
what have you got working so far?
 
Hm, i am also not really good at it. Just share what i know. First, for 16f84a you do not need to disable the comparator unless you are using chips like 16f628a.

Then since you include 16f84a lib file, you do not need to redifine the registers files and status bits

Your main EFFECT_1 code can be simplified. First just define a counter.

Code:
COUNTER   EQU   H'22'

          clrf    COUNTER

Then your main code look like this.

Code:
EFFECT_1 
                movf    COUNTER,w
                movwf  portb
                incf      COUNTER,f
                movlw  d'10'
                subwf   COUNTER,w
                btfsc     status,z
                clrf       COUNTER
                nop
                nop
                nop
                call   DELAY_ROUTINE
                goto   EFFECT_1
                end

The above code should works exactly like your previous one except it is more compact usings conditional statement. Never really tried it though, test and tell me if it works for you.

Almost forgot to tell you, you may delete all your NUMX variable. It is not used.

Basically, I can get the LED's (1-4) counting in binary from 0 to 10, but it's going waaay too fast.

Thats is because your delay function is not doing a 1 sec delay. Here is a webbie to calculate your delay https://www.golovchenko.org/cgi-bin/delay
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top