PDA

View Full Version : Crazy things



mombasa
- 4th February 2011, 16:57
Something happens that is driving me crazy: I have customized the Olympic Timer by Melanie, but the time I get are delayed by 7.5 times. I specify that I use the pic 18f252 using a 4 MHz crystal, and that all settings of Tmr1 are the same as the standard program. I measured the crystal frequency of 4 MHz and it is exactly. Could someone give me some advice? Thanks

Acetronics2
- 4th February 2011, 17:31
Hi, Mombasa



I have customized the Olympic Timer by Melanie
...
Could someone give me some advice


Sorry, my crystal ball is to be repaired ...

try to SHOW us your mods !!! :rolleyes:

IF just changing processor ... you MUST change the config lines for '452 ...
not so simple as 16F876 ...

Sooo, everything is in that f .... datasheet ... once more !!! ;)


Alain

mombasa
- 4th February 2011, 21:00
Acetronics Thanks for your interest, of course I have adapted the settings to the new pic, by the way I enclose the listing:



' Olympic Timer
' =============
' Melanie Newman
' 05/Aug/2004
' Topical Program demonstrates use of Interrupt
' Driven Background TIMER, to time events down to
' one one-hundredth of a Second (1/100 Sec).

' Bonus CALIBRATION Feature allows simple adjustments
' in 360mS steps per hour. This calibration adjustment
' range is limited to +/- 36 seconds per Hour.

' This program is for 4MHz clock (1uS Timer Ticks).

' PIC Defines
' ===========

@ __CONFIG _CONFIG1H,_OSCS_OFF_1H & _XT_OSC_1H
@ __CONFIG _CONFIG2L,_BOR_ON_2L & _PWRT_ON_2L
@ __CONFIG _CONFIG2H,_WDT_ON_2H
@ __CONFIG _CONFIG3H,_CCP2MX_OFF_3H
@ __CONFIG _CONFIG4L,_STVR_OFF_4L & _LVP_OFF_4L




DEFINE OSC 4 ' Clock a 4 Mhz
DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
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)


' Control Buttons/Lines
' ---------------------
ButStart var PortB.0 ' Take this pin low momentarily to START timing
ButStop var PortB.1 ' Take this pin low momentarily to STOP timing
ButReset var PortB.2 ' Take this pin low momentarily to RESET clock

' Hold the RESET Button pressed for at least FIVE seconds
' to jump into CALIBRATION Mode

' Software Defines
' ----------------
BannerOffset var BYTE ' Variable holding start address of Banner Display
CounterA var BYTE ' Just a Counter
CounterB var BYTE ' Just a Counter
CounterC var BYTE
DataA var BYTE
Hours var BYTE
Hundredths var BYTE
Minutes var BYTE
OverflowError var BIT
RunningFlag var BIT
Seconds var BYTE
SetupTimeOut var WORD ' Timeout counter for Calibration/Set-Up Mode
TMR1Cal var BYTE ' Calibration Value

TMR1CalAR var Byte ' Calibration 0=ADVANCE, 1=RETARD
TMR1RunOn var WORD ' variable holding TMR1 Run-On value

' EEPROM Presets
' --------------
Data @0,0 ' Advance/Retard Indicator
Data 0 ' Calibration Value
Data "Olympic Timer Powered by MeLabs PICBasic Pro"

' Software Constants
' ------------------
TMR1CalMax con 100 ' Maximum adjustment (+/-100uS per 10mS interrupt)
TMR1Preset con $D910 ' 10mS Timer Reload value, offset by 20uS
' to allow for TMR1 Setting Calculations

' Start Program
' =============

' Initialise Processor
' --------------------
TRISA=%00000000
TRISB=%00000111
TRISC=%00000000
ADCON0=%11000000
ADCON1=%00000111
' OPTION_REG.7=0 ' Enable Pull-Up's
RunningFlag=0 ' Disable actual Interrupt Time-Keeping
Pause 1000 ' Pause for LCD to initialise

' Silly Intro Banner just for Fun
' -------------------------------
LCDOut $FE,1 ' Clear LCD
BannerOffset=2:Gosub DisplayBanner
Pause 2000
For CounterA=0 to 30
BannerOffset=2+CounterA
Gosub DisplayBanner
Pause 150
Next CounterA
Pause 1000

' Initialise TMR1 Interrupts
' --------------------------
Gosub SetTimer ' Set the Timer for next 10mS Interrupt
On Interrupt goto TickCount
PIE1.0=1 ' Enable TMR1 Interrupts
INTCON.6=1 ' Enable all unmasked Interrupts
INTCON.7=1 ' Enable Global Interrupts

' -----------------------------------------------------
' Following the above "On Interrupt", no Basic Command
' is allowed that takes more than 10mS to execute
' otherwise the 10mS Interrupt interval is compromised.
' -----------------------------------------------------

' Reset Timer Variables for Start
' -------------------------------
DisplayReset:
LCDOut $FE,1 ' Clear LCD
Read 0,TMR1CalAR ' Read Calibration Advance/Retard Indicator
Read 1,TMR1Cal ' Read Calibration Value
Hundredths=0 ' Reset Timer Counter variables
Seconds=0
Minutes=0
Hours=0
OverflowError=0

' Main Program Loop
' =================
Enable
DisplayLoop:
If ButStart=0 then RunningFlag=1
If ButStop=0 then RunningFlag=0
LCDOut $FE,$80,DEC2 Hours,":",DEC2 Minutes,":",DEC2 Seconds,".",DEC2 Hundredths

If OverflowError=1 then
If Seconds.0=1 then
LCDOut $FE,$8C,"ERR"
else
LCDOut $FE,$8C," "
endif
endif
If RunningFlag=1 then goto DisplayLoop
If ButReset=1 then goto DisplayLoop
Disable

' Reset Clock
' ===========
' Momentarily Press the Reset Button for RESET action.
' Continue holding the Reset Button for MORE than 5 seconds
' to jump into Calibration/Set-Up Mode

ResetClock:
LCDOut $FE,1,"Reset OK"
Pause 1000
Seconds=1
While Seconds < 5
Pause 1000
If ButReset=1 then goto DisplayReset
Seconds=Seconds+1
Wend

' Calibration Adjustment
' ======================
' If No Button is Pressed for 20 Seconds, then the program
' will automatically exit Calibration/Set-Up Mode WITHOUT saving
' any new values.

SetUpTimeout=0
Calibration:
LCDOut $FE,1,"Calibrate: "
While ButReset=0:Wend ' Wait for User to release finger
CalibrationLoop:
LCDOut $FE,$8B
If TMR1Cal=0 then
LCDOut " "
else
If TMR1CalAR=0 then
LCDOut "+"
else
LCDOut "-"
endif
endif
LCDOut #TMR1Cal," "
' ----------------------------------------------------------
' Press Start Button to ADVANCE (speed-up) Clock
' Press STOP Button to RETARD (slow-down) Clock
' Press RESET Button to SAVE new Calibration Setting
' ----------------------------------------------------------
' Remember each Calibration 'tick' will advance or
' retard the Timing by 1uS in every 10mS period - that's
' 360mS/Hour per setting. Example: A setting of +8 will
' SPEED-UP the Timer by 2.88 Seconds (8 x 360mS) in an Hour.
' ----------------------------------------------------------
If TMR1CalAR=0 then
If ButStart=0 then Gosub CalAdvance
If ButStop=0 then Gosub CalRetard
else
If ButStart=0 then Gosub CalRetard
If ButStop=0 then Gosub CalAdvance
endif
If ButReset=0 then
Write 0,TMR1CalAR
Write 1,TMR1Cal
LCDOut $FE,1,"Have a Nice Day"
Pause 1000
Goto DisplayReset
endif
SetupTimeout=SetupTimeout+1
If SetupTimeout>200 then goto DisplayReset
Pause 100
Goto CalibrationLoop


' Subroutine Increments Calibration Value
' ---------------------------------------
CalAdvance:
SetupTimeout=0
If TMR1Cal=>TMR1CalMax then
TMR1Cal=TMR1cALmAX
TMR1CalAR=TMR1CalAR^1
else
TMR1Cal=TMR1Cal+1
endif
Return

' Subroutine Decrements Calibration Value
' ---------------------------------------
CalRetard:
SetupTimeout=0
If TMR1Cal=0 then
TMR1Cal=1
TMR1CalAR=TMR1CalAR^1
else
TMR1Cal=TMR1Cal-1
endif
Return

' Subroutine Displays Banner Intro
' --------------------------------
DisplayBanner:
CounterC=BannerOffset+15
LCDOut $FE,$80
For CounterB=BannerOffset to CounterC
Read CounterB,DataA
LCDOut DataA
Next CounterB
Return

' Subroutine Loads TMR1 values
' ============================
SetTimer:
T1CON.0=0 ' Stop the Clock
TMR1RunOn.Highbyte=TMR1H ' Load the Run-On (Over-Run) value (if any)
TMR1RunOn.Lowbyte=TMR1L
TMR1RunOn=TMR1Preset+TMR1RunOn
' Calculate the New (adjusted) value for TMR1
If TMR1CalAR=0 then ' Calibration ADVANCE (add) or RETARD (subtract)
TMR1RunOn=TMR1RunOn+TMR1Cal
else
TMR1RunOn=TMR1RunOn-TMR1Cal
endif
TMR1H=TMR1RunOn.Highbyte ' Save new values to TMR1
TMR1L=TMR1RunOn.Lowbyte
T1CON.0=1 ' Restart the Clock
PIR1.0=0 ' Reset TMR1's Interupt Flag
Return

' Timer Interrupt Handler
' =======================
TickCount:
Gosub SetTimer ' Set the Timer for next 10mS Interrupt
If RunningFlag=1 then ' If timing actually enabled... then...
Hundredths=Hundredths+1
' Increment 10mS Seconds Counter
If Hundredths>99 then
Hundredths=0
Seconds=Seconds+1
' Increment the Seconds
If Seconds>59 then
Seconds=0
Minutes=Minutes+1
' Increment the Minutes
If Minutes>59 then
Minutes=0
Hours=Hours+1
' Increment the Hours

If Hours>99 then
' Handle any Overflow
Hours=0
OverFlowError=1
endif
endif
endif
endif
endif
Resume
End