40 KHz burst


Closed Thread
Results 1 to 5 of 5

Thread: 40 KHz burst

  1. #1
    Join Date
    Jul 2009
    Posts
    13

    Default 40 KHz burst

    Is there a simple way to generate a square wave (short burst 8 pulses) at 40 KHz using a PIC12F509 and PicBasic Pro?

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Exactly 40Khz or thereabouts?

    40Khz has a period of 25uS which means you have a 12.5uS window per half cycle... hard to achieve using 1uS timing intervals at 4MHz Clock... at 8MHz Clock you can do it... but here we go... give it a shot and see how close you get with a scope...

    For CounterA=1 to 16
    Toggle PulsePin
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    Next CounterA

    I've made some assumptions... you're running at 4MHz and that the FOR/NEXT takes 1uS and the TOGGLE takes 1uS, so there's ten 1uS Delays...

    Now if you can't get close enough to 40kHz by adding or subtracting @NOP's try this one...

    For CounterA=1 to 8
    High PulsePin
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    Low PulsePin
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    Next CounterA

    If removing a NOP, try it from BOTH sections, but in the end you might find that you have one less NOP in one or other section... you may end up with an assymetric waveform, but it will be 40kHz.

    It's play time...

  3. #3
    Join Date
    Jul 2009
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    Thanks for your response, I did try, but something very odd happens, I changed it to an infinite loop to produce a frequency that I can measure:

    Loop:
    Toggle PulsePin
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    @NOP
    GOTO Loop

    But for some reason, no matter how many @NOP I add, the frequency does not change and stays at 62.5 KHz

    Any Ideas?

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    This will get you spot-on 40kHz with a 4MHz oscillator.
    Code:
      Cycles VAR BYTE 
      TRISIO.0=0  ' make pin an output first
      GOTO Main   ' jump over Pulse routine to Main
    
     ' @ 4MHz generate Cycles number of 40kHz pulses
    asm
    _Pulse
       bsf  GPIO,0 ; change to whatever port pin you have available
       goto $+1    ; 2uS per GOTO $+1
       goto $+1
       goto $+1
       goto $+1
       goto $+1     
       bcf  GPIO,0
       goto $+1 
       goto $+1
       goto $+1
       goto $+1
       goto $+1
       decfsz _Cycles,f
       goto _Pulse  ; 25uS total for 40kHz carrier
       RETLW 0      ; return to CALLing routine
    ENDASM	  
    
    Main:
      Cycles = 8    ' number of carrier cycles to generate
      CALL Pulse
      PAUSEUS 24
      GOTO Main
      END
    Just enter the number of carrier cycles you need to generate in the Cycles var, and
    CALL the routine.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Jul 2009
    Posts
    13


    Did you find this post helpful? Yes | No

    Default

    Thank you both a million for your help, I had done it with ASM and ENDASM but not as elegant as Bruce did, I just coded the delay using NOPs and the the eight pulses hard coded.

Similar Threads

  1. What's the best way to output 30 to 40 kHz from a 12F683 pin?
    By fizyxman in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st June 2009, 00:08
  2. Resonator Suppliers for 40 MHz?
    By mcphill in forum General
    Replies: 5
    Last Post: - 23rd January 2009, 02:05
  3. with 40 MHZ osc serin2 not work
    By Pedro Santos in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 13th August 2006, 22:56
  4. 40 kHz wave
    By RUBiksCUbe in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 14th December 2005, 20:01
  5. Catching 12 khz
    By SuB-ZeRo in forum Schematics
    Replies: 4
    Last Post: - 15th June 2005, 07:11

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