Generating a series of high frequency pulses with pre-defined length.


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default Generating a series of high frequency pulses with pre-defined length.

    Hello.

    I need to generate a series (5000 pulses for example, but it might be 1000 pulses) with shortest duration possible. The code like this:

    Code:
    HEAD:
    PORTB.6=1
    PORTB.6=0
    GOTO HEAD
    do works, but it has an issue - GOTO statement takes more time to execute, then simple port enable-disable, so pause between pulses is longer than pulse itself, which is not welcomed.

    Of course, I can write

    PORTB.6=1
    PORTB.6=0
    PORTB.6=1
    PORTB.6=0

    multiple times, but 5000 times?

    Tried it on various MCU's on all of them, GOTO or DO-LOOP or FOR-NEXT or any other looping instruction takes more time. Any ideas how to solve this?

    I come to partial solution like this:

    Code:
    PIKO:
    PORTB.6=1
    @NOP
    PORTB.6=0
    GOTO PIKO
    In this code, @NOP statement lengthens the pulse itself, so it is equal to width of pause, but of course, overal frequency is reduced.

    Currently using 16F1829 @ 32mhz, and I want to avoid usage of 18F or 24F family at all costs.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    You say that the pulse duration should be as short as possible, do you really require 50% dutycycle? What are you doing with the pulses?

    This is (as we're discussing in the other thread) a classic read-modfy-write trap. Since you have LAT registers available on that device you should use those instead of PORT (makes no difference to the speed though).

    You might be able to use one of the peripherals in the PIC to generete pulses in hardware, feed them back into a counter and use a CCP module to stop the output at the correct count.

    /Henrik.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,388


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    use The DSM module with clock as carrier source and switich it on/off with the MDBIT bit in the MDCON register under control of a counter




    Warning I'm not a teacher

  4. #4
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    Yes, I need duty cycle of 50%, this is for HSS strobe application.

    LATA makes no difference in speed, already tried.

    can HPWM generate 4mhz signals? and counter, count them?

    What is DSM module?

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    Like I said, LATA has nothing to do with the speed but will help you from getting into another read-modify-write problem. Feel free to ignore....

    DSM is digital signal modulator, a peripheral available on the 16F1829, see section 23 in the datasheet.

    According to the manual the maximum frequency for HPWM is 32768Hz but that doesn't stop you from configuring the CCP module manually. At 32MHz it should be capable of generating 4MHz (but not 4mHz). Check out the Reference Clock Module as well, might be better than the CCP module as Richard suggests.

    /Henrik.

  6. #6
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    I had issue with read-modify-write on 628A. 1829 in same conditions works just fine.

  7. #7
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    RWM again lol.

    Some ideas...

    Slower, but at least it’s 50% duty cycle
    Code:
    ‘ Not RWM safe!
    
    flipflop var bit
    
    cycle:
    flipflop = flipflop + 1
    portb.0 = flipflop
    goto cycle
    can you afford to waste a whole port?
    Code:
    ‘ Is RWM safe!
    
    flipflop var byte
    flipflop = %01010101
    
    cycle:
    portb = flipflop
    @comf _flipflop
    goto cycle
    faster than what you posted above.
    You need two consecutive nop instructions to balance the goto for %50 duty cycle in the loop you posted.

  8. #8
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    Thanks, but how control amount of cycles?

  9. #9
    Join Date
    May 2013
    Location
    australia
    Posts
    2,388


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    ‘ Is RWM safe!

    flipflop var byte
    flipflop = %01010101

    cycle:
    portb = flipflop
    @comf _flipflop
    goto cycle
    doing it that way won't get past a few hundred kilohertz






    its easy with the dsp module ,but the last pulse is difficult to control
    if being 1 pulsE short sometimes this works great for a 4MHz output stream

    Code:
    '****************************************************************
    '*  Name    : MODULATOR.BAS                                     *
    '*  Author  : richard                    *
    '*  Notice  : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 5/29/2016                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :  4MHZ CARRIER FOR X PULSES   x=10 to 65535        *
    '*          : 16F1825                                           *
    '****************************************************************
      #CONFIG
                 __config        _CONFIG1,    _FOSC_INTOSC & _CP_OFF & _WDTE_ON  &  _PWRTE_ON  &  _MCLRE_ON  & _CLKOUTEN_OFF
                  __config      _CONFIG2, _PLLEN_ON & _LVP_OFF            
    #ENDCONFIG
     
    OSCCON=$70 
    DEFINE OSC 32
    @TIMER1=TMR1L
    '                       PIC 16F1825
     
    TRISA     = %111111    ' Make all pins Input 
    trisc     = %101111  ;Make all pins Input   
    ANSELA=0     
    ANSELC=0
    
    CLKRCON=  %11110011
    MDCON=    %11000000
    MDSRC=    %00000000
    MDCARH   =%00100011
    MDCARL   =%00000000
    X VAR WORD    
    TIMER1     VAR WORD EXT
    '  ;debug   --------------------------
    ' TRISA.0     = 0
    ' lata.0=1
    ' pause 2000     ;debug
    ' serout2 PORTa.0,84, ["ready v3",13,10 ]    ;debug
    '  ;debug ------------------------------------
    ; moddout is portc.4
    X=40
    
    Main_Loop:     
    TIMER1=~(X-6)
    ASM 
        BCF  PIR1,0
        MOVE?CT 1,MDCON,0
        MOVE?CB 0X71,T1CON
    COFF    
        BTFSS PIR1,0 ;    
        BRA COFF
        
        MOVE?CT 0,MDCON,0
        MOVE?CT 0,T1CON,0
    ENDASM
    PAUSE 200 
    goto Main_Loop
    end
    Warning I'm not a teacher

  10. #10
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    Yes of course hardware will be faster, but neither are the original examples.
    I’m not saying it’s possible to get a 4MHz signal that way in software, just faster than what was posted.
    Last edited by Art; - 9th October 2016 at 03:53.

  11. #11
    Join Date
    Feb 2012
    Posts
    64


    Did you find this post helpful? Yes | No

    Default Re: Generating a series of high frequency pulses with pre-defined length.

    Something that toggles the port 1 time through the same loop would give you an equal 50% duty if that is what you are trying for:
    HEAD:
    PORTB.6 = PORTB.6 ^ %1
    Goto HEAD




    Quote Originally Posted by CuriousOne View Post
    Hello.
    I need to generate a series (5000 pulses for example, but it might be 1000 pulses) with shortest duration possible. The code like this:

    Code:
    HEAD:
    PORTB.6=1
    PORTB.6=0
    GOTO HEAD
    do works, but it has an issue - GOTO statement takes more time to execute, then simple port enable-disable, so pause between pulses is longer than pulse itself, which is not welcomed.

    Of course, I can write

    PORTB.6=1
    PORTB.6=0
    PORTB.6=1
    PORTB.6=0

    multiple times, but 5000 times?

    Tried it on various MCU's on all of them, GOTO or DO-LOOP or FOR-NEXT or any other looping instruction takes more time. Any ideas how to solve this?

    I come to partial solution like this:

    Code:
    PIKO:
    PORTB.6=1
    @NOP
    PORTB.6=0
    GOTO PIKO
    In this code, @NOP statement lengthens the pulse itself, so it is equal to width of pause, but of course, overal frequency is reduced.

    Currently using 16F1829 @ 32mhz, and I want to avoid usage of 18F or 24F family at all costs.

Similar Threads

  1. Measuring a variable freq/length pulse and generating more pulses.
    By retepsnikrep in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 18th September 2014, 09:10
  2. Replies: 1
    Last Post: - 9th March 2013, 07:10
  3. How to latch an output for a pre-defined time
    By Dennis in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 21st December 2009, 18:20
  4. high voltage high frequency H-Bridge
    By George in forum Off Topic
    Replies: 6
    Last Post: - 27th April 2009, 11:50
  5. High End Pre with PIC 16F876A
    By abidr in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th May 2008, 08:13

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