COUNTER

Algorithm development and general DSP issues

Moderator: frank

Post Reply
Mcfly
Posts: 46
Joined: Fri Mar 08, 2013 1:38 pm
Location: Argentina

COUNTER

Post by Mcfly » Sat Aug 29, 2015 7:25 am

Hi! i would like to know how to make a counter that finish counting at 1, and takes X seconds to get there.

Thanks!

frank
Posts: 991
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Post by frank » Sun Aug 30, 2015 9:26 am

Just increment a register every time through the code and when it hits the value you want do what you need. Since it will increment every sample period the time it takes will be (1/samplerate)* count seconds.
Frank Thomson
Experimental Noize

Mcfly
Posts: 46
Joined: Fri Mar 08, 2013 1:38 pm
Location: Argentina

Post by Mcfly » Sat Sep 05, 2015 7:33 am

frank wrote:Just increment a register every time through the code and when it hits the value you want do what you need. Since it will increment every sample period the time it takes will be (1/samplerate)* count seconds.
Frank, this is my code for turning a led to its full bright in 10 seconds. Dacl it is attached to a led:

equ counter reg1

rdax counter,1
sof 1,1/327680
wrax counter,1
wrax dacl,0

The problem is that led is always off.

frank
Posts: 991
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Post by frank » Sat Sep 05, 2015 1:06 pm

sof only accepts a S.10 bit value for the adder so 1/327680 is getting rounded to 0. You will want to clear the acc then OR the value into it since you will need 19 magnitude bits to represent 1/327680 then add the current count value.
Frank Thomson
Experimental Noize

Mcfly
Posts: 46
Joined: Fri Mar 08, 2013 1:38 pm
Location: Argentina

Post by Mcfly » Tue Sep 08, 2015 10:48 pm

frank wrote:sof only accepts a S.10 bit value for the adder so 1/327680 is getting rounded to 0. You will want to clear the acc then OR the value into it since you will need 19 magnitude bits to represent 1/327680 then add the current count value.
Frank, i tried your suggestion but Asm says: <0000>[ Pass 2] [ 1005] Line: 15 "sof 1,count " - ERROR:Coefficient out of range - 34.000000

equ counter reg1
equ count reg2
equ increment 1/327680

skp run,endclr

clr
rdax increment,1 ;read the increment
or %00000000000001111111111 ;mask 10 bits
wrax count,0

endclr:

rdax counter,1
sof 1,count
wrax counter,1
wrax dacl,0

frank
Posts: 991
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Post by frank » Wed Sep 09, 2015 11:33 am

The sof will not work in this case, also you cannot pass sof the address of a register. Try an RDAX.
Frank Thomson
Experimental Noize

Mcfly
Posts: 46
Joined: Fri Mar 08, 2013 1:38 pm
Location: Argentina

Post by Mcfly » Wed Sep 09, 2015 7:38 pm

frank wrote:The sof will not work in this case, also you cannot pass sof the address of a register. Try an RDAX.
Ok so i tried this, but led is always off. I assume that mi increment is rounded to zero again:

equ counter reg1
equ increment 0.00001 ;this coefficient turns the led on in 2 secs.)

rdax counter,1
rdax increment,1
wrax counter,1
wrax dacl,0

frank
Posts: 991
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Post by frank » Thu Sep 10, 2015 9:57 am

"rdax increment,1" requires a register as an argument, not a static value. Read the SpinAsm docs for what each instruction accepts as a parameter.

Do what I said in the past:
clear the accumulator
OR in the value to increment with
Add the counter register

This is as simple as:
sof 0,0
or 0x00000f ;what ever number
rdax counter, 1

acc now has the incremented value
Frank Thomson
Experimental Noize

Digital Larry
Posts: 279
Joined: Mon Nov 12, 2012 12:12 pm
Contact:

Post by Digital Larry » Tue Jun 07, 2016 7:30 am

Hi Frank,

I think I get what is going on with your suggestion. (I am trying to develop something like an envelope generator).

e.g.

equ counter1 reg0
equ max1 0x0ff ; reset limit for counter 1

clr
or 0x01
rdax counter1, 1.0
wrax counter1, 1.0
xor max ; this will be all zeroes if counter1 == max
skp zro, reset
ldax counter1
skp run, output
reset:
wrax counter1, 0 ; acc was zero when we jumped here, so counter1=0
output:
wrax dacl, 0

1 registers, 10 instructions

I didn't actually try running this but I think it would make a very low amplitude ramp-up sawtooth waveform.

I could do this slightly differently by allowing myself to use SOFs. The question then is, what is my resolution? And the next question is, how certain am I that I can hit zero exactly (so I can use skp zro) by decrementing by 1/count?

Actually, since I can't start at +1.0 using SOF, I could start at -1.0 and increment upwards.

So, for example:

equ counter1 reg0
equ increment 1/256

skp run, end_init
sof 0.0, -1.0
wrax counter1, 0
end_init:

rdax counter1, 1.0
sof 1.0, increment ; add the increment to count upwards towards zero
wrax counter1, 1.0
skp zro, reset ; or, skp gez, if I'm not sure I can hit zero exactly
skp run, output
reset:
sof 0,-1.0
wrax counter1, 1.0
output:
wrax dacl, 0

1 register, 11 instructions

Conceptually the first approach uses 1 fewer instructions and is a bit easier to grasp, as integers are thought of as integers, rather than fractions. Under some circumstances one might wish to use that -1.0 to 0 ramp in some way other than just for counting.

I just wrote the counter to the DACL as an example, and I understand the amplitude of the second case will be much higher than the first. In fact I'm not planning to use the counter this way - only for counting time during which other things will happen when the timers expire.

Thanks for any perspective.

DL
Last edited by Digital Larry on Tue Jun 07, 2016 2:54 pm, edited 1 time in total.

frank
Posts: 991
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Post by frank » Tue Jun 07, 2016 2:35 pm

Personally I like to load a negative count and count to zero so I can use a GEZ in a skip and it doesn't matter if I hit zero exactly or not since the skip will happen on any positive value. As far as resolution the max would be 1/8388608 assuming you load a 0x800000 into a reg (max negative number) and add 0x000001 each period and the terminal count is 0. At a sample rate of 32768 that is 256 seconds to time out, over 4 minutes.

Could be longer if you use a positive value as the terminal count but that gets messy with more instructions.
Frank Thomson
Experimental Noize

Digital Larry
Posts: 279
Joined: Mon Nov 12, 2012 12:12 pm
Contact:

Post by Digital Larry » Sat Jun 18, 2016 4:54 pm

I've found that using decimal representation for counters and increments is a little easier to debug as I can hang my scope on the FV-1 output pin (to avoid AC coupling effects) and observe the counter as it ramps from -1.0 to 0.0. I was trying to do the same simply incrementing a 24-bit value by 1 (lowest bit) and it's not possible to view the waveform as the amplitude is way down for typical counts (I'm trying to make pulses in the 100 to 500 msec range).

MacroMachines
Posts: 71
Joined: Fri Dec 12, 2014 9:45 pm
Location: Detroit,MI
Contact:

Post by MacroMachines » Sat Jun 18, 2016 6:20 pm

Digital Larry wrote:I've found that using decimal representation for counters and increments is a little easier to debug as I can hang my scope on the FV-1 output pin (to avoid AC coupling effects) and observe the counter as it ramps from -1.0 to 0.0. I was trying to do the same simply incrementing a 24-bit value by 1 (lowest bit) and it's not possible to view the waveform as the amplitude is way down for typical counts (I'm trying to make pulses in the 100 to 500 msec range).
I was trying to scope a direct pot output the other day to see if my ADC for POT0 was working and it seems like there is a DC filter on the outs, is this AC coupled or can it output DC?
http://MacroMachines.net
Digital Control for your Analog Soul.

Digital Larry
Posts: 279
Joined: Mon Nov 12, 2012 12:12 pm
Contact:

Post by Digital Larry » Sat Jun 18, 2016 8:42 pm

The FV-1 dev board definitely AC couples the outputs. I soldered a clipped off resistor lead to one of the coupling caps connected to the FV-1 right channel out so I could attach a scope lead.

MacroMachines
Posts: 71
Joined: Fri Dec 12, 2014 9:45 pm
Location: Detroit,MI
Contact:

Post by MacroMachines » Mon Jul 11, 2016 7:05 am

Cool, I ended up soldering a lead to the dacl pin directly. I am suprised at how much slew is on the pots. Also this thread is very useful stuff, it may be helpful to some of my programs.
http://MacroMachines.net
Digital Control for your Analog Soul.

Post Reply