uart4.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "uart4.h"
  2. #include "stm32f4xx.h"
  3. void uart4_init(void)
  4. {
  5. GPIO_InitTypeDef GPIO_StructInit;
  6. NVIC_InitTypeDef NVIC_InitStructure;
  7. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  8. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
  9. GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  10. GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  11. GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  12. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP;
  13. GPIO_StructInit.GPIO_Pin = GPIO_Pin_10;
  14. GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
  15. GPIO_Init(GPIOC, &GPIO_StructInit);
  16. GPIO_StructInit.GPIO_Pin = GPIO_Pin_11;
  17. // GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF;
  18. // GPIO_StructInit.GPIO_OType = GPIO_OType_PP;
  19. // GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz;
  20. // GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
  21. GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
  22. GPIO_Init(GPIOC, &GPIO_StructInit);
  23. NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; // 串口1中断通道
  24. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
  25. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级
  26. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
  27. NVIC_Init(&NVIC_InitStructure);
  28. USART_InitTypeDef USART_InitStructure;
  29. USART_InitStructure.USART_BaudRate = 115200;
  30. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  31. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  32. USART_InitStructure.USART_Parity = USART_Parity_No;
  33. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  34. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  35. USART_Init(UART4, &USART_InitStructure); // 初始化串口
  36. USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
  37. USART_Cmd(UART4, ENABLE); // 使能串口
  38. }