Microchip UART 9 bit with Address detection - Page 1
Forum section: electronics / general

author: conducteur
author_rank: Regular Contributor
timestamp: December 11, 2018, 05:31:19 pm
content: 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 datasheethttp://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 intervals1) The address (10) with the 9th bit set2) 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...Code:void main(void) {// initialize the deviceSYSTEM_Initialize();// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits// Use the following macros to:// Enable the Global InterruptsINTERRUPT_GlobalInterruptEnable();// Enable the Peripheral InterruptsINTERRUPT_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 masterCode: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);}

author: andersm
author_rank: Super Contributor
timestamp: December 11, 2018, 08:37:26 pm
content: The receiver should set the RC1STA.RX9 bit.

author: conducteur
author_rank: Regular Contributor
timestamp: December 11, 2018, 11:09:22 pm
content: Microchips Code Configurator (MCC) sets RC1STA to 0xD8 in the init method. (And so the RX9 bit is also '1').

author: cv007
author_rank: Frequent Contributor
timestamp: December 12, 2018, 12:57:16 am
content: You are testing the rx data by reading the rx register 2 times.Something like this would be better-QuoteI'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 2rx- [addren on] A10 [adden off] 1 10 [adden on] 2 [dropped, bit9 not set] A10 [adden off] 1 10 [adden off] 2 [dropped..] >> repeatjust depends where the rx started whether you see 1's or 2'sMaybe.

author: conducteur
author_rank: Regular Contributor
timestamp: December 12, 2018, 10:12:22 am
content: Quote from: cv007 on December 12, 2018, 12:57:16 amThank you very much. Indeed. That's the solution....