为了解决LCD屏上显示正负整数的麻烦,特意写出这个函数:
uint8_t* num2string(int16_t num)
{
uint8_t count = 0;
int16_t temp_num = num;
if(num <= 0)
{
count++;
temp_num = -num;
}
while (temp_num)//整数的长度
{
count++;
temp_num /= 10;
}
uint8_t* temp_str = (u8*)malloc(sizeof(u8)*(count+1));
temp_str[count] = '\0';
if(num < 0)
temp_num = -num;
else
temp_num = num;
for (u8 i = 0; i < count; i++)
{
temp_str[count - i - 1] = ((u8)('0'+temp_num % 10));
temp_num /= 10;
}
if(num < 0) temp_str[0] = '-';
return temp_str;
}