STM8S serial port application UART2 STM8S105

Publisher:紫菜包饭Latest update time:2018-06-01 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  1. //Talk less and do more. The following is the serial port sending and receiving data that I have verified to be problem-free.  

  2. //Use MCU stm8s105c6 UART2  

  3.   

  4. //Called during initialization:  

  5.   GPIO_DeInit(GPIOD);  

  6.   /* Configure PD5/6  */  

  7.   GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_IN_PU_NO_IT); //Send data IO  

  8.   GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT); //Receive data 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); //Baud rate 2400 8-bit data    

  13.   

  14. //1 stop bit, no parity check, turn off SCK, allow serial port to receive and send  

  15.   UART2_Cmd(ENABLE); //Enable the serial port  

  16.   UART2_ITConfig(UART2_IT_RXNE_OR,ENABLE); //Enable receiving interrupt  

  17.   

  18.   

  19. //Called when operating the serial port (sending and receiving data):  

  20.   

  21.   

  22.   

  23.                 if(UART2_GetFlagStatus(UART2_FLAG_TC))  

  24.                  {//No data is being sent at the moment, data can be sent  

  25.                     UART2_SendData8(Uart2TexData);   

  26.                      UART2_ClearFlag(UART2_FLAG_TC);  

  27.                  }  

  28.   

  29.   

  30.     UART2_ClearITPendingBit(UART2_FLAG_RXNE); // Clear interrupt flag  

  31.     Uart2RecData = UART2_ReceiveData8(); //Receive interrupt data //The following two sentences need to be sent  

  32.   

  33. Serial port receiving interrupt  

  34.   

  35.   

  36. void UART2_DeInit(void)  

  37. {  

  38.     u8 dummy = 0;  

  39.     /*< Clear the Idle Line Detected bit in the status rerister by a read 

  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;  /*< Set UART2_BRR2 to reset  

  47.  

  48. value 0x00 */  

  49.     UART2->BRR1 = UART2_BRR1_RESET_VALUE;  /*< Set UART2_BRR1 to reset  

  50.  

  51. value 0x00 */  

  52.   

  53.     UART2->CR1 = UART2_CR1_RESET_VALUE; /*< Set UART2_CR1 to reset value  

  54.  

  55. 0x00  */  

  56.     UART2->CR2 = UART2_CR2_RESET_VALUE; /*< Set UART2_CR2 to reset value  

  57.  

  58. 0x00  */  

  59.     UART2->CR3 = UART2_CR3_RESET_VALUE;  /*< Set UART2_CR3 to reset value  

  60.  

  61. 0x00  */  

  62.     UART2->CR4 = UART2_CR4_RESET_VALUE;  /*< Set UART2_CR4 to reset value  

  63.  

  64. 0x00  */  

  65.     UART2->CR5 = UART2_CR5_RESET_VALUE; /*< Set UART2_CR5 to reset value  

  66.  

  67. 0x00  */  

  68.     UART2->CR6 = UART2_CR6_RESET_VALUE; /*< Set UART2_CR6 to 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);  /**< Clear the word length bit */  

  101.     UART2->CR1 |= (u8)WordLength; /**< Set the word length bit according  

  102.  

  103. to UART2_WordLength value */  

  104.   

  105.     UART2->CR3 &= (u8)(~UART2_CR3_STOP);  /**< Clear the STOP bits */  

  106.     UART2->CR3 |= (u8)StopBits;  /**< Set the STOP bits number according  

  107.  

  108. to UART2_StopBits value  */  

  109.   

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

  111.  

  112. the Parity Control bit */  

  113.     UART2->CR1 |= (u8)Parity;  /**< Set the Parity Control bit to  

  114.  

  115. UART2_Parity value */  

  116.   

  117.     UART2->BRR1 &= (u8)(~UART2_BRR1_DIVM);  /**< Clear the LSB mantissa  

  118.  

  119. of UARTDIV  */  

  120.     UART2->BRR2 &= (u8)(~UART2_BRR2_DIVM);  /**< Clear the MSB mantissa  

  121.  

  122. of UARTDIV  */  

  123.     UART2->BRR2 &= (u8)(~UART2_BRR2_DIVF);  /**< Clear the Fraction bits  

  124.  

  125. of UARTDIV */  

  126.   

  127.     /**< Set the UART2 BaudRates in BRR1 and BRR2 registers according to  

  128.  

  129. UART2_BaudRate value */  

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

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

  132.   

  133. << 4));  

  134.     /**< The fraction and MSB mantissa should be loaded in one step in  

  135.  

  136. the BRR2 register*/  

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

  138.   

  139. 100))  

  140.                         << 4) / 100) & (u8)0x0F); /**< Set the fraction  

  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;           /**< Set the LSB  

  147.  

  148. mantissa of UARTDIV */  

  149.   

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

  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); /**< Clear the Clock Polarity, lock Phase, Last Bit  

  156.  

  157. Clock pulse */  

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

  159.   

  160. UART2_CR3_CPHA | UART2_CR3_LBCL));  /**< Set the Clock Polarity, lock  

  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;  /**< Set the Transmitter Enable  

  167.  

  168. bit */  

  169.     }  

  170.     else  

  171.     {  

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

  173.  

  174. Disable bit */  

  175.     }  

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

  177.     {  

  178.         UART2->CR2 |= (u8)UART2_CR2_REN;  /**< Set the Receiver Enable  

  179.  

  180. bit */  

  181.     }  

  182.     else  

  183.     {  

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

  185.  

  186. Disable bit */  

  187.     }  

  188.     /**< Set the Clock Enable bit, lock Polarity, lock Phase and Last Bit  

  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); /**< Clear the Clock Enable  

  194.  

  195. bit */  

  196.         /**< configure in Push Pull or Open Drain mode the Tx I/O line by  

  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); /**< UART2 Enable */  

  214.     }  

  215.     else  

  216.     {  

  217.         UART2->CR1 |= UART2_CR1_UARTD;  /**< UART2 Disable (for low power  

  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 << (u8)((u8)UART2_IT & (u8)0x0F));  

  234.   

  235.     if (NewState != DISABLE)  

  236.     {  

  237.         /**< Enable the Interrupt bits according to UART2_IT mask */  

  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.         /**< Disable the interrupt bits according to UART2_IT mask */  

  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.     /*< Clear the Receive Register Not Empty flag */  

  360.     if (UART2_FLAG == UART2_FLAG_RXNE)  

  361.     {  

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

  363.     }  

  364.     /*< Clear the LIN Break Detection flag */  

  365.     else if (UART2_FLAG == UART2_FLAG_LBDF)  

  366.     {  

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

  368.     }  

  369.     /*< Clear the LIN Header Detection Flag */  

  370.     else if (UART2_FLAG == UART2_FLAG_LHDF)  

  371.     {  

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

  373.     }  

  374.     /*< Clear the LIN Synch Field flag */  

  375.     else  

  376.     {  

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

  378.     }  

  379.   

  380. }  


Keywords:STM8S Reference address:STM8S serial port application UART2 STM8S105

Previous article:STM32F02R8T6Nuceo board Uart2 serial port problem
Next article:STM8 sets the system clock

Recommended ReadingLatest update time:2024-11-23 14:54

Regarding the problem that STM8S IAR cannot download the program and cannot enter the interrupt
I have used STM8 before and will use STM32 later. When I was doing a small project, I thought STM8 also had a library and it should be very convenient, so I chose it directly. As a result, I was very sad when debugging. I thought the task could be completed in one day, but it took three days. Next, I will introduce th
[Microcontroller]
STM8S HALT mode and AWU wake-up
/* ********************************************************************************************************************************** * Name : STM8S HALT mode and AWU wake-up * Author : MingMing * Release : 2014/1/2 * Update: 2014/1/2 * E-mail : clint.wang@foxmail.com **************************************************
[Microcontroller]
Usage of UART in stm8s (four types of UART interrupts)
1. Application Examples 1.1 System Function  Use STM8's USART for self-transmission and self-reception (short-connect the transmit pin RXD to the receive pin TXD), send data: 0, 1, 2... data, and receive the data you send: 0, 1, 2... Use LED to make simple instructions! 1.2 Hardware Design LED control circuit
[Microcontroller]
Usage of UART in stm8s (four types of UART interrupts)
STM8S uses timer 1 to output PWM from CH4 through official LIB
I was confused when reading the Chinese datasheet of STM8S. It took me two nights to debug this function. Post the following code for your reference: TIM1_DeInit(); TIM1_TimeBaseInit(16, TIM1_COUNTERMODE_UP, 999, 0x00); //250 TIM1_OC4Init(TIM1_OCMODE_PWM1, TIM1_OUTPUTSTATE_ENABLE, 499, TIM1_OCPOLARITY_LOW, TIM1_OC
[Microcontroller]
STM32 UART2 program--port remapping
There are many USART1 programs. Let's take a look at the USART2 program. Note the red part. First, enable the clock of the relevant port. Because USART2 can be mapped to different ports, port mapping is required. Combine your own development board and set the correct mapping port. (The correct port is PA03PA02.) The d
[Microcontroller]
STM32 UART2 program--port remapping
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号