1. 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.
    Dismiss Notice

PIC Delay calculator

Discussion in 'Microcontrollers' started by wmmullaney, Jan 12, 2008.

  1. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Hi,
    Tried runs OK, BTW 'OPen Office' will run the program.:)
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Hi Mike,
    Unzipped, keep getting this error msg in Win XP.
     
    Last edited: Jul 7, 2008
  3. mike50

    mike50 New Member

    Joined:
    Jun 29, 2007
    Messages:
    103
    Likes:
    2
    Location:
    Rochester, Minnesota
    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
     

    Attached Files:

    • TMR2.zip
      File size:
      65.5 KB
      Views:
      202
    Last edited: Apr 9, 2008
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. savnik

    savnik New Member

    Joined:
    Apr 27, 2006
    Messages:
    82
    Likes:
    0

    Is a DOS programm.
    You must to runn this from CMD
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,233
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Hi,
    Thought it was a Windows program, will try from DOS/cmd.

    Thanks.:)
     
  7. mike50

    mike50 New Member

    Joined:
    Jun 29, 2007
    Messages:
    103
    Likes:
    2
    Location:
    Rochester, Minnesota
    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
     
  8. David / W9GF

    David / W9GF New Member

    Joined:
    Apr 5, 2008
    Messages:
    17
    Likes:
    2
    Location:
    Dallas, Texas
    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: http://www.electro-tech-online.com/threads/pic-delay-calculator.35346/

    (today anyway :))
     
    Last edited: Apr 9, 2008
  9. mike50

    mike50 New Member

    Joined:
    Jun 29, 2007
    Messages:
    103
    Likes:
    2
    Location:
    Rochester, Minnesota
    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
     
  10. David / W9GF

    David / W9GF New Member

    Joined:
    Apr 5, 2008
    Messages:
    17
    Likes:
    2
    Location:
    Dallas, Texas
    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?
     
  11. mike50

    mike50 New Member

    Joined:
    Jun 29, 2007
    Messages:
    103
    Likes:
    2
    Location:
    Rochester, Minnesota
    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
     
  12. David / W9GF

    David / W9GF New Member

    Joined:
    Apr 5, 2008
    Messages:
    17
    Likes:
    2
    Location:
    Dallas, Texas
    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
     

    Attached Files:

  13. mike50

    mike50 New Member

    Joined:
    Jun 29, 2007
    Messages:
    103
    Likes:
    2
    Location:
    Rochester, Minnesota
    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: Apr 9, 2008
  14. David / W9GF

    David / W9GF New Member

    Joined:
    Apr 5, 2008
    Messages:
    17
    Likes:
    2
    Location:
    Dallas, Texas
    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
     
  15. David / W9GF

    David / W9GF New Member

    Joined:
    Apr 5, 2008
    Messages:
    17
    Likes:
    2
    Location:
    Dallas, Texas
    TIMR0 on a 18F4550 8 bits or 16?

    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

    ??????
     
  16. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Timer0 can be both an 8 bit or 16 bit timer on the 18F series PICs (you set a byte to set it as such)
     
  17. mike50

    mike50 New Member

    Joined:
    Jun 29, 2007
    Messages:
    103
    Likes:
    2
    Location:
    Rochester, Minnesota
    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
     
  18. David / W9GF

    David / W9GF New Member

    Joined:
    Apr 5, 2008
    Messages:
    17
    Likes:
    2
    Location:
    Dallas, Texas
    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
     
  19. kchriste

    kchriste New Member Forum Supporter

    Joined:
    Jul 23, 2006
    Messages:
    3,677
    Likes:
    47
    Location:
    Victoria BC, Canada
    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.
     
  20. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,642
    Likes:
    109
    Location:
    Michigan, USA
    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 (text):
    ;
    ;  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: Apr 26, 2008
  21. Just4Kason

    Just4Kason New Member

    Joined:
    Nov 8, 2009
    Messages:
    11
    Likes:
    0
    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
     

Share This Page