View previous topic :: View next topic |
Author |
Message |
arloedx
Joined: 21 Aug 2009 Posts: 15 Location: Texas
|
9th bit interrupt |
Posted: Tue Sep 08, 2009 1:38 pm |
|
|
Hi,
I am communicating between two PIC18f252 uControllers. They are connected together with two max485 chips. I can send and receive data without problems. Right now the slave is simply oscillating an LED. If the Master sends data it sets off an interrupt in the slave and it accepts the data and shows it on an LCD without stopping the oscillation of that LED. There will eventually be more devices on the network therefore I need to assign each uController an address. I plan to first transmit a destination address from the master to one of the slaves, the slave acknowledges then accepts the data.
My question is: How can I let the slave device know that an address is being transmitted and not data. I was thinking of setting up an interrupt that checks the 9th bit of data, and if it is set to a '1' then it is an address, else it is data. I know this kind of configuration exists but I don't know how to implement it on the master or the slave. Please help. THank you. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
arloedx
Joined: 21 Aug 2009 Posts: 15 Location: Texas
|
|
Posted: Tue Sep 08, 2009 2:13 pm |
|
|
I see from the driver the following line which I think sets the 9th bit.
Code: |
fputc((int16)0x100|rs485_id, RS485_CD);
|
I'm not entirely sure how that works, nor do I see how the receiver is supposed to interpret that data. Can somebody please explain? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 08, 2009 5:03 pm |
|
|
If you look in rs485.c, you will see lines like this:
Code: |
setup_uart(UART_ADDRESS);
|
This sets the Address Enable bit (ADDEN) in the RCSTA register
in the PIC. Look in the PIC data sheet to see what that means:
Quote: |
bit 3 ADDEN: Address Detect Enable bit
Asynchronous mode 9-bit (RX9 = 1):
1 = Enables address detection, enable interrupt and load of the receive
buffer when RSR<8> is set.
0 = Disables address detection, all bytes are received, and ninth bit can
be used as parity bit. |
It enables hardware Address Detection in the UART. That mode can
be disabled, and the UART put back into normal mode by this line:
Code: | setup_uart(UART_DATA); |
Now look at the #int_rda code in the rs485.c driver. Specifically look
at the state machine code to see how the driver handles the address
and data bytes in a message:
Code: | switch(state) {
case 0: // Get 'from' address
temp_ni=rs485_ni;
source=b;
cs=b;
rs485_add_to_temp(source);
break;
case 1: // Get 'to' address
to=b;
#if (getenv("AUART")&&(RS485_USE_EXT_INT==FALSE))
setup_uart(UART_DATA);
#endif
break; |
Notice that after it gets both address bytes, it switches the UART back
into normal "Data" mode with this statement:
Code: | setup_uart(UART_DATA); |
Then it will receive "data" bytes. These include the Length, message
data bytes, and the checksum byte. When it's done getting those bytes,
it switches the UART back into "Address" mode again, to be ready for the
next packet. That's shown in the remaining switch-case states, which I
didn't post. Look in rs485.c to see them.
The rs485.c driver handles all this for you. You get the received message
by calling the rs485_get_message() routine in the rs485.c driver. |
|
|
|