DT interrupt/pulse generator


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Adrian,

    All latencies can be easily compensated for, by ADDING the reload value to the timer instead of just copying the value to the time.

    At the time the interrupt occurs, the value of the timer is 0, and it continues counting while the chip is doing what ever it has to do to get to the Handler.

    When it gets to the handler, and is ready to reload the timer, the timer already has an EXACT count of how long it took to get there. So when you ADD the const to the timer, it actually subtracts the time it took from the const and EVERYTHING is accounted for.

    Take a look at this page ...
    http://www.darreltaylor.com/DT_INTS-...rTemplate.html

    That program has everything you need, but at a minimum, check out the ReloadTMR1 routine which shows an easy 8 instruction cycle adding reload.

    hth,
    DT

  2. #2
    Join Date
    May 2007
    Location
    Suffolk, UK
    Posts
    59


    Did you find this post helpful? Yes | No

    Default

    Darrel

    Thank you very much. I have taken a look and 'instantly' it doesn't look so easy to understand, however it also appears to be very crafty and very useful. I am going to plug away at your code until it IS understood and then add this new-found knowledge to the ever increasing store of accumulated wisdom from all of you experts.

    Many thanks again to you for enlightening me and of course to Skimask for his valuable input

    Adrian

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I have taken a look and 'instantly' it doesn't look so easy to understand, ...
    It does look a bit confusing at first, but it couldn't be easier.
    A complete understanding of how it works will come later after getting it to work.

    This is really the only section you need to digest.
    Set Freq to 2000, Prescaler to 1, and T1CON to 0. Fill in the Main program and T1handler that gets called on each interrupt.

    Done deal.


    Code:
    ;--- Change these to match the desired interrupt frequency -------------------
    ;--- See http://DarrelTaylor.com/DT_INTS-14/TimerTemplate.html for more Info.
    @Freq       = 2000                ; Frequency of Interrupts in Hz
    @Prescaler  = 1                   ; Timers Prescaler setting
    T1CON = $00                       ; $00 = Prescaler 1:1, TMR1 OFF
    ; $00=1:1, $10=1:2, $20=1:4, $30=1:8 --  Must match @Prescaler value
    
    @ INT_ENABLE  TMR1_INT            ; enable Timer 1 interrupts
    GOSUB StartTimer                  ; Start the Timer
    
    ;____Your Main Program goes here______________________________________________
    Main:
      ;   ---- Your Main Program goes here ----
    GOTO Main
    
    ;____This routine is Called on each TMR1 Interrupt____________________________
    T1handler:
      ;   ---- Your interrupt routine goes here ----
      
    @ INT_RETURN
    DT

  4. #4
    Join Date
    May 2007
    Location
    Suffolk, UK
    Posts
    59


    Did you find this post helpful? Yes | No

    Default DT Interrupt - saga continues!

    Darrel

    Many thanks - the programme worked spotlessly up to 1KHz. I put Freq as 1000, T1CON as 0 and Prescaler as 1 plus I just added TOGGLE PORTB.0 in the T1Handler section. With any frequency up to 1000Hz the output on PORTB.0 is (as would be expected) exactly half the frequency value. At any frequency above 1000Hz, which appears to be a magical threshold, the output is not half. e.g. at 2000 it is 1042; at 1500 it is 781; at 1200 it is 625 and even at a small increase over 1000 Hz it is still wrong ie at 1020 it is 521. What is the logical explanation for this, or am I barmy?
    PS I'm using and defining a 20Mhz osc but have tried with same results on 4 and 8 Mhz
    Some rational explanation would be greatly appreciated!

    Thanks

    adrian

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hmmm, I'm not sure Adrian.

    I've double checked it here on a 16F88 and I can go up to 12khz with a 20mhz OSC. Or 25khz by changing the Handler's "TYPE" to ASM.

    I did find some inaccuracy in the math,

    This line ...
    TimerReload = MaxCount - ((25000 / Freq) * (OSC*10/Prescaler))

    Should be like this...
    TimerReload = MaxCount - OSC*1000000/4/Prescaler/Freq

    but that shouldn't cause what you've described.

    Which PIC are you using?
    <br>
    DT

  6. #6
    Join Date
    May 2007
    Location
    Suffolk, UK
    Posts
    59


    Did you find this post helpful? Yes | No

    Default Reply to Darrel's query

    I'm using the ubiquitous 16F877A. Using the time difference between you and the UK I will change the maths anyway and try different frequencies again. I will also double check everything on the development board and will report back...!! Did you try with TOGGLE in the TIHandler? I wondered if that instruction was somehow slowing it down. Thank you for your help...this is bugging me!

    Adrian

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Yes, just a TOGGLE in the handler, and nothing in the Main loop.

    The only other thing I added was a CONFIG line for HS oscillator mode, and LVP OFF.
    <br>
    DT

  8. #8
    Join Date
    May 2007
    Location
    Suffolk, UK
    Posts
    59


    Did you find this post helpful? Yes | No

    Default Mystery Solved!

    Hi Darrel
    I changed the bit of maths and hey presto the programme instantly came good. It is cycle accurate all the way up. I'm really pleased...so many thanks. Now I'll get back to studying your programme to see how it works. The bit that is confusing is adding the reload value to the timer value - I didn't realise it then subtracted the timer value to the point of reset. You did this using ASM. Presumably this could also be done in PBP?
    Regards Adrian

Similar Threads

  1. PTO Generator
    By b1arrk5 in forum Off Topic
    Replies: 7
    Last Post: - 25th November 2013, 14:25
  2. 2 Beam Optical Pulse Generator
    By WOZZY-2010 in forum Schematics
    Replies: 8
    Last Post: - 6th April 2010, 04:03
  3. DT instant interrupts with mister_e keypad
    By Tomexx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th November 2008, 20:02
  4. Frequency Generator
    By ExYu in forum Off Topic
    Replies: 2
    Last Post: - 22nd January 2008, 18:25
  5. Big characters on HD44780 4x20
    By erpalma in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th January 2007, 02:21

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts