I have two PIC 16F1765 that need to communicate over UART. It's my intention to create a small network using RS485 tranceivers. (one master, multiple slaves).
I use MCC to configure the peripherals. They're both configured to use 9 bits instead of 8, because if you set the 9th bit, the mcu should be able to detect that addresses when bit 9 is set and ignore other bytes sent when the 9th bit is zero and the RC1STAbits.ADDEN = 1; (Address detection enable).
The explenation for the address detection is on page 438 in the datasheet
http://ww1.microchip.com/downloads/en/DeviceDoc/PIC16(L)F1764-5-8-9-14-20-Pin-8-Bit-Flash-MCU-40001775D.pdfIn my test setup the master sends on regular time intervals
1) The address (10) with the 9th bit set
2) a command byte (alternately 1 or 2) with the 9th bit cleared.
This should turn an LED ON (1) or OFF (2). Transmission of these bytes works just fine, I have checked that using my oscilloscope.
I have set a breakpoint (see code) to read the received byte from the UART. It always returns the value 10, not 1 or 2 and the LED doesn't flash either.
After receiving the address i reset the RC1STAbits.ADDEN bit so that I also get the bytes where the 9th bit isn't set, so that is what i don't understand why i don't get those bytes with value 1 or 2...
void main(void) {
// initialize the device
SYSTEM_Initialize();
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
while (1) {
RC1STAbits.ADDEN = 1;
TX1STAbits.TX9D = 1;
if ((EUSART_Read()) == 10) {
RC1STAbits.ADDEN = 0;
if ((test = EUSART_Read()) == 1) { //***************************** breakpoint here ***************************
led1_SetHigh();
}
if((test= EUSART_Read()== 2)){
led1_SetLow();
}
}
}
}
for completeness, this is the code of the master
TX1STAbits.TX9 = 1;
while (1) {
TX1STAbits.TX9D = 1;
EUSART_Write(10);
TX1STAbits.TX9D = 0;
__delay_ms(5);
EUSART_Write(1);
__delay_ms(500);
TX1STAbits.TX9D = 1;
EUSART_Write(10);
TX1STAbits.TX9D = 0;
__delay_ms(5);
EUSART_Write(2);
__delay_ms(500);
}