Rev 272 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#include <xc.h>#include "defines.h"#include "base_UART.h"static UART_DATA *uart_data_p;void UART_Init(UART_DATA *data) {uart_data_p = data;UART_TX_TRIS = 0; // Tx pin set to output#ifndef UART_TX_ONLYUART_RX_TRIS = 1; // Rx pin set to input#endifLATAbits.LATA0 = 1; // Keep the line high at startBAUDCONbits.BRG16 = 0; // 8-bit baud rate generatorSPBRGL = 25; // Set UART speed to 38400 baudTXSTAbits.BRGH = 1; // High speed modeTXSTAbits.SYNC = 0; // Async modeRCSTAbits.SPEN = 1; // Serial port enableTXSTAbits.TX9 = 0; // 8 bit transmissionTXSTAbits.TXEN = 1; // Transmission enabledPIE1bits.TXIE = 0; // Disable TX interrupt#ifndef UART_TX_ONLYRCSTAbits.RX9 = 0; // 8 bit receptionRCSTAbits.CREN = 1; // Enables receiverPIE1bits.RCIE = 1; // Enable RX interrupt// Initialize the buffer that holds UART messagesuart_data_p->buffer_in_read_ind = 0;uart_data_p->buffer_in_write_ind = 0;uart_data_p->buffer_in_len = 0;uart_data_p->buffer_in_len_tmp = 0;#elseRCSTAbits.CREN = 0;#endifuart_data_p->buffer_out_ind = 0;uart_data_p->buffer_out_len = 0;}void UART_Send_Interrupt_Handler() {// Put remaining data in TSR for transmitif (uart_data_p->buffer_out_ind != uart_data_p->buffer_out_len) {TXREG = uart_data_p->buffer_out[uart_data_p->buffer_out_ind];uart_data_p->buffer_out_ind++;} else {while (!TXSTAbits.TRMT); // Wait for last byte to finish sendingPIE1bits.TXIE = 0;uart_data_p->buffer_out_ind = 0;uart_data_p->buffer_out_len = 0;}}void UART_Write(const char *string, char length) {while (PIE1bits.TXIE); // Wait for previous message to finish sendinguart_data_p->buffer_out_len = length;uart_data_p->buffer_out_ind = 1;for (char i = 0; i < length; i++) {uart_data_p->buffer_out[i] = string[i];}TXREG = uart_data_p->buffer_out[0]; // Put first byte in TSRPIE1bits.TXIE = 1;}void UART_WriteD(const char* string, char length) {PIE1bits.TXIE = 1;for (char i = 0; i < length; i++) {TXREG = string[i];NOP();while (!PIR1bits.TXIF);}PIE1bits.TXIE = 0;}#ifndef UART_TX_ONLYvoid UART_Recv_Interrupt_Handler() {if (PIR1bits.RCIF) { // Check if data receive flag is setchar c = RCREG;// Save received data into bufferuart_data_p->buffer_in[uart_data_p->buffer_in_write_ind] = c;if (uart_data_p->buffer_in_write_ind == MAXUARTBUF - 1) {uart_data_p->buffer_in_write_ind = 0;} else {uart_data_p->buffer_in_write_ind++;}// Store the last MAXUARTBUF values enteredif (uart_data_p->buffer_in_len_tmp < MAXUARTBUF) {uart_data_p->buffer_in_len_tmp++;} else {if (uart_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {uart_data_p->buffer_in_read_ind = 0;} else {uart_data_p->buffer_in_read_ind++;}}// Update buffer size upon receiving newline (0x0D)if (c == UART_BREAK_CHAR) {uart_data_p->buffer_in_len = uart_data_p->buffer_in_len_tmp;uart_data_p->buffer_in_len_tmp = 0;}}if (RCSTAbits.OERR == 1) {// We've overrun the USART and must resetTXSTAbits.TXEN = 0; // Kill anything currently sendingRCSTAbits.CREN = 0; // Reset UART1RCSTAbits.CREN = 1;}}char UART_Buffer_Len() {return uart_data_p->buffer_in_len;}/* Reader interface to the UART buffer, returns the number of bytes read */char UART_Read_Buffer(char *buffer) {char i = 0;while (uart_data_p->buffer_in_len != 0) {buffer[i] = uart_data_p->buffer_in[uart_data_p->buffer_in_read_ind];i++;if (uart_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {uart_data_p->buffer_in_read_ind = 0;} else {uart_data_p->buffer_in_read_ind++;}uart_data_p->buffer_in_len--;}return i;}#endif