Shop OBEX P1 Docs P2 Docs Learn Events
ASM Question — Parallax Forums

ASM Question

tom90tom90 Posts: 55
edited 2008-04-18 00:04 in Propeller 1
Hey All,

I have been writing some code for an A/D now for quite some time.· I have the A/D code working, now I am trying to add averaging to the code.· I have a cog that takes the values from a cog running the code for the A/D (ADCCog) and adds them 100 times.· The next step will be for the adding cog to pass this value to a cog that divides by 100.

I have been having problems with my adding cog.· The general loop structure is like this:

:loop
read value from A/D
Add value to another variable (the total)
waitcnt (for loop syncronization)
djnz (100 times to add 100 values)

write value of the total (happens every 100th time)
reset counter
jump to previous loop


When I run the code, one of two things will happen.· First, this will output zero.· Second, this will output the same value as the A/D.· However, if the value from the A/D is changed, the output will stay the same value as it was when it first started up.· Why is this happening?

If I move the write command into the top loop and have it output every value, it works just fine.

How can I make this work so that it only outputs to total of 100 added values?

Thanks
Tom

Attached is my code (much better than my above example)

Comments

  • RaymanRayman Posts: 14,232
    edited 2008-04-17 18:26
    I see two things...

    1. counts is not initialized to 100 for the first loop.

    2. the last program line: "jmp :loop" should be "jmp #:loop"
  • tom90tom90 Posts: 55
    edited 2008-04-17 18:47
    Rayman

    Thank you Thank you Thank you

    Wow those were pretty obvious mistakes

    I have moved my code around so much trying to get it to work I guess I just messed up.



    Thanks

    Tom
  • AribaAriba Posts: 2,687
    edited 2008-04-18 00:04
    If you add not 100 values, but a power of 2, then the division to build the average is only a single shift instruction, and can be done in the same cog.

    loop:
    read ADC
    add to Total
    waitcnt
    djnz 128 times '128=2^7
    shr Total,#7 '<- the division

    Use SHR for unsigned values or ASR for signed values.

    Andy
Sign In or Register to comment.