PDA

View Full Version : Increment a Byte



savnik
- 20th November 2009, 05:20
I have a simple problem but i don't can solve.



pwr var byte

If up = 1 Then
PAUSE 50
pwr = pwr + 1
If pwr >= 255 Then pwr = 255
Endif


If pwr >= 255 always go to 0 and no to 255.Why happen this?

aratti
- 20th November 2009, 07:09
If pwr >= 255 always go to 0 and no to 255.Why happen this?

Since a byte can count up to 255 max when you add 1 to 255 the byte overflow and start from zero again (it cannot be 256).

If you want to keep your variable pwr at 255 then you can skip the add command, or you must use a word variable.


pwr var byte

If up = 1 Then
PAUSE 50
If pwr = 255 Then Skip00
pwr = pwr + 1
Skip00:
Endif


Al.

savnik
- 20th November 2009, 07:29
Thank you. It is work.
I don't want use word variable , because pwr is for duty of HPWM.

Acetronics2
- 20th November 2009, 09:52
Hi, Savnlik

You just can use :




For I = 1 to 255

PAUSE 50
HPWM 1,I,1000 ' The reason of your loop ( BTW ...). 1000 is pwm freq.

NEXT I



or ...




DO

I = I + 1

....

While I < 255





For limiting Numbers:





I = J MIN 255 ' I is the smaller ( MIN ) number between J and 255 ...



Alain

HenrikOlsson
- 20th November 2009, 10:58
Or,

If Pwr > 254 then pwr = 255

savnik
- 20th November 2009, 13:01
Or,

If Pwr > 254 then pwr = 255
I had test and not work.Because next have count 255 + 1 = 256

savnik
- 20th November 2009, 13:03
Hi, Savnlik

You just can use :




For I = 1 to 255

PAUSE 50
HPWM 1,I,1000 ' The reason of your loop ( BTW ...). 1000 is pwm freq.

NEXT I



or ...




DO

I = I + 1

....

While I < 255





For limiting Numbers:





I = J MIN 255 ' I is the smaller ( MIN ) number between J and 255 ...



Alain
I change the duty manually with two buton's (up and down)

HenrikOlsson
- 21st November 2009, 08:10
I had test and not work.Because next have count 255 + 1 = 256
Ouch, yes of course... my mistake, sorry.

savnik
- 21st November 2009, 11:09
Ouch, yes of course... my mistake, sorry.
No problem.
aratti code solve my problem.