娇小w搡bbbb搡bbb,《第一次の人妻》,中国成熟妇女毛茸茸,边啃奶头边躁狠狠躁视频免费观看

歷史上的今天

今天是:2025年06月01日(星期日)

2018年06月01日 | STM8S 串口應用 UART2 STM8S105

發布者:紫菜包飯 來源: eefocus關鍵字:STM8S  串口應用  UART2  STM8S105 手機看文章 掃描二維碼
隨時隨地手機看文章
  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.     /*< 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. }  


關鍵字:STM8S  串口應用  UART2  STM8S105 引用地址:STM8S 串口應用 UART2 STM8S105

上一篇:第一頁
下一篇:最后一頁

推薦閱讀

集微網消息(文/羅明)備受矚目的vivo NEX全面屏手機,因為屏占比接近100%,沒有跟風使用iPhone X的劉海屏,加上升降式攝像頭等黑科技的加持,雖然vivo官方現在沒有展開宣傳攻勢,但是與之相關的消息接連被曝出來,讓人很是期待。實體店海報登場微博上已經有網友曝光了vivoNEX在線下實體店的宣傳物料,并顯示新機有著極窄的邊框設計和采用了升降式前置鏡頭...
據外媒報道,Facebook申請了一項特殊的無人機專利,這款無人機將能夠利用風在空中飛行。這款“雙風箏飛行器”是由兩只風箏組成,它們能在不同的高度漂浮。 每一只風箏都能夠獨立導航,并且無人機可以生成自己的能源來延長飛行時間。跟所有專利一樣,我們不知道Facebook是否正在構建這個系統。但這表明,即便Facebook縮減了此前廣為人知的Aquila項目,這家...
基于預測控制的儲能系統多時間尺度動態響應優化研究 吳傳申,劉宇,高山*,鄒子卿,黃學良 (東南大學 電氣工程學院) 本文發表在《全球能源互聯網》2020年第3期,受國家自然科學基金項目(51907024)資助。 文章導讀 風電作為無污染可再生能源的代表,得到了廣泛應用。然而,隨著風電滲透率的不斷增加,其隨機性和不穩定性已經成為影響微網可...
日前,據海外媒體報道,美國專利商標局公布了一項蘋果公司泰坦項目相關專利,涉及自動駕駛汽車V2V通信系統。蘋果專利背景解釋稱,V2X和V2V通信是一種車輛技術,旨在讓車輛之間,以及車輛與其他設備(如行人智能手機和交通燈)進行通信。這些技術通過提供實時、高度可靠和可操作的信息流,支持安全、出行和環境相關應用,有望重新定義交通運輸。此外,這些...

史海拾趣

問答坊 | AI 解惑

一個PLC梯形圖到C語言的轉換工具,歡迎指正!

不知道有人用過PLC沒有,這是我們開發的一個梯形圖邏輯到C語言的轉換器,對于一些純粹的位邏輯控制算法,可能有些幫助。同時,網站上也有一些PLC的使用教程和開發資料,希望對大家有用。 項目介紹[http://www.visiblecontrol.com/product ...…

查看全部問答∨

菜鳥提問:有朋友說根據器件清單,可以猜到題目?

本帖最后由 paulhyde 于 2014-9-15 09:38 編輯 是不是真的? 依據什么原則呢?  …

查看全部問答∨

單片機的內部、外部結構(一)

單片機的內部、外部結構(一) 一、單片機的外部結構 拿到一塊芯片,想要使用它,首先必須要知道怎樣連線,我們用的一塊稱之為89C51的芯片,下面我們就看一下如何給它連線。 1、 電源:這當然是必不可少的了。單片機使用的是5V電源,其中正極接40引 ...…

查看全部問答∨

什么是LED洗墻燈

replyreload += \',\' + 373247;Timson,如果您要查看本帖隱藏內容請回復…

查看全部問答∨

測試一下是否能夠發帖,請版主見諒

測試一下是否能夠發帖,請版主見諒…

查看全部問答∨

請教AddDevice函數是做什么用的呀?為什么文件系統過濾驅動AddDevice函數?

向高手請教 請教AddDevice函數是做什么用的呀?為什么文件系統過濾驅動AddDevice函數?…

查看全部問答∨

關于RAS Server

現要設計一個在WinCE 5.0平臺上的等待撥入的撥號服務器,但沒有相關的經驗,不知道哪位高人指點一二,多謝…

查看全部問答∨

IBM宣布開發授權,促進嵌入式的發展

       由于缺乏高端SoC芯片技術,國內IC設計企業在嵌入式CPU開發上一直處于落后狀態。可喜的是,隨著全球半導體產業的技術轉移及自身的努力,近年來國內已涌現出一些嵌入式CPU開發優勢企業。下面就由福州卓躍教育具 ...…

查看全部問答∨

關于CY7C68013中官方例程CYStream的一點解釋

我在使用CYSteam例程來測速速度的時候一直出現了一個問題,下載固件程序后,在設備管理器中能夠看到該驅動已經正常安裝了,但是在console或者是官方提供的CYSteam中無法識別。一直以為是驅動的程序的問題。但是我利用購買的開發板就沒有這個問題。 ...…

查看全部問答∨
小廣播
設計資源 培訓 開發板 精華推薦

最新單片機文章
何立民專欄 單片機及嵌入式寶典

北京航空航天大學教授,20余年來致力于單片機與嵌入式系統推廣工作。

 
EEWorld訂閱號

 
EEWorld服務號

 
汽車開發圈

 
機器人開發圈

電子工程世界版權所有 京ICP證060456號 京ICP備10001474號-1 電信業務審批[2006]字第258號函 京公網安備 11010802033920號 Copyright ? 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
主站蜘蛛池模板: 静安区| 胶州市| 买车| 托里县| 大冶市| 建平县| 阿克苏市| 兴安盟| 施秉县| 株洲市| 南康市| 德钦县| 福贡县| 汶川县| 邹平县| 平原县| 双鸭山市| 铁岭市| 昌吉市| 九龙县| 万山特区| 临澧县| 韶山市| 曲周县| 青浦区| 讷河市| 汉阴县| 长寿区| 浦东新区| 宝鸡市| 宿州市| 板桥市| 乐陵市| 曲沃县| 吴川市| 化隆| 东安县| 策勒县| 通州市| 盐源县| 泰来县|