OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3700|回复: 3

串口通信问题

[复制链接]

2

主题

5

帖子

0

精华

新手上路

积分
25
金钱
25
注册时间
2017-10-5
在线时间
4 小时
发表于 2018-2-7 16:26:54 | 显示全部楼层 |阅读模式
3金钱
STM32F767用到2个串口:串口1,230400,DMA发送,中断接收;串口5,460800,DMA发送,中断接收。初始化程序如下:
//初始化函数
void MX_UART5_Init(void)
{
  huart5.Instance = UART5;
  huart5.Init.BaudRate = 460800;
  huart5.Init.WordLength = UART_WORDLENGTH_8B;
  huart5.Init.StopBits = UART_STOPBITS_1;
  huart5.Init.Parity = UART_PARITY_NONE;
  huart5.Init.Mode = UART_MODE_TX_RX;
  huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart5.Init.OverSampling = UART_OVERSAMPLING_16;
  huart5.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart5.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart5) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
  HAL_UART_Receive_IT(&huart5, RS422Type.RxTmpBuf, 1);
}
/* USART1 init function */

void MX_USART1_UART_Init(void)
{
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 230400;
  huart1.Init.WordLength = UART_WORDLENGTH_9B;
  huart1.Init.StopBits = UART_STOPBITS_2;
  huart1.Init.Parity = UART_PARITY_EVEN;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
  HAL_UART_Receive_IT(&huart1, RS232Type.RxTmpBuf, 1);
}
//串口中断函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
        if(huart->Instance==USART1)//如果是串口1
        {
                if(RS232Type.RxTmpBuf[0]==0x55 && RS232Type.rx_len==0)
                {
                        RS232Type.RxBuf[0]=RS232Type.RxTmpBuf[0];
                        RS232Type.rx_len=1;
                }
                else if(RS232Type.RxTmpBuf[0]==0xAA && RS232Type.rx_len==1)
                {
                        RS232Type.RxBuf[1]=RS232Type.RxTmpBuf[0];
                        RS232Type.rx_len=2;
                }
                else if(RS232Type.rx_len>1 && RS232Type.rx_len<12)
                {
                        RS232Type.RxBuf[RS232Type.rx_len]=RS232Type.RxTmpBuf[0];
                        RS232Type.rx_len++;
                        if(RS232Type.rx_len>=12)
                        {
                                RS232Type.rx_len=0;
                                HAL_UART_Transmit_DMA(&huart1,RS232Type.RxBuf,12);       
                        }
                }
                else
                {
                        RS232Type.rx_len=0;
                }
                HAL_UART_Receive_IT(&huart1,RS232Type.RxTmpBuf,1);
        }
        else if(huart->Instance==UART5)//如果是串口5
        {
                if(RS422Type.RxTmpBuf[0]==0x90 && RS232Type.rx_len==0)
                {
                        RS422Type.RxBuf[0]=RS422Type.RxTmpBuf[0];
                        RS422Type.rx_len=1;
                        HAL_GPIO_WritePin(GPIOA, LED2_Pin|LED4_Pin|LED5_Pin|LED3_Pin, GPIO_PIN_RESET);
                }
                else if(RS422Type.rx_len>=1 && RS422Type.rx_len<12)
                {
                        RS422Type.RxBuf[RS422Type.rx_len]=RS422Type.RxTmpBuf[0];
                        RS422Type.rx_len++;
                        if(RS422Type.rx_len>=12)
                        {
                                RS422Type.rx_len=0;
                                //90 FF FC 69 FF F9 A0 00 01 DC 00 CA,共12个字节
                               
                        }
                }
                else
                {
                        RS422Type.rx_len=0;
                }
                HAL_UART_Receive_IT(&huart5,RS422Type.RxTmpBuf,1);
        }
}
767先上电,设备再上电通信正常,但设备先上电,再运行767,767进不了串行中断。请指正。

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

2

主题

5

帖子

0

精华

新手上路

积分
25
金钱
25
注册时间
2017-10-5
在线时间
4 小时
 楼主| 发表于 2018-2-8 20:48:25 | 显示全部楼层
回复

使用道具 举报

3

主题

154

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
434
金钱
434
注册时间
2013-3-18
在线时间
82 小时
发表于 2018-2-9 10:18:23 | 显示全部楼层
理论上会进中断,但是最大可能是错误的中断,比如溢出,帧错误,等等(必须要清除这些才能正常通信)。建议debug,断点直接设置在中断函数中,而不是接收回调
回复

使用道具 举报

1

主题

882

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3071
金钱
3071
注册时间
2018-2-7
在线时间
285 小时
发表于 2018-2-9 10:44:10 | 显示全部楼层
中断函数里面不用清除中断标志位吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-20 06:33

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表