COUNTER
Moderator: frank
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
Experimental Noize
Frank, this is my code for turning a led to its full bright in 10 seconds. Dacl it is attached to a led: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.
equ counter reg1
rdax counter,1
sof 1,1/327680
wrax counter,1
wrax dacl,0
The problem is that led is always off.
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
Experimental Noize
Frank, i tried your suggestion but Asm says: <0000>[ Pass 2] [ 1005] Line: 15 "sof 1,count " - ERROR:Coefficient out of range - 34.000000frank 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.
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
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
Experimental Noize
Ok so i tried this, but led is always off. I assume that mi increment is rounded to zero again:frank wrote:The sof will not work in this case, also you cannot pass sof the address of a register. Try an RDAX.
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
"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
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
Experimental Noize
-
- Posts: 338
- Joined: Mon Nov 12, 2012 1:12 pm
- Contact:
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
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.
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.
Could be longer if you use a positive value as the terminal count but that gets messy with more instructions.
Frank Thomson
Experimental Noize
Experimental Noize
-
- Posts: 338
- Joined: Mon Nov 12, 2012 1:12 pm
- Contact:
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).
-
- Posts: 71
- Joined: Fri Dec 12, 2014 10:45 pm
- Location: Detroit,MI
- Contact:
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?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).
-
- Posts: 338
- Joined: Mon Nov 12, 2012 1:12 pm
- Contact:
-
- Posts: 71
- Joined: Fri Dec 12, 2014 10:45 pm
- Location: Detroit,MI
- Contact: