当前位置:   article > 正文

STM32串口配置_stm32 串口指定 gpio 口

stm32 串口指定 gpio 口

串口一

  1. void usart1_Init(u32 bound)
  2. {
  3. //GPIO端口设置
  4. GPIO_InitTypeDef GPIO_InitStructure;
  5. USART_InitTypeDef USART_InitStructure;
  6. NVIC_InitTypeDef NVIC_InitStructure;
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);//使能USART1,GPIOA,C时钟
  8. //USART1_TX GPIOA.9
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  11. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  12. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
  13. //USART1_RX GPIOA.10初始化
  14. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  15. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  16. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
  17. //Usart1 NVIC 配置
  18. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 0-3;
  19. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  20. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
  21. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  22. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  23. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  24. //USART 初始化设置
  25. USART_InitStructure.USART_BaudRate = bound;//串口波特率
  26. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  27. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  28. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  29. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  30. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  31. USART_Init(USART1, &USART_InitStructure); //初始化串口1
  32. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  33. USART_Cmd(USART1, ENABLE); //使能串口1
  34. }
  35. USART1_IRQHandler(void) //串口1中断服务程序
  36. {
  37. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  38. {
  39. }
  40. USART_ClearFlag(UART1,USART_IT_RXNE); //一定要清除接收中断
  41. }

串口二

  1. void usart2_Init(u32 bound)
  2. {
  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. USART_InitTypeDef USART_InitStructure;
  5. NVIC_InitTypeDef NVIC_InitStructure;
  6. //|RCC_APB2Periph_AFIO
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能GPIOA时钟
  8. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟
  9. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
  10. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽
  11. GPIO_Init(GPIOA, &GPIO_InitStructure);
  12. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
  13. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
  14. GPIO_Init(GPIOA, &GPIO_InitStructure);
  15. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//复位串口2
  16. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止复位
  17. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 0-3;
  18. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //使能串口2中断
  19. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //先占优先级2
  20. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级2
  21. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
  22. NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
  23. USART_InitStructure.USART_BaudRate = bound;//波特率设置
  24. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位数据长度
  25. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  26. USART_InitStructure.USART_Parity = USART_Parity_No;///奇偶校验位
  27. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  28. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//收发模式
  29. USART_Init(USART2, &USART_InitStructure); ; //初始化串口
  30. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
  31. USART_Cmd(USART2, ENABLE); //使能串口
  32. }
  33. void USART2_IRQHandler(void)
  34. {
  35. if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收到数据
  36. {
  37. }
  38. USART_ClearFlag(UART2,USART_IT_RXNE); //一定要清除接收中断
  39. }

串口3

  1. //串口3初始化
  2. void uart3_init(u32 bound)
  3. {
  4. //GPIO端口设置
  5. GPIO_InitTypeDef GPIO_InitStructure;
  6. USART_InitTypeDef USART_InitStructure;
  7. NVIC_InitTypeDef NVIC_InitStructure;
  8. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
  10. //USART3_TX GPIOB.10
  11. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  13. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  14. GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.10
  15. //USART3_RX GPIOB.11初始化
  16. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PB.11
  17. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  18. GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.11
  19. //Usart1 NVIC 配置
  20. NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  21. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1 ;//抢占优先级1
  22. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  23. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  24. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  25. //USART 初始化设置
  26. USART_InitStructure.USART_BaudRate = bound;//串口波特率
  27. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  28. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  29. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  30. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  31. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  32. USART_Init(USART3, &USART_InitStructure); //初始化串口3
  33. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //开启串口接收中断
  34. USART_Cmd(USART3, ENABLE); //使能串口3
  35. }
  36. //串口3中断服务程序
  37. void USART3_IRQHandler(void)
  38. {
  39. if(USART_GetITStatus(USART3, USART_IT_RXNE))//接收中断
  40. {
  41. }
  42. USART_ClearITPendingBit(USART3, USART_IT_RXNE);
  43. }

串口4

  1. //串口4初始化
  2. void uart4_init(u32 bound)
  3. {
  4. //GPIO端口设置
  5. GPIO_InitTypeDef GPIO_InitStructure;
  6. USART_InitTypeDef USART_InitStructure;
  7. NVIC_InitTypeDef NVIC_InitStructure;
  8. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
  10. //USART4_TX GPIOC.10
  11. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PC.10
  12. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  14. GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOC.10
  15. //USART3_RX GPIOC.11初始化
  16. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PC.11
  17. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  18. GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOC.11
  19. //Usart1 NVIC 配置
  20. NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
  21. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2 ;//抢占优先级2
  22. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  23. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  24. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  25. //UART 初始化设置
  26. USART_InitStructure.USART_BaudRate = bound;//串口波特率
  27. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  28. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  29. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  30. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  31. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  32. USART_Init(UART4, &USART_InitStructure); //初始化串口4
  33. USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //开启串口接收中断
  34. USART_Cmd(UART4, ENABLE); //使能串口4
  35. }
  36. //串口4中断服务程序
  37. void UART4_IRQHandler(void)
  38. {
  39. if(USART_GetITStatus(UART4, USART_IT_RXNE))//接收中断
  40. {
  41. }
  42. USART_ClearITPendingBit(UART4, USART_IT_RXNE);
  43. }

串口5

  1. 暂时还没用到
  2. 那就先不写

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/982193?site
推荐阅读
相关标签
  

闽ICP备14008679号