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

Code works well but only after RESET (RS485)!!

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



Joined: 08 Sep 2008
Posts: 84

View user's profile Send private message

Code works well but only after RESET (RS485)!!
PostPosted: Sun Mar 22, 2009 6:05 am     Reply with quote

Everybody hi,

I wrote two simple codes to send characters from one PIC to another through RS485,

the purpose is very simple: when I push a button connected to the first PIC, a character is sent to the second one where a Led should be lighted up depending on the character (A or B). (similar Leds are also connected the the Sender so that I can see that the caracter is correctly sent and received from the Slave )

my problem is that, the simultion runs well (ISIS) but only after RESET'ing the Receiver PIC once Rolling Eyes , Is there something to modify in my codes, so that I can make it run directly, or can it be a hardware problem ?. what do you think of my codes ? is stream =RS485 needed in the #USE RS232() directive ?

I'm using two 75176 as transceivers and my resistances values (pull up and pull down) were taken from the datasheet (I also added two termination resistances of 120 ohm), the distance between the PICs is about 25cm.


here is the schematic:
http://rapidshare.com/files/212454678/2_PICs_-RS485.pdf.html


Sender PIC:16F877
receiver PIC:16F876
Compiler version: CCS 4.057


Here is the sender's code (Master)

Code:

#include <16f877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,BROWNOUT,NODEBUG
#use delay(clock=20000000)
#include <stdlib.h>

#USE RS232(ENABLE=PIN_C0,Parity=N,BAUD=9600,XMIT=PIN_C6, RCV=PIN_C7,bits=8)

 
#define bp1  input_state(PIN_C1)
#define bp2  input_state(PIN_C2)
 
char car;
 
 
 #int_RDA
void RDA_isr()
 {
  car=getchar();
 } 
 
 
 
void  main()
 {

   
   while(TRUE)
   {
       
   
       if(bp1)
       {       
        putc('A');           
       }
   
       if(bp2)
       {
        putc('B');
       }
       
       if(car=='A')
       {   
       output_high(PIN_D0);
       delay_ms(200);
       output_low(PIN_D0);
       car='x';      // to prevent re-entering the loop
       }
       
       if(car=='B')
       {   
       output_high(PIN_D1);
       delay_ms(200);
       output_low(PIN_D1);
       car='x';      // to prevent re-entering the loop
       }
       
   
   
    }
   
   
 }
 




and here is the Receiver adress (Slave 1)


Code:
#include <16f876.h>
#device adc=8
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#include <stdlib.h>

#USE RS232(ENABLE=PIN_C0,Parity=N,BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7,bits=8)

   
char car;
 
 

#int_RDA
void RDA_isr()
 {
  car=getchar();
 } 
   
   
 
   
   
void  main()
 {
 
 
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
 
       
   while(TRUE)
   {

       if(car=='A')
       {
       output_bit(PIN_A0,1);
       delay_ms(300);
       output_bit(PIN_A0,0);
       car='x';      //to prevent re-entering the loop
       putc('A');
       }
     
         if(car=='B')
       {   
       output_bit(PIN_A1,1);
       delay_ms(300);
       output_bit(PIN_A1,0);
       car='x'  ;    //to prevent re-entering the loop
       putc('B');
       }   
     

   }
   
   
 }






PS: my codes there are a first step to write a protocol between a Master PIC and three Slaves (very close to Modbus).


your advices will be much appreciated,


thanks in advance.


Last edited by Salenko on Mon Mar 23, 2009 1:38 am; edited 1 time in total
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Mar 22, 2009 8:13 am     Reply with quote

Are you reporting simulation results or real circuit behaviour? I don't see a sense in commenting simulations, except perhaps for MPLAB SIM.

Generally, your code doesn't work without enabling interrupts. Also, you need a pull-up at RC7 to keep the high level during RS485 send.
Salenko



Joined: 08 Sep 2008
Posts: 84

View user's profile Send private message

PostPosted: Mon Mar 23, 2009 1:33 am     Reply with quote

FvM wrote:
Are you reporting simulation results or real circuit behaviour? I don't see a sense in commenting simulations, except perhaps for MPLAB SIM.

Generally, your code doesn't work without enabling interrupts. Also, you need a pull-up at RC7 to keep the high level during RS485 send.


hi FvM,

thank you for answering me,

as you said, I'm reporting simulation results awaiting my jdm programmer to come (should be today or tommorow) to make real circuit test.

thank you for reminding me to add:

Code:
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);


in the Master PIC code. I also added the pull up resistance for both transceiver's RO pins.

I have a further question if you would like to help me more: should I add stream =RS485 in the #USE RS232() directive ?, my code worked in the same way with or without this option.

thanks.
Ttelmah
Guest







PostPosted: Mon Mar 23, 2009 3:34 am     Reply with quote

Makes no difference, if you are only talking to the RS485.
The advantage of streams, appears when you are handling talking to multiple RS232/485 links, making it easier to understand what is going on (if you use logical names), and handle the control of the 'right' link.

Best Wishes
Salenko



Joined: 08 Sep 2008
Posts: 84

View user's profile Send private message

PostPosted: Mon Mar 23, 2009 12:43 pm     Reply with quote

Ttelmah wrote:
Makes no difference, if you are only talking to the RS485.
The advantage of streams, appears when you are handling talking to multiple RS232/485 links, making it easier to understand what is going on (if you use logical names), and handle the control of the 'right' link.

Best Wishes

hi,

thanks Ttelmah, Smile
Salenko



Joined: 08 Sep 2008
Posts: 84

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 9:15 am     Reply with quote

Ttelmah wrote:
Makes no difference, if you are only talking to the RS485.
The advantage of streams, appears when you are handling talking to multiple RS232/485 links, making it easier to understand what is going on (if you use logical names), and handle the control of the 'right' link.

Best Wishes


hi Ttelmah,

I tried to apply your suggestion by adding a PIC (slave 3) and send a data to it from the master via RS232 using stream=RS232 but the other slaves are receiving this data, is there anything wrong with the hardware ? (the same thing occurs with the stream=RS485, though I put the same use RS232() directive in the master and slave )

http://rapidshare.com/files/218913680/RS232_and_RS485_network.pdf.html

thanks
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