Forum - MCS Electronics

Author Message
saqmaster

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

 Posted: Tue Sep 14, 2004 11:30 am    Post subject: Best way to do modulus? 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
philm

Joined: 14 Jul 2004
Posts: 138
Location: Australia

 Posted: Tue Sep 14, 2004 12:11 pm    Post subject: 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
DJD XTREEM

Joined: 24 Jun 2004
Posts: 190
Location: 127.0.0.1

Posted: Tue Sep 14, 2004 12:41 pm    Post subject:

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.

_________________
Bascom AVR 1.11.8.8 full version
STK500 Programmer
ISP Programmer
saqmaster

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

 Posted: Tue Sep 14, 2004 1:11 pm    Post subject: Thanks.. Unfortunately its not base two, and 'A' is the sum of four bytes.. so I wont be using integers 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
DJD XTREEM

Joined: 24 Jun 2004
Posts: 190
Location: 127.0.0.1

 Posted: Tue Sep 14, 2004 1:47 pm    Post subject: Well you noted 950 as source value.. That is more than 255 and thus more than a byte.. Thats why i used integers.. _________________Bascom AVR 1.11.8.8 full version STK500 Programmer ISP Programmer
Frankeman

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

Posted: Tue Sep 14, 2004 9:54 pm    Post subject:

 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.
saqmaster

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

 Posted: Tue Sep 14, 2004 9:56 pm    Post subject: Er, I meant byte, not integer, my bad Makeint.. I'll check that out.. Thanks
oe9vfj

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

Posted: Wed Sep 15, 2004 7:53 am    Post subject:

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
saqmaster

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

 Posted: Wed Sep 15, 2004 8:04 am    Post subject: Thanks all for the ideas.. Sorted now!
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT + 1 Hour Page 1 of 1

 Jump to: Select a forum BASCOM AVR/8051----------------BASCOM-AVRBASCOM-8051BASCOM-ARDUINOShare your working BASCOM-8051 code hereShare your working BASCOM-AVR code hereBASCOM BETA-SLA BASCOM Related----------------EASY TCP/IPAVR-DOSAR7212KokkeKat FAT-free SD card libBASCOM Project Blog Other Stuff----------------VariousPCB'sRoboticsNew WebSiteAnnouncementsAVR Archive----------------BASCOM-AVR ArchiveBASCOM-8051 ArchiveBASCOM-AVR Unsupported versionsEasy TCP/IP ArchiveBASCOM-EDB
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