×
嵌入式 > 技术百科 > 详情

STM8S 串口应用 UART2 STM8S105

发布时间:2020-06-03 发布时间:
|
  1. //少说话,多做事,以下是我验证过没有问题的串口发送接受数据  

  2. //使用MCU stm8s105c6  UART2  

  3.   

  4. //初始化时调用:  

  5.   GPIO_DeInit(GPIOD);  

  6.   /* Configure PD5/6  */  

  7.   GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_IN_PU_NO_IT);//发送数据IO  

  8.   GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);//接受数据IO  

  9.   UART2_DeInit();  

  10.  UART2_Init(2400,UART2_WORDLENGTH_8D,UART2_STOPBITS_1,UART2_PARITY_NO,  

  11.             UART2_SYNCMODE_CLOCK_DISABLE,  

  12.       UART2_MODE_TX_ENABLE|UART2_MODE_RX_ENABLE);  //波特率 2400 8位数据    

  13.   

  14. //1个停止位  没有奇偶校验 关闭SCK 允许串口接受和发送  

  15.   UART2_Cmd(ENABLE);//启用串口  

  16.   UART2_ITConfig(UART2_IT_RXNE_OR,ENABLE);//允许接受中断  

  17.   

  18.   

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

  20.   

  21.   

  22.   

  23.                 if(UART2_GetFlagStatus(UART2_FLAG_TC))  

  24.                  {//当前没有在发数据,可以发数据  

  25.                     UART2_SendData8(Uart2TexData);   

  26.                      UART2_ClearFlag(UART2_FLAG_TC);  

  27.                  }  

  28.   

  29.   

  30.     UART2_ClearITPendingBit(UART2_FLAG_RXNE);//清中断标志位  

  31.     Uart2RecData = UART2_ReceiveData8();//接受中断数据//后面两句需要发在  

  32.   

  33. 串口接受中断中  

  34.   

  35.   

  36. void UART2_DeInit(void)  

  37. {  

  38.     u8 dummy = 0;  

  39.     /*

  40.        to the UART2_SR register followed by a Read to the UART2_DR  

  41.  

  42. register */  

  43.     dummy = UART2->SR;  

  44.     dummy = UART2->DR;  

  45.   

  46.     UART2->BRR2 = UART2_BRR2_RESET_VALUE;  /*

  47.  

  48. value 0x00 */  

  49.     UART2->BRR1 = UART2_BRR1_RESET_VALUE;  /*

  50.  

  51. value 0x00 */  

  52.   

  53.     UART2->CR1 = UART2_CR1_RESET_VALUE; /*

  54.  

  55. 0x00  */  

  56.     UART2->CR2 = UART2_CR2_RESET_VALUE; /*

  57.  

  58. 0x00  */  

  59.     UART2->CR3 = UART2_CR3_RESET_VALUE;  /*

  60.  

  61. 0x00  */  

  62.     UART2->CR4 = UART2_CR4_RESET_VALUE;  /*

  63.  

  64. 0x00  */  

  65.     UART2->CR5 = UART2_CR5_RESET_VALUE; /*

  66.  

  67. 0x00  */  

  68.     UART2->CR6 = UART2_CR6_RESET_VALUE; /*

  69.  

  70. 0x00  */  

  71.   

  72. }  

  73. void UART2_Init(u32 BaudRate, UART2_WordLength_TypeDef WordLength,   

  74.   

  75. UART2_StopBits_TypeDef StopBits, UART2_Parity_TypeDef Parity,   

  76.   

  77. UART2_SyncMode_TypeDef SyncMode, UART2_Mode_TypeDef Mode)  

  78. {  

  79.     u8 BRR2_1, BRR2_2 = 0;  

  80.     u32 BaudRate_Mantissa, BaudRate_Mantissa100 = 0;  

  81.   

  82.     /* assert_param: BaudRate value should be <= 625000 bps */  

  83.     assert_param(IS_UART2_BAUDRATE_OK(BaudRate));  

  84.   

  85.     assert_param(IS_UART2_WORDLENGTH_OK(WordLength));  

  86.   

  87.     assert_param(IS_UART2_STOPBITS_OK(StopBits));  

  88.   

  89.     assert_param(IS_UART2_PARITY_OK(Parity));  

  90.   

  91.     /* assert_param: UART2_Mode value should exclude values such as   

  92.  

  93. UART2_ModeTx_Enable|UART2_ModeTx_Disable */  

  94.     assert_param(IS_UART2_MODE_OK((u8)Mode));  

  95.   

  96.     /* assert_param: UART2_SyncMode value should exclude values such as 

  97.        UART2_CLOCK_ENABLE|UART2_CLOCK_DISABLE */  

  98.     assert_param(IS_UART2_SYNCMODE_OK((u8)SyncMode));  

  99.   

  100.     UART2->CR1 &= (u8)(~UART2_CR1_M);  /**

  101.     UART2->CR1 |= (u8)WordLength; /**

  102.  

  103. to UART2_WordLength value */  

  104.   

  105.     UART2->CR3 &= (u8)(~UART2_CR3_STOP);  /**

  106.     UART2->CR3 |= (u8)StopBits;  /**

  107.  

  108. to UART2_StopBits value  */  

  109.   

  110.     UART2->CR1 &= (u8)(~(UART2_CR1_PCEN | UART2_CR1_PS  ));  /**

  111.  

  112. the Parity Control bit */  

  113.     UART2->CR1 |= (u8)Parity;  /**

  114.  

  115. UART2_Parity value */  

  116.   

  117.     UART2->BRR1 &= (u8)(~UART2_BRR1_DIVM);  /**

  118.  

  119. of UARTDIV  */  

  120.     UART2->BRR2 &= (u8)(~UART2_BRR2_DIVM);  /**

  121.  

  122. of UARTDIV  */  

  123.     UART2->BRR2 &= (u8)(~UART2_BRR2_DIVF);  /**

  124.  

  125. of UARTDIV */  

  126.   

  127.     /**

  128.  

  129. UART2_BaudRate value */  

  130.     BaudRate_Mantissa    = ((u32)CLK_GetClockFreq() / (BaudRate <

  131.     BaudRate_Mantissa100 = (((u32)CLK_GetClockFreq() * 100) / (BaudRate   

  132.   

  133. <

  134.     /**

  135.  

  136. the BRR2 register*/  

  137.     BRR2_1 = (u8)((u8)(((BaudRate_Mantissa100 - (BaudRate_Mantissa *   

  138.   

  139. 100))  

  140.                         <

  141.  

  142. of UARTDIV  */  

  143.     BRR2_2 = (u8)((BaudRate_Mantissa >> 4) & (u8)0xF0);  

  144.   

  145.     UART2->BRR2 = (u8)(BRR2_1 | BRR2_2);  

  146.     UART2->BRR1 = (u8)BaudRate_Mantissa;           /**

  147.  

  148. mantissa of UARTDIV  */  

  149.   

  150.     UART2->CR2 &= (u8)~(UART2_CR2_TEN | UART2_CR2_REN); /**

  151.  

  152. Transmitter and Receiver before seting the LBCL, CPOL and CPHA bits */  

  153.     UART2->CR3 &= (u8)~(UART2_CR3_CPOL | UART2_CR3_CPHA |   

  154.   

  155. UART2_CR3_LBCL); /**

  156.  

  157. Clock pulse */  

  158.     UART2->CR3 |= (u8)((u8)SyncMode & (u8)(UART2_CR3_CPOL |   

  159.   

  160. UART2_CR3_CPHA | UART2_CR3_LBCL));  /**

  161.  

  162. Phase, Last Bit Clock pulse */  

  163.   

  164.     if ((u8)Mode & (u8)UART2_MODE_TX_ENABLE)  

  165.     {  

  166.         UART2->CR2 |= (u8)UART2_CR2_TEN;  /**

  167.  

  168. bit */  

  169.     }  

  170.     else  

  171.     {  

  172.         UART2->CR2 &= (u8)(~UART2_CR2_TEN);  /**

  173.  

  174. Disable bit */  

  175.     }  

  176.     if ((u8)Mode & (u8)UART2_MODE_RX_ENABLE)  

  177.     {  

  178.         UART2->CR2 |= (u8)UART2_CR2_REN;  /**

  179.  

  180. bit */  

  181.     }  

  182.     else  

  183.     {  

  184.         UART2->CR2 &= (u8)(~UART2_CR2_REN);  /**

  185.  

  186. Disable bit */  

  187.     }  

  188.     /**

  189.  

  190. Clock pulse bits according to UART2_Mode value */  

  191.     if ((u8)SyncMode&(u8)UART2_SYNCMODE_CLOCK_DISABLE)  

  192.     {  

  193.         UART2->CR3 &= (u8)(~UART2_CR3_CKEN); /**

  194.  

  195. bit */  

  196.         /**

  197.  

  198. setting the correct I/O Port register according the product package and  

  199.  

  200. line configuration*/  

  201.     }  

  202.     else  

  203.     {  

  204.         UART2->CR3 |= (u8)((u8)SyncMode & UART2_CR3_CKEN);  

  205.     }  

  206. }  

  207.   

  208. void UART2_Cmd(FunctionalState NewState)  

  209. {  

  210.   

  211.     if (NewState != DISABLE)  

  212.     {  

  213.         UART2->CR1 &= (u8)(~UART2_CR1_UARTD); /**

  214.     }  

  215.     else  

  216.     {  

  217.         UART2->CR1 |= UART2_CR1_UARTD;  /**

  218.  

  219. consumption) */  

  220.     }  

  221. }  

  222.   

  223. void UART2_ITConfig(UART2_IT_TypeDef UART2_IT, FunctionalState NewState)  

  224. {  

  225.     u8 uartreg, itpos = 0x00;  

  226.     assert_param(IS_UART2_CONFIG_IT_OK(UART2_IT));  

  227.     assert_param(IS_FUNCTIONALSTATE_OK(NewState));  

  228.   

  229.     /* Get the UART2 register index */  

  230.     uartreg = (u8)(UART2_IT >> 0x08);  

  231.   

  232.     /* Get the UART2 IT index */  

  233.     itpos = (u8)((u8)1 <

  234.   

  235.     if (NewState != DISABLE)  

  236.     {  

  237.         /**

  238.         if (uartreg == 0x01)  

  239.         {  

  240.             UART2->CR1 |= itpos;  

  241.         }  

  242.         else if (uartreg == 0x02)  

  243.         {  

  244.             UART2->CR2 |= itpos;  

  245.         }  

  246.         else if (uartreg == 0x03)  

  247.         {  

  248.             UART2->CR4 |= itpos;  

  249.         }  

  250.         else  

  251.         {  

  252.             UART2->CR6 |= itpos;  

  253.         }  

  254.     }  

  255.     else  

  256.     {  

  257.         /**

  258.         if (uartreg == 0x01)  

  259.         {  

  260.             UART2->CR1 &= (u8)(~itpos);  

  261.         }  

  262.         else if (uartreg == 0x02)  

  263.         {  

  264.             UART2->CR2 &= (u8)(~itpos);  

  265.         }  

  266.         else if (uartreg == 0x03)  

  267.         {  

  268.             UART2->CR4 &= (u8)(~itpos);  

  269.         }  

  270.         else  

  271.         {  

  272.             UART2->CR6 &= (u8)(~itpos);  

  273.         }  

  274.     }  

  275. }  

  276.   

  277. u8 UART2_ReceiveData8(void)  

  278. {  

  279.     return ((u8)UART2->DR);  

  280. }  

  281.   

  282. void UART2_SendData8(u8 Data)  

  283. {  

  284.     /* Transmit Data */  

  285.     UART2->DR = Data;  

  286. }  

  287.   

  288. FlagStatus UART2_GetFlagStatus(UART2_Flag_TypeDef UART2_FLAG)  

  289. {  

  290.     FlagStatus status = RESET;  

  291.   

  292.     /* Check parameters */  

  293.     assert_param(IS_UART2_FLAG_OK(UART2_FLAG));  

  294.   

  295.     /* Check the status of the specified UART2 flag*/  

  296.     if (UART2_FLAG == UART2_FLAG_LBDF)  

  297.     {  

  298.         if ((UART2->CR4 & (u8)UART2_FLAG) != (u8)0x00)  

  299.         {  

  300.             /* UART2_FLAG is set*/  

  301.             status = SET;  

  302.         }  

  303.         else  

  304.         {  

  305.             /* UART2_FLAG is reset*/  

  306.             status = RESET;  

  307.         }  

  308.     }  

  309.     else if (UART2_FLAG == UART2_FLAG_SBK)  

  310.     {  

  311.         if ((UART2->CR2 & (u8)UART2_FLAG) != (u8)0x00)  

  312.         {  

  313.             /* UART2_FLAG is set*/  

  314.             status = SET;  

  315.         }  

  316.         else  

  317.         {  

  318.             /* UART2_FLAG is reset*/  

  319.             status = RESET;  

  320.         }  

  321.     }  

  322.     else if ((UART2_FLAG == UART2_FLAG_LHDF) || (UART2_FLAG ==   

  323.   

  324. UART2_FLAG_LSF))  

  325.     {  

  326.         if ((UART2->CR6 & (u8)UART2_FLAG) != (u8)0x00)  

  327.         {  

  328.             /* UART2_FLAG is set*/  

  329.             status = SET;  

  330.         }  

  331.         else  

  332.         {  

  333.             /* UART2_FLAG is reset*/  

  334.             status = RESET;  

  335.         }  

  336.     }  

  337.     else  

  338.     {  

  339.         if ((UART2->SR & (u8)UART2_FLAG) != (u8)0x00)  

  340.         {  

  341.             /* UART2_FLAG is set*/  

  342.             status = SET;  

  343.         }  

  344.         else  

  345.         {  

  346.             /* UART2_FLAG is reset*/  

  347.             status = RESET;  

  348.         }  

  349.     }  

  350.   

  351.     /* Return the UART2_FLAG status*/  

  352.     return  status;  

  353. }  

  354.   

  355. void UART2_ClearFlag(UART2_Flag_TypeDef UART2_FLAG)  

  356. {  

  357.     assert_param(IS_UART2_CLEAR_FLAG_OK(UART2_FLAG));  

  358.   

  359.     /*

  360.     if (UART2_FLAG == UART2_FLAG_RXNE)  

  361.     {  

  362.         UART2->SR = (u8)~(UART2_SR_RXNE);  

  363.     }  

  364.     /*

  365.     else if (UART2_FLAG == UART2_FLAG_LBDF)  

  366.     {  

  367.         UART2->CR4 &= (u8)(~UART2_CR4_LBDF);  

  368.     }  

  369.     /*

  370.     else if (UART2_FLAG == UART2_FLAG_LHDF)  

  371.     {  

  372.         UART2->CR6 &= (u8)(~UART2_CR6_LHDF);  

  373.     }  

  374.     /*

  375.     else  

  376.     {  

  377.         UART2->CR6 &= (u8)(~UART2_CR6_LSF);  

  378.     }  

  379.   

  380. }  


 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
无人机新突破:或将利用手机发射塔追踪无人机