2019-08-10 16:11发布
dateher 发表于 2017-10-28 17:47 请问我串口接收到的是乱码怎么破 ? 波特率 停止位都是对上的
韩宇 发表于 2017-10-28 19:08 我的程序只是波特率不一致的情况下才出现乱码,其他情况没遇到。新手一枚,
韩宇 发表于 2017-10-30 16:42 我用的STM32F103VET6
最多设置5个标签!
我的程序只是波特率不一致的情况下才出现乱码,其他情况没遇到。新手一枚,
你用啥板 有f401代码没 我收不到数据。。
能帮我看下串口接收部分吗? 一直收不到
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//Ïàó|ê±Öóê1Äü USART1oí6ÔúAPB2éÏÆäËû′®¿ú¶¼ÔúAPB1éÏ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); //ê1ÄüUSART1ê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB , ENABLE); //ê1ÄüGPIOBê±Öó
//′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1); //GPIOB9¸′óÃÎaUSART1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1); //GPIOB10¸′óÃÎaUSART1
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ; //TX | RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure); //3õê¼»ˉPB6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ; //TX | RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_Init(GPIOB,&GPIO_InitStructure); //3õê¼»ˉPB7
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = 115200; //2¨ìØÂêéèÖÃ
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Ææż¼ìÑéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART1, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //¿aÆôÏà1ØÖD¶Ï
#if EN_USART1_RX
NVIC_Configuration();
#endif
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú1
//learFlag(USART1, USART_FLAG_TC);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //ÇàÏèóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
}
int fputc(int ch, FILE *f)
{
/* ½«rintfÄúèY·¢íù′®¿ú */
USART_SendData(USART1, (unsigned char) ch);
/* μè′y·¢Ëííê±Ï */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return (ch);
}
#if EN_USART1_RX //???????
void USART1_IRQHandler(void) //??1??????
{
printf("come ");
u8 Res;
#ifdef OS_TICKS_PER_SEC //??????????,?????ucosII?.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
printf(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; //½óêÕíêáË
}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;//
}
}
}
}
#ifdef OS_TICKS_PER_SEC //??????????,?????ucosII?.
OSIntExit();
#endif
}
#endif
一周热门 更多>