//串口初始化
void Uart_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
USART_InitStructure.USART_BaudRate=4800;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_Parity=USART_Parity_Odd;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
}
//发送字符串
void USART_SendStr(uint8_t *str)
{
while(*str)
{
while((USART_GetFlagStatus(USART1,USART_FLAG_TC))!=SET);
USART_SendData(USART1,*str);
str++;
}
}
uint8_t senddata[SEND_MAXBUFF]="ABCDEFGHIJKLMNOPQRST";
int main(void)
{
Uart_Init();
while(1)
{
USART_SendStr(senddata);
delay_ms(1000);
}
}
当使用 USART_SendStr发送字符串“ABCDEFGHIJKLMNOPQRST”时
(16进制为41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54),
通过PC机串口接收到的数据为:C1 C2 43 C4 45 46 C7 C8 49 4A CB 4C CD CE 4F D0 51 52 D3 54
其中正确的数据为: 43 45 46 49 4A 4F 51 52 54
当使用USART_SendStr发送字符串43 45 46 49 4A 4F 51 52 54时,接收正常
当使用库函数USART_SendData以单个字节发送这些正确的数据时(如发送字符'C'),结果也正常
当使用库函数USART_SendData以单个字节发送这些错误的数据时(如发送字符'A'),结果也是错误
更换了串口UART5和USART1 ,调整了波特率、停止位、效验位、数据位、以及GPIO的输出频率,结果仍然是错误,而且出错的字符始终出错,正确的字符始终都是正确的,PC机的串口通过短接2脚和3脚,收发都正常,请高手帮忙看下怎么回事?
|