void Uart1_TransmitHandler(void) { volatile UINT8 data ; if( uart[0].txDataCount == 0 ) //check whether there is data to be transmitted { #if(defined __18F8722_H) ||(defined __18F46K22_H) while(Busy1USART()); #else while(BusyUSART()); #endif #ifdef __RS485__ TX1_EN = 0; //disable RS485 control #endif TXSTA1bits.TXEN = 0; //disable transmission return; } data = uart[0].txBuff[uart[0].txDataIndex]; TXREG1 = data; //transmit uart[0].txDataIndex++; //increase the dataIndex to point to the next data to be transmitted if( uart[0].txDataIndex >= TX_PACKET_SIZE) //on rollover { uart[0].txDataIndex = 0; //reset } if( uart[0].txDataCount > 0) uart[0].txDataCount--; //decrement the data count to indicate transmission }
/************************************************************************ Flush1USART Waits until the UART is fully done with transmitting data in the TXREG1 register. This may be necessary to avoid corrupting the last byte of data being transferred by the UART before shutting down the UART and entering Deep Sleep mode. Parameters: None Return: None Side Effects: None ************************************************************************/ void Flush1USART(void) { int i; for (i = 0; i < 10000; i++) { if (Busy1USART() == 0) { break; } } }
void puts1USART( char *data) { do { // Transmit a byte while(Busy1USART()); putc1USART(*data); } while( *data++ ); }
// http://microchip.wikidot.com/faq:29 void putch(char c) { #ifdef DEBUG #ifdef WITH_HOS while (Busy2USART()) CLRWDT(); Write2USART(c); while (Busy2USART()) CLRWDT(); #else while (Busy1USART()) ; Write1USART(c); while (Busy1USART()) ; #endif #endif }
// USB/USART combined put char (handles printf) void _user_putc(unsigned char c) { #ifdef USE_USART putc1USART(c); while(Busy1USART()); #endif #if defined(DEBUG_USART2_TX) || defined(UART2_DATA_OUTPUT) Write2USART(c); while(Busy2USART()); #endif usb_putchar(c); }
/* *------------------------------------------------------------------------------ * BOOL UART_write(UINT8 data) * * Summary : Fill the transmit buffer of the uart with data * Input : data to be transmitted which is stored in the txBuff * * Output : Boolean indicating success or failure * *------------------------------------------------------------------------------ */ BOOL UART_write(UINT8 data) { while(Busy1USART()); //wait for current transmission if any //DISABLE_UART_TX_INTERRUPT(); //disable the transmit interrupt uart.txBuff[uart.txBuffIndex++] = data; //store the data in the tx buffer uart.txDataCount++; //increment the data count if(uart.txBuffIndex >= TX_PACKET_SIZE) //check for overflow { uart.txBuffIndex = 0; } //ENABLE_UART_TX_INTERRUPT(); return TRUE; }
/* *------------------------------------------------------------------------------ * BOOL UART1_write(UINT8 data) * * Summary : Fill the transmit buffer of the uart with data * Input : data to be transmitted which is stored in the txBuff * * Output : Boolean indicating success or failure * *------------------------------------------------------------------------------ */ BOOL UART1_write(UINT8 data) { #if(defined __18F8722_H) ||(defined __18F46K22_H) while(Busy1USART()); //wait for current transmission if any #else while(BusyUSART()); //wait for current transmission if any #endif //DISABLE_UART_TX_INTERRUPT(); //disable the transmit interrupt uart[0].txBuff[uart[0].txBuffIndex++] = data; //store the data in the tx buffer uart[0].txDataCount++; //increment the data count if(uart[0].txBuffIndex >= TX_PACKET_SIZE) //check for overflow { uart[0].txBuffIndex = 0; } //ENABLE_UART_TX_INTERRUPT(); return TRUE; }
void UartTransmitHandler(void) { volatile UINT8 data ; if( uart.txDataCount == 0 ) //check whether there is data to be transmitted { while(Busy1USART()); TX_EN = 0; //disable RS485 control TXSTAbits.TXEN = 0; //disable transmission return; } data = uart.txBuff[uart.txDataIndex]; TXREG = data; //transmit uart.txDataIndex++; //increase the dataIndex to point to the next data to be transmitted if( uart.txDataIndex >= TX_PACKET_SIZE) //on rollover { uart.txDataIndex = 0; //reset } if( uart.txDataCount > 0) uart.txDataCount--; //decrement the data count to indicate transmission }
/* IFI lib uses this. (IT BURNNNNSSSS) */ void Wait4TXEmpty(void) { while(Busy1USART()); }
/* * STREAM IO */ void putc(char data) { /* From the Microchip C Library Docs */ while(Busy1USART()); Write1USART(data); }
char UART1_TX_Empty() { return(!Busy1USART()); // return TXSTA1bits.TRMT; //Returns Transmit Shift Status bit }
void uart_recv_int_handler() { #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else #ifdef __USE18F46J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else if (DataRdyUSART()) { buffer_temp[buf_len] = ReadUSART(); #endif #endif uc_ptr->buflen++; buf_len++; parseUART(); // check if a message should be sent } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else #ifdef __USE18F46J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainHigh_sendmsg(0, MSGT_OVERRUN, (void *) 0); } } void init_uart_recv(uart_comm *uc) { uc_ptr = uc; uc_ptr->buflen = 0; buf_len = 0; command_length = 0; command_count = 0; State = GET_MSGID; } void parseUART() { unsigned char x = uc_ptr->buflen; // uart_write(1, &x); switch(State) { case(GET_MSGID): { command_length = buffer_temp[buf_len -1] & 0xF; command_length = command_length; if(command_length != 0) { State = GET_COMMAND; } else { State = CHECKSUM; } break; } case(GET_COMMAND): { if(command_count+1 < command_length) { command_count++; } else { State = CHECKSUM; } break; } case(CHECKSUM): { ToMainLow_sendmsg(buf_len ,MSGT_PARSE, &buffer_temp[0]); uc_ptr->buflen = 0; buf_len = 0; State = GET_MSGID; command_count = 0; command_length = 0; break; } } } void uart_write(unsigned char length, unsigned char *msg){ unsigned char i = 0; for(i = 0; i < length; i++){ #ifdef __USE18F26J50 while(Busy1USART()); Write1USART(msg[i]); #else #ifdef __USE18F46J50 while(Busy1USART()); Write1USART(msg[i]); #else while(BusyUSART()); WriteUSART(msg[i]); #endif #endif } }