初级会员

- 积分
- 65
- 金钱
- 65
- 注册时间
- 2020-5-6
- 在线时间
- 43 小时
|
3金钱
老哥们,我在配置STM32F103C8T6的串口3,但是我真的感觉已经没有什么问题了,但是就是不能和串口助手通信不管我事用的原来的PB10 11,还是重映射到PC10 11 都不行。USB转TTL自发自收没问题。结合电路图RX TX连接也没问题。求助
#include "sys.h"
#include "usart.h"
#if SYSTEM_SUPPORT_UCOS
#include "includes.h"
#endif
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
void _sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
while((USART3->SR&0X40)==0){};//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
USART3->DR = (u8) ch;
return ch;
}
#endif
/*ê1óÃmicroLibμÄ·½·¨*/
//int fputc(int ch, FILE *f)
//{
// USART_SendData(USART3, (uint8_t) ch);
// while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET) {}
//
// return ch;
//}
//int GetKey (void) {
// while (!(USART1->SR & USART_FLAG_RXNE));
// return ((int)(USART3->DR & 0x1FF));
//}
#if EN_USART3_RX
u16 USART_RX_STA=0; //½óêÕ×′쬱ê¼Ç
void uart_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); //串口3本来应该的引脚配置
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //′ò¿aê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE); //重映射后GPIOC10,11
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//
GPIO_PinRemapConfig(GPIO_PartialRemap_USART3,ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
//USART3_TX PB.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOC, &GPIO_InitStructure);
//USART3_RX PB.11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOC, &GPIO_InitStructure);
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//ÇàÕ¼óÅÏè¼¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4; //×óóÅÏè¼¶4
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//ò»°ãéèÖÃÎa9600;
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õê¼»ˉ′®¿ú
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿aÆôÖD¶Ï
USART_Cmd(USART3, ENABLE);
}
void USART3_IRQHandler(void) //′®¿ú3ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntEnter();
#endif
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART3);//(USART3->DR); //¶á衽óêÕμ½μÄêy¾Y
printf("USART3:%c\n",Res);
if((USART_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
{
if(USART_RX_STA&0x4000)//½óêÕμ½áË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
else USART_RX_STA|=0x8000; //½óêÕíê3éáË
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
}
}
}
}
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntExit();
#endif
}
#endif
|
|