Move (output instruction)
TOD: To BCD
Converts an integer to binary coded decimal, for thumbwheels and legacy displays.
Also called: integer to BCD, binary coded decimal convert.
TOD(number, a)Arguments
| Name | What it is | Accepts | Default |
|---|---|---|---|
| destination | What the instruction acts on | a register | required |
| a | Source | a number or a tag | 0 |
Arguments are positional and this table is generated from the simulator's own instruction registry, so it always matches what the engine accepts.
What it does
Binary coded decimal stores each decimal digit in its own four bits. The number 25 is held as 0010 0101, one nibble per digit, rather than as the plain binary 11001. It wastes space and it cannot be used for arithmetic, and it exists because it maps straight onto hardware that shows or reads one digit at a time.
That hardware is still out there: seven segment displays, thumbwheel switches, older printers and panel meters, and a surprising number of weighing indicators. If a device's manual talks about digits rather than a value, it is almost certainly BCD.
TOD is the outbound half. Convert at the very edge of the program, into a register that exists only to be sent to the display, and keep every value your logic actually uses as a plain integer.
A rung you can run
Send a count to a BCD display
XIC(Display_Enable)TOD(Display_Word,Parts_Made)
GRT(Parts_Made,99999999)OTE(Display_Overflow)Turn on Display_Enable, then set Parts_Made to 25 and look at Display_Word: it reads 37, because 37 in binary is 0010 0101, which is the two BCD digits 2 and 5. That is what the display hardware wants to see. Eight digits is the ceiling, and the second rung says so out loud rather than letting the value silently clamp.
Run this rung in the simulatorWhat it is called on other controllers
| Platform | Equivalent |
|---|---|
| Rockwell (Studio 5000) | TOD |
| Siemens (TIA Portal) | CONV, from Int to BCD16 |
| Mitsubishi (GX Works) | BCD |
| CODESYS and IEC 61131-3 | INT_TO_BCD, from the utility library |
Common mistakes
- Converting a value that can be negative. There is no sign in BCD. This simulator converts the magnitude, so -25 comes out as 25 and the sign is simply gone; on some controllers it faults instead. Either way the display is wrong, so handle the sign yourself.
- Doing arithmetic on the BCD value. Adding two BCD numbers with ADD gives you a binary answer that is not valid BCD. Convert at the edges only, and do the maths in between on integers.
Practise it
Interactive exercises that use TOD. Each one checks your ladder against a real scan.
