Complete the code to send a byte over serial communication.
void sendByte(unsigned char data) {
while(![1]); // Wait until transmit buffer is empty
TXREG = data;
}The TXIF flag indicates the transmit buffer is empty and ready to send data.
Complete the code to initialize serial communication at 9600 baud rate.
void initSerial() {
SPBRG = [1]; // Set baud rate generator
TXSTA = 0x24; // Enable transmitter, BRGH=1
RCSTA = 0x90; // Enable serial port and continuous receive
}For 9600 baud with 4MHz clock and BRGH=1, SPBRG is set to 25.
Fix the error in the code to correctly receive a byte over serial communication.
unsigned char receiveByte() {
while(![1]); // Wait until data is received
return RCREG;
}RCIF flag indicates that a byte has been received and is ready to read.
Complete the code to handle overrun error when receiving a byte over serial communication.
unsigned char safeReceive() {
if([1]) {
[2] = 0;
[2] = 1;
}
while(!RCIF);
return RCREG;
}The OERR (overrun error) flag must be cleared by momentarily disabling continuous receive enable (CREN).
Complete the code to enable transmit interrupt for serial communication.
void enableTXInt() {
INTCONbits.[1] = 1;
INTCONbits.[2] = 1;
PIE1bits.[3] = 1;
}INTCONbits.GIE (global), INTCONbits.PEIE (peripheral), and PIE1bits.TXIE (TX specific) must all be set.