Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Best way to do modulus?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
saqmaster

Bascom Member



Joined: 10 Apr 2004
Posts: 42
Location: United Kingdom

blank.gif
PostPosted: Tue Sep 14, 2004 11:30 am    Post subject: Best way to do modulus? Reply with quote

Whats the best way do to a simple 'mod 256' on something?

Would this work ok? (with the appropriate variable types)

A = 950 'source value
B = 950 / 256
C = B * 256
D = A - C ' D is final value

Thanks

Stu
Back to top
View user's profile MSN Messenger
philm

Bascom Member



Joined: 14 Jul 2004
Posts: 138
Location: Australia

australia.gif
PostPosted: Tue Sep 14, 2004 12:11 pm    Post subject: Reply with quote

If the number is base 2 (1, 2, 4, ....256)
then subtract 1 and then logical AND
looks better in binary.
950 = 1110110110
256 = 0100000000

950 = 1110110110
255 = 0011111111
AND= 0010110110 = 182
950 mod 256 = 182
Back to top
View user's profile
DJD XTREEM

Bascom Member



Joined: 24 Jun 2004
Posts: 190
Location: 127.0.0.1

netherlands.gif
PostPosted: Tue Sep 14, 2004 12:41 pm    Post subject: Reply with quote

Based on sagmaster example::

Code:
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim D As Integer

Do

Input "give A > " , A                                       'source value
B = A / 256
C = B * 256
D = A - C                                                   ' D is final value


Print B
Print D
Loop
End
 

Only this code takes 37% of compiling ..
In the simulator you will see the first number that is the devide number and the 2nd number is your checksum.

This was just for testing purpose.. So if you gonna use this you can leave the Print statements. Wink

_________________
Bascom AVR 1.11.8.8 full version
STK500 Programmer
ISP Programmer
Back to top
View user's profile Visit poster's website MSN Messenger
saqmaster

Bascom Member



Joined: 10 Apr 2004
Posts: 42
Location: United Kingdom

blank.gif
PostPosted: Tue Sep 14, 2004 1:11 pm    Post subject: Reply with quote

Thanks.. Unfortunately its not base two, and 'A' is the sum of four bytes.. so I wont be using integers Smile

On another note.. If i'm reading a high and low byte via serial, would it be best to use Overlay mode to then read this back as a single item?

-Stu
Back to top
View user's profile MSN Messenger
DJD XTREEM

Bascom Member



Joined: 24 Jun 2004
Posts: 190
Location: 127.0.0.1

netherlands.gif
PostPosted: Tue Sep 14, 2004 1:47 pm    Post subject: Reply with quote

Well you noted 950 as source value.. That is more than 255 and thus more than a byte..
Thats why i used integers.. Wink

_________________
Bascom AVR 1.11.8.8 full version
STK500 Programmer
ISP Programmer
Back to top
View user's profile Visit poster's website MSN Messenger
Frankeman

Bascom Member



Joined: 11 Aug 2004
Posts: 945
Location: the Netherlands

netherlands.gif
PostPosted: Tue Sep 14, 2004 9:54 pm    Post subject: Reply with quote

Quote:

Thanks.. Unfortunately its not base two, and 'A' is the sum of four bytes.. so I wont be using integers

I don't understand you, the sum of four bytes is maximum 1024 so you must use an integer !
In Bascom you can use: D = A MOD 256 (as philm stated before)

Quote:

On another note.. If i'm reading a high and low byte via serial, would it be best to use Overlay mode to then read this back as a single item?

No, use the statement MAKEINT.
var = MAKEINT(LSB , MSB)

Frank.
Back to top
View user's profile
saqmaster

Bascom Member



Joined: 10 Apr 2004
Posts: 42
Location: United Kingdom

blank.gif
PostPosted: Tue Sep 14, 2004 9:56 pm    Post subject: Reply with quote

Er, I meant byte, not integer, my bad Smile

Makeint.. I'll check that out.. Thanks
Back to top
View user's profile MSN Messenger
oe9vfj

Moderator



Joined: 17 Jun 2004
Posts: 269
Location: Austria, Hard

austria.gif
PostPosted: Wed Sep 15, 2004 7:53 am    Post subject: Reply with quote

Hi,

MOD 256 is the LS-Byte of a number (Word, Integer, Long)

So, there are two simple ways for MOD 256

1. Get the LOW-Byte of a Word/Integer/Long
2. Overlay a Byte at the Word/Integer/Long

In following example there are both methods shown:

Code:

Dim w1 as Word
Dim b1 as Byte
Dim b2 as Byte  at W1 overlay
w1 = 950
b1 = low(w1)                                     ' Get the LS-Byte

print w1 ; " " ; b1  ; " " ; b2
 

_________________
regards Josef

DOS - File System for BASCOM-AVR on http://members.aon.at/voegel
Back to top
View user's profile Visit poster's website
saqmaster

Bascom Member



Joined: 10 Apr 2004
Posts: 42
Location: United Kingdom

blank.gif
PostPosted: Wed Sep 15, 2004 8:04 am    Post subject: Reply with quote

Thanks all for the ideas.. Sorted now! Smile
Back to top
View user's profile MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum