Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Delay calculator

Status
Not open for further replies.
David / W9GF said:
I created a small excel spreadsheet which i'm attaching that may be helpful in calculating the various delays available with a given clock frequency. All you need to enter is your clock frequency in mhz - everything else is calculated based on that.

Enjoy!

David

Hi,
Tried runs OK, BTW 'OPen Office' will run the program.:)
 
mike50 said:
Here is a little program that will calculate the settings (prescale, postscale and PR2) for TMR2 to generate interrupts for whatever time interval you want. If it can generate settings for exactly what you want, it shows all the ways that can be achieved. If there are no settings to get exactly what you want it shows the closest values (one greater and one less than what you want).

You run it as:

TMR2 n

where n is the timer interval you want in instruction cycles.

examples:

Code:
C:>TMR2 10000
10000 Prescaler 1:16 Postscaler 1:5  PR2 124
10000 Prescaler 1:4  Postscaler 1:10 PR2 249

C:>TMR2 1234
 1233 Prescaler 1:1  Postscaler 1:9  PR2 136
 1235 Prescaler 1:1  Postscaler 1:13 PR2  94


The maximum value for n is 65536

Mike

Hi Mike,
Unzipped, keep getting this error msg in Win XP.
 
Last edited:
ericgibbs said:
Hi Mike,
Unzipped, keep getting this error msg in Win XP.
Hmmm. The program is written in Visual C++, and shouldn't require .NET. But that is the error you can get for .NET applications trying to run on machines without .NET (or with the wrong version).

Actually, the problem was that it was compiled to need the C-runtime library as a DLL. I've rebuilt it with the runtime contained within the .exe.

This updated version should work okay.

Mike
 

Attachments

  • TMR2.zip
    65.5 KB · Views: 325
Last edited:
savnik said:
Is a DOS programm.
You must to runn this from CMD

Hi,
Thought it was a Windows program, will try from DOS/cmd.

Thanks.:)
 
ericgibbs said:
Hi,
Thought it was a Windows program, will try from DOS/cmd.

Thanks.:)
It is a window's program, but you really need to run it from a command prompt so you can supply it with the parameter.

The error that ericgibbs was seeing is due to not being able to find a .dll that it wanted. The new version doesn't require any .dll's.

Mike
 
Made some changes to the Excel Spreadsheet

Saw your output mike50 with postscaler values so i corrected a couple of little bugs with mine, added a place to put in "Delay I need" - and it shows you all of your options - even converts the 8 bit stuff to hex for ya (guess microsoggy didn't think anyone would need a DECTOHEX32() function in Excel Grrrrr)...

Also added some timer notes i found in the Data sheet for the 18F4550 at the bottom.

Hope this stuff is accurate! I'll be testing & working with it for the next couple of weeks so i'll update my .xls if i find anything out of kelter!

David

The most current version of the attachment is here: https://www.electro-tech-online.com/threads/pic-delay-calculator.35346/

(today anyway :))
 
Last edited:
David,

I'm not sure I understand the meaning of the Postscaler in your spreadsheet. TMR0 doesn't have a postscaler. If you are doing this for TMR2 then you want to include the PR2 register as part of the calculation.

Mike
 
postscaler

I may have used the wrong terminology - but that's the value to write in the TMR0 & TMR1 register instead of 0 ... and now that i've typed this the value i have is incorrect - i forgot this thing is counting up and not down - so the value should be subtracted from 255 for the 8 bit mode and subtracted from 65535 for the 16 bit mode.

That making sense or am i way off base?
 
Oh, I see what you are getting at. What you are calling "postscaler" is more like the PR2 register of TMR2. It defines how many counts before TMR0 overflows.

There is a problem with that technique though. Some time elapses from the point where the TMR0 overflows and where your interrupt service routine can write the new value into TMR0. In some circumstances you could correct for that time, but in others (if there are other interrupts enabled, or the mainline sometimes disables interrupts) you can not accurately compensate.

That is why TMR2, with a hardware postscaler and the PR2 (match) register, is so nice. It is all in hardware. Your timing has no dependency on the time it takes to get into your interrupt service routine. The interrupts come like, well , "clockwork" :)

Mike
 
Another Fix or two or three or .....

*GASP* you mean all timer's don't work the same? :)

I guess this thing may have multiple tabs on it before i'm done?

I found some bugs and corrected them - i was looking at the wrong column - so i simplified it and removed some columns...

Curious if this matches up with what your program shows?

For example - for 20mhz clock selecting prescaler 1:8 and seeding TMR0L & TMR0H with 64285 (0xFB1D) time should run out in .001 seconds?

I gotcha loud and clear on the latency - i'm not trying to be that accurate - this whole exercise started out with me trying to figure out what to set things to in order to get a 1ms interrupt!

Thanks for your input though!

I think it's miller time! :D

David
 

Attachments

  • W9GF.PICTimerCalculator.v3.0.09Apr2008.zip
    8.6 KB · Views: 3,737
David / W9GF said:
*GASP* you mean all timer's don't work the same? :)

I guess this thing may have multiple tabs on it before i'm done?

I found some bugs and corrected them - i was looking at the wrong column - so i simplified it and removed some columns...

Curious if this matches up with what your program shows?

For example - for 20mhz clock selecting prescaler 1:8 and seeding TMR0L & TMR0H with 64285 (0xFB1D) time should run out in .001 seconds?

I gotcha loud and clear on the latency - i'm not trying to be that accurate - this whole exercise started out with me trying to figure out what to set things to in order to get a 1ms interrupt!

Thanks for your input though!

I think it's miller time! :D

David
My program is strictly for TMR2 hardware.

For your example above, (and assuming you meant TMR1, not TMR0 - because TMR0 is only 8 bits), with a 20mhz clock and 1:8 prescaler, TMR1 will update every 1.6 microseconds. (20 mhz/4 = 5 mhz instruction clock, which gives .2 microseconds, and then times 8 gives 1.6 microseconds)

Starting at 64285, TMR1 will take 65536-64285 = 1251 increments to overflow to zero. 1251 * 1.6 = 2001.6 microseconds which is about 2 milliseconds.

I think for this example you want to preload TMR1 with 64911 (0xfd8f) to get a 1 millisecond timeout. (65536-64911 = 625 and 625 * 1.6 = 1000 microseconds = 1 millisecond) You must have picked up the 64285 from the 1:4 prescaler row. But your numbers are all off by one, you should be subtracting from 65536 not 65535.

Mike
 
Last edited:
Great - same (almost) answers

I quoted you the wrong prescaler (from my spreadsheet) number.

My spreadsheet says it should be 64910 for the 1:8 prescaler - which agrees with what you are saying - so i'm feeling good about my research & playing with them!

My spreadsheet says 64285 for the 1:4 prescaler - sorry for the typo!

Another possible error i have - i'm subtracting from 255 & 65535 instead of 256 and 65536 - will have to change that!

Thanks,
David
 
TIMR0 on a 18F4550 8 bits or 16?

For your example above, (and assuming you meant TMR1, not TMR0 - because TMR0 is only 8 bits),

I'm confuzzled here - below is a cut/paste from the 18F4550 Data Sheet:

11.0 TIMER0 MODULE
The Timer0 module incorporates the following features:
• Software selectable operation as a timer or
counter in both 8-bit or 16-bit modes

??????
 
David / W9GF said:
I'm confuzzled here - below is a cut/paste from the 18F4550 Data Sheet:

11.0 TIMER0 MODULE
The Timer0 module incorporates the following features:
• Software selectable operation as a timer or
counter in both 8-bit or 16-bit modes

??????
I've never looked at any of the 18F parts. On the 12F and 16F parts (all I've ever worked with) TMR0 is only 8 bits. Sorry about that. Learn something every day :)

Mike
 
curve

ha - no problem guess that's why there are so many of them - keeps more of us employed :) You were very very helpful yesterday getting me on the right track and i thank you again for that!

My learning curve is so steep - i think i'm about to fall off of it!

David
 
Make sure that you have 0.1uF bypass capacitors on the input and output of the 7805. Both caps should be within 0.5" or 1.25cm of the 7805. This will reduce the chance of the 7805 becoming unstable and oscillating. Also make sure there is a 0.1uF cap across the power leads of the PIC if it is far from the 7805's 0.1uF output cap.
Also, make sure that the PIC is getting a steady 5V when the input to the 7805 is varied from 9 to 15V.
 
William (wmmullaney),

Just wondering how you were progressing on your binary clock?

I'm having some difficulty understanding your program and was wondering if you could provide a schematic or describe how the output LEDs are connected? How many LEDs are you using to display binary hours? How many LEDs are you using to display binary minutes?

You also asked in an earlier post about what kind of accuracy you could expect and I wanted to mention I've experienced less than 1 second variation per month while experimenting with a 16-MHz crystal and software which emulated a crystal oscillator trimmer capacitor of sorts. I believe the theoretical accuracy was around 8 or 9 seconds per year not including crystal aging and temperature drift and that's just about what I was seeing while testing the circuit and software for about 8 months.

As other members have suggested, using Timer 2 and interrupts is probably the easiest way to go, but, writing precise isochronous code without using interrupts is not as difficult as you might expect and I've included a crude example below to demonstrate the principle.

Have fun! Kind regards, Mike

Code:
;
;  example isochronous binary clock code
;
        radix   dec
Inc_Secs
        incf    Secs,F          ; increment seconds               |B0
        movf    Secs,W          ; W = seconds                     |B0
        xorlw   60              ; 60 seconds?                     |B0
        bz      Inc_Mins        ; yes, branch, else               |B0
        DelayCy(1*secs-15)      ; delay 1 second -15 cycles       |B0
        goto    Display         ; update display                  |B0
Inc_Mins
        clrf    Secs            ; reset Seconds to 0 and          |B0
        incf    Mins,F          ; increment minutes               |B0
        movf    Mins,W          ; W = minutes                     |B0
        xorlw   60              ; 60 minutes?                     |B0
        bz      Inc_Hrs         ; yes, branch, else               |B0
        DelayCy(1*secs-22)      ; delay 1 second -22 cycles       |B0
        goto    Display         ; update display                  |B0
Inc_Hrs
        clrf    Mins            ; reset Minutes to 0 and          |B0
        incf    Hours,F         ; increment hours                 |B0
        movf    Hours,W         ; W = hours                       |B0
        xorlw   24              ; 24 hours?                       |B0
        skpnz                   ; no, skip, else                  |B0
        clrf    Hours           ; reset Hours to 0                |B0
        DelayCy(1*secs-27)      ; delay 1 second -27 cycles       |B0
;
;  imaginary display update routine for demonstration purposes
;
Display
        movf    Secs,W          ;                                 |B0
        movwf   PortX           ;                                 |B0
        movf    Mins,W          ;                                 |B0
        movwf   PortY           ;                                 |B0
        movf    Hours,W         ;                                 |B0
        movwf   PortZ           ;                                 |B0
        goto    Inc_Secs        ; next 1 second loop              |B0
 
Last edited:
hi, i'm beginner of pic.
can i ask what is the meaning of below instruction

goto $+2

what is the meaning of the $ sign?
why want to use the instruction in the delay below?

movlw 0x16
movwf d1
movlw 0x74
movwf d2
movlw 0x06
movwf d3
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_0
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top