You want exactly 10msec and it's set for 8.192msec right now? Replace the crystal you're using right now to 81.92% of the value. For example, if it is 4.000Mhz right now, then replace it with a 3.2768Mhz crystal.
' PicBasic Pro program that demonstrates the use of the Timer1
' interrupt for a real-time clock. Written for the LAB-X1
' experimenter board with a 16F877
DEFINE LOADER_USED 1
' Define interrupt handler
DEFINE INTHAND myint
wsave VAR BYTE $20 system
wsave1 VAR BYTE $a0 system ' Necessary for devices with RAM in bank1
wsave2 VAR BYTE $120 system ' Necessary for devices with RAM in bank2
wsave3 VAR BYTE $1a0 system ' Necessary for devices with RAM in bank3
ssave VAR BYTE bank0 system
psave VAR BYTE bank0 system
TICK VAR BYTE bank0 ' make sure that the variables are in bank 0 if they are to be used in the interrupt handler
seconds VAR BYTE ' Elapsed seconds
minutes VAR WORD ' Elapsed minutes
minutes = 0 ' Clear time
seconds = 0
T1CON = $01 ' Turn on Timer1, prescaler = 1
INTCON = $C0 ' Enable global interrupts, peripheral interrupts
PIE1 = $01 ' Enable TMR1 overflow interrupt
GoTo main ' jump over the interrupt handler and sub
' Assembly language interrupt handler
Asm
myint
; Uncomment the following if the device has less than 2k of code space
;movwf wsave ; Save W
;swapf STATUS, W ; Swap STATUS to W (swap avoids changing STATUS)
;clrf STATUS ; Clear STATUS
;movwf ssave ; Save swapped STATUS
;movf PCLATH, W ; Move PCLATH to W
;movwf psave ; Save PCLATH
; Set the high register of Timer1 to cause an interrupt every
; 16384 counts (65536-16384=49152 or $C000). At 4MHz, prescale
; set to 1, this equates to a tick every 16384uS. This works
; out to about 61 ticks per second, with a slight error. The
; error could be reduced substantially by setting the TMR1L
; register and playing with different values for the prescaler
; and the ticks per second.
movlw 0C0h ; Prepare to set TMR1 high register
movwf TMR1H ; Set TMR1H to C0h
incf _TICK,F ; INCREMENT TICK COUNT
bcf PIR1, 0 ; Clear interrupt flag
movf psave, W ; restore the state of everything
movwf PCLATH
swapf ssave, W
movwf Status
swapf wsave, F
swapf wsave, W
retfie ; Return from interrupt
EndAsm
' PicBasic subroutine to update the minutes and seconds variables
get_time:
' Update the time when needed. The TICK variable will
' overflow if you don't update within 4 seconds. This could
' be done in the interrupt handler, but it's easier to do
' it in PicBasic, and you usually want the interrupt handler
' to be as short and fast as possible.
PIE1 = 0 ' Mask the interrupt while we're messing with TICK
seconds = seconds + (tick / 61) ' Add the accumulated seconds
tick = tick // 61 ' Retain the left-over ticks
PIE1 = $01 ' Interrupt on again
minutes = minutes + (seconds / 60) ' Add the accumulated minutes
seconds = seconds // 60 ' Retain the left-over seconds
Return ' Return to the main program
main
' **************************************************************
' Begin program code here. The minutes and seconds variables can
' be used in your code. The time will be updated when you call the
' get_time routine. Disable interrupts while executing timing-critical
' commands, like serial communications.
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTE
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE
DEFINE LCD_EBIT 1
ADCON1 = 7 ' Set PORTA and PORTE for digital operation
Low PORTE.2 ' Enable the LCD
Pause 150 ' Pause to allow LCD to initialize
LCDOut $fe,1 ' Clear LCD
loops VAR WORD
loops = 0
Loop:
loops = loops + 1
LCDOut $fe,$C0,"Loops Counted: ", DEC5 loops
GoSub get_time ' Update minutes and seconds
LCDOut $fe, 2, "Time: ",DEC5 minutes, ":", DEC2 seconds ' Display the elapsed time
GoTo loop ' Repeat main loop
End
; Set the high register of Timer1 to cause an interrupt every
; 16384 counts (65536-16384=49152 or $C000). At 4MHz, prescale
; set to 1, this equates to a tick every 16384uS. This works
; out to about 61 ticks per second, with a slight error. The
; error could be reduced substantially by setting the TMR1L
; register and playing with different values for the prescaler
; and the ticks per second.
disable
tickint:
Milli = Milli + 8.192
if Milli >= 1000 then
Milli = (Milli - 1000)
Second = Second + 1
if Second = 60 then
minute = minute + 1
Second = 0
endif
endif
Hundred = Milli / 10
INTCON.2 = 0 ' Reset timer interrupt flag
resume
enable
' Assembly language interrupt handler
Asm
myint
movlw 0C0h ; Prepare to set TMR1 high register
movwf TMR1H ; Set TMR1H to C0h
incf _TICK,F ; INCREMENT TICK COUNT
bcf PIR1, 0 ; Clear interrupt flag
movf psave, W ; restore the state of everything
movwf PCLATH
swapf ssave, W
movwf Status
swapf wsave, F
swapf wsave, W
retfie ; Return from interrupt
EndAsm
incf _TICK,F ; INCREMENT TICK COUNT
movlw 0C0h ; Prepare to set TMR1 high register
movwf TMR1H ; Set TMR1H to C0h
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?