Not quite got my finger on the pulse


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2007
    Posts
    24

    Default Not quite got my finger on the pulse

    Hi all! I’ve an interesting problem I’m trying to solve in that I need to generate a series of exactly timed pulses three separate output pins being used, I need the sequence to run like this;

    Pulse1 occurs at PRF of variable between 400Hz and 1500Hz with variable pulse width of 10 to 150uS.

    Between 2 and 50uS later, Pulse2 occurs with, again variable width of 10 to 50uS.

    The, finally, Pulse 3 (also variable pulse width of 10 to 50uS ) occurs between 2 and 150uS AFTER Pulse 2 has gone low (all pulses are active high).

    I think I can do it using the three timers in a PIC, but I need .25uS resolution to 40MHz here I come I think. I’ve tried to do it in PICBASIC primarily because I suck at assembler (hence the PICBASIC) but I just isn’t fast enough.

    The whole process repeats continuously and the PIC also has to do other things, but they are easy and are already done. It’s just this timer routine I’m stuck on.

    Can anyone help please?

  2. #2
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    I sounds like you want something like:
    Code:
     400-1500Hz | 10-150us |  2-50us  |  10-50us |  2-50us  |  10-50us | 400-1500Hz
                +----------+          +----------+          +----------+
     ___________|          |__________|          |__________|          |___
                  1st Pin               2nd Pin               3rd Pin
    Does this look correct?

    With a PIC (I'll assume an 18Fxxxx) there are some things you will need to keep in mind. Each instruction cycle is Fosc/4. So at 40Mhz it will complete most instructions in .1us. Some instructions use 2 instruction cycles to comeplete (like goto's). So you should be able to get close to .25us resolution, at least once the pulses start. It also looks like a 10/2/10/2/10 pulse string would not allow any other things to be done between starting and completing the series of 3 pulses if you need high accuracy.

    Have you tried something like this to get the pulses? It should be close to the .25us accuracy you are looking for.

    Code:
    Pulse_1	VAR BYTE
    Pulse_2	VAR BYTE
    Pulse_3	VAR BYTE
    Delay_1	VAR BYTE
    Delay_2	VAR BYTE
    '---------------------------
    ' Set the variables as needed
    Pulse_1	= 10
    Pulse_2	= 2
    Pulse_3	= 25
    Delay_1	= 10
    Delay_2	= 50
    '---------------------------
    ' Run this series of instructions
    ' at the appropriate interval
    
         portb.1 = 1
         pauseus Pulse_1	
         portb.1 = 0
    
         pauseus Delay_1	
    
         portb.2 = 1
         pauseus Pulse_2
         portb.2 = 0
    
         pauseus Delay_2
    
         portb.3 = 1
         pauseus Pulse_2
         portb.3 = 0
    Now, what are you using to time the 400-1500Hz initial pulse? Does this also need to have a .25us accuracy? If it does, you may have trouble, since even using ASM and Interrupts, there is a 3-4 instruction cycle latency for the interrupt to start running after the trigger (so .4us). And this does not count any context saving that may be needed. Some more info may assist in getting more help for you problem.

    SteveB

  3. #3
    Join Date
    Jan 2007
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Hi Steve, Thanks for the reply, but..... I think I need to use the timer as I said, the PIC has to do other things too (such as scan a front panel full of pots and switches) and this pulse train needs to be "automatic" and invisible to the rest of the processes.

    I did try something along the lines of what you suggested, but I was leaning towards setting up a timer then having it time out and reloaded to give another delay, or use one timer to generate the delay, then trigger timer 2 to provide the pulse width. When this timer times out, it would triger another to give the second delay. I've yet to sit down and figure out the fine bits, but I think I need to learn about interrupts and timers on the PIC. Any help would be appreciated.

    Thanks.

  4. #4
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    1) A timer could be used as a source for the 400Hz(2250us)-1500Hz(667us) timing. This would certainly be best with interrupts. The unavoidable latency involved could be dealt with by adjusting the value loaded into the timer by an offset.

    As a primer, have a look at Darrel Taylor's Instant Interrupts. It's a long thread, but the key stuff is in the first 8 posts, and also on his web site. Also, he has a little calculator to help come up with the values to add to the timer registers. This sould help to get the 400-1500Hz timing sorted out.

    2) If you want to get an accurate 2us pause (only 20 instruction at 40 Mhz) between pulses, you are not likely to do this with timers and interrupts, and still have an accuracy of .25us, and get anything else accomplished. It would take most of 20 instructions cylcles just to vector to the interrupt routine, save context, toggle a pin, restore context, and return to the code (the 18F does have some context saving features that help here, but not that much).

    So, use the interrupts to trigger the start of the pulse string, then control the pulse width/timing actively using PBP or ASM routines. Worst case you would have a 350us interruption in you main code to send out the pulse string followed by about 317us for the main code to run before the next interrupt. Not ideal, but consider that 317us is 3170 instruction cycles, and a fair bit can be accomplised it that time. Also, as the freqency decreases, much more time becomes available between interrupts.

    SteveB

  5. #5
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default One Other Thing...

    Something that should also be mentioned (but is probably obvious) towards the timing accuracy you seek. It will only be as accurate as the oscillator you use. Additionally, the most of the PIC with high frequencies achieve this from PLL. All this will affect that .25us goal your aiming at.

Similar Threads

  1. Detecting a pulse
    By Dennis in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 28th February 2010, 13:12
  2. Pulse Capture and byte building
    By boroko in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st July 2009, 01:59
  3. Single digit 7 Seg LED clock - PIC16F88
    By thirsty in forum Code Examples
    Replies: 4
    Last Post: - 17th July 2009, 08:42
  4. Replies: 3
    Last Post: - 13th September 2008, 17:40
  5. Pulse Frequency Multiplication
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st August 2005, 10:39

Members who have read this thread : 1

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