STM32F10x 學習筆記7(USART實現(xiàn)串口通訊 3)
整個驅(qū)動包含四個文件:
本文引用地址:http://m.butianyuan.cn/article/201611/318843.htmuart.h
uart.c
COMMRTOS.H
COMMRTOS.c
其中前兩個文件是對串口基本功能的封裝。
uart.h 的代碼如下:
- #ifndef_UART_H_
- #define_UART_H_
- voidUSART1_Init(void);
- voidUSART2_Init(void);
- voidUART_PutChar(USART_TypeDef*USARTx,uint8_tData);
- voidUART_PutStr(USART_TypeDef*USARTx,uint8_t*str);
- uint8_tUART_GetChar(USART_TypeDef*USARTx);
- #endif
uart.c 的代碼
- #include"stm32f10x.h"
- voidUSART1_Init(void)
- {
- GPIO_InitTypeDefGPIO_InitStructure;
- USART_InitTypeDefUSART_InitStructure;
- NVIC_InitTypeDefNVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
- /*ConfigureUSARTTxasalternatefunctionpush-pull*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- /*ConfigureUSARTRxasinputfloating*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate=9600;
- USART_InitStructure.USART_WordLength=USART_WordLength_8b;
- USART_InitStructure.USART_StopBits=USART_StopBits_1;
- USART_InitStructure.USART_Parity=USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
- USART_Init(USART1,&USART_InitStructure);
- USART_Cmd(USART1,ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- voidUSART2_Init(void)
- {
- GPIO_InitTypeDefGPIO_InitStructure;
- USART_InitTypeDefUSART_InitStructure;
- NVIC_InitTypeDefNVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
- /*ConfigureUSARTTxasalternatefunctionpush-pull*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- /*ConfigureUSARTRxasinputfloating*/
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
- GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
- USART_InitStructure.USART_BaudRate=9600;
- USART_InitStructure.USART_WordLength=USART_WordLength_8b;
- USART_InitStructure.USART_StopBits=USART_StopBits_1;
- USART_InitStructure.USART_Parity=USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
- USART_Init(USART2,&USART_InitStructure);
- USART_Cmd(USART2,ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- voidUART_PutChar(USART_TypeDef*USARTx,uint8_tData)
- {
- //while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)==RESET){};
- while((USARTx->SR&USART_FLAG_TXE)==0x00){};
- //USART_SendData(USARTx,Data);
- USARTx->DR=Data;
- }
- voidUART_PutStr(USART_TypeDef*USARTx,uint8_t*str)
- {
- while(0!=*str)
- {
- UART_PutChar(USARTx,*str);
- str++;
- }
- }
- uint8_tUART_GetChar(USART_TypeDef*USARTx)
- {
- //while(USART_GetFlagStatus(USARTx,USART_FLAG_RXNE)==RESET){};
- while((USARTx->SR&USART_FLAG_RXNE)==0x00){};
- //returnUSART_ReceiveData(USARTx);
- return(USARTx->DR&0xff);
- }
commrtos.h 的代碼如下:
- #ifndef_COMMRTOS_H_
- #define_COMMRTOS_H_
- #ifndefCFG_H
- #defineCOMM_RX_BUF_SIZE64/*NumberofcharactersinRxringbuffer*/
- #defineCOMM_TX_BUF_SIZE64/*NumberofcharactersinTxringbuffer*/
- #endif
- /*
- *********************************************************************************************************
- *CONSTANTS
- *********************************************************************************************************
- */
- #ifndefFALSE
- #defineFALSE0x00
- #endif
- #ifndefTRUE
- #defineTRUE0xff
- #endif
- #ifndefNUL
- #defineNUL0x00
- #endif
- #defineCOM10
- #defineCOM21
- /*ERRORCODES*/
- #defineCOMM_NO_ERR0/*Functioncallwassuccessful*/
- #defineCOMM_BAD_CH1/*Invalidcommunicationsportchannel*/
- #defineCOMM_RX_EMPTY2/*Rxbufferisempty,nocharacteravailable*/
- #defineCOMM_TX_FULL3/*Txbufferisfull,couldnotdepositcharacter*/
- #defineCOMM_TX_EMPTY4/*IftheTxbufferisempty.*/
- #defineCOMM_RX_TIMEOUT5/*Ifatimeoutoccurredwhilewaitingforacharacter*/
- #defineCOMM_TX_TIMEOUT6/*Ifatimeoutoccurredwhilewaitingtosendachar.*/
- #defineCOMM_PARITY_NONE0/*Definesforsettingparity*/
- #defineCOMM_PARITY_ODD1
- #defineCOMM_PARITY_EVEN2
- unsignedcharCommGetChar(unsignedcharch,unsignedshortto,unsignedchar*err);
- voidCOMInit(void);
- unsignedcharCommIsEmpty(unsignedcharch);
- unsignedcharCommIsFull(unsignedcharch);
- unsignedcharCommPutChar(unsignedcharch,unsignedcharc,unsignedshortto);
- voidCommPutStr(unsignedcharch,uint8_t*str);
- #endif
commrtos.c 的代碼如下:
- #include"stm32f10x_usart.h"
- #include"includes.h"
- #include"COMMRTOS.H"
- /*
- *DATATYPES
- */
- typedefstruct{
- unsignedshortRingBufRxCtr;/*NumberofcharactersintheRxringbuffer*/
- OS_EVENT*RingBufRxSem;/*PointertoRxsemaphore*/
- unsignedchar*RingBufRxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
- unsignedchar*RingBufRxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
- unsignedcharRingBufRx[COMM_RX_BUF_SIZE];/*Ringbuffercharacterstorage(Rx)*/
- unsignedshortRingBufTxCtr;/*NumberofcharactersintheTxringbuffer*/
- OS_EVENT*RingBufTxSem;/*PointertoTxsemaphore*/
- unsignedchar*RingBufTxInPtr;/*Pointertowherenextcharacterwillbeinserted*/
- unsignedchar*RingBufTxOutPtr;/*Pointerfromwherenextcharacterwillbeextracted*/
- unsignedcharRingBufTx[COMM_TX_BUF_SIZE];/*Ringbuffercharacterstorage(Tx)*/
- }COMM_RING_BUF;
- /*
- *GLOBALVARIABLES
- */
- COMM_RING_BUFComm1Buf;
- COMM_RING_BUFComm2Buf;
- staticvoidCOMEnableTxInt(unsignedcharport)
- {
- staticUSART_TypeDef*map[2]={USART1,USART2};
- //USART_ITConfig(map[port],USART_IT_TXE,ENABLE);
- map[port]->CR1|=USART_FLAG_TXE;
- }
- /*
- *********************************************************************************************************
- *REMOVECHARACTERFROMRINGBUFFER
- *
- *
- *Description:Thisfunctioniscalledbyyourapplicationtoobtainacharacterfromthecommunications
- *channel.Thefunctionwillwaitforacharactertobereceivedontheserialchannelor
- *untilthefunctiontimesout.
- *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
- *COMM1
- *COMM2
- *toistheamountoftime(inclockticks)thatthecallingfunctioniswillingto
- *waitforacharactertoarrive.Ifyouspecifyatimeoutof0,thefunctionwill
- *waitforeverforacharactertoarrive.
- *errisapointertowhereanerrorcodewillbeplaced:
- **errissettoCOMM_NO_ERRifacharacterhasbeenreceived
- **errissettoCOMM_RX_TIMEOUTifatimeoutoccurred
- **errissettoCOMM_BAD_CHifyouspecifyaninvalidchannelnumber
- *Returns:Thecharacterinthebuffer(orNULifatimeoutoccurred)
- *********************************************************************************************************
- */
- unsignedcharCommGetChar(unsignedcharch,unsignedshortto,unsignedchar*err)
- {
- unsignedcharc;
- unsignedcharoserr;
- COMM_RING_BUF*pbuf;
- OS_CPU_SRcpu_sr;
- switch(ch)
- {/*Obtainpointertocommunicationschannel*/
- caseCOM1:
- pbuf=&Comm1Buf;
- break;
- caseCOM2:
- pbuf=&Comm2Buf;
- break;
- default:
- *err=COMM_BAD_CH;
- return(NUL);
- }
- OSSemPend(pbuf->RingBufRxSem,to,&oserr);/*Waitforcharactertoarrive*/
- if(oserr==OS_TIMEOUT)
- {/*Seeifcharactersreceivedwithintimeout*/
- *err=COMM_RX_TIMEOUT;/*No,returnerrorcode*/
- return(NUL);
- }
- else
- {
- OS_ENTER_CRITICAL();
- pbuf->RingBufRxCtr--;/*Yes,decrementcharactercount*/
- c=*pbuf->RingBufRxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufRxOutPtr==&pbuf->RingBufRx[COMM_RX_BUF_SIZE])
- {/*WrapOUTpointer*/
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- }
- OS_EXIT_CRITICAL();
- *err=COMM_NO_ERR;
- return(c);
- }
- }
- voidCommPutStr(unsignedcharch,uint8_t*str)
- {
- while(0!=*str)
- {
- CommPutChar(ch,*str,0);
- str++;
- }
- }
- staticunsignedcharCOMGetTxChar(unsignedcharch,unsignedchar*err)
- {
- unsignedcharc;
- COMM_RING_BUF*pbuf;
- switch(ch)
- {/*Obtainpointertocommunicationschannel*/
- caseCOM1:
- pbuf=&Comm1Buf;
- break;
- caseCOM2:
- pbuf=&Comm2Buf;
- break;
- default:
- *err=COMM_BAD_CH;
- return(NUL);
- }
- if(pbuf->RingBufTxCtr>0)
- {/*Seeifbufferisempty*/
- pbuf->RingBufTxCtr--;/*No,decrementcharactercount*/
- c=*pbuf->RingBufTxOutPtr++;/*Getcharacterfrombuffer*/
- if(pbuf->RingBufTxOutPtr==&pbuf->RingBufTx[COMM_TX_BUF_SIZE])
- {/*WrapOUTpointer*/
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- }
- OSSemPost(pbuf->RingBufTxSem);/*Indicatethatcharacterwillbesent*/
- *err=COMM_NO_ERR;
- return(c);/*Charactersarestillavailable*/
- }
- else
- {
- *err=COMM_TX_EMPTY;
- return(NUL);/*Bufferisempty*/
- }
- }
- /*
- *********************************************************************************************************
- *INITIALIZECOMMUNICATIONSMODULE
- *Description:Thisfunctioniscalledbyyourapplicationtoinitializethecommunicationsmodule.You
- *mustcallthisfunctionbeforecallinganyotherfunctions.
- *Arguments:none
- *********************************************************************************************************
- */
- voidCOMInit(void)
- {
- COMM_RING_BUF*pbuf;
- pbuf=&Comm1Buf;/*InitializetheringbufferforCOMM1*/
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxSem=OSSemCreate(0);
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxSem=OSSemCreate(COMM_TX_BUF_SIZE);
- pbuf=&Comm2Buf;/*InitializetheringbufferforCOMM2*/
- pbuf->RingBufRxCtr=0;
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxOutPtr=&pbuf->RingBufRx[0];
- pbuf->RingBufRxSem=OSSemCreate(0);
- pbuf->RingBufTxCtr=0;
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxOutPtr=&pbuf->RingBufTx[0];
- pbuf->RingBufTxSem=OSSemCreate(COMM_TX_BUF_SIZE);
- }
- /*
- *********************************************************************************************************
- *SEEIFRXCHARACTERBUFFERISEMPTY
- *
- *
- *Description:Thisfunctioniscalledbyyourapplicationtoseeifanycharacterisavailablefromthe
- *communicationschannel.Ifatleastonecharacterisavailable,thefunctionreturns
- *FALSEotherwise,thefunctionreturnsTRUE.
- *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
- *COMM1
- *COMM2
- *Returns:TRUEifthebufferISempty.
- *FALSEifthebufferISNOTemptyoryouhavespecifiedanincorrectchannel.
- *********************************************************************************************************
- */
- unsignedcharCommIsEmpty(unsignedcharch)
- {
- unsignedcharempty;
- COMM_RING_BUF*pbuf;
- OS_CPU_SRcpu_sr;
- switch(ch)
- {/*Obtainpointertocommunicationschannel*/
- caseCOM1:
- pbuf=&Comm1Buf;
- break;
- caseCOM2:
- pbuf=&Comm2Buf;
- break;
- default:
- return(TRUE);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufRxCtr>0)
- {/*Seeifbufferisempty*/
- empty=FALSE;/*BufferisNOTempty*/
- }
- else
- {
- empty=TRUE;/*Bufferisempty*/
- }
- OS_EXIT_CRITICAL();
- return(empty);
- }
- /*
- *********************************************************************************************************
- *SEEIFTXCHARACTERBUFFERISFULL
- *Description:Thisfunctioniscalledbyyourapplicationtoseeifanymorecharacterscanbeplaced
- *intheTxbuffer.Inotherwords,thisfunctionchecktoseeiftheTxbufferisfull.
- *Ifthebufferisfull,thefunctionreturnsTRUEotherwise,thefunctionreturnsFALSE.
- *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
- *COMM1
- *COMM2
- *Returns:TRUEifthebufferISfull.
- *FALSEifthebufferISNOTfulloryouhavespecifiedanincorrectchannel.
- *********************************************************************************************************
- */
- unsignedcharCommIsFull(unsignedcharch)
- {
- unsignedcharfull;
- COMM_RING_BUF*pbuf;
- OS_CPU_SRcpu_sr;
- switch(ch)
- {/*Obtainpointertocommunicationschannel*/
- caseCOM1:
- pbuf=&Comm1Buf;
- break;
- caseCOM2:
- pbuf=&Comm2Buf;
- break;
- default:
- return(TRUE);
- }
- OS_ENTER_CRITICAL();
- if(pbuf->RingBufTxCtr
- {/*Seeifbufferisfull*/
- full=FALSE;/*BufferisNOTfull*/
- }
- else
- {
- full=TRUE;/*Bufferisfull*/
- }
- OS_EXIT_CRITICAL();
- return(full);
- }
- /*
- *********************************************************************************************************
- *OUTPUTCHARACTER
- *
- *
- *Description:Thisfunctioniscalledbyyourapplicationtosendacharacteronthecommunications
- *channel.Thefunctionwillwaitforthebuffertoemptyoutifthebufferisfull.
- *Thefunctionreturnstoyourapplicationifthebufferdoesntemptywithinthespecified
- *timeout.Atimeoutvalueof0meansthatthecallingfunctionwillwaitforeverforthe
- *buffertoemptyout.ThecharactertosendisfirstinsertedintotheTxbufferandwill
- *besentbytheTxISR.Ifthisisthefirstcharacterplacedintothebuffer,theTxISR
- *willbeenabled.
- *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
- *COMM1
- *COMM2
- *cisthecharactertosend.
- *toisthetimeout(inclockticks)towaitincasethebufferisfull.Ifyou
- *specifyatimeoutof0,thefunctionwillwaitforeverforthebuffertoempty.
- *Returns:COMM_NO_ERRifthecharacterwasplacedintheTxbuffer
- *COMM_TX_TIMEOUTifthebufferdidntemptywithinthespecifiedtimeoutperiod
- *COMM_BAD_CHifyouspecifyaninvalidchannelnumber
- *********************************************************************************************************
- */
- unsignedcharCommPutChar(unsignedcharch,unsignedcharc,unsignedshortto)
- {
- unsignedcharoserr;
- COMM_RING_BUF*pbuf;
- OS_CPU_SRcpu_sr;
- switch(ch)
- {/*Obtainpointertocommunicationschannel*/
- caseCOM1:
- pbuf=&Comm1Buf;
- break;
- caseCOM2:
- pbuf=&Comm2Buf;
- break;
- default:
- return(COMM_BAD_CH);
- }
- OSSemPend(pbuf->RingBufTxSem,to,&oserr);/*WaitforspaceinTxbuffer*/
- if(oserr==OS_TIMEOUT)
- {
- return(COMM_TX_TIMEOUT);/*Timedout,returnerrorcode*/
- }
- OS_ENTER_CRITICAL();
- pbuf->RingBufTxCtr++;/*No,incrementcharactercount*/
- *pbuf->RingBufTxInPtr++=c;/*Putcharacterintobuffer*/
- if(pbuf->RingBufTxInPtr==&pbuf->RingBufTx[COMM_TX_BUF_SIZE])
- {/*WrapINpointer*/
- pbuf->RingBufTxInPtr=&pbuf->RingBufTx[0];
- }
- if(pbuf->RingBufTxCtr==1)
- {/*Seeifthisisthefirstcharacter*/
- COMEnableTxInt(ch);/*Yes,EnableTxinterrupts*/
- }
- OS_EXIT_CRITICAL();
- return(COMM_NO_ERR);
- }
- /*
- *********************************************************************************************************
- *INSERTCHARACTERINTORINGBUFFER
- *
- *
- *Description:ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
- *Arguments:chistheCOMMportchannelnumberandcaneitherbe:
- *COMM1
- *COMM2
- *cisthecharactertoinsertintotheringbuffer.Ifthebufferisfull,the
- *characterwillnotbeinserted,itwillbelost.
- *********************************************************************************************************
- */
- staticvoidCOMPutRxChar(unsignedcharch,unsignedcharc)
- {
- COMM_RING_BUF*pbuf;
- switch(ch)
- {/*Obtainpointertocommunicationschannel*/
- caseCOM1:
- pbuf=&Comm1Buf;
- break;
- caseCOM2:
- pbuf=&Comm2Buf;
- break;
- default:
- return;
- }
- if(pbuf->RingBufRxCtr
- {/*Seeifbufferisfull*/
- pbuf->RingBufRxCtr++;/*No,incrementcharactercount*/
- *pbuf->RingBufRxInPtr++=c;/*Putcharacterintobuffer*/
- if(pbuf->RingBufRxInPtr==&pbuf->RingBufRx[COMM_RX_BUF_SIZE])
- {/*WrapINpointer*/
- pbuf->RingBufRxInPtr=&pbuf->RingBufRx[0];
- }
- OSSemPost(pbuf->RingBufRxSem);/*Indicatethatcharacterwasreceived*/
- }
- }
- //ThisfunctioniscalledbytheRxISRtoinsertacharacterintothereceiveringbuffer.
- voidUSART1_IRQHandler(void)
- {
- unsignedintdata;
- unsignedcharerr;
- OS_CPU_SRcpu_sr;
- OS_ENTER_CRITICAL();/*TelluC/OS-IIthatwearestartinganISR*/
- OSIntNesting++;
- OS_EXIT_CRITICAL();
- if(USART1->SR&0x0F)
- {
- //Seeifwehavesomekindoferror
- //Clearinterrupt(donothingaboutit!)
- data=USART1->DR;
- }
- elseif(USART1->SR&USART_FLAG_RXNE)//ReceiveDataRegFullFlag
- {
- data=USART1->DR;
- COMPutRxChar(COM1,data);//Insertreceivedcharacterintobuffer
- }
- elseif(USART1->SR&USART_FLAG_TXE)
- {
- data=COMGetTxChar(COM1,&err);//Getnextcharactertosend.
- if(err==COMM_TX_EMPTY)
- {//Dowehaveanymorecharacterstosend?
- //No,DisableTxinterrupts
- //USART_ITConfig(USART1,USART_IT_TXE|USART_IT_TC,DISABLE);
- USART1->CR1&=~USART_FLAG_TXE|USART_FLAG_TC;
- }
- else
- {
- USART1->DR=data;//Yes,Sendcharacter
- }
- }
- OSIntExit();
- }
- voidUSART2_IRQHandler(void)
- {
- unsignedintdata;
- unsignedcharerr;
- OS_CPU_SRcpu_sr;
- OS_ENTER_CRITICAL();/*TelluC/OS-IIthatwearestartinganISR*/
- OSIntNesting++;
- OS_EXIT_CRITICAL();
- if(USART2->SR&0x0F)
- {
- //Seeifwehavesomekindoferror
- //Clearinterrupt(donothingaboutit!)
- data=USART2->DR;
- }
- elseif(USART2->SR&USART_FLAG_RXNE)//ReceiveDataRegFullFlag
- {
- data=USART2->DR;
- COMPutRxChar(COM2,data);//Insertreceivedcharacterintobuffer
- }
- elseif(USART2->SR&USART_FLAG_TXE)
- {
- data=COMGetTxChar(COM2,&err);//Getnextcharactertosend.
- if(err==COMM_TX_EMPTY)
- {//Dowehaveanymorecharacterstosend?
- //No,DisableTxinterrupts
- //USART_ITConfig(USART2,USART_IT_TXE|USART_IT_TC,DISABLE);
- USART2->CR1&=~USART_FLAG_TXE|USART_FLAG_TC;
- }
- else
- {
- USART2->DR=data;//Yes,Sendcharacter
- }
- }
- OSIntExit();
- }
下面再給出個測試代碼:
- #include"stm32f10x.h"
- #include"uart.h"
- #include"led.h"
- #include"COMMRTOS.H"
- #include"ucos_ii.h"
- #defineTASK_STK_SIZE128
- OS_STKTaskStartStk[TASK_STK_SIZE];
- OS_STKTaskUartReadStk[TASK_STK_SIZE];
- voidTaskUartRead(void*pdata)
- {
- unsignedcharerr;
- unsignedcharc;
- for(;;)
- {
- c=CommGetChar(COM2,0,&err);
- if(err==COMM_NO_ERR)
- CommPutChar(COM2,c,0);
- }
- }
- voidTaskStart(void*pdata)
- {
- SysTick_Config(SystemCoreClock/10);
- USART1_Init();
- USART2_Init();
- COMInit();
- //USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- //USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
- USART1->CR1|=USART_FLAG_RXNE;
- USART2->CR1|=USART_FLAG_RXNE;
- OSTaskCreate(TaskUartRead,(void*)0,&(TaskUartReadStk[TASK_STK_SIZE-1]),2);
- UART_PutStr(USART2,"USART2HelloWorld!nr");
- //CommPutChar(COM2,+,0);
- CommPutStr(COM2,"CommPutCharBnr");
- for(;;)
- {
- LED_Spark();
- CommPutChar(COM2,+,0);
- OSTimeDly(10);
- }
- }
- intmain(void)
- {
- SystemInit();
- LED_Init();
- OSInit();
- OSTaskCreate(TaskStart,(void*)0,&(TaskStartStk[TASK_STK_SIZE-1]),1);
- OSStart();
- for(;;)
- {
- }
- }
評論