Shop OBEX P1 Docs P2 Docs Learn Events
Tricky assembly code: Can U tell me what is going on? — Parallax Forums

Tricky assembly code: Can U tell me what is going on?

Alexandre_PVAlexandre_PV Posts: 4
edited 2008-12-01 00:39 in Propeller 1
Hello,

I'm new using the propeller chip and I need to write a ADC assembly routine but now I'm having some trouble in doing so. My ADC is the MCP3208 but I'm running it at 5V and therefor the drivers for this ADC available at the OBEX do not work for they use a 1-wire protocoll and I need a 2-wire protocol. So, I have come across something that honestly I have never seen before in any Atmel nor Microchip microntrollers in the past: I write the assembly code and it works fine, until I write a 'nop' in on specific position of the code and the code is just frozen. Not it is not a loop error, its just a plain line of code written in between instructions. The code is frozen for 53 seconds and then starts to work again. 53 seconds it how long the internal 32bit counter would take to start from zero and reach its maximum number using a 80Mhz clock so I believe that there is something to do with the system clock. Now for the code:

THIS WORKS

DAT
{Toggle P16}
ORG 0 'Begin at Cog RAM addr 0

getCommand rdlong bits2, par wz ' wait for command
if_z jmp #getCommand

Toggle mov bits, #20
mov dira, Pins 'Set output pins
mov Time, cnt 'Calculate delay time (move 32bit system counter to Time reserved long)
add Time, #9 'Set minimum delay here

loop
waitcnt Time, Delay
or outa, CS_reg
mov bits, #10


bloop waitcnt Time, Delay
or outa, CLK_reg
waitcnt Time, Delay
andn outa, CLK_reg
djnz bits, #bloop

waitcnt Time, Delay
andn outa, CS_reg
nop




jmp #loop


Pins long (|<CLK) | (|<DIN) | (|<CS) 'transform numbers into bit positions and OR them all together
CLK_reg long (|<CLK)
CS_reg long (|<CS)
Delay long 500 'Clock cycles to delay
bits res 1
bits2 res 1
Time res 1 'System Counter Workspace


THIS DOESN'T WORK

DAT
{Toggle P16}
ORG 0 'Begin at Cog RAM addr 0

getCommand rdlong bits2, par wz ' wait for command
if_z jmp #getCommand

Toggle mov bits, #20
mov dira, Pins 'Set output pins
mov Time, cnt 'Calculate delay time (move 32bit system counter to Time reserved long)
add Time, #9 'Set minimum delay here
nop

loop
waitcnt Time, Delay
or outa, CS_reg
mov bits, #10


bloop waitcnt Time, Delay
or outa, CLK_reg
waitcnt Time, Delay
andn outa, CLK_reg
djnz bits, #bloop

waitcnt Time, Delay
andn outa, CS_reg
nop




jmp #loop


Pins long (|<CLK) | (|<DIN) | (|<CS) 'transform numbers into bit positions and OR them all together
CLK_reg long (|<CLK)
CS_reg long (|<CS)
Delay long 500 'Clock cycles to delay
bits res 1
bits2 res 1
Time res 1 'System Counter Workspace


Can you see the difference? A simple, small, tiny, 'nop' instruction after the 'add'. Is there anything I should know about how the program is written to the local memory in the chip in order to make it work? Any help would be most welcome.

Kind regards,

Alex

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-01 00:39
    The 53 seconds is the major hint. This is the time required for the system clock counter (CNT) to wraparound with an 80MHz system clock. Any time you see this sort of behavior, think about any WAITCNT instruction. You need to increase the minimum delay time to allow for the time required for the NOP.
Sign In or Register to comment.