你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F4 422串口通信

[复制链接]
STMCU小助手 发布时间:2022-11-24 21:06

STM32F429 422串口通信

1.422串口的硬件原理图如下:


0735b4d548c148c59942734955ab162b.png


2.422串口和232串口不同的是引脚的电平控制,还有DE是发送使能,置高即可:RE是接收使能,置低即可。422接出来的是4根线,有RS485_A、RS485_B、RS485_Z、RS485_Y,对应422串口线的DB1、DB2、DB3、DB4,,DB5连接地线,其他的配置用232串口的代码即可。


3.GPIO的配置:

  1. void RS422_Config()
  2. {
  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. USART_InitTypeDef USART_InitStructure;
  5. /* PC6/USART6_TX , PC7/USART6_RX, PG12/USART6_RTS, PG13/USART6_CTS */

  6. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC |RCC_AHB1Periph_GPIOG, ENABLE);

  7. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  8. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  9. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  10. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  11. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
  12. GPIO_Init(GPIOG, &GPIO_InitStructure);

  13. GPIO_ResetBits(GPIOG,GPIO_Pin_12); //USART6_RTS
  14. GPIO_SetBits(GPIOG,GPIO_Pin_13); //USART6_CTS

  15. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);

  16. GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);

  17. GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);

  18. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  19. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  20. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  21. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  22. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  23. GPIO_Init(GPIOC, &GPIO_InitStructure);

  24. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  25. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  26. GPIO_Init(GPIOC, &GPIO_InitStructure);

  27. USART422_NVIC_Config();

  28. USART_InitStructure.USART_BaudRate = 115200;
  29. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  30. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  31. USART_InitStructure.USART_Parity = USART_Parity_No ;
  32. //USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
  33. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  34. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  35. USART_Init(USART6, &USART_InitStructure);

  36. USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);

  37. USART_Cmd(USART6, ENABLE);

  38. USART_ClearFlag(USART6, USART_FLAG_TC);
  39. }
复制代码

4.422串口中断的配置:

  1. void USART422_NVIC_Config(void)
  2. {
  3. NVIC_InitTypeDef NVIC_InitStructure;
  4. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断分组2

  5. NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn; // 选择 USART6 通道
  6. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级设置为3
  7. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级设置为1
  8. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //开启中断
  9. NVIC_Init(&NVIC_InitStructure); //初始化配置
  10. }

  11. void USART6_IRQHandler (void)
  12. {
  13. char Date422 = 0;
复制代码

5.主函数的发送:

  1. int main(void)
  2. {

  3. Delay_Init(); //延时函数初始化
  4. RS422_Config(); //422串口初始化
  5. while(1)
  6. {
  7. Delay_ms(1000);
  8. USART_SendData(USART6,3); //将接收到的数据发回
  9. }
  10. }
复制代码

————————————————

版权声明:weixin_44553164



收藏 评论0 发布时间:2022-11-24 21:06

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版