PDA

View Full Version : Just looking for an explanation



ERMEGM
- 18th June 2013, 06:28
Hi guys,

OK, encountered a weird result and was wondering if anyone could explain it. I am using a PIC12F683 and I wrote A LOT of lines of code. I actually filled the chip and had to make modifications to the code to consolidate space. Anyway, I found that when I wrote the code,

FOR fade = 0 to 255 STEP fadevalue

PWM left, fade, 1
PWM right, fade, 1

gosub buttoncheck ' check for a button press

NEXT fade

FOR fade = 255 to 0 STEP -fadevalue

PWM left, fade, 1
PWM right, fade, 1

gosub buttoncheck ' check for a button press

NEXT fade

the chip compiled just fine, leaving VERY LITTLE space left. But when the fade result was a little too long for me and I rewrote the code to,

FOR fade = 0 to 150 STEP fadevalue

PWM left, fade, 1
PWM right, fade, 1

gosub buttoncheck ' check for a button press

NEXT fade

FOR fade = 150 to 0 STEP -fadevalue

PWM left, fade, 1
PWM right, fade, 1

gosub buttoncheck ' check for a button press

NEXT fade
I got an error message saying I was out of space. By just changing the value from 255 to 150, I ran out of space? Not sure why this was happening, but I wound up going through my code again and consolidating some more to make it fit on the chip.

I just want to know what's going on and why it was doing that. Anybody know why?

Thanks,
Tony

Acetronics2
- 18th June 2013, 07:17
Probably because this compiler is intelligent ...

0 to 255 can be obtained without any value calculation ( yesss ... 255 + 1 = 0 for a byte ! ) , but 0 -150 need to compare actual value to 150 ( implies a calculation and resetting value to 0 when 150 reached ) @ each loop ...

just a possible explanation ...

Alain

ERMEGM
- 19th June 2013, 06:49
Huh? Not really sure what you are talking about. Can you elaborate a little more please? Thanks.

peterdeco1
- 20th June 2013, 19:12
I think what Alain means is when you go from 0 to 255, it simply adds 1 to the present number. When it reaches 255 it doesn't do any resetting. It simply adds 1 to 255 and it goes back to 0 itself. On 0 to 150 it has to constantly monitor the number and reset to 0 after 150. This uses more memory. I just learned a lesson from your post myself.

Demon
- 22nd June 2013, 12:54
Comparing the generated Assembler code might give more clues.

Robert

Art
- 25th June 2013, 03:00
Yes the generated assembler would be smaller.
An example of two BASIC IF...THEN comparisons in assembler


@ movf _Variable ,W 'IF Variable = $A0 THEN...
@ sublw 0xA0 'subtract comparison
@ btfss status ,Z 'test if result is zero
// condition is here

@ movf _Variable ,W 'IF Variable = $00 THEN...
@ btfss status ,Z 'test if result is zero
// condition is here


You are always testing for zero, helps if the comparison is zero!