PDA

View Full Version : Generating a series of high frequency pulses with pre-defined length.



CuriousOne
- 4th October 2016, 08:50
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:



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:



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.

HenrikOlsson
- 4th October 2016, 10:08
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.

richard
- 4th October 2016, 10:16
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

CuriousOne
- 4th October 2016, 11:43
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?

HenrikOlsson
- 4th October 2016, 11:59
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.

CuriousOne
- 4th October 2016, 13:31
I had issue with read-modify-write on 628A. 1829 in same conditions works just fine.

Art
- 8th October 2016, 11:47
RWM again lol.

Some ideas...

Slower, but at least it’s 50% duty cycle


‘ Not RWM safe!

flipflop var bit

cycle:
flipflop = flipflop + 1
portb.0 = flipflop
goto cycle


can you afford to waste a whole port?


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

CuriousOne
- 8th October 2016, 21:24
Thanks, but how control amount of cycles?

richard
- 9th October 2016, 00:52
‘ 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


'************************************************* ***************
'* 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

Art
- 9th October 2016, 03:51
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.

JimAvanti
- 6th March 2017, 22:16
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





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:



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:



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.