Blame | Last modification | View Log | Download | RSS feed
#include "DEFINES.h"#include "EUSART.h"#include "INTERRUPTS.h"static volatile uint8_t txBuffer[UART_TX_BUFFER_SIZE];static volatile uint8_t txBufferSize = 0;static volatile uint8_t txBufferIndex = 0;static volatile uint8_t rxBuffer[UART_RX_BUFFER_SIZE];static volatile uint8_t rxBufferSize = 0;static volatile uint8_t rxBufferIndex = 0;void UART_Init() {UART_TX_TRIS = 0;UART_RX_TRIS = 1;BAUDCONbits.BRG16 = 0; // 8-bit baud rate generationSPBRG = 64; // Baud rate of 19.2kTXSTAbits.BRGH = 1; // High speed modeTXSTAbits.SYNC = 0; // Async modeRCSTAbits.SPEN = 1; // Serial port enableTXSTAbits.TX9 = 0; // 8 bit transmissionRCSTAbits.RX9 = 0; // 8 bit receptionTXSTAbits.TXEN = 1; // Transmission enabledRCSTAbits.CREN = 1; // Reception enabledPIE1bits.TXIE = 0; // TX interrupt starts disabledPIE1bits.RCIE = 1; // RX interrupt starts enabled}void UART_Write(uint8_t *msg, uint8_t length) {// Check to make sure there is enough space in buffer for messagelength = (length > UART_TX_BUFFER_SIZE) ? UART_TX_BUFFER_SIZE : length;// Wait for previous message to finish sendingwhile (PIE1bits.TXIE);txBufferSize = length;txBufferIndex = 1;for (uint8_t i = 0; i < length; i++) {txBuffer[i] = msg[i];}TXREG = txBuffer[0];PIE1bits.TXIE = 1;}void UART_TX_Interrupt_Handler() {if (txBufferIndex != txBufferSize) {// Transmit next byte in the bufferTXREG = txBuffer[txBufferIndex];txBufferIndex++;} else {// Wait for last byte to finish sendingwhile (!TXSTAbits.TRMT);PIE1bits.TXIE = 0;txBufferSize = 0;txBufferIndex = 0;}}void UART_RX_Interrupt_Handler() {if (PIR1bits.RCIF) {uint8_t c = RCREG;// Store received byte into buffer and increment write locationrxBuffer[rxBufferIndex] = c;if (rxBufferIndex == UART_RX_BUFFER_SIZE - 1) {rxBufferIndex = 0;} else {rxBufferIndex++;}// Increment received byte countif (rxBufferSize < UART_RX_BUFFER_SIZE) {rxBufferSize++;}}// If UART overrun is detected, reset moduleif (RCSTAbits.OERR) {TXSTAbits.TXEN = 0;RCSTAbits.CREN = 0;RCSTAbits.CREN = 1;}}uint8_t UART_Read(uint8_t *buffer) {// Return values in RX bufferuint8_t size = rxBufferSize;for (uint8_t i = 0; i < size; i++) {buffer[i] = rxBuffer[i];}return size;}void UART_Reset_RX() {rxBufferIndex = 0;rxBufferSize = 0;}