Log in

View Full Version : Solved - ELAPSED TIMER: TimerConst not defined



Demon
- 22nd August 2024, 14:31
SOLVED: See post #8:

https://www.picbasic.co.uk/forum/showthread.php/26770-ELAPSED-TIMER-TimerConst-not-defined?p=156018#post156018


Datasheet says the PIC 16F1936 has an "enhanced 16-bit TIMER1, TIMER2/4/6 are 8-bit", would that cause problems?

I used this as reference:
https://www.picbasic.co.uk/forum/showthread.php/19638-Temporary-central-repository-of-Darrel-Taylor-s-works-(including-Mr-E-s-Multicalc)?p=130275#post130275. Hopefully I didn't miss something.

My code:


@ ERRORLEVEL -301 ; turn off ADC clock ignored message
@ ERRORLEVEL -306 ; turn off crossing page boundary message

#CONFIG
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _WRT_OFF & _VCAPEN_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_LO & _LVP_OFF
#ENDCONFIG

INCLUDE "DT_INTS-14.bas"
INCLUDE "ReEnterPBP.bas"
Include "Elapsed_INT.bas"

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

DEFINE OSC 32

SPLLEN CON %1 ' PLL enable
IRCF CON %1110 ' to enable 8 MHz
SCS CON %00 ' system clock determined by FOSC
OSCCON = (SPLLEN << 7) | (IRCF << 3) | SCS

DEFINE LCD_DREG PORTB ' Set LCD data port
DEFINE LCD_DBIT 0 ' Set starting data bit
DEFINE LCD_RSREG PORTC ' Set LCD register select port
DEFINE LCD_RSBIT 5 ' Set LCD register select bit
DEFINE LCD_EREG PORTC ' Set LCD enable port
DEFINE LCD_EBIT 4 ' Set LCD enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size
DEFINE LCD_LINES 4 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 1000 ' Set command delay time in microseconds
DEFINE LCD_DATAUS 50 ' Set data delay time in microseconds

define CCP4_REG PORTC ' PWM Pulse out to LCD contrast
DEFINE CCP4_BIT 1 ' 2N2907 PNP with 1K on base
define CCP5_REG PORTC ' PWM Pulse out to LCD backlight
DEFINE CCP5_BIT 2 ' 2N2222A NPN with 1K on base

ANSELA = %00000000
ANSELB = %00000000

TRISA = %00000111
TRISB = %00110000
TRISC = %10000000
TRISE = %00000000

HPWM 2,100,1953
HPWM 1,180,1953

@ INT_ENABLE TMR1_INT ; Enable Timer 1 Interrupts
Gosub ResetTime ' Reset Time to 0d-00:00:00.00

Pause 500 ' Let PIC and LCD stabilize

LCDOUT $FE, 1 : Pauseus 1
LCDOUT $FE, $80, "ROTARY ENCODER TEST" : Pauseus 1

GOSUB StartTimer ' Start the Elapsed Timer

Mainloop:

IF SecondsChanged = 1 THEN
SecondsChanged = 0
LCDOUT $FE,2, DEC Days,"d-",DEC2 Hours,":",DEC2 Minutes,":",DEC2 Seconds
ENDIF

goto mainloop
end


PBPX 3.1.6.3
MCSP 5.0.0.5
MPASMX assembler enabled


EDIT: I've found this message "You'll need the ASM_INTS include from the original Elapsed Demo" here:

https://dt.picbasic.co.uk/CODEX/ETimer

Still looking....

Ioannis
- 22nd August 2024, 17:02
Have done some test a while back on Timers 0 and 1 on 1939 chip and all went just fine.

Ioannis

Demon
- 22nd August 2024, 20:57
Have done some test a while back on Timers 0 and 1 on 1939 chip and all went just fine.

Ioannis

Ok, well that's promising cause it also has the enhanced 16bit timer1.:)
https://ww1.microchip.com/downloads/en/DeviceDoc/40001574C.pdf

I even made sure to put the INCLUDES in the exact same order, but the error persists.

9731


It's either something totally obvious, or painfully obscure.:confused:

Demon
- 22nd August 2024, 21:08
FOUND IT!

https://www.picbasic.co.uk/forum/showthread.php/190-Elapsed-Timer-Demo?p=28683#post28683


The Elapsed-18.bas only has constants for 4,8,10,20 and 40 mhz.

Got some reading to do...

Ioannis
- 22nd August 2024, 21:16
Unfortunately I am away from any lab and cannot test again.

It will take some time till I can do that...

I recall that I did not use exactly Elapsed Timer but the DT-INTS only. Maybe that is why it worked OK. In any case it should work with Elapsed also.

Ioannis

Ioannis
- 22nd August 2024, 21:18
You beat me! Nice you found that detail! Hope it works OK now. But you use a 16F series chip not 18F.

Ioannis

Demon
- 22nd August 2024, 21:27
Of course I had to open a can of worms. :D

This thread goes into detail how Darrel's Elapsed timer is off by 1:


The value he used for each of the Oscillator rates is (1) less than it should be.
The reason is that he calculated the Timer1 counter overflow value as 65535 instead of using 65536.
Timer1 is a 16bit counter from 0 to 65535.
It takes 65536 counts to overflow (Wrap back to 0) and trigger the Timer1 interrupt.
This means that his 10ms interrupt will actually run longer by (1) instruction than it should.

https://www.picbasic.co.uk/forum/showthread.php/20013-Elapsed-Timer-findings?p=132789#post132789

Note to self: TimerCalc is in MultiCalc by Mr. E; (Timer Helper). :D

Demon
- 22nd August 2024, 21:39
9731

It's either something totally obvious, or painfully obscure.:confused:


I can see the future! :D It turns out it's a combination of totally obvious and painfully obscure. It was looking at me right in the face.


I added in Elapsed_INT.bas and it compiled without TimerConst errors.


If OSC == 32
TimerConst = 0FFB7h
EndIF

Ioannis
- 22nd August 2024, 21:43
Great! Just confirm it counts correctly now.

Ioannis

Demon
- 22nd August 2024, 22:07
It compiles, but now I have to find the correct value.

I'm now on Art's thread trying to find that out:

https://www.picbasic.co.uk/forum/showthread.php/20013-Elapsed-Timer-findings?p=156020#post156020


EDIT: Darrel talks about the Prescaler and adding NOPs here:

https://www.picbasic.co.uk/forum/showthread.php/190-Elapsed-Timer-Demo?p=69649#post69649

And that's about where my eyes start glossing over. I had also read a comment (somewhere) about Darrel using a prescaler of 2 for 64MHz, so I'm pretty sure I'm on the right track.


EDIT SOME MORE: For future me when I'm looking for the same answer, Prescaler is controlled in T1CON.

Demon
- 23rd August 2024, 02:06
Ok, I couldn't figure out how to incorporate Prescaler in Art's formula, so I took another route:

- used Preload from 16MHz
- set Prescaler to 1:2

Counter on LCD worked as expected.

https://www.picbasic.co.uk/forum/showthread.php/26771-SOLVED-How-to-calculate-Timer-preload?p=156025#post156025

Demon
- 23rd August 2024, 02:13
Art's formula with Prescaler:

65535 - (((( MHz / Prescaler ) / 4 ) x 10000 ) - 8 )