View previous topic :: View next topic |
Author |
Message |
rnd87
Joined: 10 Aug 2010 Posts: 3
|
Connection or source code problem?? |
Posted: Wed Aug 11, 2010 1:45 am |
|
|
I have used these codes for RS485 communication.(Based on RS485.c)But I couldn't understand where is the problem source code or hardware;
Master RS485 codes;
Code: |
#include <16F877.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock = 4000000)
#define RS485_ID 0x10 // The device's RS485 master address
#define RS485_USE_EXT_INT TRUE // Select asynchronous serial interrupt
#include <RS485.c>
int buffer[40];
int next_in = 0;
int next_out = 0;
#INT_RDA
void serial_isr()
{
int t;
buffer[next_in] = fgetc(RS485);
t = next_in;
next_in = (next_in+1) % sizeof(buffer);
if(next_in == next_out)
next_in=t; // Buffer full !!
}
void main(void)
{
int j;
int data_received[32];
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
rs485_init();
while(1)
{
rs485_wait_for_bus(FALSE);
//send address=0x11, length = 1, msg=3
//the led does blink here
if(rs485_send_message(0x11, 1, 3))
{
output_low(PIN_B1);
delay_ms(100);
output_high(PIN_B1);
delay_ms(100);
output_low(PIN_B1);
}
delay_ms(5);
if(rs485_get_message(data_received, FALSE))
{
for(j=0; j<3; ++j)
data_received[j] = buffer[j];
if(data_received[2]==6) //if slave receives the number 3 successfully
{ //slave will send back number 6
output_low(PIN_B2);
delay_ms(100);
output_high(PIN_B2);
delay_ms(100);
output_low(PIN_B2);
}
}
}
}
|
Slave RS485 codes;
Code: |
#include <16F877.h>
#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock = 4000000)
#define RS485_ID 0x11 // The device's RS485 slave address or ID
#define RS485_USE_EXT_INT TRUE // Select asynchronous serial interrupt
#include <RS485.c>
int buffer[40];
int next_in = 0;
int next_out = 0;
#INT_RDA
void serial_isr()
{
int t;
buffer[next_in] = fgetc(RS485);
t = next_in;
next_in = (next_in+1) % sizeof(buffer);
if(next_in == next_out)
next_in=t; // Buffer full !!
}
void main(void)
{
int i=1;
int j;
int data_received[32];
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
rs485_init();
while(1)
{
if(rs485_get_message(data_received, TRUE))
{
for(j=0; j<3; ++j)
data_received[j] = buffer[j];
if(data_received[0] == 0x11)
{
output_low(PIN_B2);
delay_ms(100);
output_high(PIN_B2);
delay_ms(100);
output_low(PIN_B2);
rs485_wait_for_bus(FALSE);
//after receive successfully, respond to master by sending number 6
if(rs485_send_message(0x10, 1, 6))
{
output_low(PIN_B1);
delay_ms(100);
output_high(PIN_B1);
delay_ms(100);
output_low(PIN_B1);
}
}
}
}
}
|
And here is my hadware;
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Aug 11, 2010 2:04 am |
|
|
You didn't mention an actual problem. Is it a guessing game? |
|
|
rnd87
Joined: 10 Aug 2010 Posts: 3
|
|
Posted: Wed Aug 11, 2010 3:18 am |
|
|
OK sorry, My problem is about communication. I couldn't get any result at slave side. I couldn't see any changing at RB1 and RB2. The message has not been send by these system. |
|
|
jbmiller
Joined: 07 Oct 2006 Posts: 73 Location: Greensville,Ontario
|
|
Posted: Wed Aug 11, 2010 5:09 am |
|
|
I don't see any bias resistors in the RS-485, or a common ground....in the schematic. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Aug 11, 2010 8:53 am |
|
|
Interrupts only work when you are using the hardware UART for receiving and transmitting the data. The hardware UART is connected to pins C6/Tx and C7/Rx, not to your B0 and B3. |
|
|
|