EEVblog® Electronics Community Forum
Electronics => Microcontrollers => Topic started by: conducteur on December 11, 2018, 05:31:19 pm
-
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.pdf (http://ww1.microchip.com/downloads/en/DeviceDoc/PIC16(L)F1764-5-8-9-14-20-Pin-8-Bit-Flash-MCU-40001775D.pdf)
In 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);
}
-
The receiver should set the RC1STA.RX9 bit.
-
Microchips Code Configurator (MCC) sets RC1STA to 0xD8 in the init method. (And so the RX9 bit is also '1').
-
You are testing the rx data by reading the rx register 2 times.
Something like this would be better-
if(EUSART_Read() == 10) {
RC1STAbits.ADDEN = 0;
uint8_t rx = EUSART_Read();
if(rx == 1) led1_SetHigh();
else if(rx == 2) led1_SetLow();
}
I'm not sure if your problem matches what I think is happening, but I think the double read ends up leaving the '1' or the '2' to never be seen (you will see 1,10 or 2,10 for the addressed data). The '1' or the '2' always gets dropped when back in adden mode.
tx- A10 1 A10 2 A10 1 A10 2
rx- [addren on] A10 [adden off] 1 10 [adden on] 2 [dropped, bit9 not set] A10 [adden off] 1 10 [adden off] 2 [dropped..] >> repeat
just depends where the rx started whether you see 1's or 2's
Maybe.
-
You are testing the rx data by reading the rx register 2 times.
Something like this would be better-
if(EUSART_Read() == 10) {
RC1STAbits.ADDEN = 0;
uint8_t rx = EUSART_Read();
if(rx == 1) led1_SetHigh();
else if(rx == 2) led1_SetLow();
}
I'm not sure if your problem matches what I think is happening, but I think the double read ends up leaving the '1' or the '2' to never be seen (you will see 1,10 or 2,10 for the addressed data). The '1' or the '2' always gets dropped when back in adden mode.
tx- A10 1 A10 2 A10 1 A10 2
rx- [addren on] A10 [adden off] 1 10 [adden on] 2 [dropped, bit9 not set] A10 [adden off] 1 10 [adden off] 2 [dropped..] >> repeat
just depends where the rx started whether you see 1's or 2's
Maybe.
Thank you very much. Indeed. That's the solution....