Jump to content
43oh

Converting a 3-Digit Number to Characters for a LCD Display


Recommended Posts

I have do this often for my LCD display output, and wrote a routine called BCDdecode that does it for 3 digits,

and then returns the remainder so it can work for more digits if needed.

And also note if d3 is zero, it is just doing two digits

 

unsigned short
BCDecode(unsigned short val, unsigned char *d1, unsigned char *d2, unsigned char *d3)
{
 *d1 = val % 10;
 val = val / 10;
 *d2 = val % 10;
 val = val / 10;
 if (d3)
   { *d3 = val % 10;
     val = val / 10;
   }
 return val;
}

// and example use to display temp

LCDout_temp (unsigned short temp)
{
 unsigned short val = temp;
 char d1,d2,d3,d4;

 BCDecode(val, &d1, &d2, &d3);
 if (d3 > 0) {
   LCDdigit(d3);
 }
 else {
   LCDout(1, ' ');
 }
 LCDdigit(d2);
 LCDdigit(d1);
 LCDout(1, 'F');
}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...