RT,因为调试过程中UART1被其他功能占用,因此需要使用UART3打印数据到屏幕上。但是更改了fputc函数中的uart1为uart3之后仍无法实现。程序卡在
while((USART3->SR&0X40)==0);//循环发送,直到发送完毕
这个循环中。
个人认为可能原因是USART3出于某种原因没有成功能够发送数据,但具体细节及解决方案还是不清楚。
具体代码如下:
//加入以下代码,支持printf函数,而不需要选择MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART3->SR&0X40)==0);//循环发送,直到发送完毕
USART3->DR = (u8) ch;
return ch;
}
#endif
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
https://www.jianshu.com/p/d9a33a38aab1
https://www.douban.com/note/726028148/
有,UART3串口有初始化。
#include "stdio.h"
#include "string.h"
#include "stdarg.h"
#include "uart3.h"
u8 USART3_RX_BUF[USART3_MAX_RECV_LEN]; //½óêÕ»o3å×î′ó×Ö½ú
u8 USART3_TX_BUF[USART3_MAX_SEND_LEN]; //·¢Ëí»o3å×î′ó×Ö½ú
u16 USART3_RX_STA = 0;
void uart3_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;//GPIO
USART_InitTypeDef USART_InitStructure;//′®¿ú
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
USART_DeInit(USART3); //¸′λ′®¿ú3
//USART3_TX PB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOB, &GPIO_InitStructure); //3õê¼»ˉPB10
//USART3_RX PB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOB, &GPIO_InitStructure); //3õê¼»ˉPB11
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART3, &USART_InitStructure); //3õê¼»ˉ′®¿ú 3
USART_Cmd(USART3, ENABLE); //ê1Äü′®¿ú
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿aÆôÖD¶Ï
//éèÖÃÖD¶ÏóÅÏ輶
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
}
void USART3_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART3); //¶á衽óêÕμ½μÄêy¾Y
if((USART3_RX_STA&0x8000) == 0)//½óêÕÎ′íê3é
{
if(USART3_RX_STA&0x4000)//½óêÕμ½áË0x0d
{
if(Res != 0x0a)
USART3_RX_STA = 0;//½óêÕ′íÎó
else
USART3_RX_STA|= 0x8000;//½óêÕíê3é
}
else //»1Î′½óêÕμ½0x0d
{
if(Res==0x0d)
USART3_RX_STA |= 0x4000;
else
{
USART3_RX_BUF[USART3_RX_STA&0x3fff] = Res;
USART3_RX_STA++;
if(USART3_RX_STA>(USART3_MAX_RECV_LEN-1))
USART3_RX_STA = 0;//½óêÕ′íÎó
}
}
}
}
}
/*′®¿ú3·¢Ëíoˉêy*/
void u3_printf(char* fmt,...)
{
u16 i,j;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)USART3_TX_BUF,fmt,ap);
va_end(ap);
i=strlen((const char*)USART3_TX_BUF); //′Ë′η¢Ëíêy¾YμÄ3¤¶è
for(j=0;j<i;j++) //Ñ-»··¢Ëíêy¾Y
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
USART_SendData(USART3,USART3_TX_BUF[j]);
}
}
直接添加到你的工程里,可以直接用
一周热门 更多>