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

【经验分享】STM32之模拟串口设计

[复制链接]
STMCU小助手 发布时间:2022-1-13 21:00
一、设计用途:
公司PCB制成板降成本,选择的MCU比项目需求少一个串口,为满足制成板成本和项目对串口需求,选择模拟一路串口。
二、硬件电路:

1252227-20190505232154202-1059822249.png


三、设计实现:
工具&软件:STM32F030R8    KEIL5    STM32CubeMX
1、 串口通信
串口是一种很常用的通信接口,按位(bit)发送和接收字节,串口通信是异步传输,端口能够在一根线上发送数据同时在另一根线上接收数据。串口通信最重要的参数是比特率数据位、停止位和奇偶校验。对于两个进行通信的端口,这些参数必须匹配,在我们单片机设计中发送和接收一个字节的位格式一般由起始信号位,数据位,停止位组成,很少有校验位,但是可以有。

1252227-20190505232300122-263953928.png


2、 串口STM32串口硬件实现原理
既然想要模拟串口通信,那就需要设计得最接近于硬件串口,那么硬件串口是怎样实现数据的采集的呢?

1252227-20190505232320382-137876127.png


以设置过采样率为8时进行分析:
在硬件串口接收线空闲时,串口一般会以一定频率去采集接收线上电平信号,如果辨认出一个特殊序列,1110X0X0X0X0X0X0 则确认接收到起始帧,触发接收中断。在前面判断三个1后如果后面采集到的位中有1则会降噪声位置1,所以在模拟串口时也需要加入噪声处理。

硬件串口的噪声处理:

1252227-20190505232341778-250912052.png

1252227-20190505232353559-1148280286.png

简单来说,噪声处理就是将采集到的正中间的三位值进行判定,取三个中至少两个相同值作为最终采集值。

3、 实现方式选择
在网上大概看了一些前辈做的模拟串口,大致分为以下几种:
1、 阻塞式:接收使用中断,发送直接在主函数中进行,接收时以波特率的位时间进行定时采集,尽量的将采集点位于位中间。
2、 非阻塞式:使用一个定时器或者两个定时器,将接收和发送作于中断中,但接收方式基本一致,都只采集一次,未将噪声考虑进入。
也许每个行业对噪声的关注度都不太一样,但是在我这项目中噪声确是一个不得不面对的一个问题,只为通信更可靠。

自写第一种实现方式:
以波特率位时间的1/6进行中断采集,将每一位均分六等份,采集五次,然后取中间三次进行值判断。

1252227-20190505232418441-180059386.png
  1. #include "UartSet.h"
  2. #include "string.h"

  3. uint8_t ucRecvData = 0;         //每次接收的字节
  4. uint8_t ucAcquCx = 0;           //三次滤波计数
  5. uint8_t ucRecvBitCx = 0;        //接收位计数
  6. uint8_t ucSendBitCx = 0;        //发送位计数
  7. uint8_t ucSendLengCx = 0;       //发送长度计数

  8. uint8_t ucRecvLastBit = 0;     //上次接收位记录
  9. uint8_t ucRecvNowBit = 0;      //当前位记录
  10. uint8_t ucRecvTrueCx = 0;     //正确计数

  11. uint32_t Prescaler = 0;
  12. uint32_t Period = 0;
  13. UART gsUARTBuff;               //定义串口

  14. TIM_HandleTypeDef htim6;

  15. void MX_TIM6_Init(void)
  16. {

  17.     __HAL_RCC_TIM6_CLK_ENABLE();

  18.     htim6.Instance = TIM6;
  19.     htim6.Init.Prescaler = Prescaler;
  20.     htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  21.     htim6.Init.Period = Period;
  22.     htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

  23.     HAL_NVIC_SetPriority(TIM6_IRQn, 0, 0);
  24.     HAL_NVIC_EnableIRQ(TIM6_IRQn);

  25.     HAL_TIM_Base_Init(&htim6);

  26. }


  27. void UART_GPIO_Init(void)
  28. {
  29.     GPIO_InitTypeDef GPIO_InitStruct = {0};

  30.     __HAL_RCC_GPIOB_CLK_ENABLE();

  31.     GPIO_InitStruct.Pin = COM_TX_Pin;
  32.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  33.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  34.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  35.     HAL_GPIO_Init(COM_TX_GPIO_Port, &GPIO_InitStruct);

  36.     GPIO_InitStruct.Pin = COM_RX_Pin;
  37.     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  38.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  39.     HAL_GPIO_Init(COM_RX_GPIO_Port, &GPIO_InitStruct);

  40.     HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  41.     HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

  42. }


  43. /*******************************************************************************
  44. * @FunctionName   : UART_Init.
  45. * @Description    : 模拟串口结构体初始化.
  46. * @Input          : None.
  47. * @Output         : None.
  48. * @Return         : None.
  49. * @Author&Data    : MrShuCai  2019.4.11.
  50. *******************************************************************************/
  51. void UART_Init(void)
  52. {
  53.     gsUARTBuff.CheckType = NONE;
  54.     gsUARTBuff.UartMaxLength = Uartlength;
  55.     gsUARTBuff.UartStat = COM_NONE_BIT_DEAL;
  56.     UART_GPIO_Init();

  57.     if(BaudRate == 1200)
  58.     {
  59.         Prescaler = 15;
  60.         Period = 623;
  61.     }
  62.     else if(BaudRate == 2400)
  63.     {
  64.         Prescaler = 15;
  65.         Period = 207;
  66.     }
  67.     else if(BaudRate == 4800)
  68.     {
  69. //        Prescaler = 15;
  70. //        Period = 50;  //9600
  71.              Prescaler = 15;
  72.              Period = 103;
  73.     }

  74.     MX_TIM6_Init();
  75. }


  76. /*******************************************************************************
  77. * @FunctionName   : UART_Send_Data.
  78. * @Description    : 模拟串口发送数据接口.
  79. * @Input          : None.
  80. * @Output         : None.
  81. * @Return         : None.
  82. * @Author&Data    : MrShuCai  2019.4.11.
  83. *******************************************************************************/
  84. void UART_Send_Data(uint8_t * data, uint8_t size)
  85. {
  86.     gsUARTBuff.Sendlength = size;
  87.     memcpy(gsUARTBuff.UART_Send_buf, data, size);
  88.     gsUARTBuff.TxEn = 1;
  89.     gsUARTBuff.RxEn = 0;
  90.     gsUARTBuff.UartStat = COM_START_BIT_DEAL;
  91.     HAL_TIM_Base_Start_IT(&htim6);
  92. }


  93. /*******************************************************************************
  94. * @FunctionName   : EXTI4_15_IRQHandler.
  95. * @Description    : 接收引脚外部中断,下降沿触发,触发后即进入起始位判断.
  96. * @Input          : None.
  97. * @Output         : None.
  98. * @Return         : None.
  99. * @Author&Data    : MrShuCai  2019.4.11.
  100. *******************************************************************************/
  101. void EXTI4_15_IRQHandler(void)
  102. {

  103.     if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4) != RESET)
  104.     {
  105.         __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4);

  106.         if(gsUARTBuff.UartStat == COM_NONE_BIT_DEAL)
  107.         {
  108.             gsUARTBuff.RxEn = 1;
  109.             ucRecvData = 0;
  110.             gsUARTBuff.UartStat = COM_START_BIT_DEAL;
  111.             HAL_TIM_Base_Start_IT(&htim6);
  112.         }
  113.     }
  114. }

  115. /*******************************************************************************
  116. * @FunctionName   : TIM6_IRQHandler.
  117. * @Description    : 中断处理函数,包括发送和接收两部分.
  118. * @Input          : None.
  119. * @Output         : None.
  120. * @Return         : None.
  121. * @Author&Data    : MrShuCai  2019.4.11.
  122. *******************************************************************************/
  123. void TIM6_IRQHandler(void)
  124. {
  125.     HAL_TIM_IRQHandler(&htim6);

  126.     if(gsUARTBuff.TxEn == 1)       /*数据发送,发送优先,无发送后才进入接收状态*/
  127.     {
  128.         switch(gsUARTBuff.UartStat)  /*串口发送位状态判断*/
  129.         {
  130.         case COM_START_BIT_DEAL :
  131.         {
  132.             HAL_GPIO_WritePin(COM_TX_GPIO_Port, COM_TX_Pin, GPIO_PIN_RESET);

  133.             if(++ ucAcquCx == 6)    /*由于发送时在立即拉低就进入判断,且自加,所以需要加到4,实际延时3个周期*/
  134.             {
  135.                 gsUARTBuff.UartStat = COM_DATA_BIT_DEAL;
  136.                 ucAcquCx = 0;
  137.                 ucSendBitCx = 0;
  138.             }
  139.         }
  140.         break;

  141.         case COM_DATA_BIT_DEAL :
  142.         {

  143.             HAL_GPIO_WritePin(COM_TX_GPIO_Port, COM_TX_Pin, (GPIO_PinState)((gsUARTBuff.UART_Send_buf[ucSendLengCx] >> ucSendBitCx) & 0x01));

  144.             if(++ ucAcquCx >= 6)
  145.             {
  146.                 ucAcquCx = 0;
  147.                            
  148.                 ucSendBitCx ++;

  149.                 if(ucSendBitCx >= 8)
  150.                 {
  151.                     if(gsUARTBuff.CheckType == NONE)
  152.                     {
  153.                         gsUARTBuff.UartStat = COM_STOP_BIT_DEAL;
  154.                     }
  155.                     else
  156.                     {
  157.                         gsUARTBuff.UartStat = COM_CHECK_BIT_DEAL;
  158.                     }
  159.                 }
  160.             }
  161.         }
  162.         break;

  163.         case COM_CHECK_BIT_DEAL :
  164.         {

  165.         }
  166.         break;

  167.         case COM_STOP_BIT_DEAL :
  168.         {
  169.             HAL_GPIO_WritePin(COM_TX_GPIO_Port, COM_TX_Pin, GPIO_PIN_SET);

  170.             if(++ ucAcquCx == 7)
  171.             {
  172.                 ucAcquCx = 0;
  173.                 ucSendBitCx = 0;

  174.                 if(ucSendLengCx < gsUARTBuff.Sendlength - 1)
  175.                 {
  176.                     gsUARTBuff.UartStat = COM_START_BIT_DEAL;
  177.                     ucSendLengCx ++;
  178.                 }
  179.                 else
  180.                 {
  181.                     ucSendLengCx = 0;
  182.                     gsUARTBuff.UartStat = COM_NONE_BIT_DEAL;
  183.                     gsUARTBuff.TxEn = 0;
  184.                     gsUARTBuff.RxEn = 1;

  185.                     HAL_TIM_Base_Stop_IT(&htim6);
  186.                     TIM6->CNT = 0;
  187.                 }
  188.             }
  189.         }
  190.         break;

  191.         default:
  192.             break;

  193.         }
  194.     }


  195.     if(gsUARTBuff.RxEn == 1)
  196.     {
  197.         switch(gsUARTBuff.UartStat)
  198.         {
  199.         case COM_START_BIT_DEAL :                         //起始位 必须连续三次采集正确才认可
  200.         {
  201.             ucRecvLastBit = ucRecvNowBit;
  202.             ucRecvNowBit = HAL_GPIO_ReadPin(COM_RX_GPIO_Port, COM_RX_Pin);

  203.             if(    ucAcquCx == 0)
  204.             {
  205.                 ucAcquCx = 1;
  206.                 return ;
  207.             }

  208.             if(ucRecvLastBit == ucRecvNowBit)
  209.             {
  210.                 ucRecvTrueCx ++;
  211.             }

  212.             if(++ ucAcquCx >= 5)
  213.             {
  214.                 if(ucRecvTrueCx >= 2)
  215.                 {
  216.                     gsUARTBuff.UartStat = COM_DATA_BIT_DEAL;
  217.                     ucAcquCx = 0;
  218.                     ucRecvTrueCx = 0;
  219.                 }
  220.                 else
  221.                 {
  222.                     ucAcquCx = 0;
  223.                     ucRecvTrueCx = 0;
  224.                     gsUARTBuff.UartStat = COM_STOP_BIT_DEAL;
  225.                 }
  226.             }
  227.         }
  228.         break;

  229.         case COM_DATA_BIT_DEAL :                         //数据位
  230.         {

  231.             ucRecvLastBit = ucRecvNowBit;
  232.             ucRecvNowBit = HAL_GPIO_ReadPin(COM_RX_GPIO_Port, COM_RX_Pin);

  233.             if(0 == ucAcquCx)
  234.             {
  235.                 ucAcquCx = 1;
  236.                 return;
  237.             }

  238.             if(ucRecvNowBit == ucRecvLastBit)
  239.             {
  240.                 ucRecvTrueCx ++;
  241.             }


  242.             if(++ ucAcquCx >= 6)
  243.             {
  244.                 ucAcquCx = 0;

  245.                 if(ucRecvTrueCx >= 2)
  246.                 {
  247.                     if(ucRecvLastBit == 1)
  248.                     {
  249.                         ucRecvData |= (1 << ucRecvBitCx);
  250.                     }
  251.                     else
  252.                     {
  253.                         ucRecvData &= ~(1 << ucRecvBitCx);
  254.                     }

  255.                     if(ucRecvBitCx >= 7)
  256.                     {
  257.                         ucRecvBitCx = 0;

  258.                         if(gsUARTBuff.CheckType == NONE)
  259.                         {
  260.                             gsUARTBuff.UartStat = COM_STOP_BIT_DEAL;
  261.                         }
  262.                         else
  263.                         {
  264.                             gsUARTBuff.UartStat = COM_CHECK_BIT_DEAL;
  265.                         }
  266.                     }
  267.                     else
  268.                     {
  269.                         ucRecvBitCx ++;
  270.                     }
  271.                 }
  272.                 else
  273.                 {
  274.                     ucAcquCx = 0;
  275.                     ucRecvTrueCx = 0;
  276.                     gsUARTBuff.UartStat = COM_STOP_BIT_DEAL;
  277.                 }
  278.             }
  279.         }
  280.         break;

  281.         case COM_CHECK_BIT_DEAL :                         //校验位
  282.         {

  283.         }
  284.         break;

  285.         case COM_STOP_BIT_DEAL :                         //停止位
  286.         {

  287.             ucRecvLastBit = ucRecvNowBit;
  288.             ucRecvNowBit = HAL_GPIO_ReadPin(COM_RX_GPIO_Port, COM_RX_Pin);

  289.             if(0 == ucAcquCx)
  290.             {
  291.                 ucAcquCx = 1;
  292.                 return;
  293.             }

  294.             if(ucRecvNowBit == ucRecvLastBit && ucRecvNowBit == 1)
  295.             {
  296.                 ucRecvTrueCx ++;
  297.             }


  298.             if( ++ ucAcquCx >= 6)
  299.             {
  300.                 ucAcquCx = 0;

  301.                 if(ucRecvTrueCx >= 2)
  302.                 {
  303.                                       ucRecvTrueCx = 0;
  304.                     if(gsUARTBuff.Recvlength < gsUARTBuff.UartMaxLength)
  305.                     {
  306.                         gsUARTBuff.UART_Recv_buf[gsUARTBuff.Recvlength] = ucRecvData;
  307.                         gsUARTBuff.Recvlength ++;
  308.                     }

  309.                     gsUARTBuff.UartStat = COM_NONE_BIT_DEAL;
  310.                     HAL_TIM_Base_Stop_IT(&htim6);
  311.                     TIM6->CNT = 0;
  312.                 }
  313.                 else
  314.                 {
  315.                     ucRecvTrueCx = 0;
  316.                 }
  317.             }
  318.         }
  319.         break;

  320.         default:
  321.             break;

  322.         }
  323.     }

  324. }
复制代码

在使用中已经达到要求,无错误识别,但是由于在电路中使用了光耦,所以波形存在一定形变,不能及时将电平立即拉低,同时中断次数还是比较频繁的,为减少误判和减少中断次数,采取另一种方式。

4、 最优实现

1252227-20190505232613207-706980555.png

减少中断次数,每位只需中断三次,同时为避免光耦导致的滞后,将前面部分过滤不采集,只在中间快速采集三次,动态实现定时器定时时间调整。
比如在4800波特率下,一位208us,则定时器从起始位开始,定时序列为80,20,20,168,20,20,168,20,20,168......这样的序列。168为前一位的最后定时时间加当前位的前80us时间,也就是上图的起始位的4位置到第0位的2位置的时间。
  1. #include "UartSet.h"
  2. #include "string.h"

  3. typedef enum
  4. {
  5.     TimeRecvStartStep1 = 0,
  6.     TimeRecvStep2 = 1,
  7.     TimeRecvStep3 = 2,
  8.     TimeRecvStep1 = 3,
  9.     TimeSendStep = 4,

  10. } TimeStep;

  11. uint16_t TimeSet[5];

  12. uint16_t TimeSetBuff[][5] = {{1199, 59, 59, 2378, 2498 }, //1200
  13.     {539, 59, 59, 1128, 1247 },  //2400
  14.     {239, 59, 59, 503, 624  },   //4800
  15.     {149, 29, 29, 251, 311  },   //9600
  16.     {0, 0, 0, 0, 0    },         //预留
  17. };

  18. UART gsUARTBuff;                //定义串口

  19. uint8_t ucRecvData = 0;         //每次接收的字节
  20. uint8_t ucAcquCx = 0;           //三次滤波计数
  21. uint8_t ucRecvBitCx = 0;        //接收位计数
  22. uint8_t ucSendBitCx = 0;        //发送位计数
  23. uint8_t ucSendLengCx = 0;       //发送长度计数

  24. uint8_t ucRecvBitBuff = 0;       //采集位保存


  25. TIM_HandleTypeDef htim6;

  26. void MX_TIM6_Init(void)
  27. {

  28.     __HAL_RCC_TIM6_CLK_ENABLE();

  29.     htim6.Instance = TIM6;
  30.     htim6.Init.Prescaler = 15;
  31.     htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  32.     htim6.Init.Period = TimeSet[TimeRecvStartStep1];
  33.     htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

  34.     HAL_NVIC_SetPriority(TIM6_IRQn, 0, 0);
  35.     HAL_NVIC_EnableIRQ(TIM6_IRQn);

  36.     HAL_TIM_Base_Init(&htim6);

  37. }


  38. void UART_GPIO_Init(void)
  39. {
  40.     GPIO_InitTypeDef GPIO_InitStruct = {0};

  41.     __HAL_RCC_GPIOB_CLK_ENABLE();

  42.     GPIO_InitStruct.Pin = COM_TX_Pin;
  43.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  44.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  45.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  46.     HAL_GPIO_Init(COM_TX_GPIO_Port, &GPIO_InitStruct);

  47.     GPIO_InitStruct.Pin = COM_RX_Pin;
  48.     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  49.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  50.     HAL_GPIO_Init(COM_RX_GPIO_Port, &GPIO_InitStruct);

  51.     HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0);
  52.     HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

  53. }


  54. /*******************************************************************************
  55. * @FunctionName   : UART_Init.
  56. * @Description    : 模拟串口结构体初始化.
  57. * @Input          : None.
  58. * @Output         : None.
  59. * @Return         : None.
  60. * @Author&Data    : MrShuCai  2019.4.11.
  61. *******************************************************************************/
  62. void UART_Init(void)
  63. {
  64.     gsUARTBuff.CheckType = NONE;
  65.     gsUARTBuff.UartMaxLength = Uartlength;
  66.     gsUARTBuff.UartStat = COM_NONE_BIT_DEAL;
  67.     UART_GPIO_Init();

  68.     if(BaudRate == 1200)
  69.     {
  70.         memcpy(TimeSet, &TimeSetBuff[0][0], sizeof(TimeSet));
  71.     }
  72.     else if(BaudRate == 2400)
  73.     {
  74.         memcpy(TimeSet, &TimeSetBuff[1][0], sizeof(TimeSet));
  75.     }
  76.     else if(BaudRate == 4800)
  77.     {
  78.         memcpy(TimeSet, &TimeSetBuff[2][0], sizeof(TimeSet));
  79.     }
  80.     else if(BaudRate == 9600)
  81.     {
  82.         memcpy(TimeSet, &TimeSetBuff[3][0], sizeof(TimeSet));
  83.     }
  84.     else
  85.     {

  86.     }

  87.     MX_TIM6_Init();
  88. }


  89. /*******************************************************************************
  90. * @FunctionName   : UART_Send_Data.
  91. * @Description    : 模拟串口发送数据接口.
  92. * @Input          : None.
  93. * @Output         : None.
  94. * @Return         : None.
  95. * @Author&Data    : MrShuCai  2019.4.11.
  96. *******************************************************************************/
  97. void UART_Send_Data(uint8_t * data, uint8_t size)
  98. {
  99.     gsUARTBuff.Sendlength = size;
  100.     memcpy(gsUARTBuff.UART_Send_buf, data, size);
  101.    
  102.       if(gsUARTBuff.UartStat == COM_NONE_BIT_DEAL)
  103.         {
  104.             gsUARTBuff.TxEn = 1;
  105.             gsUARTBuff.RxEn = 0;
  106.             gsUARTBuff.UartStat = COM_START_BIT_DEAL;

  107.             TIM6->ARR = TimeSet[TimeSendStep];
  108.             TIM6->EGR = TIM_EGR_UG;

  109.             HAL_TIM_Base_Start_IT(&htim6);
  110.         }
  111.       
  112. }


  113. /*******************************************************************************
  114. * @FunctionName   : EXTI4_15_IRQHandler.
  115. * @Description    : 接收引脚外部中断,下降沿触发,触发后即进入起始位判断.
  116. * @Input          : None.
  117. * @Output         : None.
  118. * @Return         : None.
  119. * @Author&Data    : MrShuCai  2019.4.11.
  120. *******************************************************************************/
  121. void EXTI4_15_IRQHandler(void)
  122. {
  123.     if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_4) != RESET)
  124.     {
  125.         __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_4);

  126.         if(gsUARTBuff.UartStat == COM_NONE_BIT_DEAL)
  127.         {
  128.             gsUARTBuff.RxEn = 1;
  129.             ucRecvData = 0;
  130.             gsUARTBuff.UartStat = COM_START_BIT_DEAL;

  131.             TIM6->ARR = TimeSet[TimeRecvStartStep1];
  132.             TIM6->EGR = TIM_EGR_UG;                  //初始化定时器
  133.                       EXTI->IMR &= ~(0x10);
  134.                       EXTI->EMR &= ~(0x10);
  135.             HAL_TIM_Base_Start_IT(&htim6);
  136.         }
  137.     }

  138. }


  139. /*******************************************************************************
  140. * @FunctionName   : BitValueChk.
  141. * @Description    : 判断采集bit值,三次中为1的次数大于等于2则值为1否则为0.
  142. * @Input          : n 采集记录的位值.
  143. * @Output         : BitValue.
  144. * @Return         : BitValue.
  145. * @Author&Data    : MrShuCai  2019.5.1.
  146. *******************************************************************************/
  147. uint8_t BitValueChk(uint8_t n)
  148. {
  149.     uint8_t BitValCx = 0;

  150.     for(BitValCx = 0; n; n >>= 1)
  151.     {
  152.         BitValCx += n & 0x01;
  153.     }

  154.     return (BitValCx < 2) ? (0) : (1);

  155. }

  156. /*******************************************************************************
  157. * @FunctionName   : TIM6_IRQHandler.
  158. * @Description    : 中断处理函数,包括发送和接收两部分.
  159. * @Input          : None.
  160. * @Output         : None.
  161. * @Return         : None.
  162. * @Author&Data    : MrShuCai  2019.4.11.
  163. *******************************************************************************/
  164. void TIM6_IRQHandler(void)
  165. {

  166.     HAL_TIM_IRQHandler(&htim6);

  167.     if(gsUARTBuff.TxEn == 1)         /*数据发送,发送优先,无发送后才进入接收状态*/
  168.     {
  169.         switch(gsUARTBuff.UartStat)  /*串口发送位状态判断*/
  170.         {
  171.         case COM_START_BIT_DEAL :
  172.         {
  173.             HAL_GPIO_WritePin(COM_TX_GPIO_Port, COM_TX_Pin, GPIO_PIN_RESET);
  174.             gsUARTBuff.UartStat = COM_DATA_BIT_DEAL;
  175.             ucSendBitCx = 0;
  176.         }
  177.         break;

  178.         case COM_DATA_BIT_DEAL :
  179.         {
  180.             HAL_GPIO_WritePin(COM_TX_GPIO_Port, COM_TX_Pin, (GPIO_PinState)((gsUARTBuff.UART_Send_buf[ucSendLengCx] >> ucSendBitCx) & 0x01));

  181.             ucSendBitCx ++;

  182.             if(ucSendBitCx >= 8)
  183.             {
  184.                 if(gsUARTBuff.CheckType == NONE)
  185.                 {
  186.                     gsUARTBuff.UartStat = COM_STOP_BIT_DEAL;
  187.                 }
  188.                 else
  189.                 {
  190.                     gsUARTBuff.UartStat = COM_CHECK_BIT_DEAL;
  191.                 }
  192.             }

  193.         }
  194.         break;

  195.         case COM_CHECK_BIT_DEAL :
  196.         {

  197.         }
  198.         break;

  199.         case COM_STOP_BIT_DEAL :
  200.         {
  201.             HAL_GPIO_WritePin(COM_TX_GPIO_Port, COM_TX_Pin, GPIO_PIN_SET);

  202.             ucSendBitCx = 0;

  203.             if(ucSendLengCx < gsUARTBuff.Sendlength - 1)
  204.             {
  205.                 gsUARTBuff.UartStat = COM_START_BIT_DEAL;
  206.                 ucSendLengCx ++;
  207.             }
  208.             else
  209.             {
  210.                 ucSendLengCx = 0;
  211.                 gsUARTBuff.UartStat = COM_NONE_BIT_DEAL;
  212.                 gsUARTBuff.TxEn = 0;
  213.                 gsUARTBuff.RxEn = 1;
  214.                 HAL_TIM_Base_Stop_IT(&htim6);
  215.                               EXTI->IMR |= 0x10;
  216.                           EXTI->EMR |= 0x10;
  217.                 TIM6 ->CNT = 0;
  218.             }

  219.         }
  220.         break;

  221.         default:
  222.             break;

  223.         }
  224.     }


  225.     if(gsUARTBuff.RxEn == 1)
  226.     {
  227.         switch(gsUARTBuff.UartStat)
  228.         {
  229.                     case COM_START_BIT_DEAL :
  230.                     {
  231.                             ucRecvBitBuff = (ucRecvBitBuff << 1) | (HAL_GPIO_ReadPin(COM_RX_GPIO_Port, COM_RX_Pin) & 0x01);

  232.                             if(++ ucAcquCx >= 3)
  233.                             {
  234.                                     if(BitValueChk(ucRecvBitBuff) == 0)                        
  235.                                     {
  236.                                             gsUARTBuff.UartStat = COM_DATA_BIT_DEAL;
  237.                                             TIM6->ARR = TimeSet[ucAcquCx];
  238.                                     }
  239.                                     else
  240.                                     {
  241.                                             gsUARTBuff.UartStat = COM_STOP_BIT_DEAL;
  242.                                     }
  243.                                     
  244.                                     ucRecvBitBuff = 0;
  245.                                     ucAcquCx = 0;
  246.                             }
  247.                             else
  248.                             {
  249.                                     TIM6->ARR = TimeSet[ucAcquCx];
  250.                             }


  251.                     }
  252.                     break;

  253.                     case COM_DATA_BIT_DEAL :                         //数据位
  254.                     {

  255.                             ucRecvBitBuff = (ucRecvBitBuff << 1) | (HAL_GPIO_ReadPin(COM_RX_GPIO_Port, COM_RX_Pin) & 0x01);

  256.                             if(++ ucAcquCx >= 3)
  257.                             {
  258.                                     ucRecvData |= (BitValueChk(ucRecvBitBuff) & 0x01) << ucRecvBitCx;

  259.                                     if(ucRecvBitCx >= 7)
  260.                                     {
  261.                                             ucRecvBitCx = 0;

  262.                                             if(gsUARTBuff.CheckType == NONE)
  263.                                             {
  264.                                                     gsUARTBuff.UartStat = COM_STOP_BIT_DEAL;
  265.                                             }
  266.                                             else
  267.                                             {
  268.                                                     gsUARTBuff.UartStat = COM_CHECK_BIT_DEAL;
  269.                                             }
  270.                                     }
  271.                                     else
  272.                                     {
  273.                                             ucRecvBitCx ++;
  274.                                     }

  275.                                     TIM6->ARR = TimeSet[ucAcquCx];

  276.                                     ucAcquCx = 0;
  277.                                     ucRecvBitBuff = 0;
  278.                             }
  279.                             else
  280.                             {
  281.                                     TIM6->ARR = TimeSet[ucAcquCx];
  282.                             }
  283.                     }
  284.                     break;

  285.                     case COM_CHECK_BIT_DEAL :                         //校验位
  286.                     {

  287.                     }
  288.                     break;

  289.                     case COM_STOP_BIT_DEAL :                         //停止位
  290.                     {

  291.                         ucRecvBitBuff = (ucRecvBitBuff << 1) | (HAL_GPIO_ReadPin(COM_RX_GPIO_Port, COM_RX_Pin) & 0x01);

  292.                             if( ++ ucAcquCx >= 3)
  293.                             {
  294.                                     if(BitValueChk(ucRecvBitBuff) == 1)   
  295.                                     {
  296.                                             if(gsUARTBuff.Recvlength < gsUARTBuff.UartMaxLength)
  297.                                             {
  298.                                                     gsUARTBuff.UART_Recv_buf[gsUARTBuff.Recvlength] = ucRecvData;
  299.                                                     gsUARTBuff.Recvlength ++;
  300.                                             }

  301.                                             gsUARTBuff.UartStat = COM_NONE_BIT_DEAL;
  302.                                             HAL_TIM_Base_Stop_IT(&htim6);
  303.                                             
  304.                                             EXTI->IMR |= 0x10;
  305.                                             EXTI->EMR |= 0x10;
  306.                                             TIM6->CNT = 0;
  307.                                     }
  308.                                     else
  309.                                     {
  310.                                             ucAcquCx = 0;
  311.                                     }
  312.                                     
  313.                                     ucRecvBitBuff = 0;
  314.                                     ucAcquCx = 0;
  315.                             }
  316.                             else
  317.                             {
  318.                                     TIM6->ARR = TimeSet[ucAcquCx];
  319.                             }
  320.                     }
  321.                     break;

  322.                     default:
  323.                             break;
  324.                     }
  325.     }
  326. }
复制代码


发送就以波特率时间定时发送位状态即可,实现简单。
仅供初学或者有兴趣的朋友参考,第一种方法由于后来放弃,只是大致实现,推荐第二种,欢迎大家指点。

收藏 评论0 发布时间:2022-1-13 21:00

举报

0个回答

所属标签

相似分享

官网相关资源

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