1. //少说话。多做事,下面是我验证过没有问题的串口发送接受数据
  2. //使用MCU stm8s105c6  UART2
  3. //初始化时调用:
  4.   GPIO_DeInit(GPIOD);
  5.   /* Configure PD5/6  */
  6.   GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_IN_PU_NO_IT);//发送数据IO
  7.   GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);//接受数据IO
  8.   UART2_DeInit();
  9.  UART2_Init(2400,UART2_WORDLENGTH_8D,UART2_STOPBITS_1,UART2_PARITY_NO,\
  10.             UART2_SYNCMODE_CLOCK_DISABLE,\
  11.       UART2_MODE_TX_ENABLE|UART2_MODE_RX_ENABLE);  //波特率 2400 8位数据  
  12. //1个停止位  没有奇偶校验 关闭SCK 同意串口接受和发送
  13.   UART2_Cmd(ENABLE);//启用串口
  14.   UART2_ITConfig(UART2_IT_RXNE_OR,ENABLE);//同意接受中断
  15. //操作串口(发送接受数据)时调用:
  16.                 if(UART2_GetFlagStatus(UART2_FLAG_TC))
  17.                  {//当前没有在发数据,能够发数据
  18.                     UART2_SendData8(Uart2TexData);
  19.                      UART2_ClearFlag(UART2_FLAG_TC);
  20.                  }
  21.     UART2_ClearITPendingBit(UART2_FLAG_RXNE);//清中断标志位
  22.     Uart2RecData = UART2_ReceiveData8();//接受中断数据//后面两句须要发在
  23. 串口接受中断中
  24. void UART2_DeInit(void)
  25. {
  26.     u8 dummy = 0;
  27.     /*< Clear the Idle Line Detected bit in the status rerister by a read
  28.        to the UART2_SR register followed by a Read to the UART2_DR 
  29. register */
  30.     dummy = UART2->SR;
  31.     dummy = UART2->DR;
  32.     UART2->BRR2 = UART2_BRR2_RESET_VALUE;  /*< Set UART2_BRR2 to reset 
  33. value 0x00 */
  34.     UART2->BRR1 = UART2_BRR1_RESET_VALUE;  /*< Set UART2_BRR1 to reset 
  35. value 0x00 */
  36.     UART2->CR1 = UART2_CR1_RESET_VALUE; /*< Set UART2_CR1 to reset value 
  37. 0x00  */
  38.     UART2->CR2 = UART2_CR2_RESET_VALUE; /*< Set UART2_CR2 to reset value 
  39. 0x00  */
  40.     UART2->CR3 = UART2_CR3_RESET_VALUE;  /*< Set UART2_CR3 to reset value 
  41. 0x00  */
  42.     UART2->CR4 = UART2_CR4_RESET_VALUE;  /*< Set UART2_CR4 to reset value 
  43. 0x00  */
  44.     UART2->CR5 = UART2_CR5_RESET_VALUE; /*< Set UART2_CR5 to reset value 
  45. 0x00  */
  46.     UART2->CR6 = UART2_CR6_RESET_VALUE; /*< Set UART2_CR6 to reset value 
  47. 0x00  */
  48. }
  49. void UART2_Init(u32 BaudRate, UART2_WordLength_TypeDef WordLength, 
  50. UART2_StopBits_TypeDef StopBits, UART2_Parity_TypeDef Parity, 
  51. UART2_SyncMode_TypeDef SyncMode, UART2_Mode_TypeDef Mode)
  52. {
  53.     u8 BRR2_1, BRR2_2 = 0;
  54.     u32 BaudRate_Mantissa, BaudRate_Mantissa100 = 0;
  55.     /* assert_param: BaudRate value should be <= 625000 bps */
  56.     assert_param(IS_UART2_BAUDRATE_OK(BaudRate));
  57.     assert_param(IS_UART2_WORDLENGTH_OK(WordLength));
  58.     assert_param(IS_UART2_STOPBITS_OK(StopBits));
  59.     assert_param(IS_UART2_PARITY_OK(Parity));
  60.     /* assert_param: UART2_Mode value should exclude values such as  
  61. UART2_ModeTx_Enable|UART2_ModeTx_Disable */
  62.     assert_param(IS_UART2_MODE_OK((u8)Mode));
  63.     /* assert_param: UART2_SyncMode value should exclude values such as
  64.        UART2_CLOCK_ENABLE|UART2_CLOCK_DISABLE */
  65.     assert_param(IS_UART2_SYNCMODE_OK((u8)SyncMode));
  66.     UART2->CR1 &= (u8)(~UART2_CR1_M);  /**< Clear the word length bit */
  67.     UART2->CR1 |= (u8)WordLength; /**< Set the word length bit according 
  68. to UART2_WordLength value */
  69.     UART2->CR3 &= (u8)(~UART2_CR3_STOP);  /**< Clear the STOP bits */
  70.     UART2->CR3 |= (u8)StopBits;  /**< Set the STOP bits number according 
  71. to UART2_StopBits value  */
  72.     UART2->CR1 &= (u8)(~(UART2_CR1_PCEN | UART2_CR1_PS  ));  /**< Clear 
  73. the Parity Control bit */
  74.     UART2->CR1 |= (u8)Parity;  /**< Set the Parity Control bit to 
  75. UART2_Parity value */
  76.     UART2->BRR1 &= (u8)(~UART2_BRR1_DIVM);  /**< Clear the LSB mantissa 
  77. of UARTDIV  */
  78.     UART2->BRR2 &= (u8)(~UART2_BRR2_DIVM);  /**< Clear the MSB mantissa 
  79. of UARTDIV  */
  80.     UART2->BRR2 &= (u8)(~UART2_BRR2_DIVF);  /**< Clear the Fraction bits 
  81. of UARTDIV */
  82.     /**< Set the UART2 BaudRates in BRR1 and BRR2 registers according to 
  83. UART2_BaudRate value */
  84.     BaudRate_Mantissa    = ((u32)CLK_GetClockFreq() / (BaudRate << 4));
  85.     BaudRate_Mantissa100 = (((u32)CLK_GetClockFreq() * 100) / (BaudRate 
  86. << 4));
  87.     /**< The fraction and MSB mantissa should be loaded in one step in 
  88. the BRR2 register*/
  89.     BRR2_1 = (u8)((u8)(((BaudRate_Mantissa100 - (BaudRate_Mantissa * 
  90. 100))
  91.                         << 4) / 100) & (u8)0x0F); /**< Set the fraction 
  92. of UARTDIV  */
  93.     BRR2_2 = (u8)((BaudRate_Mantissa >> 4) & (u8)0xF0);
  94.     UART2->BRR2 = (u8)(BRR2_1 | BRR2_2);
  95.     UART2->BRR1 = (u8)BaudRate_Mantissa;           /**< Set the LSB 
  96. mantissa of UARTDIV  */
  97.     UART2->CR2 &= (u8)~(UART2_CR2_TEN | UART2_CR2_REN); /**< Disable the 
  98. Transmitter and Receiver before seting the LBCL, CPOL and CPHA bits */
  99.     UART2->CR3 &= (u8)~(UART2_CR3_CPOL | UART2_CR3_CPHA | 
  100. UART2_CR3_LBCL); /**< Clear the Clock Polarity, lock Phase, Last Bit 
  101. Clock pulse */
  102.     UART2->CR3 |= (u8)((u8)SyncMode & (u8)(UART2_CR3_CPOL | 
  103. UART2_CR3_CPHA | UART2_CR3_LBCL));  /**< Set the Clock Polarity, lock 
  104. Phase, Last Bit Clock pulse */
  105.     if ((u8)Mode & (u8)UART2_MODE_TX_ENABLE)
  106.     {
  107.         UART2->CR2 |= (u8)UART2_CR2_TEN;  /**< Set the Transmitter Enable 
  108. bit */
  109.     }
  110.     else
  111.     {
  112.         UART2->CR2 &= (u8)(~UART2_CR2_TEN);  /**< Clear the Transmitter 
  113. Disable bit */
  114.     }
  115.     if ((u8)Mode & (u8)UART2_MODE_RX_ENABLE)
  116.     {
  117.         UART2->CR2 |= (u8)UART2_CR2_REN;  /**< Set the Receiver Enable 
  118. bit */
  119.     }
  120.     else
  121.     {
  122.         UART2->CR2 &= (u8)(~UART2_CR2_REN);  /**< Clear the Receiver 
  123. Disable bit */
  124.     }
  125.     /**< Set the Clock Enable bit, lock Polarity, lock Phase and Last Bit 
  126. Clock pulse bits according to UART2_Mode value */
  127.     if ((u8)SyncMode&(u8)UART2_SYNCMODE_CLOCK_DISABLE)
  128.     {
  129.         UART2->CR3 &= (u8)(~UART2_CR3_CKEN); /**< Clear the Clock Enable 
  130. bit */
  131.         /**< configure in Push Pull or Open Drain mode the Tx I/O line by 
  132. setting the correct I/O Port register according the product package and 
  133. line configuration*/
  134.     }
  135.     else
  136.     {
  137.         UART2->CR3 |= (u8)((u8)SyncMode & UART2_CR3_CKEN);
  138.     }
  139. }
  140. void UART2_Cmd(FunctionalState NewState)
  141. {
  142.     if (NewState != DISABLE)
  143.     {
  144.         UART2->CR1 &= (u8)(~UART2_CR1_UARTD); /**< UART2 Enable */
  145.     }
  146.     else
  147.     {
  148.         UART2->CR1 |= UART2_CR1_UARTD;  /**< UART2 Disable (for low power 
  149. consumption) */
  150.     }
  151. }
  152. void UART2_ITConfig(UART2_IT_TypeDef UART2_IT, FunctionalState NewState)
  153. {
  154.     u8 uartreg, itpos = 0x00;
  155.     assert_param(IS_UART2_CONFIG_IT_OK(UART2_IT));
  156.     assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  157.     /* Get the UART2 register index */
  158.     uartreg = (u8)(UART2_IT >> 0x08);
  159.     /* Get the UART2 IT index */
  160.     itpos = (u8)((u8)1 << (u8)((u8)UART2_IT & (u8)0x0F));
  161.     if (NewState != DISABLE)
  162.     {
  163.         /**< Enable the Interrupt bits according to UART2_IT mask */
  164.         if (uartreg == 0x01)
  165.         {
  166.             UART2->CR1 |= itpos;
  167.         }
  168.         else if (uartreg == 0x02)
  169.         {
  170.             UART2->CR2 |= itpos;
  171.         }
  172.         else if (uartreg == 0x03)
  173.         {
  174.             UART2->CR4 |= itpos;
  175.         }
  176.         else
  177.         {
  178.             UART2->CR6 |= itpos;
  179.         }
  180.     }
  181.     else
  182.     {
  183.         /**< Disable the interrupt bits according to UART2_IT mask */
  184.         if (uartreg == 0x01)
  185.         {
  186.             UART2->CR1 &= (u8)(~itpos);
  187.         }
  188.         else if (uartreg == 0x02)
  189.         {
  190.             UART2->CR2 &= (u8)(~itpos);
  191.         }
  192.         else if (uartreg == 0x03)
  193.         {
  194.             UART2->CR4 &= (u8)(~itpos);
  195.         }
  196.         else
  197.         {
  198.             UART2->CR6 &= (u8)(~itpos);
  199.         }
  200.     }
  201. }
  202. u8 UART2_ReceiveData8(void)
  203. {
  204.     return ((u8)UART2->DR);
  205. }
  206. void UART2_SendData8(u8 Data)
  207. {
  208.     /* Transmit Data */
  209.     UART2->DR = Data;
  210. }
  211. FlagStatus UART2_GetFlagStatus(UART2_Flag_TypeDef UART2_FLAG)
  212. {
  213.     FlagStatus status = RESET;
  214.     /* Check parameters */
  215.     assert_param(IS_UART2_FLAG_OK(UART2_FLAG));
  216.     /* Check the status of the specified UART2 flag*/
  217.     if (UART2_FLAG == UART2_FLAG_LBDF)
  218.     {
  219.         if ((UART2->CR4 & (u8)UART2_FLAG) != (u8)0x00)
  220.         {
  221.             /* UART2_FLAG is set*/
  222.             status = SET;
  223.         }
  224.         else
  225.         {
  226.             /* UART2_FLAG is reset*/
  227.             status = RESET;
  228.         }
  229.     }
  230.     else if (UART2_FLAG == UART2_FLAG_SBK)
  231.     {
  232.         if ((UART2->CR2 & (u8)UART2_FLAG) != (u8)0x00)
  233.         {
  234.             /* UART2_FLAG is set*/
  235.             status = SET;
  236.         }
  237.         else
  238.         {
  239.             /* UART2_FLAG is reset*/
  240.             status = RESET;
  241.         }
  242.     }
  243.     else if ((UART2_FLAG == UART2_FLAG_LHDF) || (UART2_FLAG == 
  244. UART2_FLAG_LSF))
  245.     {
  246.         if ((UART2->CR6 & (u8)UART2_FLAG) != (u8)0x00)
  247.         {
  248.             /* UART2_FLAG is set*/
  249.             status = SET;
  250.         }
  251.         else
  252.         {
  253.             /* UART2_FLAG is reset*/
  254.             status = RESET;
  255.         }
  256.     }
  257.     else
  258.     {
  259.         if ((UART2->SR & (u8)UART2_FLAG) != (u8)0x00)
  260.         {
  261.             /* UART2_FLAG is set*/
  262.             status = SET;
  263.         }
  264.         else
  265.         {
  266.             /* UART2_FLAG is reset*/
  267.             status = RESET;
  268.         }
  269.     }
  270.     /* Return the UART2_FLAG status*/
  271.     return  status;
  272. }
  273. void UART2_ClearFlag(UART2_Flag_TypeDef UART2_FLAG)
  274. {
  275.     assert_param(IS_UART2_CLEAR_FLAG_OK(UART2_FLAG));
  276.     /*< Clear the Receive Register Not Empty flag */
  277.     if (UART2_FLAG == UART2_FLAG_RXNE)
  278.     {
  279.         UART2->SR = (u8)~(UART2_SR_RXNE);
  280.     }
  281.     /*< Clear the LIN Break Detection flag */
  282.     else if (UART2_FLAG == UART2_FLAG_LBDF)
  283.     {
  284.         UART2->CR4 &= (u8)(~UART2_CR4_LBDF);
  285.     }
  286.     /*< Clear the LIN Header Detection Flag */
  287.     else if (UART2_FLAG == UART2_FLAG_LHDF)
  288.     {
  289.         UART2->CR6 &= (u8)(~UART2_CR6_LHDF);
  290.     }
  291.     /*< Clear the LIN Synch Field flag */
  292.     else
  293.     {
  294.         UART2->CR6 &= (u8)(~UART2_CR6_LSF);
  295.     }
  296. }
  297.  
  298.  

STM8S 串口应用 UART2 STM8S105的更多相关文章

  1. DM8127 更改调试串口为UART2

    1.uboot修改 1)修改宏定义 /*include/config/ti8148_evm.h*/ #define CONFIG_SYS_NS16550_COM2 0x48024000 #define ...

  2. STM8S和STM8L调试串口中断的注意点

    1. STM8L串口中断注意点 在调试PM2.5传感器GP2Y1051的时候,发现在仿真的时候开始能够进行数据的接受,但是如果暂停之后就不能接受数据,其实只是接收了一次完整的数据. 问题程序 解决方法 ...

  3. Smart210学习记录------linux串口驱动

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=27025492&id=327609 一.核心数据结构 串口驱动有 ...

  4. WS103C8例程——串口2【worldsing笔记】

    在超MINI核心板 stm32F103C8最小系统板上调试Usart2功能:用Jlink 6Pin接口连接WStm32f103c8的Uart2,PC机向mcu发送数据,mcu收到数据后数据加1,回传给 ...

  5. [RK3288][Android6.0] 调试笔记 --- 普通串口的添加 【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/54574073   标签: rk3288 串口添加 2017-01-16 14:52 1079 ...

  6. 树莓派4B串口测试与开发

    参考文档: https://shumeipai.nxez.com/2021/08/09/raspberry-pi-4-activating-additional-uart-ports.html 树莓派 ...

  7. 初识STM8S105K心得!

    最近由于公司项目需要STM8S105K这颗芯片,这两天我也捣鼓了下,正好现在开通了博客,以此记录下自己的工作. 开发环境:         window10操作系统:         IAR for ...

  8. STC12C5A60S2 双串口通信

    STC12C5A60S2单片机是一款功能比较强大的单片机,它拥有两个全双工串行通信接口,串口1的功能及操作与传统51单片机串行口相同:特殊的是STC12C5A60S2单片机内部有一个独立波特率发生器, ...

  9. stm8s103串口

    #include "uart.h" #define UART2#define uart_115200 1 void Init_UART2(void){#ifdef UART2    ...

随机推荐

  1. QTP的基本功能介绍

    • QTP的基本功能介绍 HP QuickTest Professional 支持功能測试和回归測试自己主动化,用于每一个主要软件应用程序和环境.此解决方式使用keyword驱动的測试概念,简化了測试 ...

  2. 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别

    Activity.finish()Call this when your activity is done and should be closed. 在你的activity动作完成的时候,或者Act ...

  3. 打开sa属性报错

    --如果打开sa属性报错如下:无法显示请求的对话框.属性IsLocked不可用于“登录名sa".该对象可能没有此属性,也可能是访问权限不足而无法检索 --解决办法:首先用windows登录, ...

  4. 调用opencv打开不摄像头

    调用opencv打开不摄像头,可以试试下面的语句: CvCapture* pCapture = cvCreateCameraCapture(0); 参数设为0 ,而不是-1,在自己电脑上可以 .

  5. Android调整TimePicker和DatePicker大小

    最近写了个app,里面要将DatePicker和TimePicker并列显示.但是,Android内部把DatePicker和 TimePicker大小固定了,导致4.5寸手机屏幕一行只能显示出一个, ...

  6. stringstream clear()的疑问 - yuanshuilee的日志 - 网易博客

    stringstream clear()的疑问 - yuanshuilee的日志 - 网易博客 stringstream clear()的疑问   2013-09-05 08:43:13|  分类: ...

  7. VirtualBox安装及使用说明和虚拟机安装XP系统图文教程

    virtualbox是一款开源的虚拟机软件,它能够支持多种操作系统的安装如:Solaris.Windows.DOS.Linux.OS/2 Warp.BSD等系统作为client操作系统,而且最新版本号 ...

  8. (step8.2.7)hdu 1517(A Multiplication Game——巴什博弈变形)

    题目大意:输入一个整数n.谁先报的数大于n,谁就输了.(初始值p  == 1 , 后一个人报的数必须在前一个人报的数的基础上乘上(2 ~ 9)之间的任意一个数) 解题思路:巴什博奕的变形 1) 解题思 ...

  9. Microsoft Visual Studio International Pack 1.0 SR1--关于汉字转拼音

    Microsoft Visual Studio International Pack 1.0 SR1————微软的一个类库 地址:http://www.microsoft.com/zh-cn/down ...

  10. 【linux】内核源代码下载与阅读

      原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http://blog. ...