​ ​ ​ ​ Maths making use of variable overflow
Results 1 to 5 of 5

Thread: Maths making use of variable overflow

  1. #1
    Member
    Join Date
    Jan 2016
    Location
    Alexandria, Scotland
    Posts
    74

    Default Maths making use of variable overflow

    Happy Friday!

    I was wondering if someone on here would be able to confirm something for me:
    I'm planning on using a relatively simple method for having timed functions carried out by the picaxe, while still scanning pins etc. For example, when a pin is detected to be high, 2 minutes later the picaxe sets another pin high. If the input pin is set to low, the output is immediately turned off, and the timer reset.

    In my big program, I'll have multiple functions using the timer at once, so the functions store the value of the timer when the functions is originally called, then find the difference between the "realtime" timer value and the stored value.

    My question is, however, if and when the timer overflows, would the calculations still work? From my reading, the maths should also simply overflow, so for example 10-245 = 20. Could someone confirm this?

    Code:
    'b0 = number of procedures using timer
    'w2 = timer in multiples of 25ms
    
    main:
    	if pinB.1 = 1 then
    		call sampleTimerFunction
    	else
    		call endTimeruse
    	end if
    	
    	'insert additional functions here
    	
    	call timer
    	
    	goto main
    
    end
    
    
    timer:
    	if b0 = 0 then		'if no functions are using timer, reset to 0 and return
    		w2 = 0
    		return
    	end if
    	
    	pause 25			'wait for 25 ms and add 1 to timer value
    	w2 = w2 + 1
    	return
    
    sampleTimerFunction:
    	
    	'b1 = has function passed the initial stage?
    	'w3 = repository for timer value when function is initially called
    	'b2 = latch
    	
    	if b1 = 0 then
    		w3 = w2								'store initial timer value
    		b0 = b0 + 1							'add this function to the "list" of functions currently using the timer
    		b1 = 1								'skip this stage next time function is called
    	end if
    	
    	if w2 - w3 = 4800 or b2 = 1 then		'have 120s elapsed?
    		high B.0							'do something
    		b2 = 1								'set latch - program will now always do the above until it is reset
    	end if
    	
    	return
    
    endTimerUse:
    	b0 = b0 - 1								'remove this function from the "list" of functions currently using the timer
    	b2 = 0									'reset latches from sampleTimerFunction
    	b1 = 0
    	w3 = 0									'clear stored time value
    	low B.0									'end whatever was started when the timer elapsed
    	return

  2. #2
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,884

    Default

    10 - 245 = -235, is $FF15, which as a positive binary integer is $FF15 (65301) as a word or $15 (21) as a byte.

    You can confirm that by simulating the following code under PE6 -

    Code:
    b1 = 10
    b2 = 245
    b0 = b1 - b2
    SerTxd( #b0, " = ", #b1, " - ", #b2, CR, LF )
    Code:
    w1 = 10
    w2 = 245
    w0 = w1 - w2
    SerTxd( #w0, " = ", #w1, " - ", #w2, CR, LF )
    That 21 would seem to be the correct result -

    Code:
    245 246 247 248 249 250 251 252 253 524 255
        +1  +2  +3  +4  +5  +6  +7  +8  +9  +10
    
    0   1   2   3    4   5   6   7   8   9  10
    +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21

  3. #3
    Member
    Join Date
    Jan 2016
    Location
    Alexandria, Scotland
    Posts
    74

    Default

    That should work perfectly then :-) even though I made a mistake in my original maths... (oops)

    cheers Hippy!

  4. #4
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,884

    Default

    I had expected 20 too, which is why there's that proof it is 21 :-)

    245 is of course 11 below zero, 10 below 255. Things can indeed get complicated and/or confusing when passing through zero.

  5. #5
    New Member
    Join Date
    Jan 2014
    Location
    Surrey
    Posts
    8

    Default

    I've often used a spreadsheet to work out this sort of thing. MOD(x+256,256) for byte arithmetic or MOD(x+65536,65536) for word arithmetic will give you the "positive" version of negative numbers and you can check the arithmetic very easily.

Bookmarks

Posting Permissions

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