STM32F10x 学习笔记7(USART实现串口通讯 3)
时间:11-20
来源:互联网
点击:
- ARTx);
- 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;
- defau
STM32F10xUSART串口通 相关文章:
- STM32F10x 学习笔记8(USART实现串口通讯 DMA 方式)(11-20)
- STM32F10x 学习笔记5(USART实现串口通讯 1)(11-20)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)