CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

is my RS485 connection wrong?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kitty
Guest







is my RS485 connection wrong?
PostPosted: Sat Feb 25, 2006 7:52 pm     Reply with quote

I am using MPLAP IDE v6.60, 18F458, and try to using testing code to try rs485 connection between 2 PIC that is master and slave with MAX485.

my connection pin as below:-

PIN_C7 to RO
PIN_C6 to DI
PIN_B4 to DE
PIN_B5 to RE
ground to pin 5
+5 to pin Vcc

pin A of max485(master) to pin A of max485(slave)
pin B of max485(master) to pin B of max485(slave)

after compile and write to PICs, nothing show at LCD.
Why?

Code:

//master
#include<18f458.h>

#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock = 20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=PIN_B4, stream=RS485)
#define RS485_ID           0x10        // The device's RS485 master address
#define RS485_USE_EXT_INT    TRUE        // Select asynchronous serial interrupt

#include<RS485.c>
#include <lcd.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];
   lcd_init();
//   lcd_putc("\fSystem ready.\n");     
 
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);


   rs485_init();

   while(1)
   {

      rs485_wait_for_bus(FALSE);

                   //send address=0x11, length = 1, msg=3
                               
      if(rs485_send_message(0x11, 1, 3))
      {
 
         printf("\ftest 1");
         delay_ms(100);
      }

      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
       
            printf(lcd_putc, "\ftest");
            delay_ms(100);

         }
      }

         }
}


//slave
#include<18f458.h>

#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock = 20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=PIN_B4, stream=RS485)
#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>
#include <lcd.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];
   lcd_init();

   
   enable_interrupts(INT_RDA);
      enable_interrupts(GLOBAL);

   rs485_init();
lcd_putc("\fSstem ready.\n");
   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)
         {

            printf("\ftest");
            delay_ms(100);
       

            rs485_wait_for_bus(FALSE);


            //after receive successfully, respond to master by sending number 6
            if(rs485_send_message(0x10, 1, 6))
            {

           
               printf("\ftest");
               delay_ms(100);

           
            }
         }
      }

         }
}
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sat Feb 25, 2006 10:19 pm     Reply with quote

Quote:

#define RS485_USE_EXT_INT TRUE // Select asynchronous serial interrupt

This directive should be used if you want to use the EXTERNAL INTERRUPT PIN as software UART receiver.

This directive also override the pins selected in the previous #use rs232(.....) directive.

But your intention is to use the built-in hardware UART because you selected the dedicated UART pins:(in bold)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=PIN_B4, stream=RS485)

Hence you need to use this directive, (or not use at all) because you DO NOT want to use the
EXTERNAL INTERRUPT PIN as UART receiver.

#define RS485_USE_EXT_INT FALSE // Select asynchronous serial


Quote:

PIN_C7 to RO
PIN_C6 to DI
PIN_B4 to DE
PIN_B5 to RE
ground to pin 5
+5 to pin Vcc


This is OK but DE and RE must be tied together or keep RE low.


Humberto
kitty
Guest







rs485
PostPosted: Sun Feb 26, 2006 9:54 pm     Reply with quote

Thanks, humberto.

I change my code as you told :

Code:

//master
#include<18f458.h>

#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock = 20000000)
#define RS485_ID  0x10        // The device's RS485 master address
#define RS485_USE_EXT_INT    FALSE     

#include<RS485.c>
#include <lcd.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];
   lcd_init();
   
 
 //  enable_interrupts(INT_RDA);
 //  enable_interrupts(GLOBAL);


   rs485_init();

   while(1)
   {

     rs485_wait_for_bus(FALSE);
                                 
      if(rs485_send_message(0x11, 1, 3)) 
      {
          lcd_putc("\fsend 1");
         delay_ms(100);
      }

      delay_ms(5);


    }
}


//slave
#include<18f458.h>

#fuses HS, NOWDT, NOLVP, NOBROWNOUT, NOPROTECT, PUT
#use delay(clock = 20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=PIN_B4, stream=RS485)
#define RS485_ID  0x11      // The device's RS485 slave address or ID
#define RS485_USE_EXT_INT FALSE        // Select asynchronous serial interrupt

#include<RS485.c>
#include <lcd.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];
   lcd_init();

   
  // enable_interrupts(INT_RDA);
   //   enable_interrupts(GLOBAL);

   rs485_init();

   while(1)
   {

      if(rs485_get_message(data_received, TRUE))
      {
      
        data_received[0] = buffer[0];
      data_received[1] = buffer[1];
      data_received[2] = buffer[2];
      data_received[3] = buffer[3];
printf(lcd_putc,"\f%d %d %d %d",data_received[0],data_received[1],data_received[2],data_received[3]);

      }

    }
}





When those PICs run, master keep on sending to slave. But I am trying to retrive data at slave PIC using data_receive[0] to [3], I think this is not the right way because non of them show number 3 that send from master.

Very sorry, I try to read all post from search rs485 but I can solve it.

Please help & thanks.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Feb 27, 2006 6:20 am     Reply with quote

The system is behaving according to the code that has written.

1) I don't see any code in your Slave that will send back data to the Master.

2) I don't see any code in your Master to show the retrieved string, something like this:

if(rs485_get_message(data_received, FALSE))

Does your Slave is receiving the data packet ?

Humberto
kitty
Guest







PostPosted: Mon Feb 27, 2006 11:02 pm     Reply with quote

Hi, thank for reply.

In fact my master can send data because I connected one led at TX line and It blink when sending data.

I only sending data number 0 to 15 from master to slave.

Code:

//master pic
   while(1)
   {

     rs485_wait_for_bus(FALSE);
                                 
      if(rs485_send_message(0x11, 20, j))
      {
          printf(lcd_putc,"\fsend %d",j);
         delay_ms(100);
      }

      delay_ms(1000);
      if(j<15)j++;
      else j=0;

    }


but I do not know the right way to reveice data at slave site. I try the code as below



Code:

//slave pic

  while(1)
   {

       if(rs485_get_message(data_received,FALSE)){
      for(j=0;j<data_received[1];++j)
         fputc(data_received[j+2],RS485);

         l=data_received;
   printf(lcd_putc,"\f%d %d %d ",data_received[0],
data_received[1],data_received[2]);

      }

    }


lcd at slave pin doesnt get the right massage from master.

I do not know what to do now?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group