|
|
View previous topic :: View next topic |
Author |
Message |
musti463
Joined: 19 Sep 2013 Posts: 66
|
RS485 PIC to PIC communication problem ! |
Posted: Thu Dec 25, 2014 11:17 am |
|
|
I am testing my rs485 communication between two PIC.
Sometimes master sending, but slave stopping. After i am reset the circuit, master sending and slave take data. circuit, often working correctly. Sometimes slave stopping. What is the problem? Should i use INT_RDA commands?
MASTER
Code: | #INCLUDE <18F46K80.h>
#FUSES XT,NOWDT,NOPROTECT,NOBROWNOUT,NOPUT,NOWRT,NODEBUG,NOCPD
#USE delay (clock=4000000)
#USE rs232(baud=250000,xmit=pin_c6,rcv=pin_c7,enable=pin_c5,errors,RESTART_WDT)
#DEFINE use_portb_lcd TRUE
#INCLUDE <lcd.c>
#INCLUDE <input.c>
#INCLUDE <stdlib.h>
#DEFINE RS485_ID 0x07
#DEFINE RS485_DEST_ID 0x03
#INCLUDE <rs485.c>
int data,i;
void main ( )
{
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
set_tris_a(0x00);
set_tris_b(0x00);
set_tris_c(0x80);
set_tris_d(0x00);
set_tris_e(0x00);
output_a(0x00);
output_b(0x00);
output_low(pin_c0);output_low(pin_c1);output_low(pin_c2);output_low(pin_c3);output_low(pin_c4);
output_d(0x00);
output_e(0x00);
output_high(pin_c5);
rs485_init();
delay_ms(50);
lcd_init();
delay_ms(50);
while(1)
{
printf(lcd_putc,"\f");
i=0;
data=0;
for(i;i<100;i++)
{
if(rs485_send_message(RS485_DEST_ID,1,&data))
{
printf(lcd_putc,"\fGonderildi%u",i);
delay_ms(100);
}
data++;
}
}
}
|
SLAVE
Code: | #INCLUDE <18F4685.h>
#FUSES XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#USE delay (clock=4000000)
#USE rs232(baud=250000,xmit=pin_c6,rcv=pin_c7,enable=pin_c5,errors,RESTART_WDT)
#DEFINE use_portb_lcd TRUE
#INCLUDE <lcd.c>
#INCLUDE <input.c>
#INCLUDE <stdlib.h>
#DEFINE RS485_ID 0x03
#INCLUDE <rs485.c>
int data[3];
int *pointer=&data[0];
void main()
{
setup_psp(PSP_DISABLED); // PSP birimi devre dışı
setup_spi(SPI_SS_DISABLED); // SPI birimi devre dışı
setup_timer_1(T1_DISABLED); // T1 zamanlayıcısı devre dışı
setup_timer_2(T2_DISABLED,0,1); // T2 zamanlayıcısı devre dışı
setup_adc_ports(NO_ANALOGS); // ANALOG giriş yok
setup_adc(ADC_OFF); // ADC birimi devre dışı
setup_CCP1(CCP_OFF); // CCP1 birimi devre dışı
setup_CCP2(CCP_OFF); // CCP2 birimi devre dışı
set_tris_a(0x00);
set_tris_b(0x00);
set_tris_c(0x80);
set_tris_d(0x00);
set_tris_e(0x00);
output_a(0x00);
output_b(0x00);
output_low(pin_c0);output_low(pin_c1);output_low(pin_c2);output_low(pin_c3);output_low(pin_c4);
output_d(0x00);
output_e(0x00);
output_low(pin_c5);
rs485_init();
delay_ms(50);
lcd_init();
delay_ms(50);
printf(lcd_putc,"\fBilgi: ");
delay_ms(500);
while (1)
{
rs485_get_message(pointer,false);
delay_ms(50);
printf(lcd_putc,"\fBilgi:%u",pointer[2]);
}
}
|
_________________ M.Emir SADE |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Thu Dec 25, 2014 6:33 pm |
|
|
Yes, you must use a buffered ISR to properly communicate using the UART especially when you try to do other things like 'printing'. Without an ISR and buffer, you'll lose characters,get long delays,etc.
The CCS supplied ex_sisr.c is a great place to start.
Depending on the program a small buffer of say 16 or 32 bytes might be big enough for you, though bigger is better!
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Fri Dec 26, 2014 5:29 am |
|
|
There is also a potential little hardware issue, caused by this line:
setup_spi(SPI_SS_DISABLED);
This is _wrong_. Search here and you will find 'why' explained dozens of times. |
|
|
jgschmidt
Joined: 03 Dec 2008 Posts: 184 Location: Gresham, OR USA
|
|
Posted: Fri Dec 26, 2014 11:11 am |
|
|
I don't have any questions about RS-485 but browse through all the postings here to learn from the experts, and minimize the need to post questions...
So, here you (Ttelmah) say that
Quote: | There is also a potential little hardware issue, caused by this line:
setup_spi(SPI_SS_DISABLED);
This is _wrong_. Search here and you will find 'why' explained dozens of times. |
I did a search on SPI_SS_DISABLED and got a lot of hits since a lot of the posted code has this statement in it.
Can you give a bit more of a hint what the problem with this statement is? I've never used it, but in case I do, I want to be prepared for any pitfalls.
Thanks _________________ Jürgen
www.jgscraft.com |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Fri Dec 26, 2014 12:23 pm |
|
|
The command as posted says 'turn on the SPI, with slave select disabled'. Since you are using one of the SPI pins for the enable on the RS485, this is not going to work....
The syntax to turn off the SPI, is:
setup_spi(FALSE);
It was an incorrect syntax generated by the Wizard on some compiler versions and leads to huge number of problems. It has been pointed out here on dozens of threads. |
|
|
jgschmidt
Joined: 03 Dec 2008 Posts: 184 Location: Gresham, OR USA
|
|
Posted: Fri Dec 26, 2014 5:32 pm |
|
|
Many thanks. Confirms, again, that I'm better off avoiding the Wizards. _________________ Jürgen
www.jgscraft.com |
|
|
|
|
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
|