Sign in to follow this  
badderjet

"Binary Coded Decimal"?

Recommended Posts

I tried to make a COM radio in C, but when it came to source variables, the SDK said the com frequency's data type is BCD, i. e. binary coded decimal. So what the heck is that?!? :-hmmmAny Help's appreciated.Etienne

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

Gosh Etienne,You are taking me back to my old programmimg days here.{{{{Blowing the cobwebs out}}}}}If I recall correctly, it's something like this.Computers store and retrieve information in 8-bit bytes, or groups of bytes called words (32 bits), or half words. There is always a parity bit used to ensure there are no circuit problems. In the early days, BCD used 7 bits of an 8 bit byte to store data, the 8th bit for parity.The number of bits available for data also constrained the character set size, fewer bits, less characters available. The firmware (CMOS) fetched data based on which format was used. BCD = 7 bits with 1 parity. EBCDIC used 8 bits with a 9th as parity.Is this too confusing?My guess is that C does not output BCD but rather ASCII format or you have defined the variable wrong.Sorry if this explanation doesn't help ... too much water over the dam. :-)Milton

Share this post


Link to post
Share on other sites

Actually binary coded decimal works as follows: each nibble in the word (that is, each group of four bits) is encoded with the binary equivalent of 0-9. Each successive nibble then represents one decimal place. Thus:0001-0010 is 1-2 which is 1 x 10 + 2 x 1 = 12. 0011-0100 0001-0010 is 3x1000 + 8x100 + 1x10 + 2 = 3812. The beauty of it is that you can easily access any decimal place in the word with a simple series of bit shift instructions, a feat which is obviously much harder with true binary. Hope this helps!Regards,Philip.

Share this post


Link to post
Share on other sites

As I think about it, it makes perfect sense to use this format for radios since in FS you want to be able to change any given digit without affecting the rest. BCD allows you to do this with a simple series of AND, bit shift, INC/DEC, and OR operations. Using a long int would require multiplication.

Share this post


Link to post
Share on other sites

Yeah, seems really to make perfect sense, but... Oh well! That sounds quite difficult... I think I just gotta wait a bit until I make radios... Coz I just don't know what to do with such BCD, how to use it in callbacks etc.. Whew, that's sum stuff! :-)Thanks anyway! :-hahEtienne :-wave

Share this post


Link to post
Share on other sites

It isn't too difficult. As far as I know here are some conversions included in sd2gau13.zip for C gauges. For XML I placed some here some time ago. To display a BCD coded frequency in C gauges with MAKE_STRINGs you can take into account that BCD coded numbers are very similar to hexadecimal numbers. So a string callback can look like this:...sprintf(pelement->string,"1%02x.%02x",pelement->source_var[0].var_value.d/100,pelement->source_var[0].var_value.d%100);...The ".d" member because the frequencies (VOR1,VOR2,COM1,COM2 +STDBY) are always stored in this member of the source var. The format specifier "x" to display in hexadecimal code. The above mentioned frequencies are not the whole set, the leading 1 of 1xx.xx MHz isn't stored, and the first two digits represent the whole frequency in MHz, the last two digit the frequency in 10kHz. So the frequency is clipped in two sets, the missing one is placed before it.It might be good to display the frequency first without much modifications to see the following steps easier , try first:sprintf(pelement->string,"%04x",pelement->source_var[0].var_value.d);before you try the other sprintf.In reality the frequencies are actually transferred between intruments not exactly in this fashion, but very similar, BCD coding for each digit on 4-wire lines.Arne Bartels

Share this post


Link to post
Share on other sites

Ah, the advent of modern avionics! I use to test and evaluate aircraft avionics systems. One job had to do with replacing the AN/ARC-51 UHF Radio with the AN/ARC-159V5 UHF Radio. The smart engineers at Collins Radio devised various ARC-159V(X) replacement radios for various older UHF Radios. One of the selling points was no modification to the aircraft wiring.The ARC-51 control box was in the cockpit while the Transeiver was located in an equipment bay. It used BCD from the control box to the transeiver. The number of wires from the control box to the transeiver was approximately one inch in diameter, many of the wires were for switching the frequencies. The ARC-159 used the same connector but few of the wires. Frequencies were changed by using a clock reference and frequency information sent to the new transeiver over just four wires, two for the clock reference and two for the frequency information. The frequency information lines also sent other data to the radio, as well.The AN/ARC-159V1 UHF Radio was an all inclusive UHF Radio built into the cockpit thus eliminating all the exist wiring and weight of the older radios.Just think of the advent of the cell phone. The limiting size is our clumsy big fingers.

Share this post


Link to post
Share on other sites

Oh well, thanks for the additional information, maybe I gotta give it a shot later today.Thank youEtienne

Share this post


Link to post
Share on other sites

I see these explanations about how to interpret a Binary Coded Decimal, but what data type in C can hold these?I'm sorry to bump an old (apparently)topic, but I am working on Navigation Display (using GDI+) and am "pre-coding" the gauge. I would like to know before hand which type to declare this for my buffer.Thanks!

Share this post


Link to post
Share on other sites

See sd2gau23.zip; use execute_calculator_code to get good old decimal and forget all about using/converting BCD!-Dai

Share this post


Link to post
Share on other sites

Thanks, Dai.I glanced through this at work today (I don't remember my password to the forums, so I couldn't get back to you earlier with this), and it looked like that execute_calculator_code is for XML?Is there a C version?--Matt

Share this post


Link to post
Share on other sites

>Thanks, Dai.>>I glanced through this at work today (I don't remember my>password to the forums, so I couldn't get back to you earlier>with this), and it looked like that execute_calculator_code is>for XML?>>Is there a C version?>>--MattThat IS the "C version!"execute_calculator_code is the name of a function(); in the gauges.h file, and will request/receive any XML token variable for use in a C gauge.You can see a full explanation of how this works on my "Panel & Gauge Wiki" here:http://forums.flightsim.com/fswiki/index.p...les_in_C_Gauges

Share this post


Link to post
Share on other sites

Note that the COM and NAV frequencies assume the highest order digit in the instrument (gauge) is 1 (one), it is not recorded in the BCD representation. So for a frequency of 136.25 the recorded BCD is 3625.These are the BITs in a 16 bit number with their order value for a BCD encoded number:8 4 2 1 . 8 4 2 1 . 8 4 2 1 . 8 4 2 1This is 2 bytes and can store a 4 digit BDC16 encoded number, one BCD digit in each half byte. BCO16 is the same as BCD16, BCD32 is 4 bytes (twice as big) for an 8 digit BCD encoded number.Suppose we have a BCD encoded number, 3625, we will then have these BITs:0011 0110 0010 01014th, 3rd, 2nd, 1st digit (from right to left, low order to high order)Well that's just fine, but how do you extract the digits in C:The bitwise AND (&) operator masks the bits you are not interested in.Extract first digit (from right to left):digit1 = source & 15;notes: source AND 15, note that 8+4+2+1=15Extract second digit (from right to left):digit2 = (source >> 4) & 15;notes: source SHIFT RIGHT 4 BITS then AND 15Extract third digit (from right to left):digit3 = (source >> 8) & 15;Extract fourth digit (from right to left):digit4 = (source >> 12) & 15;Same for BCD32, you just keep shifting more BITs each time (shift 16, 20, 24 and 28).So... How do you set the BITs?To set all the BITs at once (as in changing the COM radio settings):result = 0; We always want to start clean, there is a technicality in C and other languages that when you shift BITs you can rotate them or fill with zeros. Starting with zero allows you to do either without thinking too much about unforseen consequences.The following is for setting the 4 digits all at once and must be performed in the given order. The bitwise OR (|) sets the BITs. A different method is needed to change just one BCD digit while retaining the rest of the BCD digits.Set fourth digit (from right to left):result = result | digit4;Set third digit (from right to left):result = (result 8);notes: mask is all BITs = 1 except the BITs we want to clear (third digit - right to left), '!' is a bitwise NOTresult = source & mask; 8); <-- 7 is the value we want to set, SHIFTed into the 3rd digit positionIf I've made a mistake here it's because I did this very early in the morning. And yes, this can make your brain hurt.

Share this post


Link to post
Share on other sites

>That IS the "C version!">>execute_calculator_code is the name of a function(); in the>gauges.h file, and will request/receive any XML token variable>for use in a C gauge.>>You can see a full explanation of how this works on my "Panel>& Gauge Wiki" here:>>http://forums.flightsim.com/fswiki/index.p...les_in_C_GaugesBill, Awesome, thanks! Thanks also for that link to your article; I think that may actually help with something I've wanted to do in a gauge before but wasn't sure how to go about doing it.>Note that the COM and NAV frequencies assume the highest>order digit in the instrument (gauge) is 1 (one), it is not>recorded in the BCD representation.>>etc>>If I've made a mistake here it's because I did this very early>in the morning. And yes, this can make your brain hurt.Thanks for this as well. I've never really gotten the hang of some of this nibbles and bits stuff, even though I work with them at work. I'm a self-taught coder and this stuff does indeed make my brain hurt!

Share this post


Link to post
Share on other sites

No problem. Between Dai's wonderful 103 page C Gauge Handbook (there's really no other way to describe it!), my flightsim.com Wiki, and the "sticky posts" at my flightsim.com "Panel & Gauges" forum, there are more explanations of how to perform such operations.In fact, there are three unique methods described which all perform the same task:1) Arne Bartels BcdDec(); and DecBcd(); functions (pure C code)2) Jean-Luc's (RealityXP) "string method"3) The "execute_calculator_code(); function method to use XML tokens.Quite honestly, all new projects I'm developing now use method #3 to obtain ALL fs variables, which saves me a ton of time during development, since I no longer have to perform so much "conversion kung-fu" on the raw C tokens to massage them into a rational unit system... ;)For example:double adf1_frequency;execute_calculator_code("(A:ADF ACTIVE FREQUENCY:1, KHz)",&adf1_frequency,NULL,NULL);sprintf(pelement->string,"%6.2f",adf1_frequency );

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this