两种办法,一种使用微库,一种不使用:
//不使用microLib*/
#include "sys.h"
#include "usart.h"
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef’ d in stdio.h. */
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
#endif
int main()/*主函数“整数”类型*/
{
char buffer[50];/*“字符”类型的数组,下面共有50个元素。*/
int n,a=5,b=3;/*三个变量都为“整数”类型,intn中间要有空格*/
while(1)
{
//n=sprintf(buffer,"%d plus %d is %d",a,b,a+b);/*赋予数值*/
printf("[%s]is a string %d chars long\n",buffer,n);/*“格式输出函数”*/