[应用相关] STM8S 串口应用 UART2 STM8S105

[复制链接]
13538|1
 楼主| grfqq325 发表于 2021-6-22 23:29 | 显示全部楼层 |阅读模式

  1. //使用MCU stm8s105c6  UART2

  2. //初始化时调用:
  3.   GPIO_DeInit(GPIOD);
  4.   /* Configure PD5/6  */
  5.   GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_IN_PU_NO_IT);//发送数据IO
  6.   GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);//接受数据IO
  7.   UART2_DeInit();
  8. UART2_Init(2400,UART2_WORDLENGTH_8D,UART2_STOPBITS_1,UART2_PARITY_NO,\
  9.             UART2_SYNCMODE_CLOCK_DISABLE,\
  10.       UART2_MODE_TX_ENABLE|UART2_MODE_RX_ENABLE);  //波特率 2400 8位数据  

  11. //1个停止位  没有奇偶校验 关闭SCK 允许串口接受和发送
  12.   UART2_Cmd(ENABLE);//启用串口
  13.   UART2_ITConfig(UART2_IT_RXNE_OR,ENABLE);//允许接受中断


  14. //操作串口(发送接受数据)时调用:



  15.                 if(UART2_GetFlagStatus(UART2_FLAG_TC))
  16.                  {//当前没有在发数据,可以发数据
  17.                     UART2_SendData8(Uart2TexData);
  18.                      UART2_ClearFlag(UART2_FLAG_TC);
  19.                  }


  20.     UART2_ClearITPendingBit(UART2_FLAG_RXNE);//清中断标志位
  21.     Uart2RecData = UART2_ReceiveData8();//接受中断数据//后面两句需要发在

  22. 串口接受中断中



 楼主| grfqq325 发表于 2021-6-22 23:30 | 显示全部楼层

  1. void UART2_DeInit(void)
  2. {
  3.     u8 dummy = 0;
  4.     /*< Clear the Idle Line Detected bit in the status rerister by a read
  5.        to the UART2_SR register followed by a Read to the UART2_DR
  6. register */
  7.     dummy = UART2->SR;
  8.     dummy = UART2->DR;

  9.     UART2->BRR2 = UART2_BRR2_RESET_VALUE;  /*< Set UART2_BRR2 to reset
  10. value 0x00 */
  11.     UART2->BRR1 = UART2_BRR1_RESET_VALUE;  /*< Set UART2_BRR1 to reset
  12. value 0x00 */

  13.     UART2->CR1 = UART2_CR1_RESET_VALUE; /*< Set UART2_CR1 to reset value
  14. 0x00  */
  15.     UART2->CR2 = UART2_CR2_RESET_VALUE; /*< Set UART2_CR2 to reset value
  16. 0x00  */
  17.     UART2->CR3 = UART2_CR3_RESET_VALUE;  /*< Set UART2_CR3 to reset value
  18. 0x00  */
  19.     UART2->CR4 = UART2_CR4_RESET_VALUE;  /*< Set UART2_CR4 to reset value
  20. 0x00  */
  21.     UART2->CR5 = UART2_CR5_RESET_VALUE; /*< Set UART2_CR5 to reset value
  22. 0x00  */
  23.     UART2->CR6 = UART2_CR6_RESET_VALUE; /*< Set UART2_CR6 to reset value
  24. 0x00  */

  25. }
  26. void UART2_Init(u32 BaudRate, UART2_WordLength_TypeDef WordLength,
  27. UART2_StopBits_TypeDef StopBits, UART2_Parity_TypeDef Parity,
  28. UART2_SyncMode_TypeDef SyncMode, UART2_Mode_TypeDef Mode)
  29. {
  30.     u8 BRR2_1, BRR2_2 = 0;
  31.     u32 BaudRate_Mantissa, BaudRate_Mantissa100 = 0;

  32.     /* assert_param: BaudRate value should be <= 625000 bps */
  33.     assert_param(IS_UART2_BAUDRATE_OK(BaudRate));

  34.     assert_param(IS_UART2_WORDLENGTH_OK(WordLength));

  35.     assert_param(IS_UART2_STOPBITS_OK(StopBits));

  36.     assert_param(IS_UART2_PARITY_OK(Parity));

  37.     /* assert_param: UART2_Mode value should exclude values such as  
  38. UART2_ModeTx_Enable|UART2_ModeTx_Disable */
  39.     assert_param(IS_UART2_MODE_OK((u8)Mode));

  40.     /* assert_param: UART2_SyncMode value should exclude values such as
  41.        UART2_CLOCK_ENABLE|UART2_CLOCK_DISABLE */
  42.     assert_param(IS_UART2_SYNCMODE_OK((u8)SyncMode));

  43.     UART2->CR1 &= (u8)(~UART2_CR1_M);  /**< Clear the word length bit */
  44.     UART2->CR1 |= (u8)WordLength; /**< Set the word length bit according
  45. to UART2_WordLength value */

  46.     UART2->CR3 &= (u8)(~UART2_CR3_STOP);  /**< Clear the STOP bits */
  47.     UART2->CR3 |= (u8)StopBits;  /**< Set the STOP bits number according
  48. to UART2_StopBits value  */

  49.     UART2->CR1 &= (u8)(~(UART2_CR1_PCEN | UART2_CR1_PS  ));  /**< Clear
  50. the Parity Control bit */
  51.     UART2->CR1 |= (u8)Parity;  /**< Set the Parity Control bit to
  52. UART2_Parity value */

  53.     UART2->BRR1 &= (u8)(~UART2_BRR1_DIVM);  /**< Clear the LSB mantissa
  54. of UARTDIV  */
  55.     UART2->BRR2 &= (u8)(~UART2_BRR2_DIVM);  /**< Clear the MSB mantissa
  56. of UARTDIV  */
  57.     UART2->BRR2 &= (u8)(~UART2_BRR2_DIVF);  /**< Clear the Fraction bits
  58. of UARTDIV */

  59.     /**< Set the UART2 BaudRates in BRR1 and BRR2 registers according to
  60. UART2_BaudRate value */
  61.     BaudRate_Mantissa    = ((u32)CLK_GetClockFreq() / (BaudRate << 4));
  62.     BaudRate_Mantissa100 = (((u32)CLK_GetClockFreq() * 100) / (BaudRate

  63. << 4));
  64.     /**< The fraction and MSB mantissa should be loaded in one step in
  65. the BRR2 register*/
  66.     BRR2_1 = (u8)((u8)(((BaudRate_Mantissa100 - (BaudRate_Mantissa *

  67. 100))
  68.                         << 4) / 100) & (u8)0x0F); /**< Set the fraction
  69. of UARTDIV  */
  70.     BRR2_2 = (u8)((BaudRate_Mantissa >> 4) & (u8)0xF0);

  71.     UART2->BRR2 = (u8)(BRR2_1 | BRR2_2);
  72.     UART2->BRR1 = (u8)BaudRate_Mantissa;           /**< Set the LSB
  73. mantissa of UARTDIV  */

  74.     UART2->CR2 &= (u8)~(UART2_CR2_TEN | UART2_CR2_REN); /**< Disable the
  75. Transmitter and Receiver before seting the LBCL, CPOL and CPHA bits */
  76.     UART2->CR3 &= (u8)~(UART2_CR3_CPOL | UART2_CR3_CPHA |

  77. UART2_CR3_LBCL); /**< Clear the Clock Polarity, lock Phase, Last Bit
  78. Clock pulse */
  79.     UART2->CR3 |= (u8)((u8)SyncMode & (u8)(UART2_CR3_CPOL |

  80. UART2_CR3_CPHA | UART2_CR3_LBCL));  /**< Set the Clock Polarity, lock
  81. Phase, Last Bit Clock pulse */

  82.     if ((u8)Mode & (u8)UART2_MODE_TX_ENABLE)
  83.     {
  84.         UART2->CR2 |= (u8)UART2_CR2_TEN;  /**< Set the Transmitter Enable
  85. bit */
  86.     }
  87.     else
  88.     {
  89.         UART2->CR2 &= (u8)(~UART2_CR2_TEN);  /**< Clear the Transmitter
  90. Disable bit */
  91.     }
  92.     if ((u8)Mode & (u8)UART2_MODE_RX_ENABLE)
  93.     {
  94.         UART2->CR2 |= (u8)UART2_CR2_REN;  /**< Set the Receiver Enable
  95. bit */
  96.     }
  97.     else
  98.     {
  99.         UART2->CR2 &= (u8)(~UART2_CR2_REN);  /**< Clear the Receiver
  100. Disable bit */
  101.     }
  102.     /**< Set the Clock Enable bit, lock Polarity, lock Phase and Last Bit
  103. Clock pulse bits according to UART2_Mode value */
  104.     if ((u8)SyncMode&(u8)UART2_SYNCMODE_CLOCK_DISABLE)
  105.     {
  106.         UART2->CR3 &= (u8)(~UART2_CR3_CKEN); /**< Clear the Clock Enable
  107. bit */
  108.         /**< configure in Push Pull or Open Drain mode the Tx I/O line by
  109. setting the correct I/O Port register according the product package and
  110. line configuration*/
  111.     }
  112.     else
  113.     {
  114.         UART2->CR3 |= (u8)((u8)SyncMode & UART2_CR3_CKEN);
  115.     }
  116. }

  117. void UART2_Cmd(FunctionalState NewState)
  118. {

  119.     if (NewState != DISABLE)
  120.     {
  121.         UART2->CR1 &= (u8)(~UART2_CR1_UARTD); /**< UART2 Enable */
  122.     }
  123.     else
  124.     {
  125.         UART2->CR1 |= UART2_CR1_UARTD;  /**< UART2 Disable (for low power
  126. consumption) */
  127.     }
  128. }

  129. void UART2_ITConfig(UART2_IT_TypeDef UART2_IT, FunctionalState NewState)
  130. {
  131.     u8 uartreg, itpos = 0x00;
  132.     assert_param(IS_UART2_CONFIG_IT_OK(UART2_IT));
  133.     assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  134.     /* Get the UART2 register index */
  135.     uartreg = (u8)(UART2_IT >> 0x08);

  136.     /* Get the UART2 IT index */
  137.     itpos = (u8)((u8)1 << (u8)((u8)UART2_IT & (u8)0x0F));

  138.     if (NewState != DISABLE)
  139.     {
  140.         /**< Enable the Interrupt bits according to UART2_IT mask */
  141.         if (uartreg == 0x01)
  142.         {
  143.             UART2->CR1 |= itpos;
  144.         }
  145.         else if (uartreg == 0x02)
  146.         {
  147.             UART2->CR2 |= itpos;
  148.         }
  149.         else if (uartreg == 0x03)
  150.         {
  151.             UART2->CR4 |= itpos;
  152.         }
  153.         else
  154.         {
  155.             UART2->CR6 |= itpos;
  156.         }
  157.     }
  158.     else
  159.     {
  160.         /**< Disable the interrupt bits according to UART2_IT mask */
  161.         if (uartreg == 0x01)
  162.         {
  163.             UART2->CR1 &= (u8)(~itpos);
  164.         }
  165.         else if (uartreg == 0x02)
  166.         {
  167.             UART2->CR2 &= (u8)(~itpos);
  168.         }
  169.         else if (uartreg == 0x03)
  170.         {
  171.             UART2->CR4 &= (u8)(~itpos);
  172.         }
  173.         else
  174.         {
  175.             UART2->CR6 &= (u8)(~itpos);
  176.         }
  177.     }
  178. }

  179. u8 UART2_ReceiveData8(void)
  180. {
  181.     return ((u8)UART2->DR);
  182. }

  183. void UART2_SendData8(u8 Data)
  184. {
  185.     /* Transmit Data */
  186.     UART2->DR = Data;
  187. }

  188. FlagStatus UART2_GetFlagStatus(UART2_Flag_TypeDef UART2_FLAG)
  189. {
  190.     FlagStatus status = RESET;

  191.     /* Check parameters */
  192.     assert_param(IS_UART2_FLAG_OK(UART2_FLAG));

  193.     /* Check the status of the specified UART2 flag*/
  194.     if (UART2_FLAG == UART2_FLAG_LBDF)
  195.     {
  196.         if ((UART2->CR4 & (u8)UART2_FLAG) != (u8)0x00)
  197.         {
  198.             /* UART2_FLAG is set*/
  199.             status = SET;
  200.         }
  201.         else
  202.         {
  203.             /* UART2_FLAG is reset*/
  204.             status = RESET;
  205.         }
  206.     }
  207.     else if (UART2_FLAG == UART2_FLAG_SBK)
  208.     {
  209.         if ((UART2->CR2 & (u8)UART2_FLAG) != (u8)0x00)
  210.         {
  211.             /* UART2_FLAG is set*/
  212.             status = SET;
  213.         }
  214.         else
  215.         {
  216.             /* UART2_FLAG is reset*/
  217.             status = RESET;
  218.         }
  219.     }
  220.     else if ((UART2_FLAG == UART2_FLAG_LHDF) || (UART2_FLAG ==

  221. UART2_FLAG_LSF))
  222.     {
  223.         if ((UART2->CR6 & (u8)UART2_FLAG) != (u8)0x00)
  224.         {
  225.             /* UART2_FLAG is set*/
  226.             status = SET;
  227.         }
  228.         else
  229.         {
  230.             /* UART2_FLAG is reset*/
  231.             status = RESET;
  232.         }
  233.     }
  234.     else
  235.     {
  236.         if ((UART2->SR & (u8)UART2_FLAG) != (u8)0x00)
  237.         {
  238.             /* UART2_FLAG is set*/
  239.             status = SET;
  240.         }
  241.         else
  242.         {
  243.             /* UART2_FLAG is reset*/
  244.             status = RESET;
  245.         }
  246.     }

  247.     /* Return the UART2_FLAG status*/
  248.     return  status;
  249. }

  250. void UART2_ClearFlag(UART2_Flag_TypeDef UART2_FLAG)
  251. {
  252.     assert_param(IS_UART2_CLEAR_FLAG_OK(UART2_FLAG));

  253.     /*< Clear the Receive Register Not Empty flag */
  254.     if (UART2_FLAG == UART2_FLAG_RXNE)
  255.     {
  256.         UART2->SR = (u8)~(UART2_SR_RXNE);
  257.     }
  258.     /*< Clear the LIN Break Detection flag */
  259.     else if (UART2_FLAG == UART2_FLAG_LBDF)
  260.     {
  261.         UART2->CR4 &= (u8)(~UART2_CR4_LBDF);
  262.     }
  263.     /*< Clear the LIN Header Detection Flag */
  264.     else if (UART2_FLAG == UART2_FLAG_LHDF)
  265.     {
  266.         UART2->CR6 &= (u8)(~UART2_CR6_LHDF);
  267.     }
  268.     /*< Clear the LIN Synch Field flag */
  269.     else
  270.     {
  271.         UART2->CR6 &= (u8)(~UART2_CR6_LSF);
  272.     }

  273. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

65

主题

687

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部