C: Decimal to BCD

From FSDeveloper Wiki
Jump to: navigation, search

C: BCD to Decimal and Decimal to BCD

This scheme was supplied by Arne Bartels which allows BCD conversion in both directions. Add the following to the master source file:-

#include <math.h>
#define Bcd2Dec(BcdNum) HornerScheme(BcdNum,0x10,10)
#define Dec2Bcd(DecNum) HornerScheme(DecNum,10,0x10)

UINT32 HornerScheme(UINT32 Num,UINT32 Divider,UINT32 Factor)
{
   UINT32 Remainder=0,Quotient=0,Result=0;
   Remainder=Num%Divider;
   Quotient=Num/Divider;
   if(!(Quotient==0&&Remainder==0))
   Result+=HornerScheme(Quotient,Divider,Factor)*Factor+Remainder;
   return Result;
}

Then for each sub-gauge that requires a BCD conversion use the following syntax:-

Bcd2Dec(bcd_number) for BCD to decimal and
Dec2Bcd(dec_number) for decimal to BCD conversions.

In order to send frequencies to the ADF radios, multiply the desired frequency by 10000:

float adf_freq = 0;
adf_freq = 286.5;
trigger_key_event(KEY_ADF_COMPLETE_SET, Dec2Bcd(adf_freq*10000));

In order to send frequencies to the COM or NAV radios, multiply the desired frequency by 100:

float com_freq = 0;
com_freq = 122.875;
trigger_key_event(KEY_COM_RADIO_SET, Dec2Bcd(com_freq * 100));//Change the event ID as needed for the NAV radio or the particular COM radio you wish to set