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

rs485

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



Joined: 19 Dec 2005
Posts: 2

View user's profile Send private message

rs485
PostPosted: Mon Dec 19, 2005 10:03 am     Reply with quote

Hello
I am trying implement a rs485 network using CCS rs485.c file. I use adm485 tranceiver. I believe that I have the hardware setup correct. My code compiles OK but both the master and slave could not receive anything. The master transmits ok. (compiler version 3.222, picdem2 plus boards). Thank you



Code:

//master
#include<18f452.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
#include<18f452.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);
            }
         }
      }

         }
}



Last edited by rutnn33 on Mon Dec 19, 2005 10:47 am; edited 1 time in total
John Morley



Joined: 09 Aug 2004
Posts: 97

View user's profile Send private message

PostPosted: Mon Dec 19, 2005 10:46 am     Reply with quote

Hi,

I'd write a very simple test program to test your hardware that uses getc and putc to echo any characters received. Put these calls inside a while loop in Main(), and don't use the serial interrupt until this is working. Also, do you have an RS-232 to RS-485 converter? While I was developing an RS-485 application, I found it very useful to send and receive data using a PC (using Hyperterminal) with the PIC.

John
_________________
John Morley
Humberto



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

View user's profile Send private message

PostPosted: Mon Dec 19, 2005 10:48 am     Reply with quote

#include<RS485.c>

This included general RS485.C reference file has the Pre-processor directives:

#use rs232(baud=9600, xmit=RS485_TX_PIN, rcv=RS485_RX_PIN, enable=RS485_ENABLE_PIN, bits=9, long_data, errors, stream=RS485)

It is your job to re-check that the Pins definitions contained on there are in coincidence with the hardware UART of the PIC used in your application.

Humberto
rutnn33



Joined: 19 Dec 2005
Posts: 2

View user's profile Send private message

PostPosted: Mon Dec 19, 2005 10:51 am     Reply with quote

Thank you.

I do not have a rs232-485 converter. "I will try to use getc". I have tried simple getchar and putchar and it works.
Thank you John.


I have checked the pins and they are correct Humberto. Thank you
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