我想請點擊此鏈接的答案來配置我的我的STM32F0探索板的USART: stm32f0 uart programmingPC從STM32F0 USART接收到錯誤的數據
我用USART2將數據發送到我的電腦與波特率9600和115200都試過。
我從'0' - >'9'發送字符到PC,但我收到'cg3fe2d'和一些不可見的字符總是,似乎有一些規定,任何人都可以幫忙嗎?
我的STM32F0配置爲使用內部osc,48MHz。
我的參考代碼如下:
void test_uart()
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
uint16_t usart_data;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
//Configure USART2 pins: Rx and Tx ----------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Configure USART2 setting: ----------------------------
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2,ENABLE);
usart_data = '0';
while(1)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
if (usart_data > '9')
usart_data = '0';
USART_SendData(USART2, usart_data++);
}
}
有沒有人看過這個? – yunfei
看看收到的字符與發送的字符的比特串,如果波特率真的是正確的,我懷疑你的邏輯電平可能會變得太多(或太少)一個倒數。 – Notlikethat
是的,可能。我現在懷疑在OSC上,可能會嘗試一個外側獨立的OSC。 – yunfei