SOLVED with timing results - Problem with PASM counters (CTRA)
I've been running some programs to see the amount of time it takes for the program to switch a pin from output high to low. This was inspired by Andy's comments on overhead time in the propeller education kit (PEK) lesson on counters to measure RC time.
So far I've run programs on the Prop Prof Development Board where I have a pin (P23) pulled low through a 10k resistor to ground. I use the method from the PEK which sets DIRA and OUTA for the pin high, starts CTRA in POS Detector mode with frqa =1. It then clears the PHSA register, sets DIRA for the pin to input (pulls low), and reads PHSA. The value in PHSA is the number of ticks for the pin to go low. I've added the use of one of the PPDB buttons so I can manually start when the transition happens.
I run programs in Spin, propeller C (using different library functions and memory models), and pfth. They all work and give reasonable answers when compared with the PEK time for a setup which had a capacitor in the circuit (mine didn't).
But when I try writing PASM code to do the same thing, I don't get a reasonable answer -- I don't think the counter is counting.
The code is below. It's messy. And its about my tenth version (one reason it's messy.)
I do get a return value of the PHSA into the Spin code, and I do get a printed result (I tested setting PHSA to #5 in the PASM as well as the normal #0).
The problem is I do not get a value of PHSA that is different than the value entered in the mov phsa statement. If I enter #0, the Spin prints 0, If I enter #5, 5 is printed.
I expected to get a result around 4 ticks.
I even moved the waitpne p0,p0 statement between the "mov phsa, #0" and the "andn dira, pinval" statements so I would have a long delay with P23 high which should have resulted in a high phsa value. But it didn't change.
I also tried connecting P23 to one of the PPDB leds to make sure the pin was high and then went low when the button was pressed. It did.
I've come to the conclusion that I am not starting the CTRA correctly in the desired mode, although I used the same values in the other languages.
Any help would be appreciated.
Tom
So far I've run programs on the Prop Prof Development Board where I have a pin (P23) pulled low through a 10k resistor to ground. I use the method from the PEK which sets DIRA and OUTA for the pin high, starts CTRA in POS Detector mode with frqa =1. It then clears the PHSA register, sets DIRA for the pin to input (pulls low), and reads PHSA. The value in PHSA is the number of ticks for the pin to go low. I've added the use of one of the PPDB buttons so I can manually start when the transition happens.
I run programs in Spin, propeller C (using different library functions and memory models), and pfth. They all work and give reasonable answers when compared with the PEK time for a setup which had a capacitor in the circuit (mine didn't).
But when I try writing PASM code to do the same thing, I don't get a reasonable answer -- I don't think the counter is counting.
The code is below. It's messy. And its about my tenth version (one reason it's messy.)
I do get a return value of the PHSA into the Spin code, and I do get a printed result (I tested setting PHSA to #5 in the PASM as well as the normal #0).
The problem is I do not get a value of PHSA that is different than the value entered in the mov phsa statement. If I enter #0, the Spin prints 0, If I enter #5, 5 is printed.
I expected to get a result around 4 ticks.
I even moved the waitpne p0,p0 statement between the "mov phsa, #0" and the "andn dira, pinval" statements so I would have a long delay with P23 high which should have resulted in a high phsa value. But it didn't change.
I also tried connecting P23 to one of the PPDB leds to make sure the pin was high and then went low when the button was pressed. It did.
I've come to the conclusion that I am not starting the CTRA correctly in the desired mode, although I used the same values in the other languages.
Any help would be appreciated.
Tom
{ time_dir_change_asm.spin
Pin P5 is connected to button 5 (Active Low)
Pin P23 is APIN connected to ground through 10k ohm, and is the CTRA apin (the pin that will switch and timed)
Used CTRA mode POS detect, " CTRA = (1<<29) | 23; "
}
CON
_clkmode = xtal1 + pll16x ' system clock = 80MHz
_xinfreq = 5_000_000
VAR
long pinval, flag
OBJ
pst : "Parallax Serial Terminal"
PUB Init
'Start PST and waits 1 s to click enable button
pst.Start(115200)
waitcnt(clkfreq*2 + cnt)
pinval := 23
flag := 1
cognew(@gotest, @pinval)
repeat while pinval == 23
pst.Str(String(pst#NL, "PHSA = "))
pst.Dec(pinval)
waitcnt(clkfreq/100 + cnt)
pst.Str(String(pst#NL, "PHSA = "))
pst.Dec(pinval)
DAT org
gotest mov addr1, par ' put the return address in addr1
mov dira, pinvalue
or outa, pinvalue ' P23 goes high
mov frqa, #1
mov ctra, ctraval ' start counter, PHSA should start incrementing with P23 high
waitpne p0, p0 'Wait until button 5 is pressed,
mov phsa, #0 ' I also tried #5 just to confirm the value was being passed to Spin
andn dira, pinvalue ' set pin to input, P23 should go low and stop incrementing PHSA
' ********* the next line is the fix that I had to insert ********
mov phsa, phsa
wrlong phsa, addr1
mov pinnum, addr1
add pinnum, #4
wrlong zero, pinnum ' Let Spin know the result is ready
loop nop
nop
nop
jmp #loop
addr1 long 0
pinnum long 23
ctraval long %0_01000_000_00000000_000000_000_010111 '1<<29 | 23
pinvalue long |< 23 '%1000_0000_0000_0000_0000_0000
zero long 0
t1 long 0
p0 long %10_0000 ' pin 5 is button 5
Delay long 600_000_000 'Clock cycles to delay
Time res 1
Comments
I think you can read PHSA only on the source side of the PASM instruction, so try something like:
... mov phsa, phsa wrlong phsa, addr1 ...
AndyThanks for your help.
Just for grins here's my results. I show the language, the code used to make the transition and the number of clocks from phsa. For C I also show the memory model.
PASM mov phsa, #0 andn dira, pinvalue 6 clocks Spin phsa := 0 dira[23] := 0 595 clocks phsa ~ dira[23] ~ 643 phsa ~ dira[23] :=0 592 Prop C PHSA = 0; PHSA is in propeller.h input(pinnum); LMM 147, CMM 707 input() from SimpleTools.h PHSA = 0; low(pinnum); LMM 129, CMM 689 low() from SimpleTools.h PHSA = 0; DIRA = bpinval; LMM 35, CMM 163 DIRA from propeller.h (note: bpinval = DIRA & (~pinval);) (pinval = 0b1 << pin_number;) PHSA = 0; OUTA = opinval; LMM 17, CMM 145, OUTA from propeller.h (note: opinval = OUTA & (~pinval);) pfth ("standard forth" with some propeller words ) 0 phsa! mypin outaclrbit 1137 (slow but I find it useful for trying new things) note: mypin is defined as: 1 23 lshift constant mypin