Increment a Byte


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302

    Default Increment a Byte

    I have a simple problem but i don't can solve.

    Code:
    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?

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    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.

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

    Al.
    Last edited by aratti; - 20th November 2009 at 07:13.
    All progress began with an idea

  3. #3
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Thank you. It is work.
    I don't want use word variable , because pwr is for duty of HPWM.

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink

    Hi, Savnlik

    You just can use :

    Code:
    For I = 1 to 255
    
    PAUSE 50
    HPWM 1,I,1000              ' The reason of your loop ( BTW ...). 1000 is pwm freq.
    
    NEXT I
    or ...

    Code:
    DO
    
    I = I + 1
    
    ....
    
    While I < 255


    For limiting Numbers:


    Code:
    I = J MIN 255  ' I is the smaller ( MIN ) number between J and 255 ...
    Alain
    Last edited by Acetronics2; - 20th November 2009 at 09:55.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Or,
    Code:
    If Pwr > 254 then pwr = 255

  6. #6
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HenrikOlsson View Post
    Or,
    Code:
    If Pwr > 254 then pwr = 255
    I had test and not work.Because next have count 255 + 1 = 256

  7. #7
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi, Savnlik

    You just can use :

    Code:
    For I = 1 to 255
    
    PAUSE 50
    HPWM 1,I,1000              ' The reason of your loop ( BTW ...). 1000 is pwm freq.
    
    NEXT I
    or ...

    Code:
    DO
    
    I = I + 1
    
    ....
    
    While I < 255


    For limiting Numbers:


    Code:
    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)

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    I had test and not work.Because next have count 255 + 1 = 256
    Ouch, yes of course... my mistake, sorry.

  9. #9
    Join Date
    Jun 2006
    Location
    Greece
    Posts
    302


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HenrikOlsson View Post
    Ouch, yes of course... my mistake, sorry.
    No problem.
    aratti code solve my problem.

Similar Threads

  1. LCD freeze
    By harryweb in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 5th October 2009, 08:01
  2. Memory use - how much do you use?
    By keymuu in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 10th June 2009, 22:39
  3. byte compression
    By Norbert in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th June 2007, 18:04
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. 16F877 RAM Question
    By Art in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 6th August 2005, 11:47

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts