|
我用例程上的程序能正确通过串口1发送数据,但是用我自己写的串口代码就发送不正确,例如发送个0x01,上位机显示的是0x1C
我的代码如下:
void Usart1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=USART1_BAUND;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
//USART_ClearFlag(USART1,USART_FLAG_TC);//ÏÈÇå³ý·¢ËÍÍê³É±êÖ¾£¬·ÀÖ¹ÔÚ¸ÕÅäÖÃÍê´®¿ÚºóµÚÒ»¸ö×Ö½Ú·¢ËͲ»³öÈ¥£¨BUG£©
}
int main(void)
{
SystemCoreClockUpdate();
Usart1_Init();
while(1)
{
Delay(0xfff);
USART_SendData(USART1, 0x01);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
我的keil上选的是stm32f429BI型号,这个应该不影响吧 |
|