新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32F10x 學習筆記6(USART實現(xiàn)串口通訊 2)

STM32F10x 學習筆記6(USART實現(xiàn)串口通訊 2)

作者: 時間:2016-11-20 來源:網(wǎng)絡 收藏
這次講講利用串口收發(fā)中斷來進行串口通訊。STM32 上為每個串口分配了一個中斷。也就是說無論是發(fā)送完成還是收到數(shù)據(jù)或是數(shù)據(jù)溢出都產(chǎn)生同一個中斷。程序需在中斷處理函數(shù)中讀取狀態(tài)寄存器(USART_SR)來判斷當前的是什么中斷。下面的中斷映像圖給出了這些中斷源是如何匯合成最終的中斷信號的。圖中也給出了如何控制每一個單獨的中斷源是否起作用。

本文引用地址:http://m.butianyuan.cn/article/201611/318841.htm

另外,Cortex-M3內核中還有個NVIC,可以控制這里的中斷信號是否觸發(fā)中斷處理函數(shù)的執(zhí)行,還有這些外部中斷的級別。關于NVIC可以參考《ARMCortexM3權威指南》,里面講解的非常詳細。

簡單的說,為了開啟中斷,我們需要如下的代碼:

  1. NVIC_InitTypeDefNVIC_InitStructure;
  2. NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
  3. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  4. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
  5. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  6. NVIC_Init(&NVIC_InitStructure);
  7. USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//開啟接收中斷
  8. USART_ITConfig(USART1,USART_IT_TXE,ENABLE);//開啟發(fā)送中斷

這里多說一句,串口的發(fā)送中斷有兩個,分別是:

  1. l發(fā)送數(shù)據(jù)寄存器空中斷(TXE)
  2. l發(fā)送完成中斷(TC)

一般來說我們會使用發(fā)送數(shù)據(jù)寄存器空中斷,用這個中斷發(fā)送的效率會高一些。

中斷處理函數(shù)的框架如下,如果檢測到錯誤就清除錯誤,收到數(shù)了就處理。發(fā)完當前數(shù)據(jù)了就發(fā)下一個。

  1. voidUSART1_IRQHandler(void)
  2. {
  3. unsignedintdata;
  4. if(USART1->SR&0x0F)
  5. {
  6. //Seeifwehavesomekindoferror,Clearinterrupt
  7. data=USART1->DR;
  8. }
  9. elseif(USART1->SR&USART_FLAG_RXNE)//ReceiveDataRegFullFlag
  10. {
  11. data=USART1->DR;
  12. //對收到的數(shù)據(jù)進行處理,或者干些其他的事
  13. }
  14. elseif(USART1->SR&USART_FLAG_TXE)
  15. {
  16. {//可以發(fā)送數(shù)據(jù)了,如果沒有數(shù)據(jù)需要發(fā)送,就在這里關閉發(fā)送中斷
  17. USART1->DR=something;//Yes,Sendcharacter
  18. }
  19. }
  20. }

下面給一個利用環(huán)形緩沖區(qū)的串口驅動程序。

  1. #ifndef_COM_BUFFERED_H_
  2. #define_COM_BUFFERED_H_
  3. #defineCOM10
  4. #defineCOM21
  5. #defineCOM_RX_BUF_SIZE64/*NumberofcharactersinRxringbuffer*/
  6. #defineCOM_TX_BUF_SIZE64/*NumberofcharactersinTxringbuffer*/
  7. #defineCOM_NO_ERR0/*Functioncallwassuccessful*/
  8. #defineCOM_BAD_CH1/*Invalidcommunicationsportchannel*/
  9. #defineCOM_RX_EMPTY2/*Rxbufferisempty,nocharacteravailable*/
  10. #defineCOM_TX_FULL3/*Txbufferisfull,couldnotdepositcharacter*/
  11. #defineCOM_TX_EMPTY4/*IftheTxbufferisempty.*/
  12. /************************************************************
  13. *function:COMGetCharB
  14. *parameter:charport,portcanbeCOM1/COM2
  15. *parameter:char*errisapointertowhereanerrorcodewillbeplaced:
  16. **errissettoCOM_NO_ERRifacharacterisavailable
  17. **errissettoCOM_RX_EMPTYiftheRxbufferisempty
  18. **errissettoCOM_BAD_CHifyouhavespecifiedaninvalidchannel
  19. *return:char
  20. *usage:Thisfunctioniscalledbyyourapplicationtoobtainacharacterfromthecommunications
  21. *channel.
  22. *changelog:
  23. *************************************************************/
  24. unsignedcharCOMGetCharB(unsignedcharch,unsignedchar*err);
  25. /************************************************************
  26. *function:COMPutCharB
  27. *parameter:charport,portcanbeCOM1/COM2
  28. *return:COMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
  29. *COMM_TX_FULLifthebufferwasfull
  30. *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
  31. *usage:Thisfunctioniscalledbyyourapplicationtosendacharacteronthecommunications
  32. *channel.ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
  33. *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
  34. *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
  35. *changelog:
  36. *************************************************************/
  37. unsignedcharCOMPutCharB(unsignedcharport,unsignedcharc);
  38. /************************************************************
  39. *function:COMBufferInit
  40. *parameter:
  41. *return:
  42. *usage:Thisfunctioniscalledbyyourapplicationtoinitializethecommunicationsmodule.You
  43. *mustcallthisfunctionbeforecallinganyotherfunctions.
  44. *changelog:
  45. *************************************************************/
  46. voidCOMBufferInit(void);
  47. /************************************************************
  48. *function:COMBufferIsEmpty
  49. *parameter:charport,portcanbeCOM1/COM2
  50. *return:char
  51. *usage:Thisfunctioniscalledbyyourapplicationtosee
  52. *ifanycharacterisavailablefromthecommunicationschannel.
  53. *Ifatleastonecharacterisavailable,thefunctionreturns
  54. *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
  55. *changelog:
  56. *************************************************************/
  57. unsignedcharCOMBufferIsEmpty(unsignedcharport);
  58. /************************************************************
  59. *function:COMBufferIsFull
  60. *parameter:charport,portcanbeCOM1/COM2
  61. *return:char
  62. *usage:Thisfunctioniscalledbyyourapplicationtoseeifanymorecharacterscanbeplaced
  63. *intheTxbuffer.Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  64. *Ifthebufferisfull,thefunctionreturnsTRUEotherwise,thefunctionreturnsFALSE.
  65. *changelog:
  66. *************************************************************/
  67. unsignedcharCOMBufferIsFull(unsignedcharport);
  68. #endif

  1. /*
  2. *file:com_buffered.c
  3. *author:LiYuan
  4. *platform:STM32F107
  5. *date:2013-5-5
  6. *version:0.0.1
  7. *description:UARTRingBuffer
  8. **/
  9. #include"stm32f10x_usart.h"
  10. #include"com_buffered.h"
  11. #defineOS_ENTER_CRITICAL()__set_PRIMASK(1)
  12. #defineOS_EXIT_CRITICAL()__set_PRIMASK(0)
  13. /**
  14. *EnablesTransmiterinterrupt.
  15. **/
  16. staticvoidCOMEnableTxInt(unsignedcharport)
  17. {
  18. staticUSART_TypeDef*map[2]={USART1,USART2};
  19. USART_ITConfig(map[port],USART_IT_TXE,ENABLE);
  20. }
  21. /*
  22. *********************************************************************************************************
  23. *DATATYPES
  24. *********************************************************************************************************
  25. */
  26. typedefstruct{
  27. shortRingBufRxCtr;/*NumberofcharactersintheRxringbuffer*/
  28. unsignedchar*RingBufRxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
  29. unsignedchar*RingBufRxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
  30. unsignedcharRingBufRx[COM_RX_BUF_SIZE];/*Ringbuffercharacterstorage(Rx)*/
  31. shortRingBufTxCtr;/*NumberofcharactersintheTxringbuffer*/
  32. unsignedchar*RingBufTxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
  33. unsignedchar*RingBufTxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
  34. unsignedcharRingBufTx[COM_TX_BUF_SIZE];/*Ringbuffercharacterstorage(Tx)*/
  35. }COM_RING_BUF;
  36. /*
  37. *********************************************************************************************************
  38. *GLOBALVARIABLES
  39. *********************************************************************************************************
  40. */
  41. COM_RING_BUFCOM1Buf;
  42. COM_RING_BUFCOM2Buf;
  43. /************************************************************
  44. *function:COMGetCharB
  45. *parameter:charport,portcanbeCOM1/COM2
  46. *parameter:char*errisapointertowhereanerrorcodewillbeplaced:
  47. **errissettoCOM_NO_ERRifacharacterisavailable
  48. **errissettoCOM_RX_EMPTYiftheRxbufferisempty
  49. **errissettoCOM_BAD_CHifyouhavespecifiedaninvalidchannel
  50. *return:char
  51. *usage:Thisfunctioniscalledbyyourapplicationtoobtainacharacterfromthecommunications
  52. *channel.
  53. *changelog:
  54. *************************************************************/
  55. unsignedcharCOMGetCharB(unsignedcharport,unsignedchar*err)
  56. {
  57. //unsignedcharcpu_sr;
  58. unsignedcharc;
  59. COM_RING_BUF*pbuf;
  60. switch(port)
  61. {/*Obtainpointertocommunicationschannel*/
  62. caseCOM1:
  63. pbuf=&COM1Buf;
  64. break;
  65. caseCOM2:
  66. pbuf=&COM2Buf;
  67. break;
  68. default:
  69. *err=COM_BAD_CH;
  70. return(0);
  71. }
  72. OS_ENTER_CRITICAL();
  73. if(pbuf->RingBufRxCtr>0)/*Seeifbufferisempty*/
  74. {
  75. pbuf->RingBufRxCtr--;/*No,decrementcharactercount*/
  76. c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
  77. if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[COM_RX_BUF_SIZE])
  78. {
  79. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];/*WrapOUTpointer*/
  80. }
  81. OS_EXIT_CRITICAL();
  82. *err=COM_NO_ERR;
  83. return(c);
  84. }else{
  85. OS_EXIT_CRITICAL();
  86. *err=COM_RX_EMPTY;
  87. c=0;/*Bufferisempty,return0*/
  88. return(c);
  89. }
  90. }
  91. /************************************************************
  92. *function:COMPutCharB
  93. *parameter:charport,portcanbeCOM1/COM2
  94. *return:COMM_NO_ERRifthefunctionwassuccessful(thebufferwasnotfull)
  95. *COMM_TX_FULLifthebufferwasfull
  96. *COMM_BAD_CHifyouhavespecifiedanincorrectchannel
  97. *usage:Thisfunctioniscalledbyyourapplicationtosendacharacteronthecommunications
  98. *channel.ThecharactertosendisfirstinsertedintotheTxbufferandwillbesentby
  99. *theTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISRwillbe
  100. *enabled.IftheTxbufferisfull,thecharacterwillnotbesent(i.e.itwillbelost)
  101. *changelog:
  102. *1.firstimplimentedbyliyuan2010.11.5
  103. *************************************************************/
  104. unsignedcharCOMPutCharB(unsignedcharport,unsignedcharc)
  105. {
  106. //unsignedcharcpu_sr;
  107. COM_RING_BUF*pbuf;
  108. switch(port)
  109. {/*Obtainpointertocommunicationschannel*/
  110. caseCOM1:
  111. pbuf=&COM1Buf;
  112. break;
  113. caseCOM2:
  114. pbuf=&COM2Buf;
  115. break;
  116. default:
  117. return(COM_BAD_CH);
  118. }
  119. OS_ENTER_CRITICAL();
  120. if(pbuf->RingBufTxCtr
  121. pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
  122. *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
  123. if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[COM_TX_BUF_SIZE]){/*WrapINpointer*/
  124. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  125. }
  126. if(pbuf->RingBufTxCtr==1){/*Seeifthisisthefirstcharacter*/
  127. COMEnableTxInt(port);/*Yes,EnableTxinterrupts*/
  128. OS_EXIT_CRITICAL();
  129. }else{
  130. OS_EXIT_CRITICAL();
  131. }
  132. return(COM_NO_ERR);
  133. }else{
  134. OS_EXIT_CRITICAL();
  135. return(COM_TX_FULL);
  136. }
  137. }
  138. /************************************************************
  139. *function:COMBufferInit
  140. *parameter:
  141. *return:
  142. *usage:Thisfunctioniscalledbyyourapplicationtoinitializethecommunicationsmodule.You
  143. *mustcallthisfunctionbeforecallinganyotherfunctions.
  144. *changelog:
  145. *************************************************************/
  146. voidCOMBufferInit(void)
  147. {
  148. COM_RING_BUF*pbuf;
  149. pbuf=&COM1Buf;/*InitializetheringbufferforCOM0*/
  150. pbuf->RingBufRxCtr=0;
  151. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  152. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  153. pbuf->RingBufTxCtr=0;
  154. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  155. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  156. pbuf=&COM2Buf;/*InitializetheringbufferforCOM1*/
  157. pbuf->RingBufRxCtr=0;
  158. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  159. pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
  160. pbuf->RingBufTxCtr=0;
  161. pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
  162. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
  163. }
  164. /************************************************************
  165. *function:COMBufferIsEmpty
  166. *parameter:charport,portcanbeCOM1/COM2
  167. *return:char
  168. *usage:Thisfunctioniscalledbyyourapplicationtosee
  169. *ifanycharacterisavailablefromthecommunicationschannel.
  170. *Ifatleastonecharacterisavailable,thefunctionreturns
  171. *FALSE(0)otherwise,thefunctionreturnsTRUE(1).
  172. *changelog:
  173. *************************************************************/
  174. unsignedcharCOMBufferIsEmpty(unsignedcharport)
  175. {
  176. //unsignedcharcpu_sr;
  177. unsignedcharempty;
  178. COM_RING_BUF*pbuf;
  179. switch(port)
  180. {/*Obtainpointertocommunicationschannel*/
  181. caseCOM1:
  182. pbuf=&COM1Buf;
  183. break;
  184. caseCOM2:
  185. pbuf=&COM2Buf;
  186. break;
  187. default:
  188. return(1);
  189. }
  190. OS_ENTER_CRITICAL();
  191. if(pbuf->RingBufRxCtr>0)
  192. {/*Seeifbufferisempty*/
  193. empty=0;/*BufferisNOTempty*/
  194. }
  195. else
  196. {
  197. empty=1;/*Bufferisempty*/
  198. }
  199. OS_EXIT_CRITICAL();
  200. return(empty);
  201. }
  202. /************************************************************
  203. *function:COMBufferIsFull
  204. *parameter:charport,portcanbeCOM1/COM2
  205. *return:char
  206. *usage:Thisfunctioniscalledbyyourapplicationtoseeifanymorecharacterscanbeplaced
  207. *intheTxbuffer.Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
  208. *Ifthebufferisfull,thefunctionreturnsTRUEotherwise,thefunctionreturnsFALSE.
  209. *changelog:
  210. *************************************************************/
  211. unsignedcharCOMBufferIsFull(unsignedcharport)
  212. {
  213. //unsignedcharcpu_sr;
  214. charfull;
  215. COM_RING_BUF*pbuf;
  216. switch(port)
  217. {/*Obtainpointertocommunicationschannel*/
  218. caseCOM1:
  219. pbuf=&COM1Buf;
  220. break;
  221. caseCOM2:
  222. pbuf=&COM2Buf;
  223. break;
  224. default:
  225. return(1);
  226. }
  227. OS_ENTER_CRITICAL();
  228. if(pbuf->RingBufTxCtr
  229. full=0;/*BufferisNOTfull*/
  230. }else{
  231. full=1;/*Bufferisfull*/
  232. }
  233. OS_EXIT_CRITICAL();
  234. return(full);
  235. }
  236. //ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
  237. staticvoidCOMPutRxChar(unsignedcharport,unsignedcharc)
  238. {
  239. COM_RING_BUF*pbuf;
  240. switch(port)
  241. {/*Obtainpointertocommunicationschannel*/
  242. caseCOM1:
  243. pbuf=&COM1Buf;
  244. break;
  245. caseCOM2:
  246. pbuf=&COM2Buf;
  247. break;
  248. default:
  249. return;
  250. }
  251. if(pbuf->RingBufRxCtr
  252. pbuf->RingBufRxCtr++;/*No,incrementcharactercount*/
  253. *pbuf->RingBufRxInPtr++=c;/*Putcharacterintobuffer*/
  254. if(pbuf->RingBufRxInPtr==&pbuf->RingBufRx[COM_RX_BUF_SIZE]){/*WrapINpointer*/
  255. pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
  256. }
  257. }
  258. }
  259. //ThisfunctioniscalledbytheTxISRtoextractthenextcharacterfromtheTxbuffer.
  260. //ThefunctionreturnsFALSEifthebufferisemptyafterthecharacterisextractedfrom
  261. //thebuffer.ThisisdonetosignaltheTxISRtodisableinterruptsbecausethisisthe
  262. //lastcharactertosend.
  263. staticunsignedcharCOMGetTxChar(unsignedcharport,unsignedchar*err)
  264. {
  265. unsignedcharc;
  266. COM_RING_BUF*pbuf;
  267. switch(port)
  268. {/*Obtainpointertocommunicationschannel*/
  269. caseCOM1:
  270. pbuf=&COM1Buf;
  271. break;
  272. caseCOM2:
  273. pbuf=&COM2Buf;
  274. break;
  275. default:
  276. *err=COM_BAD_CH;
  277. return(0);
  278. }
  279. if(pbuf->RingBufTxCtr>0){/*Seeifbufferisempty*/
  280. pbuf->RingBufTxCtr--;/*No,decrementcharactercount*/
  281. c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
  282. if(pbuf->RingBufTxOutPtr==&pbuf->RingBufTx[COM_TX_BUF_SIZE])
  283. {
  284. pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];/*WrapOUTpointer*/
  285. }
  286. *err=COM_NO_ERR;
  287. return(c);/*Charactersarestillavailable*/
  288. }else{
  289. *err=COM_TX_EMPTY;
  290. return(0);/*Bufferisempty*/
  291. }
  292. }
  293. voidUSART1_IRQHandler(void)
  294. {
  295. unsignedintdata;
  296. unsignedcharerr;
  297. if(USART1->SR&0x0F)
  298. {
  299. //Seeifwehavesomekindoferror
  300. //Clearinterrupt(donothingaboutit!)
  301. data=USART1->DR;
  302. }
  303. elseif(USART1->SR&USART_FLAG_RXNE)//ReceiveDataRegFullFlag
  304. {
  305. data=USART1->DR;
  306. COMPutRxChar(COM1,data);//Insertreceivedcharacterintobuffer
  307. }
  308. elseif(USART1->SR&USART_FLAG_TXE)
  309. {
  310. data=COMGetTxChar(COM1,&err);//Getnextcharactertosend.
  311. if(err==COM_TX_EMPTY)
  312. {//Dowehaveanymorecharacterstosend?
  313. //No,DisableTxinterrupts
  314. //USART_ITConfig(USART1,USART_IT_TXE|USART_IT_TC,ENABLE);
  315. USART1->CR1&=~USART_FLAG_TXE|USART_FLAG_TC;
  316. }
  317. else
  318. {
  319. USART1->DR=data;//Yes,Sendcharacter
  320. }
  321. }
  322. }
  323. voidUSART2_IRQHandler(void)
  324. {
  325. unsignedintdata;
  326. unsignedcharerr;
  327. if(USART2->SR&0x0F)
  328. {
  329. //Seeifwehavesomekindoferror
  330. //Clearinterrupt(donothingaboutit!)
  331. data=USART2->DR;
  332. }
  333. elseif(USART2->SR&USART_FLAG_RXNE)//ReceiveDataRegFullFlag
  334. {
  335. data=USART2->DR;
  336. COMPutRxChar(COM2,data);//Insertreceivedcharacterintobuffer
  337. }
  338. elseif(USART2->SR&USART_FLAG_TXE)
  339. {
  340. data=COMGetTxChar(COM2,&err);//Getnextcharactertosend.
  341. if(err==COM_TX_EMPTY)
  342. {//Dowehaveanymorecharacterstosend?
  343. //No,DisableTxinterrupts
  344. //USART_ITConfig(USART2,USART_IT_TXE|USART_IT_TC,ENABLE);
  345. USART2->CR1&=~USART_FLAG_TXE|USART_FLAG_TC;
  346. }
  347. else
  348. {
  349. USART2->DR=data;//Yes,Sendcharacter
  350. }
  351. }
  352. }

下面給個例子主程序,來演示如何使用上面的串口驅動代碼。

  1. #include"misc.h"
  2. #include"stm32f10x.h"
  3. #include"com_buffered.h"
  4. voidUART_PutStrB(unsignedcharport,uint8_t*str)
  5. {
  6. while(0!=*str)
  7. {
  8. COMPutCharB(port,*str);
  9. str++;
  10. }
  11. }
  12. voidUSART1_Init(void)
  13. {
  14. GPIO_InitTypeDefGPIO_InitStructure;
  15. USART_InitTypeDefUSART_InitStructure;
  16. NVIC_InitTypeDefNVIC_InitStructure;
  17. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
  18. /*ConfigureUSARTTxasalternatefunctionpush-pull*/
  19. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
  20. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
  21. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  22. GPIO_Init(GPIOA,&GPIO_InitStructure);
  23. /*ConfigureUSARTRxasinputfloating*/
  24. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  25. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
  26. GPIO_Init(GPIOA,&GPIO_InitStructure);
  27. USART_InitStructure.USART_BaudRate=9600;
  28. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  29. USART_InitStructure.USART_StopBits=USART_StopBits_1;
  30. USART_InitStructure.USART_Parity=USART_Parity_No;
  31. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  32. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
  33. USART_Init(USART1,&USART_InitStructure);
  34. USART_Cmd(USART1,ENABLE);
  35. NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
  36. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  37. NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
  38. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  39. NVIC_Init(&NVIC_InitStructure);
  40. }
  41. voidUSART2_Init(void)
  42. {
  43. GPIO_InitTypeDefGPIO_InitStructure;
  44. USART_InitTypeDefUSART_InitStructure;
  45. NVIC_InitTypeDefNVIC_InitStructure;
  46. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
  47. /*ConfigureUSARTTxasalternatefunctionpush-pull*/
  48. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
  49. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
  50. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  51. GPIO_Init(GPIOD,&GPIO_InitStructure);
  52. /*ConfigureUSARTRxasinputfloating*/
  53. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  54. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
  55. GPIO_Init(GPIOD,&GPIO_InitStructure);
  56. GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
  57. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
  58. USART_InitStructure.USART_BaudRate=9600;
  59. USART_InitStructure.USART_WordLength=USART_WordLength_8b;
  60. USART_InitStructure.USART_StopBits=USART_StopBits_1;
  61. USART_InitStructure.USART_Parity=USART_Parity_No;
  62. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  63. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
  64. USART_Init(USART2,&USART_InitStructure);
  65. USART_Cmd(USART2,ENABLE);
  66. NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
  67. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  68. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
  69. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  70. NVIC_Init(&NVIC_InitStructure);
  71. }
  72. intmain(void)
  73. {
  74. unsignedcharc;
  75. unsignedcharerr;
  76. USART1_Init();
  77. USART2_Init();
  78. COMBufferInit();
  79. USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  80. USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
  81. UART_PutStrB(COM1,"HelloWorld!n");
  82. for(;;)
  83. {
  84. c=COMGetCharB(COM1,&err);
  85. if(err==COM_NO_ERR)
  86. {
  87. COMPutCharB(COM1,c);
  88. }
  89. }
  90. }



評論


技術專區(qū)

關閉