Created
December 17, 2020 15:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@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