#include "stm32f2xx.h"
void GPIO_Config_AFMode(void);
void USART3_Config(void);
int main(void)
{
while (1)
{
USART_SendData(USART3, 0xA5);
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
}
}
void GPIO_Config_AFMode(void)
{
/* Connect PC10, PC11 to USART3_Tx, USART3_Rx */
/* Configure USART3_Tx and USART3_Rx as alternative function */
void USART3_Config(void)
{
USART_InitTypeDef USART_InitStructure;
/* Enable USART3 Clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* Configure USART3 */
USART_InitStructure.USART_BaudRate = 115200;
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(USART3, &USART_InitStructure);
/* Enable USART3 */
USART_Cmd(USART3, ENABLE);
}
/*
<USART Configuration>
1. USART Clock Enable
@reg RCC->AHB1ENR
@fn void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
@param RCC_APB1Periph_USART3, ENABLE
@ref stm32f2xx_rcc.h, stm32f2xx.h
2. Configure USART port (1) Word length, Parity, Mode
3. Configure USART port (2) Stop bits
@reg USARTx->CR2
@desc Bits 13:12, STOP: STOP bits configuration
@param USART_StopBits_1
@ref stm32f2xx_usart.h
4. Configure USART port (3) Hardware flow control
*/
void GPIO_Config_AFMode(void);
void USART3_Config(void);
int main(void)
{
GPIO_Config_AFMode();
USART3_Config();while (1)
{
USART_SendData(USART3, 0xA5);
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
}
}
void GPIO_Config_AFMode(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOC Clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/* Connect PC10, PC11 to USART3_Tx, USART3_Rx */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
/* Configure USART3_Tx and USART3_Rx as alternative function */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}void USART3_Config(void)
{
USART_InitTypeDef USART_InitStructure;
/* Enable USART3 Clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/* Configure USART3 */
USART_InitStructure.USART_BaudRate = 115200;
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(USART3, &USART_InitStructure);
/* Enable USART3 */
USART_Cmd(USART3, ENABLE);
}
/*
<USART Configuration>
1. USART Clock Enable
@reg RCC->AHB1ENR
@fn void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
@param RCC_APB1Periph_USART3, ENABLE
@ref stm32f2xx_rcc.h, stm32f2xx.h
2. Configure USART port (1) Word length, Parity, Mode
@reg USARTx->CR1
@desc Bit 12, M: Word length bits,
Bit 10, PCE: Parity control enable (by Hardware),
Bit 9, PS: Parity Selection (Even / Odd),
Bit 3, TE: Transmitter enable,
Bit 2, RE: Receiver enable
@param USART_WordLength_8b, USART_Parity_No, USART_Mode_Rx, USART_Mode_Tx
@ref stm32f2xx_usart.h
3. Configure USART port (2) Stop bits
@reg USARTx->CR2
@desc Bits 13:12, STOP: STOP bits configuration
@param USART_StopBits_1
@ref stm32f2xx_usart.h
4. Configure USART port (3) Hardware flow control
@reg USARTx->CR3
@desc Bit 9, CTSE: CTS enable
Bit 8, RTSE: RTS enable
@param USART_HardwareFlowControl_None
@ref stm32f2xx_usart.h
5. Configure USART port (4) Baudrate
@desc Bit 9, CTSE: CTS enable
Bit 8, RTSE: RTS enable
@param USART_HardwareFlowControl_None
@ref stm32f2xx_usart.h
5. Configure USART port (4) Baudrate
@reg USARTx->BRR
@desc Bits 15:4, DIV_Mantissa[11:0]: Mantissa of USARTDIV
Bits 3:0, DIV_Fraction[3:0]: Fraction of USARTDIV
Bits 3:0, DIV_Fraction[3:0]: Fraction of USARTDIV
@param USART_CR1_OVER8 (Oversampling)
@ref stm32f2xx_usart.h
6. USART enable
@reg USARTx->CR1
@desc Bit 13, UE: USART enable
@param USART_CR1_UE
@ref stm32f2xx_usart.h
<USART Data Transmission>
1. USART Send Data
@reg USARTx->DR
@desc Bits 8:0, DR[8:0]: Data value to send or receive
@fn void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
@param USART3
@ref stm32f2xx.h
2. USART Check Status
@reg USARTx->SR
@desc Bit 6, TC: Transmission complete
@fn FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
@param USART3, USART_FLAG_TC
@ref stm32f2xx.h, stm32f2xx_usart.h
Why is the GPIO configuration for USART "pull-up"?
ReplyDelete