PDA

View Full Version : Isomax: Need (16b -- c ) conversion?


Pacetech
06-21-04, 05:46 PM
Ok, I need to generate a packet to send out the 2nd serial port and the end of the packet has a checksum.

Now I know that 8 bit chars are stored as 16 bit numbers, but my checksum must be the 8 bit sum of the chars in the data packet. So I add up all my 8 bit chars and sometimes I get a larger number than FF. So, I've been doing a MOD to get it to be less than or equal to FF.

So I just do a FF MOD SCI1 TX

For example

: MOTSTARTMOTION ( ADDR -- )
AA SCI1 TX (packet header, do not add to checksum)
DUP SCI1 TX ( TO ADDR on stack )
05 SCI1 TX ( COMMAND BYTE - set gain )
05 + FF MOD SCI1 TX
; EEWORD

Since TX will only send out an 8 bit value, the question is, if I don't do the FF MOD, will only the lowest 8 bits of the 16 bit value get sent?

RMDumse
06-21-04, 08:07 PM
I really don't recommend using FF MOD for stripping out the lower 8 bits. Problem is, FF MOD is a signed mathematical function, and will do something different for positive vs. negative numbers, iirc.

Anyway, do an FF AND and only the lower 8 will be left.

I believe you are correct, if you just stored the value into the SCI, only the lower 8 would go out, but there's also a suggestion in Forth standards that all possible bits be sent, so the FF AND is a much surer way to get what you want.