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

Connection or source code problem??

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



Joined: 10 Aug 2010
Posts: 3

View user's profile Send private message

Connection or source code problem??
PostPosted: Wed Aug 11, 2010 1:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 2:04 am     Reply with quote

You didn't mention an actual problem. Is it a guessing game?
rnd87



Joined: 10 Aug 2010
Posts: 3

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 3:18 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 5:09 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 11, 2010 8:53 am     Reply with quote

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