同为新手,参考例子配置的串口,测试可用。
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOA, GPIOB and GPIOF Peripheral clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOF, ENABLE);
/* USART1(PB6->TX, PB7->RX) Pins Configuration */
/* Connect pin to Peripheral */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
/* Configure pins as AF pushpull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void USART_Configuration(void) //´®¿Ú³õʼ»¯º¯Êý
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable USART1 Peripheral clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
/* USART1(PB6->TX, PB7->RX) Configuration(9600, 8, 1, N) */
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(USART1, &USART_InitStructure); //ÅäÖÃÈë½á¹¹Ìå
/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable the USART1 interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*----------------------------------------------------------------------------*/
USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú1
} |