birdman0_o
Active Member
Hey guys, I just can't seem to make it work!
It's running 1s faster than it should be every minute!!!
Here's my code, maybe you can spot something out!
32.768Khz Osc, Two 10pF caps.
It's running 1s faster than it should be every minute!!!
Here's my code, maybe you can spot something out!
32.768Khz Osc, Two 10pF caps.
Code:
;*************************************
; Author : Mike Baird
; Program : Clock
; Date : October 23rd,2009
;*************************************
List P=16F882
#include "P16F882.INC"
__CONFIG _CONFIG1, _PWRTE_ON & _WDT_OFF & _INTOSC & _BOR_OFF & _LVP_OFF & _CP_OFF & _MCLRE_OFF
__CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF
;*** Cblock ***
CBLOCK 0x20
d1 ;
d2 ;
SecLow ;
SecHigh ;
MinLow ;
MinHigh ;
HourLow ;
HourHigh ;
MinL ;
MinH ;
HourL ;
HourH ;
W_TEMP ;
STATUS_TEMP ;
X ;
TEST ;
ENDC
;*** Defines ***
#Define Bits PORTB,2
#Define Clock PORTB,1
#Define Latch PORTB,0
#Define MLow PORTC,4
#Define MHigh PORTC,5
#Define HLow PORTC,6
#Define HHigh PORTC,7
;*** Macro ***
HC595 MACRO Var,Var1
Local Loop ; Local Label
MOVLW .8 ; Transfer 8 bits
MOVWF Var1 ; Initializing counter
Loop
RLF Var,f ; Rotate "Var" one place to the left
BTFSS STATUS,C ; Is carry 1?
BCF Bits ; If not set data line to 0
BTFSC STATUS,C ; Is carry 0?
BSF Bits ; If not set data line to 1
BSF Clock ; Generate one clock
BCF Clock ;
DECFSZ Var1,f ; Has 8 bits been sent?
GOTO Loop ; If not, repeat
BSF Latch ; If all 8 bits have been sent, set latch
BCF Latch ;
RLF Var,f ; Restore to Orginal
ENDM
;*** START OF RAM ***
ORG 0x000 ; Start of program vector
GOTO Start ;
ORG 0x004 ; Interrupt vector
;*** Interrupt Service Routine ***
ISR:
MOVWF W_TEMP ;
SWAPF STATUS,W ; Context Saving
MOVWF STATUS_TEMP ;
BCF PIR1,TMR1IF ; Clear Interupt Flag
BSF TMR1H,7 ; Reload the start up value into the timer
;*** Code Insert ***
; INCF SecLow
; MOVF SecLow,W
; SUBLW .10
; BTFSS STATUS,Z
; GOTO Exit
; CLRF SecLow
; INCF SecHigh
; MOVF SecHigh,W
; SUBLW .6
; BTFSS STATUS,Z
; GOTO Exit
; CLRF SecHigh
INCF MinLow
MOVF MinLow,W
SUBLW .10
BTFSS STATUS,Z
GOTO Exit
CLRF MinLow
INCF MinHigh
MOVF MinHigh,W
SUBLW .6
BTFSS STATUS,Z
GOTO Exit
CLRF MinHigh
INCF HourLow
BTFSS TEST,1
GOTO TwoHours
MOVF HourLow,W
SUBLW .10
BTFSS STATUS,Z
GOTO Exit
GOTO $+5
TwoHours
MOVF HourLow,W
SUBLW .3
BTFSS STATUS,Z
GOTO Exit
BTFSC TEST,1
GOTO One
Blank
MOVLW .1
MOVWF HourLow
MOVLW .10
MOVWF HourHigh
BSF TEST,1
GOTO Exit
One
CLRF HourLow
MOVLW .1
MOVWF HourHigh
BCF TEST,1
;*** End of Insert ***
Exit
SWAPF STATUS_TEMP,W ; Restore Status and W
MOVWF STATUS ;
SWAPF W_TEMP,F ;
SWAPF W_TEMP,W ;
RETFIE ;
;*** End of ISR ***
Start
CLRF PORTA ; PortA all low
CLRF PORTB ; PortB all low
BSF STATUS,RP0 ; Bank 1
CLRF TRISA ; PortA all output except MCLR
CLRF TRISB ; Move to PortB
CLRF TRISC
BSF TRISC,0 ; Osc inputs
BSF TRISC,1
BSF PIR1,TMR1IE
BCF STATUS,RP0 ; Bank 0
BANKSEL ANSEL
CLRF ANSEL
CLRF ANSELH
BANKSEL PORTA
;*** Timer1 set up ***
BCF T1CON,6
BCF T1CON,4 ; 1,2,4,8 prescaler
BCF T1CON,5 ;
BSF INTCON,6 ; Enable all unmasked interrupts
BSF INTCON,7 ; Enable Global interrupts
BSF TMR1H,7 ; Load a start up value into the timer
; MOVLW b’00001111’ ; Configure for external clock, from pic16f88 datasheet
; MOVWF T1CON ; Asynchronous operation, external oscillator
BSF T1CON,T1OSCEN ;3 ; Timer1 External Clock Input Synchronization Control bit
BSF T1CON,T1SYNC ;2 ; Sync to external clock
BSF T1CON,TMR1CS ;1 ; External clock from pin RB6/T1OSO/T1CKI/PGC (on the rising edge)
BCF PIR1,0 ; Reset interupt flag
BSF T1CON,0 ;0 ; Start the timer
MOVLW d'0'
MOVWF SecLow
MOVLW d'0'
MOVWF SecHigh
MOVLW d'0'
MOVWF MinLow
MOVLW d'0'
MOVWF MinHigh
MOVLW d'2'
MOVWF HourLow
MOVLW d'1'
MOVWF HourHigh
CLRF TEST
Main:
Call TurnOffNumbers
MOVF MinLow,W
Call Table
MOVWF MinL
HC595 MinL,X
BSF MLow
Call Delay
Call TurnOffNumbers
MOVF MinHigh,W
Call Table
MOVWF MinH
HC595 MinH,X
BSF MHigh
Call Delay
Call TurnOffNumbers
MOVF HourLow,W
Call Table
MOVWF HourL
HC595 HourL,X
BSF HLow
Call Delay
Call TurnOffNumbers
MOVF HourHigh,W
Call Table
MOVWF HourH
HC595 HourH,X
BSF HHigh
Call Delay
GOTO Main
TurnOffNumbers:
BCF MLow
BCF MHigh
BCF HLow
BCF HHigh
RETURN
Table:
ADDWF PCL
RETLW b'00111111' ; 0 (3)
RETLW b'00000110' ; 1 (4)
RETLW b'01011011' ; 2 (5)
RETLW b'01001111' ; 3 (6)
RETLW b'01100110' ; 4 (7)
RETLW b'01101101' ; 5 (8)
RETLW b'01111101' ; 6 (9)
RETLW b'00000111' ; 7 (10)
RETLW b'01111111' ; 8 (11)
RETLW b'01101111' ; 9 (12)
RETLW b'00000000' ; Blank
; *** Delay ***
Delay
MOVLW d'6'
MOVWF d1
D1 MOVLW d'20'
MOVWF d2
D2 DECFSZ d2,F
GOTO D2
DECFSZ d1,F
GOTO D1
RETURN
;***
END