Skip to content

Instantly share code, notes, and snippets.

@Luctins
Created December 17, 2020 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luctins/c0cc3ad8e17b43358e4e3c8d87d72a80 to your computer and use it in GitHub Desktop.
Save Luctins/c0cc3ad8e17b43358e4e3c8d87d72a80 to your computer and use it in GitHub Desktop.
Routine for printing numbers on platforms that have no printf (this one specifically is for NIOS II), DEBUG_OUTPUT is your the string printing routine of choice.
/**
@brief print a number as a decimal (because of memory constraints the limited printf only works for hex and strings, no decimals).
*/
void print_num(alt_u32 num)
{
char buff[15],tmp;
char * str = buff;
alt_u8 dp=0; /*number of decimal places*/
if(num == 0)
{
*str ='0';
++str;
}
else
{
while(num>0)
{
*(str)='0'+(num%10); /*set the digit as the remainder of dividing by ten (because '0' + 1 = '1')*/
++str;/*advance the pointer by one*/
num -= num%10; /*remove the last digit*/
num /= 10; /*shift the number one *decimal* place*/
}
dp=str-buff;
/*this is needed otherwise the number would be written in reverse*/
for(alt_u8 i = 0;i<(dp>>1/*dp/2*/);++i)
{
tmp = buff[i];
buff[i] = buff[dp-i-1];
buff[dp-i-1] = tmp;
}
}
*str='\0'; /*null termination*/
DEBUG_OUTPUT(buff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment