PDA

View Full Version : My third Attempt At ASM



SailerMan
12-14-2009, 08:25 AM
Last night I wrote a Spin program to Decode and Send Sony IR Signals.. I got it all working and decided to see if I could write it in PASM.

I'm a novice in ASM period but with a lot of reading and more reading my first program was a simple binary counter to LED's

My second program sent data to a 74hc595 Serial to Parallel Latch.

My Third Program decodes a sony remote and displays Key Value and Device. I was hoping that someone with more experience than me can read over this program and show me some code optimization techniques.
Currently this program works!!

Thanks in Advance for any help,
Eric



Con
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000



Var
Long P
Long O

Obj
Debug : "TV_Text"


Pub Main|Temp,Index
P:=16
Debug.start(12)
CogNew (@Entry,@P)

Repeat

Debug.Out(1)
Debug.Bin(O,20)
Debug.Out(13)
Debug.Str(String("Value="))
Debug.Dec(O & %1111111)
Debug.Str(String(" ",13) )
Debug.Str(String("Device="))
Debug.Dec(O>>7 & %11111)
'WaitCnt (Cnt+1_000_000)

Dat

Entry ORG 0


Mov Temp1, Par
Mov PAddr, Temp1
Add Temp1, #4
Mov OAddr, Temp1
Add Temp1, #4
Mov DAddr, Temp1

RdLong Pin, PAddr
RdLong PinMode,PAddr

mov Pin, #1 wz ' Configure DataPin
shl Pin, PinMode
muxz dira, Pin ' Set Pin as Input

Mov CTRA, #0
Mov CTRA, CntrModeIn ' Logic !A
Movs CTRA, PinMode ' Input Pin Number
Mov Frqa, #1 ' PHSA + 1

Loop

Call #Pulse ' Get Input Pulse From Pin

CMP Duration,PStart WC ' Is pulse a Start Pulse??
IF_AE Call #Start ' If Yes Then Start Reading
JMP #Loop ' If No then Wait for good Start Pulse
Start
Mov Sample,Reset ' Grab 20 Pulses
:Loop
Call #Pulse ' If Pulse is Between Low and High
CMP Duration,PLowBit WC ' Set Bit = #0
IF_AE CMP Duration,PHighBit WC
IF_B or Data,#0
IF_B JMP #:Skip

CMP Duration,PHighBit WC ' If Pulse is Between High and Start
IF_AE CMP Duration,PStart WC ' Set Bit = #1
IF_B or Data,#1
:SKIP

Shl Data,#1 ' Shift Into Data Register


DJNZ Sample,#:Loop ' Repeat until all 20 bits are accumulated

Rev Data,#11 ' Put bits in proper order
WRLong Data,OAddr ' Send to Spin

Start_Ret Ret

PULSE WaitPNE Pin,Pin ' Grab Pulse on Input PIN
Mov PHSA,#0
WaitPEQ Pin,Pin
WaitPNE Pin,Pin

Mov Duration,PHSA 'Accumulate PHSA
PULSE_RET RET

Pin Long 0
PinMode Long 0
PAddr Long 0
OAddr Long 0 'Back to Spin Address
DAddr Long 0

CNTRModeOut Long (%00100 << 26 ) | (%001 << 23) | (0 << 9) 'NCO
FmodeOut Long %1_1111_0010_0001_0010_1101 '38KHZ
CNTRModeIn Long (%10101 << 26 ) | (%010 << 23) | (0 << 9) 'Logic !A

Temp1 Long 0


Duration Long 0

PStart Long 190_000
PLowBit Long 50_000
PHighBit Long 90_000
Sample Long 0
Reset Long 20

Data Long 0

JonnyMac
12-14-2009, 08:47 AM
I wrote Sony decoding and encoding objects for my January column. Have a look at the code -- perhaps you'll find something useful.

SailerMan
12-14-2009, 09:05 AM
Thanks as always. I wrote mine without looking at any other code... Now that I look at yours I like how you move the Carry bit into the right. Thanks for all of the code..

JonnyMac
12-14-2009, 09:46 AM
You'll note, too, that my rx object allows for any valid SIRCS code -- it's not fixed at 20 bits. I have a Sony TV/DVD remote that mixes 12- and 20-bit commands so it's important to discriminate them. If you follow the code you'll see that my program allows for bits to come in for 44ms; the Sony spec says that a code should be transmitted in 45. I use 44 to give a little time (more than I need, really) to clean-up and report what was just received to the hub.

SailerMan
12-14-2009, 11:28 AM
Yes I see that with the CheckFrame... It took me a little bit of time to figure out what you were doing.. From the start Bit you are accepting all bits until CheckFrame times out or 20bits.. I like it! :)

JonnyMac
12-14-2009, 11:42 AM
Exactly. When waiting for the start of a new frame a falling edge (start of new bit) resets the frame timer; if this bit is in fact a start bit then the code keeps going, otherwise the code tries again. What this means is that the 44ms window starts at the leading edge of the start bit. What I like is being able to use one counter in free run mode (frame timer) and the other in negative detect mode (bit width measurement); makes the code pretty darn easy, I think.

SailerMan
12-14-2009, 08:26 PM
I agree... Even the code I wrote that is stuck in 20 bit mode was fairly easy to write for a beginner like me it was good practice.. I am going to continue to improve me code for learning purposes... I don't want to duplicate yours.. but I like the guidance.

Thanks,
Eric

PS I'm not sure if you remember instructing me on the SX... Two motor controller... I've decided to move my ASM efforts to the Propeller. Your help back a few months helped a great deal

Graham Stabler
12-14-2009, 10:42 PM
I noticed something, you clear ctra and then immediately initialise it with mov, so no need to clear it first, you wasted four clock cycles :) :O

Looks like you are doing great.

Graham

SailerMan
12-15-2009, 12:42 AM
Thanks Graham... those are the things I'm looking for... Much appreciated.