- void connectUsart1ToExt(uint32_t baudrate, uint8_t parity, uint8_t length, uint8_t stopbits)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- #if defined (STM32F051) || defined (STM32F042)
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
-
- // GPIOµÄ¹«¹²ÌØÐÔ
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- //COM1
- //USARTx_TX GPIO_Mode_AF_PP
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Pin = GetPinOf(ST_COM1_Tx);
- GPIO_Init(GetPortOf(ST_COM1_Tx) , &GPIO_InitStructure);
- SetAF_COM1_Tx;//±ØÐëÑ¡Ôñ¸´Óù¦ÄÜ
- //USARTx_RX GPIO_Mode_AF_PP & IPU
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Pin = GetPinOf(ST_COM1_Rx);
- GPIO_Init(GetPortOf(ST_COM1_Rx), &GPIO_InitStructure);
- SetAF_COM1_Rx;//±ØÐëÑ¡Ôñ¸´Óù¦ÄÜ
-
- //RS485-DE
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Pin = GetPinOf(ST_COM1_DE);
- GPIO_Init(GetPortOf(ST_COM1_DE), &GPIO_InitStructure);
- GPIO_SetBits(RS485_DE);
-
- #else
- #warning "Make sure needn't define STM32F051 etc. ?"
- #endif
- USART_Cmd(USART1, DISABLE); //ÓÃ×÷ÖØÐÂÅäÖÃUSART1£¬Ïȹش®¿Ú
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
-
- USART_InitStructure.USART_BaudRate = baudrate;
-
- switch (parity)
- {
- case 1://Ææ
- USART_InitStructure.USART_Parity = USART_Parity_Odd;
- if (length == 7)
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- else
- USART_InitStructure.USART_WordLength = USART_WordLength_9b;
- break;
- case 2://ż
- USART_InitStructure.USART_Parity = USART_Parity_Even;
- if (length == 7)
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- else
- USART_InitStructure.USART_WordLength = USART_WordLength_9b;
- break;
- default://0ÎÞ8λÊý¾Ýλ
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- break;
- }
- switch (stopbits)
- {
- case 2:
- USART_InitStructure.USART_StopBits = USART_StopBits_2;
- break;
- case 3:
- USART_InitStructure.USART_StopBits = USART_StopBits_1_5;
- break;
- #if defined (STM32F051) || defined (STM32F042)
- //null
- #elif defined (STM32F100) || defined (STM32F101) || defined (STM32F103)
- case 0://±£ÁôSTM32F05²»Ö§³Ö,STM32F10Ö§³Ö
- USART_InitStructure.USART_StopBits = USART_StopBits_0_5;
- break;
- #else
- #warning "Make sure needn't define STM32F051 etc. ?"
- #endif
- default://1
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- break;
- }
-
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure);
-
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
- USART_Cmd(USART1, ENABLE);
- }
|