Author Topic: Microchip UART 9 bit with Address detection  (Read 2242 times)

0 Members and 1 Guest are viewing this topic.

Offline conducteurTopic starter

  • Regular Contributor
  • *
  • Posts: 121
  • Country: be
Microchip UART 9 bit with Address detection
« 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

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...


Code: [Select]
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

Code: [Select]

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);
       
       
    }

« Last Edit: December 11, 2018, 11:09:56 pm by conducteur »
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: Microchip UART 9 bit with Address detection
« Reply #1 on: December 11, 2018, 08:37:26 pm »
The receiver should set the RC1STA.RX9 bit.

Offline conducteurTopic starter

  • Regular Contributor
  • *
  • Posts: 121
  • Country: be
Re: Microchip UART 9 bit with Address detection
« Reply #2 on: December 11, 2018, 11:09:22 pm »
Microchips Code Configurator (MCC) sets RC1STA to 0xD8 in the init method. (And so the RX9 bit is also '1').
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 828
Re: Microchip UART 9 bit with Address detection
« Reply #3 on: December 12, 2018, 12:57:16 am »
You are testing the rx data by reading the rx register 2 times.

Something like this would be better-
Quote
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.
 

Offline conducteurTopic starter

  • Regular Contributor
  • *
  • Posts: 121
  • Country: be
Re: Microchip UART 9 bit with Address detection
« Reply #4 on: December 12, 2018, 10:12:22 am »
You are testing the rx data by reading the rx register 2 times.

Something like this would be better-
Quote
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....
« Last Edit: December 12, 2018, 10:43:08 am by conducteur »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf