Pulse Input > Array > Output help
Hi All
Im just playing around with some code that im trying to use to look at incoming pulses, write them to an array and then output them with a specific delay (ie an offset in the array)
Pulses are coming in from a PWM signal (which i want to delay to the output) at around 600hz
my code on a 16f1847 roughly as follows:
Code:
DEFINE OSC 4
OSCCON = %01101010 ' Oscillator internal 4 mhz PLL disabled
CM1CON0.7 = 0 ' Comparator 1 off
CM2CON0.7 = 0 ' Comparator 2 off
CPSCON0.7 = 0 ' Capsense module off
MDCON = %00000000 ' Disable the Modulator
ANSELB = %00000000 ' Select no analog
TRISA=%00000000 ' Port A all output
TRISB=%00001001 ' Port B all output except 3 ,1
PulseArray var WORD [40] ' 40 words, 640 bits total if we can run at 1khz this will allow delay of upto 640ms
ArrayBit var word
ArrayBit=0
RunController:
PulseArray.0[0] = PortB.0 'set the new 0 bit in the array as per b.0
For ArrayBit = 1 to 639
PulseArray.0[ArrayBit] = PulseArray.0[ArrayBit-1] ' move them all over 1x
Next ArrayBit 'Go do next count
toggle portb.4 ' use p4 to check loop time on scope
PortB.5 = PulseArray.0[250] ' output this bit ~ 250ms delay
goto RunController
the problem im seeing is my loop time on p4 is around 3hz, whereas the loop time within the for..next loop is around 3khz
so even if i up my clock speed to 20mhz it looks like its still not going to be fast enough to do this?
Does anyone have any better solutions or advise?
Re: Pulse Input > Array > Output help
what you are looking for is a ring buffer.
this will take ages and is unnecessary, just increment a pointer
Quote:
For ArrayBit = 1 to 639
PulseArray.0[ArrayBit] = PulseArray.0[ArrayBit-1] ' move them all over 1x
Next ArrayBit 'Go do next count
according to this thread you may need to review the parameters
http://www.picbasic.co.uk/forum/show...ht=ring+buffer
Code:
TRISA=%00000000 ' Port A all output
TRISB=%00001001 ' Port B all output except 3 ,1
PulseArray var byte [32] ' 256 bits total if we can run at 1khz this will allow delay of upto 256ms
Arraytail var byte
clear
Arraytail=0
RunController:
PulseArray.0[Arraytail] = PortB.0 'set the last bit in the array as per b.0
Arraytail= Arraytail+1
latb.4 = !latb.4 ' use p4 to check loop time on scope
latb.5 = PulseArray.0[Arraytail] ' output this bit ~ 250ms delay
pauseus 900 ; adj to suit
goto RunController
1 Attachment(s)
Re: Pulse Input > Array > Output help
longer delay cascading arrays upto 700ms
Code:
'****************************************************************
'* Name : delayline.BAS *
'* Author : richard *
'* Notice : *
'* : All Rights Reserved *
'* Date : *
'* Version : 1.0 *
'* Notes : 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 ' Oscillator internal
ANSELA=0
ANSELC=0 ' Select no analog
TRISA=%111111
TRISC=%001111
PulseArray0 var byte [32] ' 1023 bits total this will allow delay of upto 700ms
PulseArray1 var byte [32]
PulseArray2 var byte [32]
PulseArray3 var byte [32]
tail var byte
head var byte
Arraytail var byte
clear
Arraytail=0
RunController:
tail = Arraytail
head = tail+1
PulseArray0.0[tail] = Porta.5 'set the last bit in the array as per b.0
PulseArray1.0[tail]= PulseArray0.0[head]
PulseArray2.0[tail]= PulseArray1.0[head]
PulseArray3.0[tail]= PulseArray2.0[head]
Arraytail = head
latc.4 = !latc.4 ' use p4 to check loop time on scope
latc.5 = PulseArray3.0[head] ' output this bit ~ 700ms delay
pauseus 500 ; adj to suit
goto RunController
Re: Pulse Input > Array > Output help
thanks for the replies and code richard ;)
After i saw your first reply, i messed around a little and came up with this
Code:
DEFINE OSC 4
OSCCON = %01101010 ' Oscillator internal 4 mhz PLL disabled
CM1CON0.7 = 0 ' Comparator 1 off
CM2CON0.7 = 0 ' Comparator 2 off
CPSCON0.7 = 0 ' Capsense module off
MDCON = %00000000 ' Disable the Modulator
ANSELB = %00000000 ' Select no analog
TRISA=%00000000 ' Port A all output
TRISB=%00001001 ' Port B all output except 3 ,1
PulseArray var byte[80] ' 88 bytes 640 bits total
ArrayTail var word
GetThisPulse var word
Difference var word
Offset var word
Arraytail=0
GetThisPulse=0
Offset = 250 '(250)
' ---------- COLD START
' set all unused pins low
Low PortA.0
Low PortA.1
Low PortA.2
Low PortA.3
Low PortA.4
'Low PortB.0
Low PortB.1
Low PortB.2
Low PortB.4
Low PortB.5
Low PortB.6
Low PortB.7
Pause 25
clear
Arraytail=0
RunController:
PulseArray.0[Arraytail] = PortB.0 'set the last bit in the array as per b.0
if arraytail >= 639 then arraytail = 0 ' circle
Arraytail= Arraytail+1
latb.4 = !latb.4 ' use p4 to check loop time on scope
if offset > arraytail then
difference = offset-arraytail
GetThisPulse = 639 - difference
else
GetThisPulse = ArrayTail - offset
endif
latb.5 = PulseArray.0[GetThisPulse] ' output this bit
pauseus 300 ; adj to suit
goto RunController
it has compiled ok etc but I havent had time to scope it, but i think your second reply is a much cleaner and more practical solution