I have set up the Enhanced USART module for a PIC18F4331 and have been trying to do a loop test. I am using MPLABX, programming in XC8, using Pickit3 for debugging and monitoring serial pins with an oscilloscope.
Without going into much detail, this is basically what I am doing:
1) In the main function, USART is transmitting values (solely for debugging) 2) Receiver Interrupt is set, when triggered it stops transmitting and jumps to ISR
This is what happens when I connect TX to RX:
1) USART transmits fine. 2) The Rx register receives a byte and sets flag RCIF after the first STOP bit, but the ISR does not trigger. Therefore the RX register overruns because it hasn't been read. The program does not go in the ISR at all.
I am thinking that the problem might be that it may not be possible to trigger an interrupt by receiving a byte while USART is transmitting? They are independent though so I don't understand why I couldn't do it.
My code:
int main(int argc, char *argv[]) {
//***********************Initializing Values****************************//
unsigned int ResultADC, FLAG;
unsigned char temp, idle; //High Byte result store, 8bits long
//***********************ADC and SPI Settings****************************//
Initialize_control(); //Initialize Control Configuration Pins
InitializeADC(); //Initialize ADC in Continuous Mode
USART_initialize(); //Initialize USART module
InitializeMasterSPI(); //Initialize SPI module
//***********************ADC Capture and Output to SPI******************//
while(1){ //While ADC buffer has something
//Enable transmission
TXREG = 0xff; //Debugger
while(!TXSTAbits.TRMT);//wait while TSR is full
TXREG = 0x0; //Debugger
while(!TXSTAbits.TRMT);//wait while TSR is full
}
return 0;
}
//////////////////INTERRUPT SERVICE ROUTINE/////////////////
static void interrupt isr(void){
//Disable Interrupt
PIE1bits.RCIE = 0;
int count;
//Read USART data
//PIR1bits.RCIF;//Data has been passed to RCREG
RX_Data[count] = RCREG; //Read RX register
count++;
//Reading RX_data clears RCIF, how to read more than 1 byte?
if (count==3){
//Use data for control
Control_Arduino(RX_Data);
count = 0;
}
PIE1bits.RCIE = 1;
}
//**********************Functions****************************//
void USART_initialize(void){
//Configuration TX and RX pins
TRISCbits.RC6 = 0; //TX output
TRISCbits.RC7 = 1; //RX input
//TXSTA: Transmit Status and Control Register
TXSTAbits.SYNC = 0; //Asynchronous mode
TXSTAbits.TX9 = 0; //8bit transmission
TXSTAbits.BRGH = 1; //Set HIGH Baud rate
TXSTAbits.TXEN = 1; //Enable transmitter
TXSTAbits.SENDB = 0; //Disable break
//RCSTA: Receive Status and Control Register
RCSTAbits.SPEN = 1; //Serial Port enabled
RCSTAbits.RX9 = 0; //8bit reception
RCSTAbits.CREN = 1; //Enables Receiver
//Test bits
// RCSTAbits.FERR = 0; //No framing error, cleared by reading
// RCSTAbits.OERR = 0; //No Overrun error, cleared by clearing CREN
//Disable receiver CREN 0
//BAUDCON Control register
BAUDCONbits.BRG16 = 1; //16bit baud rate generator
SPBRG = 0xCF; // Set to 19200 baud rate, 12Mhz, High Speed, BRG16
//Test bits
// BAUDCONbits.RCIDL = 0; //Receive in progress
// USART interrupts configuration
RCONbits.IPEN = 1; // ENABLE interrupt priority
INTCONbits.GIE = 1; // ENABLE interrupts
INTCONbits.PEIE = 1; // ENable peripheral interrupts.
PIE1bits.RCIE = 1; // ENABLE USART receive interrupt
PIE1bits.TXIE = 0; // disable USART TX interrupt
}