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 CCS Technical Support

1 o
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Sat May 07, 2011 11:51 pm     Reply with quote

I have designed and made a project similar to yours, that uses 1 master and several slaves connected through a RS 485 network.

I used a protocol that I designed myself.

In my protocol I had some general rules.

1. Only one node is allowed to start a conversation, it is usually the master that initiates the communication with a particular slave.

2. Slaves cannot start a conversation by themselves.

3. A checksum data must be included in every packet.

4. Addresses for each node is preset either by DIP switches or in the EEPROM, you must ensure each node has an unique address.

I suggest you make the actual hardware (if you have not done) for, one master, one slave and a RS232-RS485 converter for the PC.

You can search the internet for RS485 resources, there are many projects there.

thanks
a
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Sun May 08, 2011 8:32 am     Reply with quote

Hi,

On it's current trajectory, I don't think there is any hope that you will be able to successfully complete this project. Several people who have actually completed projects like this have made a number of suggestions to help get you started on this project, but for whatever reason, you seem to think that writing software is a simple matter of cutting and pasting code you find on the internet.

In your most recent post, you seem to have changed PIC's in mid-stream from the 16F887A to the 18F542?? You have dropped 'RS485.c', only to replace it with the mysterious 'MAX485.c', and your code has a serial interrupt handler (Int_RDA), but you are not enabling this interrupt..... I could go on, but I think you get the idea!

Unless you understand what each line in your code is doing (and I think it's clear you don't!), you will never get a project like this to work correctly. I'm really not trying to be overly critical here, I'm really just trying to point out that your code development strategy seems to run counter to any successful effort I've ever seen.

As far as I can tell, the ONLY way you are going to make this work is to go back to square 1. You need to connect a PIC to your PC (Computer -> MAX232 IC -> PIC) and write the code to allow the PIC to receive serial data from the PC. As a start, simply read a character, and echo it back to the PC. Once you can do that, you need to refine the code to receive complete command "packets" from the PC. Again, make your life easy by defining unique "start" and "end" characters. This part needs to be 100% reliable, and robust. Once you can do all this, it's a simple matter to add addressing to the command packet, so the PIC does not respond unless the packet address is equal to the PIC address (set with DIP switches, or hard-wired in EEPROM). Once that is done, you can go back to the RS-485 topology, and add multiple nodes to the network.

Making small networks is actually pretty fun. My home has a total of 20 PIC-based temperature/humidity sensors distributed throughout the house that are read by my home automation system. I can read them locally via a tablet computer, or away from home via the internet. Coding can be hard work, but it is also very satisfying to work through these types of problems and end up with a functional system. For example, I was in Vietnam last month, and I was able to monitor the water temperature in my home spa. That alone made the whole project worthwhile!

John
vortexe9000



Joined: 07 Jun 2010
Posts: 50
Location: Banned - spammer

View user's profile Send private message

dtry
PostPosted: Sun May 08, 2011 11:08 am     Reply with quote

sqdzsds
_________________
Banned for spamming his own posts


Last edited by vortexe9000 on Sun Feb 19, 2012 7:47 am; edited 2 times in total
vortexe9000



Joined: 07 Jun 2010
Posts: 50
Location: Banned - spammer

View user's profile Send private message

PostPosted: Sun May 08, 2011 11:42 am     Reply with quote

sdferds
_________________
Banned for spamming his own posts


Last edited by vortexe9000 on Sun Feb 19, 2012 7:34 am; edited 1 time in total
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Sun May 08, 2011 6:47 pm     Reply with quote

Hi,

I think you will find that some of the people offering help here on the CCS forums are some of the most generous people you will find on the net. It amazes me how some folks will go way out of their way to help others to learn about PIC micros, and CCS 'C'. At the same time, no one here likes to simply give away whole programs because the recipient learns nothing in the process. In short, you have to take the initiative and do the work, and not expect others to do it for you.

Here is my suggestion: totally scrap 'RS485.c' or 'MAX485.c' and do the network communications yourself. Again (3rd time now!), define your own protocol, and write the code to receive the entire command string, and then act upon it. If your network traffic is relatively infrequent, you can use a linear buffer to do this, otherwise, you'll need a continuously running circular buffer.

Here is an example of the interrupt routine I use to catch an incoming command packet:

Code:

//-----< Serial Port Definition >-----
#use rs232(baud=9600, xmit=Tx_Out, rcv=Rx_In, Enable = RS485_Enable, ERRORS, stream = Network)

//-----< RS232 Packet definitions >-----
int RX_Command_Ready;      // TRUE If a receive packet is ready

#define RX_SIZE 10            // RS232 buffer for serial reception
char RxBuffer[RX_SIZE];       // RS232 serial RX buffer
int Index = 0;              // RS232 RX data IN index

#INT_RDA   // Interrupt driven RS232 routine
void rs232_isr(void)
{
   char temp;   // local data storage
   
   temp = fgetc(Network);   // get rx data

   // If we got a '#', then the command is beginning.
   if (temp == '#')
      {   
         Index = 0;
         RxBuffer[Index] = temp;
         Index++;
         return;
      }
   // If we got a CR, then the command is completed.
   if (temp == CR)
      {
         RxBuffer[Index] = temp;
         RX_Command_Ready = TRUE;
         return;
      }

   // Save the character to the receive buffer.
   RxBuffer[Index]=temp;

   // Check for buffer overflow.
   if ( Index >= (RX_SIZE - 1) )
      Index = 0;
   else
      Index++;
}


As you can see, there is a 10 character buffer called 'RxBuffer'. Elements in the buffer are indexed by the iIndex variable. As soon as the '#' character is received, the buffer index is reset to the beginning, and as soon as the 'CR' is received, a flag (Rx_Command_Ready) is set to flag the main program that a complete command string has been received.

This code is from an older project, and I have since refined the serial ISR with a 'state machine' to reject any characters until the '#' is received, but that only makes the routine more efficient, this one does work.

So, in Main() you'll need a continuous loop check the state of the received data flag. When it goes true, you'll need to (1) disable interrupts, (2) test the received packet address against the node address, (3) take action (set a relay, etc.) if you have an address match, (4) reset the received data flag, and (5) re-enable interrupts.

As I said, this will work great for networks without a large volume of traffic, otherwise you'll need to implement a circular buffer.

OK, a solution has been pretty much handed to you now, so you'll need to get to work if you accept this approach.

Good Luck,

John
vortexe9000



Joined: 07 Jun 2010
Posts: 50
Location: Banned - spammer

View user's profile Send private message

PostPosted: Sun May 08, 2011 11:30 pm     Reply with quote

sqdfsdcsfcv
_________________
Banned for spamming his own posts


Last edited by vortexe9000 on Sun Feb 19, 2012 7:35 am; edited 1 time in total
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon May 09, 2011 12:50 pm     Reply with quote

Hi Again,

Here is a snippet from an improved serial interrupt handler. The use of a 'state machine' prevents anything from getting buffered unless it is the desired data!

Code:

   RcvChar = fgetc(Network);

   switch (RxState)
   {
      Case 0:
         if (RcvChar == '#')
            RxState++;   //"#" received, so RxState = 1
         break;

      Case 1:
         if (RcvChar == 'N')
            RxState++;   //"N" received, so RxState = 2
         else
            RxState=0;   //wrong character received, so we reset RxState
         break;


With this code you will, of course, need to store all characters while RxState >0, check for buffer overflow (or wrap if its a circular buffer), and look for the <CR> character which signals RxState=3 and a valid received command.

John
temtronic



Joined: 01 Jul 2010
Posts: 9658
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon May 09, 2011 5:01 pm     Reply with quote

Is this real world chips or still a simulation????

If real world chips....

As others have suggested...go back to 'square one'.
Get two PICs, two Max232 chips,one breadboard and use the examples in the 'examples' folder or cut your own code to have a 'master' send some data to a 'slave'.
Have the master send 'a...z' to the slave, say 1 character per second.
Have the slave display them on the LCD, then send back that character+1 to the 'host'

host sends a,b,c----> slave
slave sends b,c,d---> to host

if simulation
delete simulator from PC
then
buy 2 PICs, 2 max232 chips and one breadboard.
vortexe9000



Joined: 07 Jun 2010
Posts: 50
Location: Banned - spammer

View user's profile Send private message

PostPosted: Mon May 09, 2011 5:18 pm     Reply with quote

ergege
_________________
Banned for spamming his own posts


Last edited by vortexe9000 on Sun Feb 19, 2012 7:35 am; edited 1 time in total
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Mon May 09, 2011 9:53 pm     Reply with quote

Now you should be able to play around a bit, send and receive data from either nodes.

If you have not added a PC to the network, then you should get started on that, make an RS 232 -RS 485 converter and connect the PC to the network. The code for sending and receiving data from the PC must also be made.

thanks
a
bkamen



Joined: 07 Jan 2004
Posts: 1619
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Tue May 10, 2011 7:39 am     Reply with quote

Speaking of RS485, I just did an RS485 project for a client and it was pretty straight forward.

In my environment, I used the PIC at one and and then a PC with a USB->RS485 converter at the other with various quick and dirty scripts (TCL) to generate traffic... OR RealTerm's "Send Mode" -- which I thought was nice and handy.

(I also used my agilent scope in the beginning to check timing and signals and stuff.)

Thought I'd toss that in. 2PIC's w/rs485 are really a *must*. In fact, having one end being a known quantity (like a PC) is nice.

A scope helps too.

Cheers,

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE
PostPosted: Tue May 10, 2011 11:13 pm     Reply with quote

Quote:
Thought I'd toss that in. 2PIC's w/rs485 are really a *must*. In fact, having one end being a known quantity (like a PC) is nice.

A scope helps too.


I agree without a scope its like groping in the dark.

thanks
a
mojsa2000



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

PostPosted: Sun May 15, 2011 12:48 am     Reply with quote

dear vortexe9000
Hi
To control a group a PICs by a PC, you'll need a Pic as a master to control RS485 chip. You know that the RS485 chips (for example max485) have a DE and RE pin that must be control. But you can't control them by a PC's COM port. So, you need to do this routine:
PC--->max232---->PIC(master)----->(rs485 module)----->other PICs
on the bus that have a rs485 module as input to convert data to rs232.
Good luck.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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