News:

;) This forum is the property of Proton software developers

Main Menu

PIC Timer Calculator by Ronald Nisble

Started by trastikata, Feb 15, 2021, 12:39 AM

Previous topic - Next topic

trastikata

Very useful tool written by Ronald Nisble, have been using it ever since.

PTC.jpg

John Drew

Thanks Trastikata,
On my machine it tries to install and make changes to Microsoft Office. I prevented that and after a few 'cancel' button presses the utility came up on the screen and seems to function. I don't like the program trying to change my copy of Office. Should this be happening? Has anyone else had this problem?
John

trastikata

Never had this problem John. It's a zipped standalone exe file, shouldn't try to install at all.

Maybe other members can advise on it?

Dompie

Yes John I had the same problem here. I had to click cancel several times. It is NOT a standalone .exe it is an installation .exe!!! It wants to change my Office 2013 installation.
I have trashed the program.

Johan

See_Mos

#4
Because I have used it for so long I default to MikroElektronika Timer Calculator but I also use the much better Mister E PIC multi-calc

keytapper

#5
I tried to run, but I got some dependency problem.
I'll search for such file and set it correctly.
This may guide for the missing ocx component.
Ignorance comes with a cost

Maxi


keytapper

I installed the OCX, but I can't solve.
Ignorance comes with a cost

Dompie

The new version 0.9.7 still wants to change my Office installation. Trashed.

Johan

keytapper

Ignorance comes with a cost

Giuseppe

Version 0.9.7 did not create any problems for me.
However, I use open office

top204

There is no need to use a seperate program to calculate a timer's overflow interrupt timing once the mechanism behind them is understood, and is the same calculation used by the seperate programs, but at the user's fingertips whenever required.

The program below shows a simple method of calculating an overflow interrupt time, all in the program's code, so it can be changed to suit and can be transferred to other programs, and the calculation can be used with other timers etc... A much better method in my opinion, and it shows the flexability and power of the compiler's preprocessor.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Calculate an interrupt overflow rate and start an interrupt to toggle a port's pin
' Written for the Proton8 BASIC compiler by Les Johnson
'   
    Device = 18F25K20                                   ' Tell the compiler what device to compile for
    Declare Xtal = 64                                   ' Tell the compiler that the device will be running at 64MHz
    On_Hardware_Interrupt GoTo ISR_Handler              ' Tell the compiler that interrupts are being used and where to find the interrupt handler routine
  
    Dim wTimer0 As TMR0L.Word                           ' Combine TMR0L and TMR0H into a 16-bit SFR
  
'------------------------------------------------------------------------------
' General interrupt preprocessor meta-macros
'
$define IntGlobal_Enable()  Set INTCONbits_GIE          ' Enable global interrupts
$define IntGlobal_Disable() Clear INTCONbits_GIE        ' Disable global interrupts
$define IntPeriph_Enable()  Set INTCONbits_GIEL         ' Enable peripheral interrupts
$define IntPeriph_Disable() Clear INTCONbits_GIEL       ' Disable peripheral interrupts
 
'------------------------------------------------------------------------------
' Timer0 preprocessor meta-macros for a PIC18F25K20 device
'
$define Timer0_Flag() INTCONbits_T0IF                   ' Timer0 Flag
$define Timer0_FlagClear()  Clear Timer0_Flag()         ' Clear the Timer0 interrupt flag
$define Timer0_IntEnable()  Set INTCONbits_TMR0IE       ' Enable a Timer0 interrupt
$define Timer0_IntDisable() Clear INTCONbits_TMR0IE     ' Disable a Timer0 interrupt
$define Timer0_Start() Set T0CONbits_TMR0ON             ' Start Timer0
$define Timer0_Stop()  Clear T0CONbits_TMR0ON           ' Stop Timer0
'
' Preprocessor meta-macros to alter the bits for a required Timer0 Prescaler on a PIC18F25K20 device
'
$define Timer0_Prescaler2() '
    T0CONbits_T0PS0 = 0     '
    T0CONbits_T0PS1 = 0     '
    T0CONbits_T0PS2 = 0
 
$define Timer0_Prescaler4() '
    T0CONbits_T0PS0 = 1     '
    T0CONbits_T0PS1 = 0     '
    T0CONbits_T0PS2 = 0
   
$define Timer0_Prescaler8() '
    T0CONbits_T0PS0 = 0     '
    T0CONbits_T0PS1 = 1     '
    T0CONbits_T0PS2 = 0
   
$define Timer0_Prescaler16() '
    T0CONbits_T0PS0 = 1      '
    T0CONbits_T0PS1 = 1      '
    T0CONbits_T0PS2 = 0
   
$define Timer0_Prescaler32() '
    T0CONbits_T0PS0 = 0      '
    T0CONbits_T0PS1 = 0      '
    T0CONbits_T0PS2 = 1
   
$define Timer0_Prescaler64() '
    T0CONbits_T0PS0 = 1      '
    T0CONbits_T0PS1 = 0      '
    T0CONbits_T0PS2 = 1
   
$define Timer0_Prescaler128() '
    T0CONbits_T0PS0 = 0       '
    T0CONbits_T0PS1 = 1       '
    T0CONbits_T0PS2 = 1 
   
$define Timer0_Prescaler256() '
    T0CONbits_T0PS0 = 1       '
    T0CONbits_T0PS1 = 1       '
    T0CONbits_T0PS2 = 1

'------------------------------------------------------------------------------
' Calculate the value to place into the TMR0L\H registers in order to achieve a certain overflow interrupt rate (in uS)
' The calculation will also work for other timers, and can be duplicated in a program with different $define names used, such as Timer1_cMicroSeconds, Timer1_cValue etc...
' See the compiler's manual on how to create meta-macros with the compiler's preprocessor.
' If calculating for an 8-bit timer, use the value 256 instead of 65536 and check for greater than 255 instead of 65535
'
$define Timer0_cPrescaler 2                     ' The prescaler used for the timer (must be the same prescaler value as used for the relevant timer's setup)
$define Timer0_cMicroSeconds 100                ' Interrupt rate (in uS)
$define Timer0_cTweakValue 8                    ' Holds a tweak value for the timer calculation

$define Timer0_cValue $eval ((65536 + Timer0_cTweakValue) - ((Timer0_cMicroSeconds / Timer0_cPrescaler) * (_xtal / 4)))

$if Timer0_cValue > 65535
    $error "Timer0_cValue is too large for the interrupt duration"
$elseif Timer0_cValue <= 0
    $error "Timer0_cValue is too small for the interrupt duration"
$endif

'---------------------------------------------------------------------------------
' The main program starts here
'
Main:
    Low PORTB.0
    Timer0_Setup()                  ' Setup Timer0  
    IntGlobal_Enable()              ' Enable global interrupts   
    Stop

'---------------------------------------------------------------------------------    
' Setup Timer0

Proc Timer0_Setup()  
    T0CON = 0
    Timer0_Prescaler2()             ' Set the prescaler for Timer0
    wTimer0 = Timer0_cValue         ' Load the value for a specific overflow time into TMR0L/H
    Timer0_FlagClear()              ' Clear the Timer0 interrupt flag
    Timer0_IntEnable()              ' Enable a Timer0 interrupt
    Timer0_Start()                  ' Start Timer0
EndProc

'---------------------------------------------------------------------------------
' Interrupt handler
' Toggles pin PORTB.0 when the Timer0 overflow interrupt occurs
'  
ISR_Handler:
    Context Save

    If Timer0_Flag() = 1 Then       ' Is it a Timer0 interrupt?
        Btg PORTB.0                 ' Yes. So toggle PORTB.0
        Timer0_FlagClear()          ' Clear the interrupt flag
        wTimer0 = Timer0_cValue     ' Re-load TMR0L\H with the value to give a specified overflow time       
    EndIf

    Context Restore

'-------------------------------------------------------------
' Setup the fuses for the 4xPLL on a PIC18F25K20 device
'
Config_Start
    FOSC = HSPLL        ' HS oscillator. PLL enabled and under software control
    DEBUG = Off         ' Background debugger disabled. RB6 and RB7 configured as general purpose I/O pins
    XINST = Off         ' Instruction set extension and Indexed Addressing mode disabled
    STVREN = Off        ' Reset on stack overflow/underflow disabled
    WDTEN = Off         ' WDT disabled (control is placed on SWDTEN bit)
    FCMEN = Off         ' Fail-Safe Clock Monitor disabled
    IESO = Off          ' Two-Speed Start-up disabled
    WDTPS = 128         ' Watchdog is 1:128
    BOREN = Off         ' Brown-out Reset disabled in hardware and software
    BORV = 18           ' VBOR set to 1.8 V nominal
    MCLRE = On          ' MCLR pin enabled. RE3 input pin disabled
    HFOFST = Off        ' The system clock is held Off until the HF-INTOSC is stable
    LPT1OSC = Off       ' T1 operates in standard power mode
    PBADEN = Off        ' PORTB<4:0> pins are configured as digital I/O on Reset
    CCP2MX = PORTC      ' CCP2 input/output is multiplexed with RC1
    LVP = Off           ' Single-Supply ICSP disabled
    CP0 = Off           ' Block 0 not code-protected
    CP1 = Off           ' Block 1 not code-protected
    CPB = Off           ' Boot block not code-protected
    CPD = Off           ' EEPROM not code-protected
    WRT0 = Off          ' Block 0 not write-protected
    WRT1 = Off          ' Block 1 not write-protected
    WRTB = Off          ' Boot block not write-protected
    WRTC = Off          ' Configuration registers not write-protected
    WRTD = Off          ' EEPROM not write-protected
    EBTR0 = Off         ' Block 0 not protected from table reads executed in other blocks
    EBTR1 = Off         ' Block 1 not protected from table reads executed in other blocks
    EBTRB = Off         ' Boot block not protected from table reads executed in other blocks
Config_End

The Timer0_cTweakValue constant is used to fine tune the overflow timing to take into account interrupt overheads, such as interrupt type checking etc... For example, the Timer0_cTweakValue value of 8 with a Timer0 prescaler of 2, in the above program, will give an exact 100uS Timer0 overflow timing because it takes into account the line of code "If Timer0_Flag() = 1 Then", and it is used to take into account any SFRs and variables saved with Context Save. Without this tweak value, no datasheet calculation will work correctly because they create timing values that do not take into account a "real" device's operation.

The image below shows the program working and toggling pin PORTB.0 every Timer0 overflow interrupt. It can be seen by the frequency of 20000Hz that the timing for the interrupt is 100uS.

Interrupt Timing.jpg