User Manual

MCP Series
Brushed DC Motor Controllers
MCP Series User Manual
132
BIN2BCD
This command let you convert binary to “packed” binary coded decimal (BCD). A BCD number
is one in which each decimal digit is represented by a 4 bit binary number (from 0 to 9). Packed
BCD packs two 4 bit decimal digits in a single byte of memory.
For example, the decimal number 93 is represented in binary as:
Values 128 64 32 16 8 4 2 1
Binairy 0 1 0 1 1 1 0 1
The same number is expressed in packed BCD as:
Values 8 4 2 1 8 4 2 1
Binairy 1 0 0 1 0 0 1 1
Assuming that “answer” is a byte variable and “num” has the decimal value of 93, the statement
answer = bin2bcd num
will set answer to a binary value of 10010011 (which is 93 in packed BCD).
RANDOM
Random generates a 32 bit (Long) random number from the seed value. As with most random
number generators, the random numbers generated will follow a predictable pattern depending
on the seed value(ie: it is psuedo random), and each time the program is run the random
number sequence will be the same if the seed value is the same. The code snippet below will
return a pseudo random set of numbers by re-seeding from the results:
seed var long
seed = 123456
seed = random seed
There are steps that can be taken to avoid repeating random number sequences. This is typically
done using hardware based features. One common method is using an internal hardware timer
for the seed value and asking the user to press a button at the beginning of a game. Each time
the button is pressed the timer value will likely be different. Another method is reading an A/D
pin that is left oating and near a noisy signal trace.