今天用STM32单片机写一个素数计算程序,用串口反馈计算结果。
因STM32串口HAL库函数只能用字符串发送,在网上找了几个程序,不能用,不如自己写一个。
uint8_t Str_buf[10]; //数值字符串数组
void Num2str(uint32_t x)
{
uint32_t i = 10000000, Number_Integer;
uint8_t bz = 0, ls;
Number_Integer = (uint32_t)x;
while (Number_Integer > 0)
{
ls = Number_Integer / i % 10;
Str_buf[bz] = (uint8_t)ls + 0x30;
Number_Integer = Number_Integer - ls * i;
i = i / 10;
if (ls > 0 | (ls == 0 & Str_buf[0] > 0x30)) //将头部0舍去
bz++;
}
}
//调用方式
Num2str(500091);
该程序在STM32F030K ,用KEIL MDK 编译通过,并运行。