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

Problem with clock program

Status
Not open for further replies.

birdman0_o

Active Member
Hey guys I wrote this program in ASM for a 12 hour clock. Unfortunately it seems to reset on a random interval (usually between 10 mins and an hour). Help me figure it out I've been working on this project for far too long!


Code:
;***************************************************************
;Project : 12 Hour Clock
;Author: Mike Baird
;Last Updated : July 6th, 2009
;
;***************************************************************
	LIST	p=16F628a
	#INCLUDE	<p16F628a.inc>
	__CONFIG	_CP_OFF & _WDT_OFF & _PWRTE_ON & _BOREN_OFF &_INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _LVP_OFF
	ERRORLEVEL	-302
;***************************************************************
	CBLOCK	0x20			; Delay files d1, d2 and d3 are at 20h, 21h and 22h
	d1
	d2
	d3
	d4
	Count1
	Count10
	Count100
	Test
	Test2
	Dummy
	W_TEMP
	STATUS_TEMP
	ENDC
; Table
;		    					lefttop = rb4
;		    __   middle = rb6   rightop = rb3   
;		   |__|  top    = rb5   leftbot = rb2 
;		   |__|  bottom = rb0	righbot = rb1
;
;0 = b'00111111'
;1 = b'00001010'
;2 = b'01101101'
;3 = b'01101011'
;4 = b'01011010'
;5 = b'01110011'
;6 = b'01110111'			
;7 = b'00101010'
;8 = b'01111111'
;9 = b'01111010'
;
;
;**************************************************************

	ORG	000h
	GOTO	init
	ORG	004h
	Goto  ISR		

INITIALIZE

	MOVLW	H'07'			; Turn off comparators
	MOVWF	CMCON			; ^
	CLRF	PORTA			; All pins low
	CLRF	PORTB			; All pins low
	BSF	STATUS,RP0			; Select bank 1
	MOVLW	B'11100000'		; Pin 7,6,5 input    Pin 6 and 7 are for time incrementers
	MOVWF	TRISA			; 
	MOVLW b'00000000'		;
	MOVWF PORTB				; 
	BSF PIE1,TMR1IE     	; Enable TMR1 interupts
	BCF	STATUS,RP0			; Select bank 0

; Timer1 set up      **********************************

	BSF T1CON,4            ;  1-8 prescaler  
	BSF T1CON,5            ; 

					
	BSF INTCON,6                   ;enable all unmasked interrupts
	BSF INTCON,7                 ;enable Global interrupts
	BCF PIR1,0                    ;reset interupt flag
	BCF T1CON,0                     ;stop the timer
	MOVLW b'00001011'				;load a start up value into the timer
	MOVWF TMR1H
	MOVLW b'11011011'
	MOVWF TMR1L
	BSF T1CON,0


; 12:00 Flashing********************* Flash 12:00 when clock starts ( "set time" )
	MOVLW d'1'
	MOVWF Count1
	MOVWF Count10
	MOVLW d'13'
	Movwf Count100	
	BSF Test,2   ; Bit to see which loop to be in (see in isr)

Flash
	BTFSC PORTA,6
	Goto MAIN1
	BTFSC PORTA,7
	Goto MAIN1

	MOVLW b'00001000'
	MOVWF PORTA
	MOVF Count1,w
	Call Table		
	MOVWF PORTB	

	Call delay1

	MOVLW b'00000100'
	MOVWF PORTA
	MOVF Count10,w
	Call Table		
	MOVWF PORTB	

	Call delay1
	
	MOVLW b'00000001'
	MOVWF PORTA
	MOVF Count100,w
	Call Table1		
	MOVWF PORTB	

	Call delay1

	MOVLW b'00000010'
	MOVWF PORTA
	MOVF Count100,w
	Call Table2		
	MOVWF PORTB	

	Call delay1

	GOTO Flash


; ************************************************

ISR 
	MOVWF W_TEMP
	SWAPF STATUS,W   ; CONTEXT SAVING
	MOVWF STATUS_TEMP
	

	BCF INTCON,GIE
	BCF PIR1,0                    ;reset interupt flag
	BCF T1CON,0
	MOVLW b'00001011'				;load a start up value into the timer
	MOVWF TMR1H
	MOVLW b'11011011'
	MOVWF TMR1L
	BSF T1CON,0
	BSF INTCON,GIE 

;******* Make 12 flash loop
	BTFSC Test,2
	CLRF PORTB
	BTFSC Test,2
	Call delay4
	BTFSC Test,2
	RETFIE
;*******
;divide counter by 120
	INCF Dummy,f   ; timer1 = 2hz, dividing it in 120 makes 1/60 HZ
	MOVF Dummy,w
	SUBLW d'119'
	BTFSC STATUS,C
	RETFIE

; INCREASE MINUTE
	CLRF Dummy
	INCF Count1,f   ; INCREMENT COUNT1

;******

	SWAPF STATUS_TEMP,W
	MOVWF STATUS
	SWAPF W_TEMP,F
	SWAPF W_TEMP,W
	RETFIE

; ***********************************************

MAIN1
     BCF Test,2

MAIN

	MOVF Count1,w 
	SUBLW  d'10'                                                                                                                                                                                                                                                                                                 
	BTFSS STATUS,C	;is it 10
	Goto BLAH      


	MOVLW b'00001000'
	MOVWF PORTA
	MOVF Count1,w
	Call Table		
	MOVWF PORTB	

	Call delay1

	MOVLW b'00000100'
	MOVWF PORTA
	MOVF Count10,w
	Call Table		
	MOVWF PORTB	

	Call delay1
	
	MOVLW b'00000001'
	MOVWF PORTA
	MOVF Count100,w
	Call Table1		
	MOVWF PORTB	

	Call delay1

	MOVLW b'00000010'
	MOVWF PORTA
	MOVF Count100,w
	Call Table2		
	MOVWF PORTB	

	Call delay1

	BTFSC PORTA,6
	CALL INCMIN
	BTFSC PORTA,7
	CALL INCHOUR

	GOTO MAIN

; NUMBER FIXER **************************************************************************

BLAH 
	MOVLW 1     ;RESET COUNT1
	MOVWF Count1	
 
	MOVF Count10,w
	SUBLW d'5'
	BTFSS STATUS,C	; is it 5?
	GOTO BLAH2
    INCF Count10,f  ; INCREMENT COUNT10
	GOTO MAIN

BLAH2
	MOVLW d'1'     ;RESET COUNT1
	MOVWF Count10	
	
	MOVF Count100,w
	SUBLW d'13'
	BTFSS STATUS,C	; is it 14?
	GOTO BLAH3
    INCF Count100,f  ; INCREMENT COUNT100
	GOTO MAIN

BLAH3
	MOVLW 2
	MOVWF Count100
	GOTO MAIN



;*****BUTTON CONTROLS****************************************************************

INCHOUR
	CLRF PORTB
	MOVLW	d'255'
	MOVWF	d3
	MOVLW	d'15'
	MOVWF	d4
	Call delay4
	INCF Count1,f
	Return

INCMIN
	CLRF PORTB
	MOVLW	d'255'
	MOVWF	d3
	MOVLW	d'200'
	MOVWF	d4
	Call delay4
	INCF Count1,f
	Return
;*****************************

Table
	ADDWF PCL,f
	retlw	0	; blank
	retlw   b'00111111' ;0
	retlw	b'00001010'	;1
	retlw	b'01101101'	;2
	retlw	b'01101011'	;3
	retlw	b'01011010'	;4
	retlw	b'01110011' ;5
	retlw   b'01110111' ;6
	retlw	b'00101010'	;7
	retlw	b'01111111'	;8
	retlw	b'01111011'	;9
	Goto MAIN

Table1
	ADDWF PCL,f
	retlw	0	; blank
	retlw   b'00111111' ;0
	retlw	b'00001010'	;1
	retlw	b'01101101'	;2
	retlw	b'01101011'	;3
	retlw	b'01011010'	;4
	retlw	b'01110011' ;5
	retlw   b'01110111' ;6
	retlw	b'00101010'	;7
	retlw	b'01111111'	;8
	retlw	b'01111011'	;9
	retlw   b'00111111' ;10
	retlw	b'00001010'	;11
	retlw	b'01101101'	;12
	Goto BLAH3

Table2
	ADDWF PCL,f
	retlw	0	; blank
	retlw   0 ;0
	retlw	0	;1
	retlw	0	;2
	retlw	0	;3
	retlw	0   ;4
	retlw	0 ;5
	retlw 	0 ;6
	retlw	0	;7
	retlw	0	;8
	retlw	0	;9
	retlw	b'00001010'	;10
	retlw	b'00001010'	;11
	retlw	b'00001010'	;12


;*********************************************************

delay1	
	MOVLW	d'5'
	MOVWF	d1
	MOVLW	d'3'
	MOVWF	d2
delay2	
	DECFSZ	d1,F
	GOTO	delay2
	DECFSZ	d2,F
	GOTO	delay2
	Return

delay4	
	DECFSZ	d3,F
	GOTO	delay4
	DECFSZ	d4,F
	GOTO	delay4
	Return



	END
 

Pommie

Well-Known Member
Most Helpful Member
You are occasionally returning from the ISR without restoring W and STATUS.

Code:
;******* Make 12 flash loop  
		btfsc	Test,2
		clrf	PORTB
		btfsc	Test,2
		call	delay4
		btfsc	Test,2
		[COLOR="Red"]retfie[/COLOR]  ;<----eeek
;*******
;divide counter by 120 
		incf	Dummy,f		; timer1 = 2hz, dividing it in 120 makes 1/60 HZ
		movf	Dummy,w
		sublw	d'119'
		btfsc	STATUS,C
		[COLOR="Red"]retfie[/COLOR]  ;<----eeek

; INCREASE MINUTE 
		clrf	Dummy
		incf	Count1,f	; INCREMENT COUNT1

;******

		swapf	STATUS_TEMP,W
		movwf	STATUS
		swapf	W_TEMP,F
		swapf	W_TEMP,W
		retfie
Mike.
 

Pommie

Well-Known Member
Most Helpful Member
The easiest way to fix it is,
Code:
;******* Make 12 flash loop  
		btfsc	Test,2
		clrf	PORTB
		btfsc	Test,2
		call	delay4
		btfsc	Test,2
		goto	ExitISR
;*******
;divide counter by 120 
		incf	Dummy,f		; timer1 = 2hz, dividing it in 120 makes 1/60 HZ
		movf	Dummy,w
		sublw	d'119'
		btfsc	STATUS,C
		goto	ExitISR

; INCREASE MINUTE 
		clrf	Dummy
		incf	Count1,f	; INCREMENT COUNT1

;******
ExitISR
		swapf	STATUS_TEMP,W
		movwf	STATUS
		swapf	W_TEMP,F
		swapf	W_TEMP,W
		retfie
BTW, sorry about the case change - I have a program that tidies code for me and it does it automatically.

Mike.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top