PDA

View Full Version : A stopwatch to improve



mombasa
- 7th February 2011, 14:11
For my stopwatch I need a precision of a hundredth of a second. By using a 18f252 Timer1 driven by a quartz to 32768 hz, I try to get an interrupt every 10 ms that I should increase a variable, but I can not get the right precision although the use of instant interrupt Darrel Taylor. Could someone give me some hints with timer setting? Thanks

cncmachineguy
- 7th February 2011, 14:30
You will need to fine tune the preload value maybe. Do you have an O-scope? How do you know it is not correct? What else does the code do? Did you set your configs correctly? We really will need to know a bit more if we are going to be able to help you. We HAVE NO crystal ball!!

Acetronics2
- 7th February 2011, 14:39
Search engine ................................

Keyword : " Elapsed timer " .............................

Result Ok ................................. ;)

Alain

mombasa
- 7th February 2011, 15:12
Sorry Alain but I use in my stopwatch a ds32khz chip that drives the Timer1, and a crystal of 4 MHz for the 18f252. I believe that the 'elapsed timer using also a crystal of 4 MHz for the pic and 4 mhz/4 (1 Mhz) for Timer1. Am I wrong? Anyway I attach the listing.

'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 26/01/2011 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
@ __CONFIG _CONFIG1H,_OSCS_OFF_1H & _HS_OSC_1H
@ __CONFIG _CONFIG2L,_BOR_OFF_2L & _PWRT_ON_2L
@ __CONFIG _CONFIG2H,_WDT_OFF_2H
@ __CONFIG _CONFIG3H,_CCP2MX_OFF_3H
@ __CONFIG _CONFIG4L,_STVR_OFF_4L & _LVP_OFF_4L





'---------------SETTAGGI PORTE ----------------------

INTCON2.7=0 'ABILITA PORTB PULLUP
TRISB =%00000111
PORTB =%00000111
TRISC =%00000001


' --------------SETTAGGI VARI ----------------------------



DEFINE OSC 4 ' Clock a 4 Mhz

DEFINE LCD_BITS 4 'NUMERO LINEE DI COMANDO 4 O 8
DEFINE LCD_DREG PORTB 'PORTA ChE GESTISCE LCD
DEFINE LCD_DBIT 4 'BIT INIZIALE BUS
DEFINE LCD_RSREG PORTC 'PORTA CHE GESTISCE RS
DEFINE LCD_RSBIT 5 'BIT CHE GESTISCE RS
DEFINE LCD_EREG PORTB 'PORTA CHE GESTISCE E
DEFINE LCD_EBIT 3 'PIN CHE GESTISCE IL SEGNALE E
DEFINE LCD_LINES 2 'NUMERO LINEE LCD

Define LCD_COMMANDUS 2000
' Command Delay (uS)
Define LCD_DATAUS 50 ' Data Delay (uS)


CENT VAR BYTE
ORE VAR BYTE
MINS VAR BYTE
SEC VAR BYTE


CENT=0
ORE=0
MINS=0
SEC=0


TIMERUNO VAR WORD




INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts



ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

T1CON=%00000111




START:
IF PORTB.0=1 THEN START



timeruno=65214

TMR1H=TIMERUNO.HIGHBYTE
TMR1L=TIMERUNO.LOWBYTE

high porta.5
pause 10
low porta.5

@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts


STO:
IF CENT=100 THEN
cent=0
SEC=SEC+1
if sec=60 then
sec=0
mins=mins+1
if mins=60 then
mins=0
ore=ore+1
endif
endif
LCDOUT $FE,$80,DEC2 ore," ",dec2 mins," ",dec2 sec
ENDIF

GOTO STO

'---[INT - interrupt handler]---------------------------------------------------


ToggleLED1:

T1CON.0=0 ' Stop the Clock

TMR1H=TIMERUNO.HIGHBYTE
TMR1L=TIMERUNO.LOWBYTE

T1CON.0=1 ' Restart the Clock
CENT=CENT+1


@ INT_RETURN

Acetronics2
- 7th February 2011, 15:40
Ohhhh

not difficult to use the Low freq osc with a 32768 Hz Xtal ....instead of the Clock Osc : the mod is really easy.

I did it for my lawn tractor computer ...

Alain