完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
所以我试图通过RS232接收字符串。我决定使用MCC代码生成器,但是现在我不知道这些函数是如何工作的。这是头文件。这是EuasARTHEAD中的EuSART.cNoW,EuSARTrxCube每次成功接收字节时都递减,但我似乎在代码中找不到EUSAARTXC中的任何地方。ONT除了0之外还被赋值,如果我试图接收一个字符串,并将它存储在一个数组中,那么我应该在for循环中使用EuStudioRead吗?就我所见,这个函数确实实现了一个循环机制来填充EuSARTrxBuffic。我真的迷路了。如果使用EasARTHARIVEVEXISR,那么它应该和EasARTHAREAD一起使用吗?因为在这个循环中,我可以看到eusartRxBuffer被分配的值存储在RCREG中,但是没有循环。我应该使用中断来接收字符串,因为我不应该在ISRS中放置阻塞机制吗?有人能帮我吗?或者一些关于如何理解这些功能的一般指导,我已经读了几次数据表,但我仍然想使用MCC生成的代码,因为它将STDIO函数分配给EUSAT,并且我喜欢打印东西。道谢
以上来自于百度翻译 以下为原文 so i am trying to receive strings over RS232. And i decided to use the MCC code generator, but now im lost on how these functions work. this is the header file .#ifndef _EUSART_H #define _EUSART_H /** Section: Included Files */ #include #include #include #include #ifdef __cplusplus // Provide C++ Compatibility extern "C" { #endif /** Section: Macro Declarations */ /** Section: Data Type Definitions */ /** Section: Global variables */ /** Section: EUSART APIs */ void (*EUSART_TxDefaultInterruptHandler)(void); void (*EUSART_RxDefaultInterruptHandler)(void); /** * @brief Initialization routine that takes inputs from the EUSART GUI. * */ void EUSART_Initialize(void); /** * @brief Check if the EUSART transmitter is empty * * @return The number of available bytes that EUSART has remaining in its transmit buffer. * @retval 0 the EUSART transmitter is not empty * @retval 1+ the EUSART transmitter has available space for writing */ uint8_t EUSART_is_tx_ready(void); /** * @brief Check if the EUSART receiver is not empty * * @return The number of bytes EUSART has available for reading. * @retval 0 the EUSART receiver is empty * @retval 1+ the EUSART receiver has data available to read */ uint8_t EUSART_is_rx_ready(void); /** * @brief Check if EUSART data is transmitted * * @return Receiver ready status * @retval false Data is not completely shifted out of the shift register * @retval true Data completely shifted out if the USART shift register */ bool EUSART_is_tx_done(void); /** * @brief Read one character from EUSART * * @return Data read from the EUSART module */ uint8_t EUSART_Read(void); /** * @brief Write one character on EUSART * * @param[in] data The character to write to the EUSART bus * * @return Nothing */ void EUSART_Write(uint8_t txData); /** * @brief Maintains the driver's transmitter state machine and implements its ISR. * * @return Nothing */ void EUSART_Transmit_ISR(void); /** * @brief Maintains the driver's receiver state machine and implements its ISR * * @return Nothing */ void EUSART_Receive_ISR(void); /** * @brief sets a custom function to be called at the end of the transmit ISR * * @param handler */ void EUSART_SetTxInterruptHandler(void* handler); /** * @brief sets a custom function to be called at the end of the receive ISR * * @param handler */ void EUSART_SetRxInterruptHandler(void* handler); #ifdef __cplusplus // Provide C++ Compatibility } #endif #endif // _EUSART_H /** End of File */ this is the eusart.c #include "eusart.h" /** Section: Macro Declarations */ #define EUSART_TX_BUFFER_SIZE 8 #define EUSART_RX_BUFFER_SIZE 8 /** Section: Global Variables */ volatile uint8_t eusartTxHead = 0; volatile uint8_t eusartTxTail = 0; volatile uint8_t eusartTxBuffer[EUSART_TX_BUFFER_SIZE]; volatile uint8_t eusartTxBufferRemaining; volatile uint8_t eusartRxHead = 0; volatile uint8_t eusartRxTail = 0; volatile uint8_t eusartRxBuffer[EUSART_RX_BUFFER_SIZE]; volatile uint8_t eusartRxCount; /** Section: EUSART APIs */ void EUSART_Initialize(void) { // disable interrupts before changing states PIE1bits.RCIE = 0; PIE1bits.TXIE = 0; EUSART_SetTxInterruptHandler(EUSART_Transmit_ISR); EUSART_SetRxInterruptHandler(EUSART_Receive_ISR); // Set the EUSART module to the options selected in the user interface. // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled; BAUDCON = 0x08; // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled; RCSTA = 0x90; // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave; TXSTA = 0x24; // Baud Rate = 9600; SPBRGL 25; SPBRGL = 0x19; // Baud Rate = 9600; SPBRGH 0; SPBRGH = 0x00; // initializing the driver state eusartTxHead = 0; eusartTxTail = 0; eusartTxBufferRemaining = sizeof(eusartTxBuffer); eusartRxHead = 0; eusartRxTail = 0; eusartRxCount = 0; // enable receive interrupt PIE1bits.RCIE = 1; } uint8_t EUSART_is_tx_ready(void) { return eusartTxBufferRemaining; } uint8_t EUSART_is_rx_ready(void) { return eusartRxCount; } bool EUSART_is_tx_done(void) { return TXSTAbits.TRMT; } uint8_t EUSART_Read(void) { uint8_t readValue = 0; while(0 == eusartRxCount) { } readValue = eusartRxBuffer[eusartRxTail++]; if(sizeof(eusartRxBuffer) <= eusartRxTail) { eusartRxTail = 0; } PIE1bits.RCIE = 0; eusartRxCount--; PIE1bits.RCIE = 1; return readValue; } void EUSART_Write(uint8_t txData) { while(0 == eusartTxBufferRemaining) { } if(0 == PIE1bits.TXIE) { TXREG = txData; } else { PIE1bits.TXIE = 0; eusartTxBuffer[eusartTxHead++] = txData; if(sizeof(eusartTxBuffer) <= eusartTxHead) { eusartTxHead = 0; } eusartTxBufferRemaining--; } PIE1bits.TXIE = 1; } char getch(void) { return EUSART_Read(); } void putch(char txData) { EUSART_Write(txData); } void EUSART_Transmit_ISR(void) { // add your EUSART interrupt custom code if(sizeof(eusartTxBuffer) > eusartTxBufferRemaining) { TXREG = eusartTxBuffer[eusartTxTail++]; if(sizeof(eusartTxBuffer) <= eusartTxTail) { eusartTxTail = 0; } eusartTxBufferRemaining++; } else { PIE1bits.TXIE = 0; } } void EUSART_Receive_ISR(void) { if(1 == RCSTAbits.OERR) { // EUSART error - restart RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; } // buffer overruns are ignored eusartRxBuffer[eusartRxHead++] = RCREG; if(sizeof(eusartRxBuffer) <= eusartRxHead) { eusartRxHead = 0; } eusartRxCount++; } void EUSART_SetTxInterruptHandler(void* handler){ EUSART_TxDefaultInterruptHandler = handler; } void EUSART_SetRxInterruptHandler(void* handler){ EUSART_RxDefaultInterruptHandler = handler; } now in EUSART_Read, eusartRxCount is decremented every time there is a successful reception of a byte, but i do not seem to find any where in the code where eusartRxCount has been assigned anyvalue other than 0. also if I'm trying to receive a string, and to store it in an array, should i use EUSART_Read within a for Loop? as far as i can see, this function does implement a looping mechanism to fill the eusartRXBuffer. i am really lost here. also if Eusart_receive_ISR is used, then it should be used in conjuction with EUSART_READ?, because within this loop, i can see eusartRxBuffer is being assigned values stored in RCREG, but no loops. should i use interrupts for receiving of strings, as i shouldn't place blocking mechanisms within ISRs? Can anyone help me? or some general guidance on how to understand these functions, i have read the data sheet couple of times, but i still want to use MCC generated codes, because it assigns STDIO functions to Eusart, and i like printing things. :D Thanks |
|
相关推荐
1个回答
|
|
我摆弄了MCC,生成了UART代码,看了看,但最后还是写了自己的。似乎基本的循环缓冲区设计与我所使用的没有太大的不同,但是MCC代码似乎过于臃肿,对单个代码行的函数调用非常容易使用。只需调用EuStudioWrrad发送一个字节,并用EuStudioRead来接收(如果缓冲区中有数据,WHICHEUSAARTIX ISXRXYEADE会告诉您)。它们从主ISR例程调用,并将移动字节转移到循环发送和接收缓冲区。或者,如果这是K42,放在IVT EUSAARTHARY写会把TX字节放在圆形TX缓冲器上,TX ISR会把它们吐出串行端口。如果缓冲区满了,EuStudioWrad将阻塞直到它不存在,所以我会把TX缓冲区从8字节上调。我通常使用256字节,除非它是一个小RAM的小图片。EUARTARYRADE从循环RX缓冲区中提取字节。您需要检查缓冲区上是否有字节。我只是在主循环中进行一个检查,如果有字节,执行一个读,并把字节发送到接收处理程序来处理数据。
以上来自于百度翻译 以下为原文 I fiddled with MCC and generated UART code and looked at it but ended up writing my own. It seems the basic circular buffer design isn't much different from what I've used, but the MCC code seems too bloated with function calls for single lines of code. Really easy to use. Just call EUSART_Write to send a byte, and EUSART_Read to receive (if there is data in the buffer which EUSART_is_rx_ready will tell you). The ISR functions you don't call. They are called from the main ISR routine and take care of moving bytes to/from the circular transmit and receive buffers. Or if this is K42, put in the IVT. EUSART_Write will put TX bytes on the circular TX buffer and the TX ISR will spit them out the serial port. If the buffer is full, EUSART_Write will block until it isn't, so I would bump the TX buffer up from 8bytes. I usually use 256 bytes, unless it's a small PIC with little RAM. EUSART_Read pulls bytes from the circular rx buffer. You need to check if there are bytes on the buffer. I just put a check in the main loop and if there are bytes, execute a read and send the byte to a receive handler to process the data. |
|
|
|
只有小组成员才能发言,加入小组>>
5244 浏览 9 评论
2035 浏览 8 评论
1955 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3214 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2261 浏览 5 评论
779浏览 1评论
673浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
599浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
683浏览 0评论
580浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-25 18:07 , Processed in 1.420472 second(s), Total 48, Slave 42 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号