|
網(wǎng)上找到的都是 USART1的。。我寫了一個USART2的。在103上驗證通過。請參考。。
void USART2_Initialise( u32 bound )
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStruct;
USART_InitTypeDef USART_InitStructure;
/* Enable the USART2 Pins Software Remapping */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //開啟USART2時鐘
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置USART2
USART_InitStructure.USART_BaudRate = bound; //波特率9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位數(shù)據(jù)
USART_InitStructure.USART_StopBits = USART_StopBits_1; //在幀結(jié)尾傳輸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;//發(fā)送、接收使能
//配置USART2時鐘
USART_ClockInitStruct.USART_Clock = USART_Clock_Disable; //時鐘低電平活動
USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low; //SLCK引腳上時鐘輸出的極性->低電平
USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge; //時鐘第二個邊沿進行數(shù)據(jù)捕獲
USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable; //最后一位數(shù)據(jù)的時鐘脈沖不從SCLK輸出
USART_Init(USART2, &USART_InitStructure);
USART_ClockInit(USART2, &USART_ClockInitStruct);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);
}
|
評分
-
查看全部評分
|