My programs for evaluation of the binary to BCD conversion using successive rotation and C++ edition of the AN526 Microchip algorithm:
//For DevC++, may be translated into the VC++ (console).
//http://ww1.microchip.com/downloads/en/AppNotes/00526e.pdf
//sr.cpp
//A program of the Bin To BCD subroutine evaluation
/*
H ; T ; O ; B
- ; - ; - ; 1010 0010 ; 162
- ; - ; xxx1 ; 010 0010 ; <<#1
- ; - ; xx10 ; 10 0010 ; <<#2
- ; - ; x101 ; 0 0010 ; <<#3
- ; - ; 1000 ; ; add 3
- ; xxx1 ; 0000 ; 0010 ; <<#4
- ; xx10 ; 0000 ; 010 ;<<#5
- ; x100 ; 0000 ; 10 ;<<#6
- ; x1000 ; 0001 ; 0 ;<<#7
- ; 1011 ; ; ; add 3
1 ; 0110 ; 0010 ; ;<<#8
1 ; 6 ; 2 ;
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
//#include "stdafx.h"
int main()
{
uint16_t x;//=1023;
uint8_t i;
for (x=0;x<=1023;x++)
{
// Successive rotation
uint8_t Th=0;
uint8_t H=0;
uint8_t T=0;
uint8_t O=0;
for (i=0 ; i<=15 ; i++) //to 7 for
{
// add 3 for columns >=5
if(Th>=5) { Th+=3; }
if(H>=5) { H+=3; }
if(T>=5) { T+=3; }
if(O>=5) { O+=3; }
Th=0x0F&(Th<<1)| ((H>>3)&0x01); //Th=0x0F&((Th<<1)|(H.bit3)//using low nibble mask of the byte after shifting right and
H=0x0F&(H<<1)| ((T>>3)&0x01); //H=0x0F&((H<<1)|(T.bit3) // using carry from bit 3 of the previous digit to the bit0
T=0x0F&(T<<1)| ((O>>3)&0x01); //T=0x0F&((T<<1)|(O.bit3)
O=(uint8_t)0x0F&(O<<1)|((x>>15-i )&0x01); // MSB to O.bit0 ,then MSB<<=1 or you can use carry of the PIC
//printf ("\n x=%d ; %d %d % d %d i=%d",(int)x,(int)Th, (int)H ,(int)O>>4 ,(int)O&0x0F , (int)i );
}
printf ("\n x=%d ; %d %d % d %d i=%d",(int)x,(int)Th, (int)H ,(int)T ,(int)O , (int)i );
}
return 0 ;
}
/********************************************************/
//using AN526 ,C++ edition
//using the Successive rotation algorithm and modifyed AN526 Microchip algorithm, translated into C++ for alternative number of digits
//algsr526.cpp
// //A program of the Bin To BCD subroutine evaluation
/*
H ; T ; O ; B
- ; - ; - ; 1010 0010 ; 162
- ; - ; xxx1 ; 010 0010 ; <<#1
- ; - ; xx10 ; 10 0010 ; <<#2
- ; - ; x101 ; 0 0010 ; <<#3
- ; - ; 1000 ; ; add 3
- ; xxx1 ; 0000 ; 0010 ; <<#4
- ; xx10 ; 0000 ; 010 ;<<#5
- ; x100 ; 0000 ; 10 ;<<#6
- ; x1000 ; 0001 ; 0 ;<<#7
- ; 1011 ; ; ; add 3
1 ; 0110 ; 0010 ; ;<<#8
1 ; 6 ; 2 ;
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
//#include "stdafx.h"
uint8_t adjBCD(uint8_t r)
{
uint8_t tmp=r+0x03;
if(tmp&0x08) { r=tmp; } // if (r+0x03)>7 (bit 3==1) r+=0x03
tmp=r+0x30;
if(tmp&0x80 ) {r =tmp ; } //bit7 , for packed if in the (r+0x03<<4 ) (bit 7==1) r+=0x03<<4 ;
return r;
}
uint8_t * BinToBCD(uint16_t x )
{
uint8_t count ;
uint8_t R[3];
R[0]=0;
R[1]=0;
//R[2]=0;
//clear Carry and use shift ing of x or load MSB of x to Carry without shifting, then use shifting after
// Successive rotation
for (count=16 ; count>0 ; count--) //total 16 steps
{
// add 3 for columns >=5
/*
if(Th>=5) { Th+=3; }
if(H>=5) { H+=3; }
if(T>=5) { T+=3; }
if(O>=5) { O+=3; }
*/
/*
Th=adjBCD(Th);
H=adjBCD(H);
T=adjBCD(T);
O=adjBCD(O);
*/
// R[2]=adjBCD(R[2]);
R[1]=adjBCD(R[1]);
R[0]=adjBCD(R[0]);
//R[2]= (R[2]<<1)|((R[1]>>7)&0x01); //rlf r2
R[1]= (R[1]<<1)|((R[0]>>7)&0x01); // rlf r1, R1= (H<<1)| ((R0>>3)&0x01);
R[0]= (R[0]<<1)|((x>>count-1)&0x01); // use carry for x shifting into the
//x=x<<1; (shift left once to load data into carry )
/*
Th=0x0F&(Th<<1)| ((H>>3)&0x01); //Th=0x0F&((Th<<1)|(H.bit3)
H=0x0F&(H<<1)| ((T>>3)&0x01); //H=0x0F&((H<<1)|(T.bit3)
T=0x0F&(T<<1)| ((O>>3)&0x01); //T=0x0F&((T<<1)|(O.bit3)
O=(uint8_t)0x0F&(O<<1)|((x>>15-i )&0x01); // MSB to O.bit0
*/
}
return R ;
}
int main()
{
uint16_t x; //=220;
for (x=0; x<=1023 ;x++)
{
uint8_t* R=BinToBCD(x ); //packed, use masks
printf ("\n x=%d ; %d %d % d %d ",(int)x,(int)R[1]>>4, (int)R[1]&0x0f ,(int)R[0]>>4 ,(int)R[0] &0x0F );
}
return 0 ;
}