Continue to Site

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.

  • 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.

How to generate a pulse of 20kHz?

Status
Not open for further replies.
I tried the following code but it didn't work


movlw .25
movwf M1
movlw .25
movwf M2


;goto main

main:

bsf GPIO,5
nop
decfsz M1,1
goto $-2
bcf GPIO,5
nop
decfsz M2,1
goto $-2

goto main
 
I want to create a delay loop in microseconds

as in change values of M1 and M2 to any microseconds and get the desired frequency

i tried the following code but I have to enter one fifth time of the duration of pulse to get the desired frequency

main:
movlw .10 ;on time of pulse #1
movwf M1
bsf GPIO,5


nop
decfsz M1,1
goto $-2
;nop

movlw .10 ;off time of pulse #1
movwf M2
bcf GPIO,5
nop
decfsz M2,1
goto $-2

goto main
 
It seems your goals have changed since you started this thread. What exactly would you like to do? One or two signals? Frequency range?
 
I just want to know if he is planing to use this to feed a amp to make sound controlled sparks
 
@mike

actually i am trying to get frequencies in kHz range at GPIO5, GPIO4, GPIO3, GPIO2, GPIO1

i have to control the frequencies at these GPIO pins.

i wanna be able to just change the duration of the pulse and change the frequency at each to any desired value in the kHZ range.

using the nop instructions multiple times works but i want to create a loop that will take care of any duration.


please let me know if you have any idea

thank you
 
That's a tall order you could change frequency but it would change in sink. And GP3 is input only.

And that would knock Mr RB idea in the head

And if you used the timer I don't think you could keep all four output pins in the khz range.
 
That really is a pretty tall order.

You might consider using the Bresenham phase accumulator portion of DDS (direct digital synthesis). Just copy the most significant bit from each of the 16 bit phase accumulators onto the output pins for a 50% duty cycle square wave. If you need variable pulse width output then you can add a duty cycle value to the most significant phase accumulator bits and pull the output from the Carry flag. Something like this perhaps (please note crystal frequency is 8388608-Hz);

Code:
;
;  DDS Frequency (Fdds) = 65536 Hz (Fosc = 8.388608 MHz)
;  Resolution (Fres) = Fdds / 2^16 = 1.0 Hz (16 bit accumulators)
;  1.0-KHz phase offset = 1000 / Fres = 1000
;  2.1-KHz phase offset = 2100 / Fres = 2100
;
        radix   dec
dds1
        clrf    shadow          ;                                 |B0
        movlw   low(2100)       ; phase offset for 2100-Hz        |B0
        addwf   accum1+0,F      ; accum1 += phase1                |B0
        movlw   high(2100)      ;                                 |B0
        skpnc                   ;                                 |B0
        addlw   1               ;                                 |B0
        addwf   accum1+1,F      ;                                 |B0
        movlw   128             ; 64=25%, 128=50%, 192=75%        |B0
        addwf   accum1+1,W      ; (pulse width)                   |B0
        rlf     shadow,F        ; collect the Carry bit           |B0
dds2
        movlw   low(1000)       ; phase offset for 1000 Hz        |B0
        addwf   accum2+0,F      ; accum2 += phase2                |B0
        movlw   high(1000)      ;                                 |B0
        skpnc                   ;                                 |B0
        addlw   1               ;                                 |B0
        addwf   accum2+1,F      ;                                 |B0
        movlw   128             ; 64=25%, 128=50%, 192=75%        |B0
        addwf   accum2+1,W      ; (pulse width)                   |B0
        rlf     shadow,W        ; collect the Carry bit           |B0
        movwf   GPIO            ; update GP1 & GP0 outputs        |B0
        DelayCy(32-22)          ; 32 cycle DDS loop               |B0
        goto    dds1            ;                                 |B0
;
You might consider researching DDS, though I'm not sure you'll be able to get the number of outputs and the frequency range that you're lookin' for. This example will get you two outputs of up to 16384-Hz with 25%, 50%, or 75% pulse widths.

Regards, Mike
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top