ASM Question
tom90
Posts: 55
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)
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
1. counts is not initialized to 100 for the first loop.
2. the last program line: "jmp :loop" should be "jmp #:loop"
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
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