\def\avr@bindec@addmap#1#2{%
  \csdef{avr@bin@#1}{#2}%
  \csdef{avr@dec@#2}{#1}%
}

\avr@bindec@addmap{0}{00000000}
\avr@bindec@addmap{1}{00000001}
\avr@bindec@addmap{2}{00000010}
\avr@bindec@addmap{3}{00000011}
\avr@bindec@addmap{4}{00000100}
\avr@bindec@addmap{5}{00000101}
\avr@bindec@addmap{6}{00000110}
\avr@bindec@addmap{7}{00000111}
\avr@bindec@addmap{8}{00001000}
\avr@bindec@addmap{9}{00001001}
\avr@bindec@addmap{10}{00001010}
\avr@bindec@addmap{11}{00001011}
\avr@bindec@addmap{12}{00001100}
\avr@bindec@addmap{13}{00001101}
\avr@bindec@addmap{14}{00001110}
\avr@bindec@addmap{15}{00001111}
\avr@bindec@addmap{16}{00010000}
\avr@bindec@addmap{17}{00010001}
\avr@bindec@addmap{18}{00010010}
\avr@bindec@addmap{19}{00010011}
\avr@bindec@addmap{20}{00010100}
\avr@bindec@addmap{21}{00010101}
\avr@bindec@addmap{22}{00010110}
\avr@bindec@addmap{23}{00010111}
\avr@bindec@addmap{24}{00011000}
\avr@bindec@addmap{25}{00011001}
\avr@bindec@addmap{26}{00011010}
\avr@bindec@addmap{27}{00011011}
\avr@bindec@addmap{28}{00011100}
\avr@bindec@addmap{29}{00011101}
\avr@bindec@addmap{30}{00011110}
\avr@bindec@addmap{31}{00011111}
\avr@bindec@addmap{32}{00100000}
\avr@bindec@addmap{33}{00100001}
\avr@bindec@addmap{34}{00100010}
\avr@bindec@addmap{35}{00100011}
\avr@bindec@addmap{36}{00100100}
\avr@bindec@addmap{37}{00100101}
\avr@bindec@addmap{38}{00100110}
\avr@bindec@addmap{39}{00100111}
\avr@bindec@addmap{40}{00101000}
\avr@bindec@addmap{41}{00101001}
\avr@bindec@addmap{42}{00101010}
\avr@bindec@addmap{43}{00101011}
\avr@bindec@addmap{44}{00101100}
\avr@bindec@addmap{45}{00101101}
\avr@bindec@addmap{46}{00101110}
\avr@bindec@addmap{47}{00101111}
\avr@bindec@addmap{48}{00110000}
\avr@bindec@addmap{49}{00110001}
\avr@bindec@addmap{50}{00110010}
\avr@bindec@addmap{51}{00110011}
\avr@bindec@addmap{52}{00110100}
\avr@bindec@addmap{53}{00110101}
\avr@bindec@addmap{54}{00110110}
\avr@bindec@addmap{55}{00110111}
\avr@bindec@addmap{56}{00111000}
\avr@bindec@addmap{57}{00111001}
\avr@bindec@addmap{58}{00111010}
\avr@bindec@addmap{59}{00111011}
\avr@bindec@addmap{60}{00111100}
\avr@bindec@addmap{61}{00111101}
\avr@bindec@addmap{62}{00111110}
\avr@bindec@addmap{63}{00111111}
\avr@bindec@addmap{64}{01000000}
\avr@bindec@addmap{65}{01000001}
\avr@bindec@addmap{66}{01000010}
\avr@bindec@addmap{67}{01000011}
\avr@bindec@addmap{68}{01000100}
\avr@bindec@addmap{69}{01000101}
\avr@bindec@addmap{70}{01000110}
\avr@bindec@addmap{71}{01000111}
\avr@bindec@addmap{72}{01001000}
\avr@bindec@addmap{73}{01001001}
\avr@bindec@addmap{74}{01001010}
\avr@bindec@addmap{75}{01001011}
\avr@bindec@addmap{76}{01001100}
\avr@bindec@addmap{77}{01001101}
\avr@bindec@addmap{78}{01001110}
\avr@bindec@addmap{79}{01001111}
\avr@bindec@addmap{80}{01010000}
\avr@bindec@addmap{81}{01010001}
\avr@bindec@addmap{82}{01010010}
\avr@bindec@addmap{83}{01010011}
\avr@bindec@addmap{84}{01010100}
\avr@bindec@addmap{85}{01010101}
\avr@bindec@addmap{86}{01010110}
\avr@bindec@addmap{87}{01010111}
\avr@bindec@addmap{88}{01011000}
\avr@bindec@addmap{89}{01011001}
\avr@bindec@addmap{90}{01011010}
\avr@bindec@addmap{91}{01011011}
\avr@bindec@addmap{92}{01011100}
\avr@bindec@addmap{93}{01011101}
\avr@bindec@addmap{94}{01011110}
\avr@bindec@addmap{95}{01011111}
\avr@bindec@addmap{96}{01100000}
\avr@bindec@addmap{97}{01100001}
\avr@bindec@addmap{98}{01100010}
\avr@bindec@addmap{99}{01100011}
\avr@bindec@addmap{100}{01100100}
\avr@bindec@addmap{101}{01100101}
\avr@bindec@addmap{102}{01100110}
\avr@bindec@addmap{103}{01100111}
\avr@bindec@addmap{104}{01101000}
\avr@bindec@addmap{105}{01101001}
\avr@bindec@addmap{106}{01101010}
\avr@bindec@addmap{107}{01101011}
\avr@bindec@addmap{108}{01101100}
\avr@bindec@addmap{109}{01101101}
\avr@bindec@addmap{110}{01101110}
\avr@bindec@addmap{111}{01101111}
\avr@bindec@addmap{112}{01110000}
\avr@bindec@addmap{113}{01110001}
\avr@bindec@addmap{114}{01110010}
\avr@bindec@addmap{115}{01110011}
\avr@bindec@addmap{116}{01110100}
\avr@bindec@addmap{117}{01110101}
\avr@bindec@addmap{118}{01110110}
\avr@bindec@addmap{119}{01110111}
\avr@bindec@addmap{120}{01111000}
\avr@bindec@addmap{121}{01111001}
\avr@bindec@addmap{122}{01111010}
\avr@bindec@addmap{123}{01111011}
\avr@bindec@addmap{124}{01111100}
\avr@bindec@addmap{125}{01111101}
\avr@bindec@addmap{126}{01111110}
\avr@bindec@addmap{127}{01111111}
\avr@bindec@addmap{128}{10000000}
\avr@bindec@addmap{129}{10000001}
\avr@bindec@addmap{130}{10000010}
\avr@bindec@addmap{131}{10000011}
\avr@bindec@addmap{132}{10000100}
\avr@bindec@addmap{133}{10000101}
\avr@bindec@addmap{134}{10000110}
\avr@bindec@addmap{135}{10000111}
\avr@bindec@addmap{136}{10001000}
\avr@bindec@addmap{137}{10001001}
\avr@bindec@addmap{138}{10001010}
\avr@bindec@addmap{139}{10001011}
\avr@bindec@addmap{140}{10001100}
\avr@bindec@addmap{141}{10001101}
\avr@bindec@addmap{142}{10001110}
\avr@bindec@addmap{143}{10001111}
\avr@bindec@addmap{144}{10010000}
\avr@bindec@addmap{145}{10010001}
\avr@bindec@addmap{146}{10010010}
\avr@bindec@addmap{147}{10010011}
\avr@bindec@addmap{148}{10010100}
\avr@bindec@addmap{149}{10010101}
\avr@bindec@addmap{150}{10010110}
\avr@bindec@addmap{151}{10010111}
\avr@bindec@addmap{152}{10011000}
\avr@bindec@addmap{153}{10011001}
\avr@bindec@addmap{154}{10011010}
\avr@bindec@addmap{155}{10011011}
\avr@bindec@addmap{156}{10011100}
\avr@bindec@addmap{157}{10011101}
\avr@bindec@addmap{158}{10011110}
\avr@bindec@addmap{159}{10011111}
\avr@bindec@addmap{160}{10100000}
\avr@bindec@addmap{161}{10100001}
\avr@bindec@addmap{162}{10100010}
\avr@bindec@addmap{163}{10100011}
\avr@bindec@addmap{164}{10100100}
\avr@bindec@addmap{165}{10100101}
\avr@bindec@addmap{166}{10100110}
\avr@bindec@addmap{167}{10100111}
\avr@bindec@addmap{168}{10101000}
\avr@bindec@addmap{169}{10101001}
\avr@bindec@addmap{170}{10101010}
\avr@bindec@addmap{171}{10101011}
\avr@bindec@addmap{172}{10101100}
\avr@bindec@addmap{173}{10101101}
\avr@bindec@addmap{174}{10101110}
\avr@bindec@addmap{175}{10101111}
\avr@bindec@addmap{176}{10110000}
\avr@bindec@addmap{177}{10110001}
\avr@bindec@addmap{178}{10110010}
\avr@bindec@addmap{179}{10110011}
\avr@bindec@addmap{180}{10110100}
\avr@bindec@addmap{181}{10110101}
\avr@bindec@addmap{182}{10110110}
\avr@bindec@addmap{183}{10110111}
\avr@bindec@addmap{184}{10111000}
\avr@bindec@addmap{185}{10111001}
\avr@bindec@addmap{186}{10111010}
\avr@bindec@addmap{187}{10111011}
\avr@bindec@addmap{188}{10111100}
\avr@bindec@addmap{189}{10111101}
\avr@bindec@addmap{190}{10111110}
\avr@bindec@addmap{191}{10111111}
\avr@bindec@addmap{192}{11000000}
\avr@bindec@addmap{193}{11000001}
\avr@bindec@addmap{194}{11000010}
\avr@bindec@addmap{195}{11000011}
\avr@bindec@addmap{196}{11000100}
\avr@bindec@addmap{197}{11000101}
\avr@bindec@addmap{198}{11000110}
\avr@bindec@addmap{199}{11000111}
\avr@bindec@addmap{200}{11001000}
\avr@bindec@addmap{201}{11001001}
\avr@bindec@addmap{202}{11001010}
\avr@bindec@addmap{203}{11001011}
\avr@bindec@addmap{204}{11001100}
\avr@bindec@addmap{205}{11001101}
\avr@bindec@addmap{206}{11001110}
\avr@bindec@addmap{207}{11001111}
\avr@bindec@addmap{208}{11010000}
\avr@bindec@addmap{209}{11010001}
\avr@bindec@addmap{210}{11010010}
\avr@bindec@addmap{211}{11010011}
\avr@bindec@addmap{212}{11010100}
\avr@bindec@addmap{213}{11010101}
\avr@bindec@addmap{214}{11010110}
\avr@bindec@addmap{215}{11010111}
\avr@bindec@addmap{216}{11011000}
\avr@bindec@addmap{217}{11011001}
\avr@bindec@addmap{218}{11011010}
\avr@bindec@addmap{219}{11011011}
\avr@bindec@addmap{220}{11011100}
\avr@bindec@addmap{221}{11011101}
\avr@bindec@addmap{222}{11011110}
\avr@bindec@addmap{223}{11011111}
\avr@bindec@addmap{224}{11100000}
\avr@bindec@addmap{225}{11100001}
\avr@bindec@addmap{226}{11100010}
\avr@bindec@addmap{227}{11100011}
\avr@bindec@addmap{228}{11100100}
\avr@bindec@addmap{229}{11100101}
\avr@bindec@addmap{230}{11100110}
\avr@bindec@addmap{231}{11100111}
\avr@bindec@addmap{232}{11101000}
\avr@bindec@addmap{233}{11101001}
\avr@bindec@addmap{234}{11101010}
\avr@bindec@addmap{235}{11101011}
\avr@bindec@addmap{236}{11101100}
\avr@bindec@addmap{237}{11101101}
\avr@bindec@addmap{238}{11101110}
\avr@bindec@addmap{239}{11101111}
\avr@bindec@addmap{240}{11110000}
\avr@bindec@addmap{241}{11110001}
\avr@bindec@addmap{242}{11110010}
\avr@bindec@addmap{243}{11110011}
\avr@bindec@addmap{244}{11110100}
\avr@bindec@addmap{245}{11110101}
\avr@bindec@addmap{246}{11110110}
\avr@bindec@addmap{247}{11110111}
\avr@bindec@addmap{248}{11111000}
\avr@bindec@addmap{249}{11111001}
\avr@bindec@addmap{250}{11111010}
\avr@bindec@addmap{251}{11111011}
\avr@bindec@addmap{252}{11111100}
\avr@bindec@addmap{253}{11111101}
\avr@bindec@addmap{254}{11111110}
\avr@bindec@addmap{255}{11111111}

\newcount\avr@count@tmpa
\newcount\avr@count@tmpb

% \avr@count@tobin@b uint8_t count|integer, \result -> \result = bin(arg)
\def\avr@count@tobin@b#1#2{%
  \ifnum #1 > 255 %
     \avr@error{#1 to large value}%
  \fi%
  \ifnum #1 < 0 %
     \avr@error{#1 to small value}%
  \fi%
  \avr@count@tmpa=#1%
  \ifcsdef{avr@bin@\the\avr@count@tmpa}{}{%
    \avr@error{Incorrect Number: #1}%
  }%
  \edef#2{\csuse{avr@bin@\the\avr@count@tmpa}}%
}

% \avr@count@tobin@w uint16_t count|integer, \result -> \result = bin(arg)
\def\avr@count@tobin@w#1#2{%
  \ifnum #1 > 65535 %
     \avr@error{#1 to large value}%
  \fi%
  \ifnum #1 < 0 %
     \avr@error{#1 to small value}%
  \fi%
  \avr@count@tmpa=#1%
  \avr@count@tmpb=\avr@count@tmpa%
  \divide\avr@count@tmpa by 256\relax%
  \multiply\avr@count@tmpa by 256\relax%
  \advance\avr@count@tmpb by -\avr@count@tmpa\relax%
  \divide\avr@count@tmpa by 256\relax%
  \ifcsdef{avr@bin@\the\avr@count@tmpa}{}{%
    \avr@error{Incorrect Number: #1}%
  }%
  \ifcsdef{avr@bin@\the\avr@count@tmpb}{}{%
    \avr@error{Incorrect Number: #1}%
  }%
  \edef#2{\csuse{avr@bin@\the\avr@count@tmpa}\csuse{avr@bin@\the\avr@count@tmpb}}%
}


\def\avr@bin@tocount#1#2{% BitString Counter
  \avr@count@tmpa=0%
  \edef\@tempa{#1}%
  \expandafter\avr@bin@tocount@helper\@tempa\@nnil%
  #2=\avr@count@tmpa%
}
\def\avr@bin@tocount@helper#1#2#3#4#5#6#7#8#9\@nnil{%
  \multiply\avr@count@tmpa by 256\relax%
  \ifcsdef{avr@dec@#1#2#3#4#5#6#7#8}{}{%
    \avr@error{Incorrect Number: #1}%
  }%
  \advance\avr@count@tmpa by \csuse{avr@dec@#1#2#3#4#5#6#7#8}%
  \if&#9&\else%
     \avr@bin@tocount@helper #9\@nnil%
  \fi%
}

\csdef{avr@hex@0}{0000}
\csdef{avr@hex@1}{0001}
\csdef{avr@hex@2}{0010}
\csdef{avr@hex@3}{0011}
\csdef{avr@hex@4}{0100}
\csdef{avr@hex@5}{0101}
\csdef{avr@hex@6}{0110}
\csdef{avr@hex@7}{0111}
\csdef{avr@hex@8}{1000}
\csdef{avr@hex@9}{1001}
\csdef{avr@hex@A}{1010}
\csdef{avr@hex@B}{1011}
\csdef{avr@hex@C}{1100}
\csdef{avr@hex@D}{1101}
\csdef{avr@hex@E}{1110}
\csdef{avr@hex@F}{1111}
\csdef{avr@hex@a}{1010}
\csdef{avr@hex@b}{1011}
\csdef{avr@hex@c}{1100}
\csdef{avr@hex@d}{1101}
\csdef{avr@hex@e}{1110}
\csdef{avr@hex@f}{1111}

% Hexstring to Bitstring: hexstring, \result macro
\def\avr@hex@tobin#1#2{%
  \def\avr@hex@helper##1##2\@nnil{%
    \xdef#2{#2\csuse{avr@hex@##1}}%
    \ifx&##2&\else%
      \avr@hex@helper ##2\@nnil%
    \fi%
  }%
  \def#2{}%
  \edef\@tempa{#1}\relax%
  \expandafter \avr@hex@helper \@tempa \@nnil%
}


\def\avr@count@overflow#1{%
  \def\avr@count@overflow@flag{0}%
  \ifnum #1 < 0%
    #1=65535%
    \def\avr@count@overflow@flag{1}%
  \fi%
  \ifnum #1 > 65535%
    #1=0%
    \def\avr@count@overflow@flag{1}%
  \fi%
}

\def\avr@count@modulo@byte#1{%
  \avr@count@tmpa=#1%
  \divide \avr@count@tmpa by 256%
  \multiply \avr@count@tmpa by 256%
  \advance #1 by -\avr@count@tmpa%
}



%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
