STM32 103 USART
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE );
USART2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
USART3
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE );
GPIO設(shè)置 TX配置為 復用推挽 RX配置為 開漏輸入
void GPIO_Configuration(void)
{
GPIO_InitTypeDefGPIO;
GPIO.GPIO_Pin =GPIO_Pin_9;
GPIO.GPIO_Speed =GPIO_Speed_50MHz;
GPIO.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO);//端口初始化
GPIO.GPIO_Pin =GPIO_Pin_10;
GPIO.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO);//端口初始化
}
USART配置
void USART_Configuration(void)//初始化USART
{USART_InitTypeDef USART_InitStructure;//聲明參數(shù)結(jié)構(gòu)
USART_InitStructure.USART_BaudRate = 9600;//速率9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//數(shù)據(jù)長度
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;//發(fā)送接收使能
USART_Init(USART1, &USART_InitStructure);//根據(jù)USART_InitStruct中指定的參數(shù)初始化外設(shè)USART1寄存器
USART_Cmd(USART1,ENABLE);//開啟USART外設(shè)
}
發(fā)送數(shù)據(jù)
發(fā)送數(shù)據(jù)
USART_SendData(USART1, ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET) {}
引腳定義
IO | USART | 重定義 | 重定義 | |||
PA8 | USART1_CK | |||||
PA9 | USART1_TX | PB6 | ||||
PA10 | USART1_RX | PB7 | ||||
PA11 | USART1_CTS | |||||
PA12 | USART1_RTS | |||||
PA0 | USART2_CTS | PD3 | ||||
PA1 | USART2_RTS | PD4 | ||||
PA2 | USART2_TX | PD5 | ||||
PA3 | USART2_RX | PD6 | ||||
PA4 | USART2_CK | PD7 | ||||
PB10 | USART3_TX | PD8 | PC10 | |||
PB11 | USART3_RX | PD9 | PC11 | |||
PB12 | USART3_CK | PD10 | PC12 | |||
PB13 | USART3_CTS | PD11 | ||||
PB14 | USART3_RTS | PD12 | ||||
評論