Searching \ for ' Hex to BCD in C' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/language/index.htm?key=c
Search entire site for: 'Hex to BCD in C'.

No exact or substring matches. trying for part
PICList Thread
'[PICLIST] Hex to BCD in C'
2000\12\06@065733 by John Walshe

flavicon
face
Hi Guys,
   anyone got a routine to convert from HEX to BCD - written in C(Hitech).
Regards,
John

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spam_OUTlistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


2000\12\06@111649 by JP.BROWN

flavicon
face
This program written in CC5X should give you a good idea how to do this.
The program displays a 16 bit integer as a 5 digit decimal value, the
number of digits can easily be changed (see comments).

---- John

// freq4.c                         12.04.00
// This program prints the frequency of a bit stream on the VDU screen,
// as a decimal value (5 digit).
// this version uses the RTCC

#include "c:\cc5x\16F84.H"
#define CP_off |= 0x3FFF    // copy protect off for 16F84
#pragma config CP_off, PWRTE=on, WDTE=off, FOSC=XT

#pragma bit RS232_out @ PORTB.7  // Sout (to host PC)
#pragma bit RS232_in  @ PORTA.4  // Sin  (from host PC)

#define delayOneBit  {          \
       char ti;                \
       ti = 255;               \
       do ; while( --ti > 0);  \
       nop();                  \
       ti = 18;                \
       do ; while( --ti > 0);  \
       nop();                  \
    }  /* total: 5+(255x3)-1+1+(18x3)-1+9+1=833 microsec. at 4 MHz */

#define text1 28   // number of characters in message

char textOne( char W)   // this function returns an indexed ascii code
{
   skip(W);
  #pragma return[text1] = "freq4.c RTCC Frequency Meter"
}

/* assign names to ports and pins */
#pragma bit BIT_STREAM @ PORTA.4

/* function prototypes */
void delay (void);
void DispHex(char);
char HexToAscii(char n);
void NewLine(void);
void sendData(char dout);
char counter(void);
void Pause (void);

void main(void)
{

   char j;
   PORTB = 0b.0000.0000;  /* initial value */
   TRISB = 0b.0000.0000;  /* Port B all o/p (serial out bit 7) */
   PORTA = 0b.0.0000;     /* initial value */
   TRISA = 0b.1111.1001;  /* xxx0 1001     */
   OPTION = 0b.0010.1000; // RTCC to pin RA4 no pre scaler

NewLine();
for ( j = 0; j < text1; j++)
 sendData( textOne(j));  // Prints title of program
NewLine();
NewLine();

char temp;
uns16 digit, temp16, freq;
while(1)
{
 NewLine();
 RTCC=0;
 delay(); // each one of these takes one milisecond
 delay();
 delay();
 delay();
 temp=RTCC;
 sendData('$'); // indicate RTCC value is hex
 DispHex(temp);
 sendData(' ');
 freq=temp*250;    // convert to frequency

// digit=1000 prints 4 digits, digit=100,000 prints 6 digits etc. etc.
digit=10000;    // This code block prints a 16 bit variable
while(digit>0)  // as a 5 digit decimal number
{
if (freq>=digit)
 { temp=freq/digit;            // derive decimal msd
   temp16=(uns16)temp*digit;
   freq=freq-temp16;           // ready for next digit
   sendData(HexToAscii(temp)); // print it
 }
 else sendData('0'); // fill spaces with zeros
digit/=10;
} // end of while(digit....

 sendData(' ');
 sendData('H');
 sendData('z');
 Pause();
}
} // end of main


void Pause (void)
{
uns16 T;
for(T=0;T<65000;T++);
{ nop();
  nop();
  nop();
 }
}


void DispHex(char n)
{
 char temp;
 temp=n;
 n=(n >> 4); // get top nibble
 sendData(HexToAscii(n));   // print it
 temp=(temp & 0x0F); // get bottom nibble
 sendData(HexToAscii(temp));
}


char HexToAscii(char n) // can also be used to convert a bcd to ASCII.
{
 n += 0x36;  // start converting nibble to ASCII
 if (n > 63) // find out if n is 0-9 or A-F
   n += 7;   // adjust for A-F
 n -= 6;     // adjust ASCII value
 return n;
}


void NewLine(void)
{
 sendData(10);  // LF
 sendData(13);  // CR
}


void delay (void)
{
  char ti;
  ti = 255;
  do ; while( --ti > 0);
  nop();
  ti = 76;
  do ; while( --ti > 0);
  nop();
/*total: 2+2+(255x3)-1+1+(76x3)-1+1+2=1000 microsec. at 4 MHz */
} // end of delay               /* including call and return */


void sendData(char dout)   /* sends one byte to serial out */
{
char bitCount;
RS232_out = 0;  /* startbit */
for ( bitCount = 9; ; bitCount--)  {
   delayOneBit
   if ( bitCount == 0)
     break;
   Carry = 1;  /* incl. stopbit */
   dout = rr( dout);
   RS232_out = Carry;
 }
}


char counter(void)
{       char count=0;
       while ((BIT_STREAM) ==1)
       { }     // endless loop to find falling edge
       while ((BIT_STREAM) ==0)        // at falling edge
       {count+=1;}                // start counter
       return count;
} // end of counter function

On Wed, 6 Dec 2000, John Walshe @Inpact wrote:

{Quote hidden}

         -----  John P. Brown      J.P.BrownspamKILLspambradford.ac.uk ----
          \            --- Witty remark goes here ---         /
           --------------------------------------------------

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email .....listservKILLspamspam.....mitvma.mit.edu with SET PICList DIGEST in the body


2000\12\06@132252 by rich+piclist

flavicon
face
Sorry, this isn't making a lot of sense... "hex" is a method of displaying
numbers, "bcd" is a method of storing numbers. If you want to convert a
hex string to an int, see sscanf. If you want to convert an int to a
decimal string, see sprintf. If you really do want to convert an int to
bcd, try this (for n=0-9999):

bcd = ((n/1000)<<12)+(((n/100)%10)<<8)+(((n/10)%10)<<4)+(n%10);

To display the bcd number, you can use printf("%x",n).

hth == Rich

On Wed, 6 Dec 2000, John Walshe @Inpact wrote:

{Quote hidden}

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


2000\12\06@141757 by Brent Brown

picon face
Not sure if this what you want, but you might get some ideas from
it. It takes a signed 16 bit integer and outputs the representative
ASCII characters. In this example SendByte() outputs to the serial
port. Output is from "-32767" to "+32768". For unsigned values
delete the first 5 lines of this function. Easy to modify for different
lengths too.

void SendNumSInt(int x){
       if(x < 0)
               SendByte('-');
       else
               SendByte('+');
       x=abs(x);

       SendByte((x/10000) + '0');
       SendByte((x%10000)/1000 + '0');
       SendByte((x%1000)/100 + '0');
       SendByte((x%100)/10 + '0');
       SendByte((x%10) + '0');
}

Brent Brown
Electronic Design Solutions
16 English Street
Hamilton, New Zealand
Ph/fax: +64 7 849 0069
Mobile/text: 025 334 069
eMail:  spamBeGonebrent.brownspamBeGonespamclear.net.nz

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email TakeThisOuTlistservEraseMEspamspam_OUTmitvma.mit.edu with SET PICList DIGEST in the body


More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...