usart.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "usart.h"
  2. void init_usart(void){
  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. USART_InitTypeDef USART_InitStructure;
  5. /* enable peripheral clock for USART2 */
  6. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  7. /* GPIOA clock enable */
  8. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  9. /* GPIOA Configuration: USART2 TX on PA2 */
  10. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  14. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  15. GPIO_Init(GPIOA, &GPIO_InitStructure);
  16. /* Connect USART2 pins to AF2 */
  17. // TX = PA2
  18. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
  19. USART_InitStructure.USART_BaudRate = 9600;
  20. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  21. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  22. USART_InitStructure.USART_Parity = USART_Parity_No;
  23. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  24. USART_InitStructure.USART_Mode = USART_Mode_Tx;
  25. USART_Init(USART2, &USART_InitStructure);
  26. USART_Cmd(USART2, ENABLE); // enable USART2
  27. }