Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 121 → Rev 122

/PIC Stuff/PIC_27J13/uart.c/uart.c
9,32 → 9,33
// UART1 TX RC6
// UART1 RX RC7
 
TRISCbits.TRISC6 = 0; // Tx pin set to output
TRISCbits.TRISC7 = 1; // Rx pin set to input
TRISCbits.TRISC6 = 0; // Tx pin set to output
TRISCbits.TRISC7 = 1; // Rx pin set to input
 
BAUDCON1bits.BRG16 = 0; // 8-bit baud rate generator
SPBRG1 = 25; // Set UART speed to 115200 baud
TXSTA1bits.BRGH = 1; // High speed mode
TXSTA1bits.SYNC = 0; // Async mode
RCSTA1bits.SPEN = 1; // Serial port enable
TXSTA1bits.TX9 = 0; // 8 bit transmission
RCSTA1bits.RX9 = 0; // 8 bit reception
RCSTA1bits.CREN = 1; // Continuous receive mode
SPBRG1 = 25; // Set UART speed to 115200 baud
TXSTA1bits.BRGH = 1; // High speed mode
TXSTA1bits.SYNC = 0; // Async mode
RCSTA1bits.SPEN = 1; // Serial port enable
TXSTA1bits.TX9 = 0; // 8 bit transmission
RCSTA1bits.RX9 = 0; // 8 bit reception
RCSTA1bits.CREN = 1; // Continuous receive mode
 
#ifdef _DEBUG // In debug mode we want to have TXEN constantly enabled
TXSTA1bits.TXEN = 1; // TX is always enabled
PIE1bits.TX1IE = 0; // Disable TX interrupt
TXSTA1bits.TXEN = 1; // TX is always enabled
PIE1bits.TX1IE = 0; // Disable TX interrupt
#else
TXSTA1bits.TXEN = 0; // Enable transmission
PIE1bits.TX1IE = 1; // Enable TX interrupt
TXSTA1bits.TXEN = 0; // Enable transmission
PIE1bits.TX1IE = 1; // Enable TX interrupt
#endif
 
PIE1bits.RC1IE = 1; // Enable RX interrupt
PIE1bits.RC1IE = 1; // Enable RX interrupt
 
// Initialize the buffer that holds UART messages
uart_1_data.buffer_in_read_ind = 0;
uart_1_data.buffer_in_write_ind = 0;
uart_1_data.buffer_in_len = 0;
uart_1_data.buffer_in_len_tmp = 0;
}
 
//void uart_2_init() {
55,29 → 56,41
 
void UART1_Recv_Interrupt_Handler() {
unsigned char c;
if (PIR1bits.RC1IF) { // Check if data receive flag is set
if (uart_1_data.buffer_in_len == MAXUARTBUF-1) {
TXSTA1bits.TXEN = 0; // Kill anything currently sending
DBG_PRINT_UART("UART1: buffer overflow\r\n");
c = RCREG1; // Read RCREG to clear it
if (PIR1bits.RC1IF) { // Check if data receive flag is set
c = RCREG1;
 
// Save received data into buffer
uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = c;
if (uart_1_data.buffer_in_write_ind == MAXUARTBUF - 1) {
uart_1_data.buffer_in_write_ind = 0;
} else {
// Save received data into buffer
uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = RCREG1;
if (uart_1_data.buffer_in_write_ind == MAXUARTBUF-1) {
uart_1_data.buffer_in_write_ind = 0;
uart_1_data.buffer_in_write_ind++;
}
 
// Store the last MAXUARTBUF values entered
if (uart_1_data.buffer_in_len_tmp < MAXUARTBUF) {
uart_1_data.buffer_in_len_tmp++;
} else {
if (uart_1_data.buffer_in_read_ind == MAXUARTBUF - 1) {
uart_1_data.buffer_in_read_ind = 0;
} else {
uart_1_data.buffer_in_write_ind++;
uart_1_data.buffer_in_read_ind++;
}
uart_1_data.buffer_in_len++;
}
 
// Update buffer size upon receiving newline (0x0D)
if (c == UART1_BREAK_CHAR) {
uart_1_data.buffer_in_len = uart_1_data.buffer_in_len_tmp;
uart_1_data.buffer_in_len_tmp = 0;
}
}
 
if (RCSTAbits.OERR == 1) {
// We've overrun the USART and must reset
RCSTA1bits.CREN = 0; // Reset UART1
RCSTA1bits.CREN = 0; // Reset UART1
RCSTA1bits.CREN = 1;
TXSTA1bits.TXEN = 0; // Kill anything currently sending
DBG_PRINT_UART("UART1: overrun\r\n");
DBG_PRINT_UART("UART1: (ERROR) overrun\r\n");
TXSTA1bits.TXEN = 0; // Kill anything currently sending
}
}
 
99,8 → 112,8
TXREG1 = uart_1_data.buffer_out[uart_1_data.buffer_out_ind];
uart_1_data.buffer_out_ind++;
} else {
while (!TXSTA1bits.TRMT); // Wait for last byte to finish sending
TXSTA1bits.TXEN = 0; // End transmission and disable TX interrupt
while (!TXSTA1bits.TRMT); // Wait for last byte to finish sending
TXSTA1bits.TXEN = 0; // End transmission and disable TX interrupt
uart_1_data.buffer_out_ind = 0;
uart_1_data.buffer_out_len = 0;
}
108,35 → 121,39
 
void UART1_WriteS(const rom char *fmt, ...) {
va_list args;
while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
va_start(args, fmt);
vsprintf((char *)uart_1_data.buffer_out, fmt, args);
vsprintf((char *) uart_1_data.buffer_out, fmt, args);
va_end(args);
uart_1_data.buffer_out_len = strlen((char *)uart_1_data.buffer_out);
uart_1_data.buffer_out_len = strlen((char *) uart_1_data.buffer_out);
uart_1_data.buffer_out_ind = 1;
TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
TXSTA1bits.TXEN = 1; // Begin transmission
TXSTA1bits.TXEN = 1; // Begin transmission
}
 
void UART1_WriteB(const char *msg, unsigned char length) {
unsigned char i;
while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
uart_1_data.buffer_out_len = length;
uart_1_data.buffer_out_ind = 1;
for (i = 0; i < length; i++) {
for (i = 0; i < length; i++) {
uart_1_data.buffer_out[i] = msg[i];
}
TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
TXSTA1bits.TXEN = 1; // Begin transmission
TXSTA1bits.TXEN = 1; // Begin transmission
}
 
unsigned char UART1_Buffer_Len() {
return uart_1_data.buffer_in_len;
}
 
/* Reader interface to the UART buffer, returns the number of bytes read */
unsigned char UART1_Read(char *buffer) {
unsigned char UART1_Read_Buffer(unsigned char *buffer) {
unsigned char i = 0;
while (uart_1_data.buffer_in_len != 0) {
buffer[i] = uart_1_data.buffer_in[uart_1_data.buffer_in_read_ind];
i++;
if (uart_1_data.buffer_in_read_ind == MAXUARTBUF-1) {
if (uart_1_data.buffer_in_read_ind == MAXUARTBUF - 1) {
uart_1_data.buffer_in_read_ind = 0;
} else {
uart_1_data.buffer_in_read_ind++;